app-locations.js 7.5 KB

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