app-project-form.js 5.4 KB

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