app-locations.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. response.locations.forEach(location => {
  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($(event.currentTarget).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. $createProjectButton.prop("disabled", (exportIds.length === 0));
  61. });
  62. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  63. event.preventDefault();
  64. const $form = $("#locations-export-form");
  65. const action = $(event.currentTarget).attr("data-action");
  66. $form.find("input[name='ids[]']").remove();
  67. $form.find("input[name='action']").val(action);
  68. for(const i in exportIds) {
  69. if(!exportIds.hasOwnProperty(i)) continue;
  70. let locationId = exportIds[i];
  71. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  72. }
  73. $form.trigger("submit");
  74. });
  75. const $createProjectButton = $("#create-project-button");
  76. $createProjectButton.on("click", function(event) {
  77. event.preventDefault();
  78. if(exportIds.length === 0) return;
  79. const $form = $("#locations-create-project-form");
  80. $form.find("input[name='location_ids']").remove();
  81. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  82. $form.trigger("submit");
  83. });
  84. }
  85. function displayInfoWindow(marker) {
  86. const locationId = parseInt(marker.location.id);
  87. const index = exportIds.indexOf(locationId);
  88. const archived = marker.location.archived;
  89. let badge = "";
  90. if(archived) {
  91. badge = `<span class="badge badge-secondary">Archived</span>`;
  92. }
  93. let exportText = "Select";
  94. let exportClass = "btn-primary";
  95. if(index > -1) {
  96. exportText = "Deselect";
  97. exportClass = "btn-success";
  98. }
  99. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  100. <p>
  101. ${marker.location.name}
  102. ${badge}
  103. </p>
  104. <div class="form-row">
  105. <div class="col">
  106. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  107. </div>
  108. <div class="col">
  109. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  110. </div>
  111. </div>
  112. </div>`;
  113. infoWindow.setContent(content);
  114. infoWindow.open(map, marker);
  115. }