app-project-form.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. $("#template-id, #time-of-day, #date-start-month, #date-start-year").on("change", () => autogenProjectName());
  37. autogenProjectName();
  38. $("#autogen-name").on("change", event => {
  39. const checked = event.currentTarget.checked;
  40. $("#name").prop("readonly", checked);
  41. if(checked) {
  42. autogenProjectName();
  43. }
  44. });
  45. });
  46. function handleSelectButton() {
  47. let lgi = $(this).closest(".list-group-item");
  48. let button = this;
  49. lgi.slideUp("fast", function() {
  50. $(button).find("span").text("Deselect");
  51. $(button).removeClass("btn-outline-success").addClass("btn-outline-danger");
  52. $(button).find("[data-icon]").attr("data-icon", "times");
  53. lgi.detach();
  54. $("#selected-locations-list-group").append(lgi);
  55. let lgis = $("#selected-locations-list-group .list-group-item");
  56. lgis.sort(function(a, b) {
  57. let aName = $(a).data("name").toLowerCase();
  58. let bName = $(b).data("name").toLowerCase();
  59. if(aName == bName) return 0;
  60. return (aName > bName) ? 1 : -1;
  61. }).detach();
  62. $("#selected-locations-list-group").append(lgis);
  63. lgi.slideDown("fast");
  64. refreshLocationAlerts();
  65. refreshLocationIds();
  66. });
  67. }
  68. function handleDeselectButton() {
  69. let lgi = $(this).closest(".list-group-item");
  70. let button = this;
  71. lgi.slideUp("fast", function() {
  72. $(button).find("span").text("Select");
  73. $(button).removeClass("btn-outline-danger").addClass("btn-outline-success");
  74. $(button).find("[data-icon]").attr("data-icon", "check");
  75. lgi.detach();
  76. $("#not-selected-locations-list-group").append(lgi);
  77. let lgis = $("#not-selected-locations-list-group .list-group-item");
  78. lgis.sort(function(a, b) {
  79. let aName = $(a).data("name").toLowerCase();
  80. let bName = $(b).data("name").toLowerCase();
  81. if(aName == bName) return 0;
  82. return (aName > bName) ? 1 : -1;
  83. });
  84. $("#not-selected-locations-list-group").append(lgis);
  85. lgi.slideDown("fast");
  86. refreshLocationAlerts();
  87. refreshLocationIds();
  88. });
  89. }
  90. function refreshLocationIds() {
  91. let lgis = $("#selected-locations-list-group .list-group-item");
  92. let ids = [];
  93. lgis.each(function(index, lgi) {
  94. ids.push($(lgi).data("location-id"));
  95. });
  96. $("#location-ids").val(ids.join(", "));
  97. }
  98. function refreshLocationAlerts() {
  99. let count = 0;
  100. count = $("#not-selected-locations-list-group .list-group-item").length;
  101. if(count > 0) {
  102. $("#not-selected-locations-alert").hide();
  103. }
  104. else {
  105. $("#not-selected-locations-alert").show();
  106. }
  107. count = $("#selected-locations-list-group .list-group-item").length;
  108. if(count > 0) {
  109. $("#selected-locations-alert").hide();
  110. }
  111. else {
  112. $("#selected-locations-alert").show();
  113. }
  114. }
  115. function autogenProjectName() {
  116. const autogenChecked = $("#autogen-name").is(":checked");
  117. if(!autogenChecked) return;
  118. const customerName = $("#customer-name").text();
  119. const templateName = $("#template-id").find("option:selected").text();
  120. const dateStartMonth = $("#date-start-month").find("option:selected").text();
  121. const dateStartYear = $("#date-start-year").find("option:selected").text();
  122. const timeOfDay = $("#time-of-day").find("option:selected").text();
  123. const templateWords = templateName.split(" ");
  124. const templateWordCount = templateWords.length;
  125. const templateInitials = [];
  126. for(let i = 0; i < templateWordCount; i++) {
  127. templateInitials.push(templateWords[i].charAt(0));
  128. }
  129. const projectName = `${customerName} ${dateStartMonth} ${dateStartYear} ${templateInitials.join("")} ${timeOfDay}`;
  130. $("#name").val(projectName);
  131. }