app-map-overview.js 9.9 KB

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