new-document-modal.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.$supplierFormGroup = this.$supplierDropdown.closest(".mb-3");
  16. this.$aruSection = $("#new-document-aru-section");
  17. this.$itemDescriptionField = this.getField("item_description");
  18. this.$itemSerialNumberField = this.getField("item_serial_number");
  19. this.$productLookupDropdown = $("#new-document-product-lookup");
  20. this.$productLookupDropdown.select2({
  21. theme: "bootstrap-5",
  22. dropdownParent: this.$modal
  23. });
  24. this.$productLookupDropdown.on("change", () => {
  25. const $option = this.$productLookupDropdown.find("option:selected");
  26. this.$itemDescriptionField.val($option.data("name"));
  27. });
  28. this.$locationDropdown.select2({
  29. theme: "bootstrap-5",
  30. dropdownParent: this.$locationDropdown.closest("div")
  31. });
  32. }
  33. getField(name) {
  34. return this.$form.find(`[name=${name}]`);
  35. }
  36. // Modal lifecycle
  37. onShow(event) {
  38. const $element = $(event.relatedTarget);
  39. const data = $element.data();
  40. const { type, locationId, ticketId } = data;
  41. this.$supplierFormGroup.show();
  42. this.$aruSection.show();
  43. switch(type) {
  44. case DocumentTypes.SalesOrder:
  45. this.$title.text("New Sales Order");
  46. this.baseUrl = "/sales-orders";
  47. this.$aruSection.hide();
  48. this.$supplierFormGroup.hide();
  49. break;
  50. case DocumentTypes.GoodsIn:
  51. this.$title.text("New Goods-In");
  52. this.baseUrl = "/goods-in";
  53. this.$aruSection.hide();
  54. break;
  55. case DocumentTypes.AdvancedReplacementUnit:
  56. this.$title.text("New ARU");
  57. this.baseUrl = "/arus";
  58. this.$supplierFormGroup.hide();
  59. break;
  60. }
  61. if(locationId) {
  62. this.$locationDropdown.val(locationId);
  63. }
  64. if(ticketId) {
  65. this.$ticketIdField.val(ticketId);
  66. }
  67. }
  68. onShown() {
  69. this.$companyDropdown.trigger("focus");
  70. }
  71. onHidden() {
  72. this.$form.get(0).reset();
  73. this.$ticketIdField.val(null);
  74. }
  75. onSubmit(event) {
  76. event.preventDefault();
  77. const data = this.$form.serialize();
  78. $.post(`${this.baseUrl}/create`, data).then(response => {
  79. this.$modal.modal("hide");
  80. window.location = response.url;
  81. }).catch(error => {
  82. console.log(error);
  83. });
  84. }
  85. }
  86. $(() => new NewDocumentModal());