app-locations.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. legal_entity_name: $("#legal-entity-name").val()
  14. }, function(response) {
  15. if(!response.success) return false;
  16. if(!response.locations) return false;
  17. let map = maps["map"];
  18. let bounds = new google.maps.LatLngBounds;
  19. response.locations.forEach(location => {
  20. let position = {
  21. lat: location.latitude,
  22. lng: location.longitude
  23. };
  24. let options = {
  25. map: map,
  26. position: position,
  27. title: location.name
  28. };
  29. if(location.icon) {
  30. options.icon = "/img/markers/building-red.png";
  31. }
  32. bounds.extend(position);
  33. let key = "marker-" + location.id;
  34. let marker = new google.maps.Marker(options);
  35. marker.location = location;
  36. marker.addListener("click", function() {
  37. displayInfoWindow(this);
  38. });
  39. markerMap[key] = marker;
  40. });
  41. map.fitBounds(bounds);
  42. });
  43. $(document).on("click", "a[data-export-location-id]", function(event) {
  44. let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
  45. let index = exportIds.indexOf(locationId);
  46. let key = "marker-" + locationId;
  47. let marker = markerMap[key];
  48. if(index === -1) {
  49. exportIds.push(locationId);
  50. marker.setOptions({
  51. icon: "/img/markers/building-green.png"
  52. });
  53. }
  54. else {
  55. exportIds.splice(index, 1);
  56. marker.setOptions({
  57. icon: "/img/markers/building-red.png"
  58. });
  59. }
  60. displayInfoWindow(marker);
  61. $createProjectButton.prop("disabled", (exportIds.length === 0));
  62. });
  63. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  64. event.preventDefault();
  65. const $form = $("#locations-export-form");
  66. const action = $(event.currentTarget).attr("data-action");
  67. $form.find("input[name='ids[]']").remove();
  68. $form.find("input[name='action']").val(action);
  69. for(const i in exportIds) {
  70. if(!exportIds.hasOwnProperty(i)) continue;
  71. let locationId = exportIds[i];
  72. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  73. }
  74. $form.trigger("submit");
  75. });
  76. const $createProjectButton = $("#create-project-button");
  77. $createProjectButton.on("click", function(event) {
  78. event.preventDefault();
  79. if(exportIds.length === 0) return;
  80. const $form = $("#locations-create-project-form");
  81. $form.find("input[name='location_ids']").remove();
  82. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  83. $form.trigger("submit");
  84. });
  85. }
  86. function displayInfoWindow(marker) {
  87. const locationId = parseInt(marker.location.id);
  88. const index = exportIds.indexOf(locationId);
  89. const archived = marker.location.archived;
  90. let badge = "";
  91. if(archived) {
  92. badge = `<span class="badge badge-secondary">Archived</span>`;
  93. }
  94. let exportText = "Select";
  95. let exportClass = "btn-primary";
  96. if(index > -1) {
  97. exportText = "Deselect";
  98. exportClass = "btn-success";
  99. }
  100. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  101. <p>
  102. ${marker.location.name}
  103. ${badge}
  104. </p>
  105. <div class="form-row">
  106. <div class="col">
  107. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  108. </div>
  109. <div class="col">
  110. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  111. </div>
  112. </div>
  113. </div>`;
  114. infoWindow.setContent(content);
  115. infoWindow.open(map, marker);
  116. }