1
0

app-map-overview.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $.get("/account/map-overview/get-customer-locations", {
  66. customer_id: customerId,
  67. submit: $("#submit").val()
  68. }, function(response) {
  69. if(!response.success) return false;
  70. let map = maps["map"];
  71. let key = "customer-" + customerId;
  72. markerGroups[key] = [];
  73. for(let i in response.locations) {
  74. let l = response.locations[i];
  75. let position = {
  76. lat: l.latitude,
  77. lng: l.longitude
  78. };
  79. var options = {
  80. position: position,
  81. title: l.name,
  82. map: map
  83. };
  84. if(l.icon) options.icon = "/img/markers/" + l.icon;
  85. infoWindow.setContent("");
  86. let marker = new google.maps.Marker(options);
  87. addMarkerClickListenerForLocation(marker, l);
  88. markerGroups[key].push(marker);
  89. }
  90. $("a[data-customer-id=" + customerId + "]").data("selected", true);
  91. $("a[data-customer-id=" + customerId + "]").find(".badge").addClass("badge-danger").text(response.locations.length);
  92. });
  93. }
  94. function loadProjectLocations(projectId) {
  95. if(infoWindow == false) {
  96. infoWindow = new google.maps.InfoWindow();
  97. }
  98. $.get("/account/map-overview/get-project-locations", {
  99. project_id: projectId,
  100. submit: $("#submit").val()
  101. }, function(response) {
  102. if(!response.success) return false;
  103. let map = maps["map"];
  104. let key = "project-" + projectId;
  105. markerGroups[key] = [];
  106. for(let i in response.locations) {
  107. let l = response.locations[i];
  108. let position = {
  109. lat: l.latitude,
  110. lng: l.longitude
  111. };
  112. var options = {
  113. position: position,
  114. title: l.name,
  115. map: map
  116. };
  117. if(l.icon) options.icon = "/img/markers/" + l.icon;
  118. let marker = new google.maps.Marker(options);
  119. addMarkerClickListenerForLocation(marker, l);
  120. markerGroups[key].push(marker);
  121. }
  122. $("a[data-project-id=" + projectId + "]").data("selected", true);
  123. $("a[data-project-id=" + projectId + "]").find(".badge").addClass("badge-danger").text(response.locations.length);
  124. });
  125. }
  126. function loadAllAddresses() {
  127. if(infoWindow == false) {
  128. infoWindow = new google.maps.InfoWindow();
  129. }
  130. $.get("/api/addresses/get-all", {
  131. submit: $("#submit").val()
  132. }, function(response) {
  133. if(!response.success) return false;
  134. let map = maps["map"];
  135. let key = "all-addresses";
  136. markerGroups[key] = [];
  137. for(let i in response.addresses) {
  138. let a = response.addresses[i];
  139. let position = {
  140. lat: a.latitude,
  141. lng: a.longitude
  142. };
  143. var options = {
  144. position: position,
  145. title: a.name,
  146. map: map
  147. };
  148. let marker = new google.maps.Marker(options);
  149. addMarkerClickListenerForAddress(marker, a);
  150. markerGroups[key].push(marker);
  151. }
  152. $("a[data-all-addresses]").data("selected", true);
  153. $("a[data-all-addresses]").find(".badge").addClass("badge-danger").text(response.addresses.length);
  154. });
  155. }
  156. function addMarkerClickListenerForLocation(m, l) {
  157. let map = maps["map"];
  158. m.addListener("click", function() {
  159. let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
  160. if(l.internalId) {
  161. content += "Internal ID: " + l.internalId + "<br />";
  162. }
  163. content += "ID: " + l.id + "<br />";
  164. infoWindow.setContent(content);
  165. infoWindow.open(map, m);
  166. });
  167. }
  168. function addMarkerClickListenerForAddress(m, a) {
  169. let map = maps["map"];
  170. m.addListener("click", function() {
  171. let content = "<strong><a href=\"/account/users/" + a.userId + "\" target=\"_blank\">" + a.name + "</a></strong><br />";
  172. content += "ID: " + a.userId + "<br />";
  173. infoWindow.setContent(content);
  174. infoWindow.open(map, m);
  175. });
  176. }