ticket-modal.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class TicketModal {
  2. constructor() {
  3. this.$modal = $("#ticket-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.$subjectField = this.$form.find("[name=subject]");
  12. this.$locationRow = $("#location-row");
  13. this.$locationDropdown = this.$form.find("[name=location_id]");
  14. this.$noLocationAction = $("#no-location-action");
  15. this.$noLocationAction.on("click", event => {
  16. event.preventDefault();
  17. this.onNoLocation();
  18. });
  19. this.$noLocationRow = $("#no-location-row");
  20. }
  21. // Modal lifecycle
  22. onShow(event) {
  23. const $element = $(event.relatedTarget);
  24. const data = $element.data();
  25. }
  26. onShown() {
  27. this.$subjectField.trigger("focus");
  28. fetch("/locations/get-options").then(body => {
  29. return body.json()
  30. }).then(response => {
  31. const $options = response.options.map(option => {
  32. const $option = $(`<option value="${option.value}">`);
  33. $option.text(option.display);
  34. return $option;
  35. });
  36. this.$locationDropdown.append($options);
  37. });
  38. }
  39. onHidden() {
  40. this.$form.get(0).reset();
  41. this.$locationDropdown.find("option[value!='']").remove();
  42. this.$locationRow.slideDown("fast").find("select").prop("required", true);
  43. this.$noLocationRow.slideUp("fast").find("select, input").prop("required", false);
  44. }
  45. onNoLocation() {
  46. this.$locationRow.slideUp("fast").find("select, input").prop("required", false);
  47. this.$noLocationRow.slideDown("fast").find("select, input").prop("required", true);
  48. }
  49. onSubmit(event) {
  50. event.preventDefault();
  51. $.ajax({
  52. url: "/tickets/create",
  53. type: "POST",
  54. data: new FormData(this.$form.get(0)),
  55. processData: false,
  56. contentType: false,
  57. success: response => {
  58. this.$modal.modal("hide");
  59. window.location = `/tickets/${response.id}`;
  60. }
  61. })
  62. }
  63. }
  64. $(() => new TicketModal());