| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- class TicketModal {
- constructor() {
- this.$modal = $("#ticket-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.$subjectField = this.getField("subject");
- this.$locationRow = $("#location-row");
- this.$locationDropdown = this.getField("location_id");
- this.$saleNumberField = this.getField("sale_number");
- this.$aruNumberField = this.getField("aru_number");
- this.$noLocationAction = $("#no-location-action");
- this.$noLocationAction.on("click", event => {
- event.preventDefault();
- this.onNoLocation();
- });
- this.$noLocationRow = $("#no-location-row");
- this.$locationDropdown.select2({
- theme: "bootstrap-5",
- dropdownParent: this.$locationDropdown.closest("div")
- });
- }
- getField(name) {
- return this.$form.find(`[name=${name}]`);
- }
- // Modal lifecycle
- onShow(event) {
- const $element = $(event.relatedTarget);
- const data = $element.data();
- const { locationId, saleNumber } = data;
- this.locationId = locationId;
- if(saleNumber) {
- this.$saleNumberField.val(saleNumber);
- }
- }
- onShown() {
- this.$subjectField.trigger("focus");
- fetch("/locations/get-options").then(body => {
- return body.json()
- }).then(response => {
- const $options = response.options.map(option => {
- const $option = $(`<option value="${option.value}">`);
- $option.text(option.display);
- return $option;
- });
- this.$locationDropdown.append($options);
- if(this.locationId) {
- this.$locationDropdown.val(this.locationId);
- }
- });
- }
- onHidden() {
- this.$form.get(0).reset();
- this.$locationDropdown.find("option[value!='']").remove();
- this.$locationRow.slideDown("fast").find("select").prop("required", true);
- this.$noLocationRow.slideUp("fast").find("select, input").prop("required", false);
- }
- onNoLocation() {
- this.$locationRow.slideUp("fast").find("select, input").prop("required", false);
- this.$noLocationRow.slideDown("fast").find("select, input").prop("required", true);
- }
- onSubmit(event) {
- event.preventDefault();
- $.ajax({
- url: "/tickets/create",
- type: "POST",
- data: new FormData(this.$form.get(0)),
- processData: false,
- contentType: false,
- success: response => {
- this.$modal.modal("hide");
- window.location = `/tickets/${response.id}`;
- }
- })
- }
- }
- $(() => new TicketModal());
|