app-locations.js 3.7 KB

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