app-locations.js 7.5 KB

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