new-document-modal.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. class NewDocumentModal {
  2. constructor() {
  3. this.$modal = $("#new-document-modal");
  4. this.modal = bootstrap.Modal.getInstance(this.$modal.get(0));
  5. this.$modal.on("show.bs.modal", event => this.onShow(event));
  6. this.$modal.on("shown.bs.modal", () => this.onShown());
  7. this.$modal.on("hidden.bs.modal", () => this.onHidden());
  8. this.$title = this.$modal.find(".modal-title");
  9. this.$form = this.$modal.find("form");
  10. this.$form.on("submit", event => this.onSubmit(event));
  11. this.$ticketIdField = this.getField("ticket_id");
  12. this.$companyDropdown = this.getField("company_id");
  13. this.$locationDropdown = this.getField("location_id");
  14. this.$supplierDropdown = this.getField("supplier_id");
  15. this.$aruSection = $("#new-document-aru-section");
  16. this.$itemDescriptionField = this.getField("item_description");
  17. this.$itemSerialNumberField = this.getField("item_serial_number");
  18. this.$locationDropdown.select2({
  19. theme: "bootstrap-5",
  20. dropdownParent: this.$locationDropdown.closest("div")
  21. });
  22. }
  23. getField(name) {
  24. return this.$form.find(`[name=${name}]`);
  25. }
  26. // Modal lifecycle
  27. onShow(event) {
  28. const $element = $(event.relatedTarget);
  29. const data = $element.data();
  30. const { type, locationId, ticketId } = data;
  31. this.$supplierDropdown.closest(".mb-3").show();
  32. this.$aruSection.show();
  33. switch(type) {
  34. case DocumentTypes.GoodsIn:
  35. this.$title.text("New Goods-In");
  36. this.baseUrl = "/goods-in";
  37. this.$aruSection.hide();
  38. break;
  39. case DocumentTypes.AdvancedReplacementUnit:
  40. this.$title.text("New ARU");
  41. this.baseUrl = "/arus";
  42. this.$supplierDropdown.closest(".mb-3").hide();
  43. break;
  44. }
  45. if(locationId) {
  46. this.$locationDropdown.val(locationId);
  47. }
  48. if(ticketId) {
  49. this.$ticketIdField.val(ticketId);
  50. }
  51. }
  52. onShown() {
  53. this.$companyDropdown.trigger("focus");
  54. }
  55. onHidden() {
  56. this.$form.get(0).reset();
  57. this.$ticketIdField.val(null);
  58. }
  59. onSubmit(event) {
  60. event.preventDefault();
  61. const data = this.$form.serialize();
  62. $.post(`${this.baseUrl}/create`, data).then(response => {
  63. this.$modal.modal("hide");
  64. window.location = response.url;
  65. }).catch(error => {
  66. console.log(error);
  67. });
  68. }
  69. }
  70. $(() => new NewDocumentModal());