| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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");
- }
- 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());
|