new-document-modal.js 2.4 KB

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