app-locations.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. let infoWindow = false;
  2. let exportIds = [];
  3. let markerMap = {};
  4. // noinspection JSUnusedGlobalSymbols
  5. function getMarkerKey(locationId) {
  6. return `marker-${locationId}`;
  7. }
  8. function initialiseLocations() {
  9. if(!infoWindow) {
  10. infoWindow = new google.maps.InfoWindow();
  11. }
  12. $.get(`/api/customers/${customerId}/get-locations`, {
  13. id: $("#id").val(),
  14. internal_id: $("#internal-id").val(),
  15. name: $("#name").val(),
  16. legal_entity_name: $("#legal-entity-name").val(),
  17. include_archived: $("#include-archived").prop("checked") ? 1 : 0,
  18. include_closed: $("#include-closed").prop("checked") ? 1 : 0,
  19. filter: $("#filter").val(),
  20. min_date: $("#min-date").val(),
  21. max_date: $("#max-date").val(),
  22. }, function(response) {
  23. if(!response.success) return false;
  24. if(!response.locations) return false;
  25. let map = maps["map"];
  26. let bounds = new google.maps.LatLngBounds;
  27. let locationCount = response.locations.length;
  28. const $markerCountSpan = $("#map-marker-count");
  29. if(locationCount > 0) {
  30. $markerCountSpan.text(locationCount);
  31. }
  32. else {
  33. $markerCountSpan.text("");
  34. }
  35. response.locations.forEach(location => {
  36. let position = {
  37. lat: location.latitude,
  38. lng: location.longitude
  39. };
  40. let options = {
  41. map: map,
  42. position: position,
  43. title: location.name
  44. };
  45. if(!location.icon) {
  46. options.icon = "/img/markers/building-red.png";
  47. }
  48. else {
  49. options.icon = "/img/markers/" + location.icon;
  50. }
  51. bounds.extend(position);
  52. let key = getMarkerKey(location.id);
  53. let marker = new google.maps.Marker(options);
  54. marker.location = location;
  55. marker.originalIcon = options.icon;
  56. marker.addListener("click", function() {
  57. displayInfoWindow(this);
  58. });
  59. markerMap[key] = marker;
  60. });
  61. map.fitBounds(bounds);
  62. });
  63. $(document).on("click", "a[data-export-location-id]", function(event) {
  64. event.preventDefault();
  65. let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
  66. let index = exportIds.indexOf(locationId);
  67. let key = getMarkerKey(locationId);
  68. let marker = markerMap[key];
  69. if(index === -1) {
  70. exportIds.push(locationId);
  71. marker.setOptions({
  72. icon: "/img/markers/building-green.png"
  73. });
  74. }
  75. else {
  76. exportIds.splice(index, 1);
  77. marker.setOptions({
  78. icon: marker.originalIcon
  79. });
  80. }
  81. displayInfoWindow(marker);
  82. $createProjectButton.prop("disabled", (exportIds.length === 0));
  83. $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
  84. });
  85. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  86. event.preventDefault();
  87. const $form = $("#locations-export-form");
  88. const action = $(event.currentTarget).attr("data-action");
  89. $form.find("input[name='ids[]']").remove();
  90. $form.find("input[name='action']").val(action);
  91. for(const i in exportIds) {
  92. if(!exportIds.hasOwnProperty(i)) continue;
  93. let locationId = exportIds[i];
  94. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  95. }
  96. $form.trigger("submit");
  97. });
  98. const $createProjectButton = $("#create-project-button");
  99. $createProjectButton.on("click", function(event) {
  100. event.preventDefault();
  101. if(exportIds.length === 0) return;
  102. const $form = $("#locations-create-project-form");
  103. $form.find("input[name='location_ids']").remove();
  104. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  105. $form.trigger("submit");
  106. });
  107. const $addLocationsToProjectButton = $("#add-locations-to-project-button");
  108. new AddLocationsToProjectModal();
  109. $(".btn-map").on("click", event => {
  110. const locationId = $(event.currentTarget).data("location-id");
  111. const key = getMarkerKey(locationId);
  112. const marker = markerMap[key];
  113. const map = maps.map;
  114. const center = marker.getPosition();
  115. map.setCenter(center);
  116. map.setZoom(14);
  117. displayInfoWindow(markerMap[key]);
  118. });
  119. }
  120. function displayInfoWindow(marker) {
  121. const escapeText = (text) => {
  122. const span = document.createElement("span");
  123. span.innerText = text;
  124. return span.innerHTML;
  125. };
  126. const locationId = parseInt(marker.location.id);
  127. const index = exportIds.indexOf(locationId);
  128. const location = marker.location;
  129. const badges = [];
  130. const lines = [];
  131. if(location.doNotTest) {
  132. badges.push(`<span class="badge badge-warning">Do Not Test</span>`);
  133. }
  134. if(location.archived) {
  135. badges.push(`<span class="badge badge-secondary">Archived</span>`);
  136. }
  137. if(location.closed) {
  138. badges.push(`<span class="badge badge-danger">Closed</span>`);
  139. }
  140. lines.push({title: "Internal ID", text: location.internalId});
  141. lines.push({title: "Location ID", text: location.id});
  142. if(location.test) {
  143. lines.push({divider: true});
  144. lines.push({title: "Test ID", html: `<a href="/account/tests/${location.test.id}" target="_blank">${location.test.id}</a>`});
  145. if(location.test.formattedStartDts) {
  146. lines.push({title: "Date/Time", text: location.test.formattedStartDts});
  147. }
  148. if(location.test.tester) {
  149. const { id, name } = location.test.tester;
  150. lines.push({title: "Tester ID", text: id});
  151. lines.push({title: "Tester Name", html: `<a href="/account/users/${id}" target="_blank">${name}</a>`});
  152. }
  153. if(location.test.pastDue) {
  154. badges.push(`<span class="badge badge-danger">Past Due</span>`);
  155. }
  156. if(location.test.formattedDueDts) {
  157. lines.push({title: "Due", text: location.test.formattedDueDts});
  158. }
  159. if(location.test) {
  160. lines.push({title: "Result", text: location.test.resultText});
  161. }
  162. }
  163. let linesHtml = "";
  164. lines.forEach(line => {
  165. if(line.divider) {
  166. linesHtml += "<hr />\n";
  167. }
  168. if(line.title) {
  169. const titleHtml = escapeText(line.title);
  170. let contentHtml = line.html;
  171. if(line.text) {
  172. contentHtml = escapeText(line.text);
  173. }
  174. linesHtml += `<div class="small"><strong>${titleHtml}:</strong> ${contentHtml}</div>`;
  175. }
  176. });
  177. let exportText = "Select";
  178. let exportClass = "btn-primary";
  179. if(index > -1) {
  180. exportText = "Deselect";
  181. exportClass = "btn-success";
  182. }
  183. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  184. <div class="mb-1">
  185. ${marker.location.name}<br />
  186. ${badges.join(" ")}
  187. </div>
  188. <hr />
  189. ${linesHtml}
  190. <hr />
  191. <div class="form-row">
  192. <div class="col">
  193. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  194. </div>
  195. <div class="col">
  196. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  197. </div>
  198. </div>
  199. </div>`;
  200. infoWindow.setContent(content);
  201. infoWindow.open(map, marker);
  202. }
  203. class AddLocationsToProjectModal {
  204. constructor() {
  205. this.$modal = $("#add-locations-to-project-modal");
  206. this.$modal.on("show.bs.modal", event => this.onShow(event));
  207. this.$form = this.$modal.find("form");
  208. this.$form.on("submit", event => this.onSubmit(event));
  209. this.$projectDropdown = this.$form.find("[name=project_id]");
  210. }
  211. onShow() {
  212. this.$form.find(`input[name="location_ids[]"]`).remove();
  213. exportIds.forEach(locationId => {
  214. this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
  215. });
  216. }
  217. onSubmit(event) {
  218. event.preventDefault();
  219. const projectId = this.$projectDropdown.val();
  220. const data = this.$form.serialize();
  221. $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
  222. if(response.success) {
  223. if(response.dupeLocations.length > 0) {
  224. const locationNames = response.dupeLocations.map(location => location.name).join("\n");
  225. alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
  226. }
  227. else {
  228. alert("Locations added to project successfully.");
  229. }
  230. }
  231. this.$modal.modal("hide");
  232. }).catch(error => {
  233. alert(`An error has occurred: ${error}`);
  234. });
  235. }
  236. }