class NewSaleModal { constructor() { this.$modal = $("#new-sale-modal"); this.modal = bootstrap.Modal.getInstance(this.$modal.get(0)); this.$modal.on("show.bs.modal", event => this.onShow(event)); this.$modal.on("shown.bs.modal", () => this.onShown()); this.$modal.on("hidden.bs.modal", () => this.onHidden()); this.$title = this.$modal.find(".modal-title"); this.$form = this.$modal.find("form"); this.$form.on("submit", event => this.onSubmit(event)); this.$companyDropdown = this.getField("company_id"); this.$locationDropdown = this.getField("location_id"); this.$locationDropdown.select2({ theme: "bootstrap-5", dropdownParent: this.$locationDropdown.closest("div") }); const parameters = new URLSearchParams(window.location.search); const locationId = parameters.get("add_location_id"); if(locationId) { this.$modal.modal("show"); } } getField(name) { return this.$form.find(`[name=${name}]`); } // Modal lifecycle onShow(event) { const $element = $(event.relatedTarget); const data = $element.data(); } onShown() { this.$companyDropdown.trigger("focus"); } onHidden() { this.$form.get(0).reset(); } onSubmit(event) { } } $(() => new NewSaleModal());