new-sale-modal.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. class NewSaleModal {
  2. constructor() {
  3. this.$modal = $("#new-sale-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.$companyDropdown = this.getField("company_id");
  12. this.$locationDropdown = this.getField("location_id");
  13. this.$locationDropdown.select2({
  14. theme: "bootstrap-5",
  15. dropdownParent: this.$locationDropdown.closest("div")
  16. });
  17. const parameters = new URLSearchParams(window.location.search);
  18. const locationId = parameters.get("add_location_id");
  19. if(locationId) {
  20. this.$modal.modal("show");
  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. }
  31. onShown() {
  32. this.$companyDropdown.trigger("focus");
  33. }
  34. onHidden() {
  35. this.$form.get(0).reset();
  36. }
  37. onSubmit(event) {
  38. }
  39. }
  40. $(() => new NewSaleModal());