| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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.$form.find("[name=subject]");
- this.$locationDropdown = this.$form.find("[name=location_id]");
- }
- // Modal lifecycle
- onShow(event) {
- const $element = $(event.relatedTarget);
- const data = $element.data();
- }
- 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);
- });
- }
- onHidden() {
- this.$form.get(0).reset();
- this.$locationDropdown.find("option[value!='']").remove();
- }
- 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());
|