1
0

app-map-overview.js 10 KB

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