app-map-overview.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. var markerGroups = [];
  2. var infoWindow = false;
  3. const clearMarkers = key => {
  4. if(typeof markerGroups[key] !== "undefined") {
  5. for(let i in markerGroups[key]) {
  6. if(markerGroups[key].hasOwnProperty(i)) {
  7. let m = markerGroups[key][i];
  8. m.setMap(null);
  9. }
  10. }
  11. }
  12. };
  13. $(function() {
  14. const $hideInactiveTestersSwitch = $("#hide-inactive-testers");
  15. $hideInactiveTestersSwitch.on("change", () => loadAllAddresses());
  16. // On customer select
  17. $("a[data-customer-id]").on("click", function() {
  18. let customerId = $(this).data("customer-id");
  19. let key = "customer-" + customerId;
  20. clearMarkers(key);
  21. let selected = $(this).data("selected");
  22. if(selected) {
  23. $(this).data("selected", false);
  24. $(this).find(".badge").removeClass("badge-success").text("");
  25. $(this).find("small").text("");
  26. }
  27. else {
  28. loadCustomerLocations(customerId);
  29. }
  30. return false;
  31. });
  32. // On project select
  33. $("a[data-project-id]").on("click", function() {
  34. let projectId = $(this).data("project-id");
  35. let key = "project-" + projectId;
  36. clearMarkers(key);
  37. let selected = $(this).data("selected");
  38. if(selected) {
  39. $(this).data("selected", false);
  40. $(this).find(".badge").removeClass("badge-success").text("");
  41. }
  42. else {
  43. loadProjectLocations(projectId);
  44. }
  45. return false;
  46. });
  47. // On all addresses select
  48. $("a[data-all-addresses]").on("click", function() {
  49. clearMarkers("all-addresses");
  50. let selected = $(this).data("selected");
  51. if(selected) {
  52. $(this).data("selected", false);
  53. $(this).find(".badge").removeClass("badge-success").text("");
  54. }
  55. else {
  56. loadAllAddresses();
  57. }
  58. return false;
  59. });
  60. // Filters
  61. $("[data-filter-target]").on("input", event => {
  62. const $element = $(event.currentTarget);
  63. const value = $element.val();
  64. const $targetElement = $($element.data("filter-target"));
  65. $targetElement.children().each((index, element) => {
  66. const filterValue = $(element).data("filter-value");
  67. let method = "show";
  68. if(value.length > 0) {
  69. method = filterValue.indexOf(value.toLowerCase()) !== -1 ? "show" : "hide";
  70. }
  71. $(element)[method]();
  72. });
  73. });
  74. });
  75. function loadCustomerLocations(customerId) {
  76. if(infoWindow === false) {
  77. infoWindow = new google.maps.InfoWindow();
  78. }
  79. let minDate = $("#min-date").val();
  80. let maxDate = $("#max-date").val();
  81. let $customerItem = $("a[data-customer-id=" + customerId + "]");
  82. /**
  83. * @property {Object} response
  84. * @property {Array} response.locations
  85. * @property {String} response.testPeriod
  86. * @property {Boolean} response.success
  87. */
  88. $.get("/account/map-overview/get-customer-locations", {
  89. customer_id: customerId,
  90. min_date: minDate,
  91. max_date: maxDate,
  92. submit: $("#submit").val()
  93. }, function(response) {
  94. if(!response.success) return false;
  95. let key = "customer-" + customerId;
  96. markerGroups[key] = [];
  97. let count = response.locations.length;
  98. for(let i = 0; i < count; ++i) {
  99. let marker = addLocationMarker(response.locations[i]);
  100. markerGroups[key].push(marker);
  101. }
  102. $customerItem.data("selected", true);
  103. $customerItem.find(".badge").addClass("badge-danger").text(response.locations.length);
  104. $customerItem.find("small").text(response.testPeriod);
  105. });
  106. }
  107. function loadProjectLocations(projectId) {
  108. if(infoWindow === false) {
  109. infoWindow = new google.maps.InfoWindow();
  110. }
  111. let minDate = $("#min-date").val();
  112. let maxDate = $("#max-date").val();
  113. let $projectItem = $("a[data-project-id=" + projectId + "]");
  114. /**
  115. * @property {Object} response
  116. * @property {Array} response.locations
  117. * @property {Boolean} response.success
  118. */
  119. $.get("/account/map-overview/get-project-locations", {
  120. project_id: projectId,
  121. min_date: minDate,
  122. max_date: maxDate,
  123. submit: $("#submit").val()
  124. }, function(response) {
  125. if(!response.success) return false;
  126. let key = "project-" + projectId;
  127. markerGroups[key] = [];
  128. let count = response.locations.length;
  129. for(let i = 0; i < count; ++i) {
  130. let marker = addLocationMarker(response.locations[i]);
  131. markerGroups[key].push(marker);
  132. }
  133. $projectItem.data("selected", true);
  134. $projectItem.find(".badge").addClass("badge-danger").text(response.locations.length);
  135. });
  136. }
  137. function addLocationMarker(location) {
  138. let map = maps["map"];
  139. let position = {
  140. lat: location.latitude,
  141. lng: location.longitude
  142. };
  143. var options = {
  144. position: position,
  145. title: location.name,
  146. map: map
  147. };
  148. if(location.icon) {
  149. options.icon = "/img/markers/" + location.icon;
  150. }
  151. else {
  152. if(location.doNotTest) options.icon = "/img/markers/do_not_test.png";
  153. if(location.closed) options.icon = "/img/markers/closed.png";
  154. }
  155. infoWindow.setContent("");
  156. let marker = new google.maps.Marker(options);
  157. addMarkerClickListenerForLocation(marker, location);
  158. return marker;
  159. }
  160. function loadAllAddresses() {
  161. if(infoWindow === false) {
  162. infoWindow = new google.maps.InfoWindow();
  163. }
  164. /**
  165. * @property {Object} response
  166. * @property {Array} response.addresses
  167. * @property {Boolean} response.success
  168. */
  169. clearMarkers("all-addresses");
  170. const $allAddresses = $("a[data-all-addresses]");
  171. const hideInactive = $("#hide-inactive-testers").prop("checked");
  172. $.get("/api/addresses/get-all", {
  173. submit: $("#submit").val()
  174. }, function(response) {
  175. if(!response.success) return false;
  176. const map = maps["map"];
  177. const key = "all-addresses";
  178. markerGroups[key] = [];
  179. let count = 0;
  180. response.addresses.forEach(address => {
  181. const user = address.user;
  182. let icon = "user-red.png";
  183. if(user.daysSinceLastTest !== null) {
  184. if(user.daysSinceLastTest > 90) {
  185. icon = "user-red.png";
  186. }
  187. else if(user.daysSinceLastTest > 60) {
  188. icon = "user-grey.png";
  189. }
  190. else if(user.daysSinceLastTest > 30) {
  191. icon = "user-orange.png";
  192. }
  193. else {
  194. icon = "user-green.png";
  195. }
  196. }
  197. if(hideInactive && icon === "user-red.png") {
  198. return;
  199. }
  200. let marker = new google.maps.Marker({
  201. position: {
  202. lat: address.latitude,
  203. lng: address.longitude
  204. },
  205. title: address.name,
  206. map: map,
  207. icon: `/img/markers/${icon}`
  208. });
  209. addMarkerClickListenerForAddress(marker, address);
  210. markerGroups[key].push(marker);
  211. count++;
  212. });
  213. $allAddresses.data("selected", true);
  214. $allAddresses.find(".badge").addClass("badge-danger").text(count);
  215. });
  216. }
  217. function addMarkerClickListenerForLocation(marker, location) {
  218. let map = maps["map"];
  219. marker.addListener("click", function() {
  220. let content = "<div><strong><a href=\"/account/locations/" + location.id + "\" target=\"_blank\">" + location.name + "</a></strong></div>";
  221. if(location.doNotTest) {
  222. content += "<span class=\"badge badge-warning\">Do Not Test</span>";
  223. }
  224. if(location.closed) {
  225. content += "<span class=\"badge badge-danger\">Closed</span>";
  226. }
  227. if(location.internalId) {
  228. content += "<div class=\"small\"><strong>Internal ID:</strong> " + location.internalId + "</div>";
  229. }
  230. content += "<div class=\"small\"><strong>Location ID:</strong> " + location.id + "</div>";
  231. if(location.test) {
  232. content += '<hr />';
  233. content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + location.test.id + '" target="_blank">' + location.test.id + '</a></div>';
  234. if(location.test.formattedStartDts) {
  235. content += '<div class="small"><strong>Date/Time:</strong> ' + location.test.formattedStartDts + '</div>';
  236. }
  237. if(location.test.tester) {
  238. content += '<div class="small"><strong>Tester ID:</strong> ' + location.test.tester.id + '</div>';
  239. content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + location.test.tester.id + '" target="_blank">' + location.test.tester.name + '</a></div>';
  240. }
  241. let pastDueBadge = '';
  242. if(location.test.pastDue) {
  243. pastDueBadge = '<span class="badge badge-lg badge-primary">Past Due</span>';
  244. }
  245. if(location.test.formattedDueDts) {
  246. content += '<div class="small"><strong>Due:</strong> ' + location.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
  247. }
  248. }
  249. if(typeof location.testCount !== "undefined") {
  250. content += '<hr />';
  251. content += `<div class="small"><strong>Test Count:</strong> ${location.testCount}</div>`;
  252. }
  253. infoWindow.setContent(content);
  254. infoWindow.open(map, marker);
  255. });
  256. }
  257. function addMarkerClickListenerForAddress(marker, address) {
  258. let map = maps["map"];
  259. marker.addListener("click", function() {
  260. let dateLastTest = address.user.dateLastTest;
  261. if(dateLastTest === null) {
  262. dateLastTest = "Never";
  263. }
  264. const content = `
  265. <div class="text-center mb-2">
  266. <strong><a href="/account/users/${address.userId}" target="_blank">${address.name}</a></strong>
  267. </div>
  268. <table class="table small table-sm mb-0">
  269. <tr><th>ID</th><td class="text-right">${address.user.id}</td></tr>
  270. <tr><th>Score</th><td class="text-right">${address.user.score}</td></tr>
  271. <tr><th>Last Test</th><td class="text-right">${dateLastTest}</td></tr>
  272. <tr><th>Registered</th><td class="text-right">${address.user.dateRegistered}</td></tr>
  273. </table>
  274. `;
  275. infoWindow.setContent(content);
  276. infoWindow.open(map, marker);
  277. });
  278. }