app-map-overview.js 8.6 KB

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