new-document-modal.js 2.1 KB

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