app-map-overview.js 7.4 KB

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