app-project-form.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. $(function() {
  2. refreshLocationAlerts();
  3. $(document).on("click", "#not-selected-locations-list-group button", handleSelectButton);
  4. $(document).on("click", "#selected-locations-list-group button", handleDeselectButton);
  5. $("input[data-filter-target]").on("input", event => {
  6. const $input = $(event.currentTarget);
  7. const value = $input.val().toLowerCase();
  8. const target = $input.data("filter-target");
  9. if(value.length == 0) {
  10. $(target).children().show();
  11. return true;
  12. }
  13. $(target).children().each((index, element) => {
  14. let number = parseInt(value);
  15. let show;
  16. if(isNaN(number)) {
  17. show = $(element).data("filter-value").indexOf(value) != -1;
  18. }
  19. else {
  20. let test1 = (number == $(element).data("location-id"));
  21. let test2 = (number == $(element).data("internal-id"));
  22. show = test1 || test2;
  23. }
  24. if(show) {
  25. $(element).show();
  26. }
  27. else {
  28. $(element).hide();
  29. }
  30. });
  31. return true;
  32. });
  33. $("#project-form").on("submit", function() {
  34. $(this).find("button[type=submit]").attr("disabled", "disabled");
  35. });
  36. initialiseTooltips();
  37. const $templateDropdown = $("#template-id");
  38. $("#template-id, #time-of-day, #date-start").on("change", () => autogenProjectName());
  39. $templateDropdown.on("change", () => {
  40. const keys = $templateDropdown.find("option:selected").data("keys");
  41. const keyCount = keys.length;
  42. const $extraFieldsRow = $("#template-extra-fields-row");
  43. const $extraFieldsContainer = $("#template-extra-fields-container");
  44. $extraFieldsContainer.empty();
  45. if(keyCount === 0) {
  46. $extraFieldsRow.collapse("hide");
  47. }
  48. else {
  49. const initialTemplateKeys = JSON.parse($("#initial-template-keys").val());
  50. $.each(keys, (index, value) => {
  51. const id = `template-key-${index}`;
  52. const $element = $(`<div class="custom-control custom-switch">
  53. <input type="checkbox" class="custom-control-input" id="${id}" name="template_keys[]" />
  54. <label class="custom-control-label" for="${id}">Enable ${value} fields</label>
  55. </div>`);
  56. const checked = initialTemplateKeys.indexOf(value) !== -1;
  57. $element.find("input").attr("value", value).prop("checked", checked);
  58. $extraFieldsContainer.append($element);
  59. });
  60. $extraFieldsRow.collapse("show");
  61. }
  62. });
  63. $(document).on("change", "#template-extra-fields-container input", () => autogenProjectName());
  64. $("#template-id").trigger("change");
  65. const $autogenSwitch = $("#autogen-name");
  66. $autogenSwitch.on("change", event => {
  67. const checked = $autogenSwitch.prop("checked");
  68. $("#name").prop("readonly", checked);
  69. if(checked) autogenProjectName();
  70. });
  71. });
  72. function getCheckedTemplateKeys() {
  73. const keys = [];
  74. $("#template-extra-fields-container input").each((index, element) => {
  75. const $checkbox = $(element);
  76. const isChecked = $checkbox.prop("checked");
  77. if(isChecked) keys.push($checkbox.val());
  78. });
  79. return keys;
  80. }
  81. function handleSelectButton() {
  82. let lgi = $(this).closest(".list-group-item");
  83. let button = this;
  84. lgi.slideUp("fast", function() {
  85. $(button).find("span").text("Deselect");
  86. $(button).removeClass("btn-outline-success").addClass("btn-outline-danger");
  87. $(button).find("[data-icon]").attr("data-icon", "times");
  88. lgi.detach();
  89. $("#selected-locations-list-group").append(lgi);
  90. let lgis = $("#selected-locations-list-group .list-group-item");
  91. lgis.sort(function(a, b) {
  92. let aName = $(a).data("name").toLowerCase();
  93. let bName = $(b).data("name").toLowerCase();
  94. if(aName == bName) return 0;
  95. return (aName > bName) ? 1 : -1;
  96. }).detach();
  97. $("#selected-locations-list-group").append(lgis);
  98. lgi.slideDown("fast");
  99. refreshLocationAlerts();
  100. refreshLocationIds();
  101. });
  102. }
  103. function handleDeselectButton() {
  104. let lgi = $(this).closest(".list-group-item");
  105. let button = this;
  106. lgi.slideUp("fast", function() {
  107. $(button).find("span").text("Select");
  108. $(button).removeClass("btn-outline-danger").addClass("btn-outline-success");
  109. $(button).find("[data-icon]").attr("data-icon", "check");
  110. lgi.detach();
  111. $("#not-selected-locations-list-group").append(lgi);
  112. let lgis = $("#not-selected-locations-list-group .list-group-item");
  113. lgis.sort(function(a, b) {
  114. let aName = $(a).data("name").toLowerCase();
  115. let bName = $(b).data("name").toLowerCase();
  116. if(aName == bName) return 0;
  117. return (aName > bName) ? 1 : -1;
  118. });
  119. $("#not-selected-locations-list-group").append(lgis);
  120. lgi.slideDown("fast");
  121. refreshLocationAlerts();
  122. refreshLocationIds();
  123. });
  124. }
  125. function refreshLocationIds() {
  126. let lgis = $("#selected-locations-list-group .list-group-item");
  127. let ids = [];
  128. lgis.each(function(index, lgi) {
  129. ids.push($(lgi).data("location-id"));
  130. });
  131. $("#location-ids").val(ids.join(", "));
  132. }
  133. function refreshLocationAlerts() {
  134. let count = $("#not-selected-locations-list-group .list-group-item").length;
  135. if(count > 0) {
  136. $("#not-selected-locations-alert").hide();
  137. }
  138. else {
  139. $("#not-selected-locations-alert").show();
  140. }
  141. count = $("#selected-locations-list-group .list-group-item").length;
  142. if(count > 0) {
  143. $("#selected-locations-alert").hide();
  144. }
  145. else {
  146. $("#selected-locations-alert").show();
  147. }
  148. }
  149. function autogenProjectName() {
  150. const autogenChecked = $("#autogen-name").prop("checked");
  151. if(!autogenChecked) return;
  152. const customerName = $("#customer-name").text();
  153. const templateName = $.trim($("#template-id").find("option:selected").text());
  154. const dateStart = new Date($("#date-start").val());
  155. const timeOfDay = $("#time-of-day").find("option:selected").text();
  156. const templateWords = templateName.split(" ");
  157. const templateInitials = [];
  158. for(const templateWord of templateWords) {
  159. const initial = templateWord.charAt(0);
  160. if(initial === "(") {
  161. break;
  162. }
  163. templateInitials.push(templateWord.charAt(0));
  164. }
  165. const templateKeys = getCheckedTemplateKeys();
  166. const parts = [];
  167. parts.push(customerName);
  168. parts.push(dateStart.toLocaleString("default", {month: "long"}));
  169. parts.push(dateStart.getFullYear());
  170. parts.push(templateInitials.join(""));
  171. if(templateKeys.length > 0) {
  172. parts.push(`(${templateKeys.join(" + ")})`);
  173. }
  174. parts.push(timeOfDay);
  175. const projectName = parts.join(" ");
  176. $("#name").val(projectName);
  177. }