app-locations.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var infoWindow = false;
  2. var exportIds = [];
  3. var markerMap = {};
  4. function initialiseLocations() {
  5. if(!infoWindow) {
  6. infoWindow = new google.maps.InfoWindow();
  7. }
  8. $.get("/api/customers/" + customerId + "/get-locations", {
  9. id: $("#id").val(),
  10. internal_id: $("#internal-id").val(),
  11. name: $("#name").val()
  12. }, function(response) {
  13. if(!response.success) return false;
  14. if(!response.locations) return false;
  15. let map = maps["map"];
  16. let bounds = new google.maps.LatLngBounds;
  17. for(let i in response.locations) {
  18. let location = response.locations[i];
  19. let position = {
  20. lat: location.latitude,
  21. lng: location.longitude
  22. };
  23. let options = {
  24. map: map,
  25. position: position,
  26. title: location.name
  27. }
  28. if(location.icon) {
  29. options.icon = "/img/markers/building-red.png";
  30. }
  31. bounds.extend(position);
  32. let key = "marker-" + location.id;
  33. let marker = new google.maps.Marker(options);
  34. marker.location = location;
  35. marker.addListener("click", function() {
  36. displayInfoWindow(this);
  37. });
  38. markerMap[key] = marker;
  39. }
  40. map.fitBounds(bounds);
  41. });
  42. $(document).on("click", "a[data-export-location-id]", function(event) {
  43. let locationId = parseInt($(this).attr("data-export-location-id"));
  44. let index = exportIds.indexOf(locationId);
  45. let key = "marker-" + locationId;
  46. let marker = markerMap[key];
  47. if(index == -1) {
  48. exportIds.push(locationId);
  49. marker.setOptions({
  50. icon: "/img/markers/building-green.png"
  51. });
  52. }
  53. else {
  54. exportIds.splice(index, 1);
  55. marker.setOptions({
  56. icon: "/img/markers/building-red.png"
  57. });
  58. }
  59. displayInfoWindow(marker);
  60. });
  61. $("#export-csv-button, #export-kml-button").click(function(event) {
  62. if(exportIds.length > 0) {
  63. event.preventDefault();
  64. let action = $(this).attr("data-action");
  65. $("#locations-export-form input[name='ids[]']").remove();
  66. $("#locations-export-form input[name='action']").val(action);
  67. for(let i in exportIds) {
  68. let locationId = exportIds[i];
  69. $("#locations-export-form").append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  70. }
  71. $("#locations-export-form").submit();
  72. }
  73. });
  74. }
  75. function displayInfoWindow(marker) {
  76. let locationId = parseInt(marker.location.id);
  77. let index = exportIds.indexOf(locationId);
  78. let exportText = "Select for Export";
  79. let exportClass = "btn-primary";
  80. if(index > -1) {
  81. exportText = "Deselect for Export";
  82. exportClass = "btn-success";
  83. }
  84. let content = "<div class=\"container-fluid\" style=\"font-size: 1rem; font-weight: normal;\">";
  85. content += "<p>" + marker.location.name + "</p>\n";
  86. content += "<div class=\"row\">";
  87. content += "<div class=\"col\"><a class=\"btn btn-block " + exportClass + "\" href=\"#\" data-export-location-id=\"" + marker.location.id + "\">" + exportText + "</a></div>";
  88. content += "<div class=\"col\"><a class=\"btn btn-block btn-primary\" href=\"/account/locations/" + marker.location.id + "\" target=\"_blank\">View Details</a></div>";
  89. content += "</div>";
  90. content += "</div>";
  91. infoWindow.setContent(content);
  92. infoWindow.open(map, marker);
  93. }