app-map-overview.js 6.3 KB

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