app-map-overview.js 7.5 KB

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