app-map-overview.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. });
  43. function loadCustomerLocations(customerId) {
  44. if(infoWindow == false) {
  45. infoWindow = new google.maps.InfoWindow();
  46. }
  47. $.get("/account/map-overview/get-customer-locations", {
  48. customer_id: customerId,
  49. submit: $("#submit").val()
  50. }, function(response) {
  51. if(!response.success) return false;
  52. let map = maps["map"];
  53. let key = "customer-" + customerId;
  54. markerGroups[key] = [];
  55. for(let i in response.locations) {
  56. let l = response.locations[i];
  57. let position = {
  58. lat: l.latitude,
  59. lng: l.longitude
  60. };
  61. var options = {
  62. position: position,
  63. title: l.name,
  64. map: map
  65. };
  66. if(l.icon) options.icon = l.icon;
  67. infoWindow.setContent("");
  68. let marker = new google.maps.Marker(options);
  69. addMarkerClickListener(marker, l);
  70. markerGroups[key].push(marker);
  71. }
  72. $("a[data-customer-id=" + customerId + "]").data("selected", true);
  73. $("a[data-customer-id=" + customerId + "]").find(".badge").addClass("badge-danger").text(response.locations.length);
  74. });
  75. }
  76. function loadProjectLocations(projectId) {
  77. if(infoWindow == false) {
  78. infoWindow = new google.maps.InfoWindow();
  79. }
  80. $.get("/account/map-overview/get-project-locations", {
  81. project_id: projectId,
  82. submit: $("#submit").val()
  83. }, function(response) {
  84. if(!response.success) return false;
  85. let map = maps["map"];
  86. let key = "project-" + projectId;
  87. markerGroups[key] = [];
  88. for(let i in response.locations) {
  89. let l = response.locations[i];
  90. let position = {
  91. lat: l.latitude,
  92. lng: l.longitude
  93. };
  94. var options = {
  95. position: position,
  96. title: l.name,
  97. map: map
  98. };
  99. if(l.icon) options.icon = l.icon;
  100. let marker = new google.maps.Marker(options);
  101. addMarkerClickListener(marker, l);
  102. markerGroups[key].push(marker);
  103. }
  104. $("a[data-project-id=" + projectId + "]").data("selected", true);
  105. $("a[data-project-id=" + projectId + "]").find(".badge").addClass("badge-danger").text(response.locations.length);
  106. });
  107. }
  108. function addMarkerClickListener(m, l) {
  109. let map = maps["map"];
  110. m.addListener("click", function() {
  111. let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
  112. if(l.internalId) {
  113. content += "Internal ID: " + l.internalId + "<br />";
  114. }
  115. content += "ID: " + l.id + "<br />";
  116. infoWindow.setContent(content);
  117. infoWindow.open(map, m);
  118. });
  119. }