app-locations.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 Die</span>`);
  155. }
  156. if(location.test.formattedDueDts) {
  157. lines.push({title: "Due", text: location.test.formattedDueDts});
  158. }
  159. }
  160. let linesHtml = "";
  161. lines.forEach(line => {
  162. if(line.divider) {
  163. linesHtml += "<hr />\n";
  164. }
  165. if(line.title) {
  166. const titleHtml = escapeText(line.title);
  167. let contentHtml = line.html;
  168. if(line.text) {
  169. contentHtml = escapeText(line.text);
  170. }
  171. linesHtml += `<div class="small"><strong>${titleHtml}:</strong> ${contentHtml}</div>`;
  172. }
  173. });
  174. let exportText = "Select";
  175. let exportClass = "btn-primary";
  176. if(index > -1) {
  177. exportText = "Deselect";
  178. exportClass = "btn-success";
  179. }
  180. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  181. <div class="mb-1">
  182. ${marker.location.name}<br />
  183. ${badges.join(" ")}
  184. </div>
  185. <hr />
  186. ${linesHtml}
  187. <hr />
  188. <div class="form-row">
  189. <div class="col">
  190. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  191. </div>
  192. <div class="col">
  193. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  194. </div>
  195. </div>
  196. </div>`;
  197. infoWindow.setContent(content);
  198. infoWindow.open(map, marker);
  199. }
  200. class AddLocationsToProjectModal {
  201. constructor() {
  202. this.$modal = $("#add-locations-to-project-modal");
  203. this.$modal.on("show.bs.modal", event => this.onShow(event));
  204. this.$form = this.$modal.find("form");
  205. this.$form.on("submit", event => this.onSubmit(event));
  206. this.$projectDropdown = this.$form.find("[name=project_id]");
  207. }
  208. onShow() {
  209. this.$form.find(`input[name="location_ids[]"]`).remove();
  210. exportIds.forEach(locationId => {
  211. this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
  212. });
  213. }
  214. onSubmit(event) {
  215. event.preventDefault();
  216. const projectId = this.$projectDropdown.val();
  217. const data = this.$form.serialize();
  218. $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
  219. if(response.success) {
  220. if(response.dupeLocations.length > 0) {
  221. const locationNames = response.dupeLocations.map(location => location.name).join("\n");
  222. alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
  223. }
  224. else {
  225. alert("Locations added to project successfully.");
  226. }
  227. }
  228. this.$modal.modal("hide");
  229. }).catch(error => {
  230. alert(`An error has occurred: ${error}`);
  231. });
  232. }
  233. }