app-map-overview.js 7.7 KB

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