app-locations.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. let infoWindow = false;
  2. let exportIds = [];
  3. let markerMap = {};
  4. // noinspection JSUnusedGlobalSymbols
  5. function getMarkerKey(locationId) {
  6. return `marker-${locationId}`;
  7. }
  8. function initialiseLocations() {
  9. if(!infoWindow) {
  10. infoWindow = new google.maps.InfoWindow();
  11. }
  12. const includeArchived = $("#include-archived").prop("checked") ? 1 : 0;
  13. const includeClosed = $("#include-closed").prop("checked") ? 1 : 0;
  14. $.get(`/api/customers/${customerId}/get-locations`, {
  15. id: $("#id").val(),
  16. internal_id: $("#internal-id").val(),
  17. name: $("#name").val(),
  18. legal_entity_name: $("#legal-entity-name").val(),
  19. include_archived: includeArchived,
  20. include_closed: includeClosed
  21. }, function(response) {
  22. if(!response.success) return false;
  23. if(!response.locations) return false;
  24. let map = maps["map"];
  25. let bounds = new google.maps.LatLngBounds;
  26. response.locations.forEach(location => {
  27. let position = {
  28. lat: location.latitude,
  29. lng: location.longitude
  30. };
  31. let options = {
  32. map: map,
  33. position: position,
  34. title: location.name
  35. };
  36. if(location.icon) {
  37. options.icon = "/img/markers/building-red.png";
  38. }
  39. bounds.extend(position);
  40. let key = getMarkerKey(location.id);
  41. let marker = new google.maps.Marker(options);
  42. marker.location = location;
  43. marker.addListener("click", function() {
  44. displayInfoWindow(this);
  45. });
  46. markerMap[key] = marker;
  47. });
  48. map.fitBounds(bounds);
  49. });
  50. $(document).on("click", "a[data-export-location-id]", function(event) {
  51. let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
  52. let index = exportIds.indexOf(locationId);
  53. let key = getMarkerKey(locationId);
  54. let marker = markerMap[key];
  55. if(index === -1) {
  56. exportIds.push(locationId);
  57. marker.setOptions({
  58. icon: "/img/markers/building-green.png"
  59. });
  60. }
  61. else {
  62. exportIds.splice(index, 1);
  63. marker.setOptions({
  64. icon: "/img/markers/building-red.png"
  65. });
  66. }
  67. displayInfoWindow(marker);
  68. $createProjectButton.prop("disabled", (exportIds.length === 0));
  69. $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
  70. });
  71. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  72. event.preventDefault();
  73. const $form = $("#locations-export-form");
  74. const action = $(event.currentTarget).attr("data-action");
  75. $form.find("input[name='ids[]']").remove();
  76. $form.find("input[name='action']").val(action);
  77. for(const i in exportIds) {
  78. if(!exportIds.hasOwnProperty(i)) continue;
  79. let locationId = exportIds[i];
  80. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  81. }
  82. $form.trigger("submit");
  83. });
  84. const $createProjectButton = $("#create-project-button");
  85. $createProjectButton.on("click", function(event) {
  86. event.preventDefault();
  87. if(exportIds.length === 0) return;
  88. const $form = $("#locations-create-project-form");
  89. $form.find("input[name='location_ids']").remove();
  90. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  91. $form.trigger("submit");
  92. });
  93. const $addLocationsToProjectButton = $("#add-locations-to-project-button");
  94. new AddLocationsToProjectModal();
  95. $(".btn-map").on("click", event => {
  96. const locationId = $(event.currentTarget).data("location-id");
  97. const key = getMarkerKey(locationId);
  98. const marker = markerMap[key];
  99. const map = maps.map;
  100. const center = marker.getPosition();
  101. map.setCenter(center);
  102. map.setZoom(14);
  103. displayInfoWindow(markerMap[key]);
  104. });
  105. }
  106. function displayInfoWindow(marker) {
  107. const locationId = parseInt(marker.location.id);
  108. const index = exportIds.indexOf(locationId);
  109. const archived = marker.location.archived;
  110. let badge = "";
  111. if(archived) {
  112. badge = `<span class="badge badge-secondary">Archived</span>`;
  113. }
  114. let exportText = "Select";
  115. let exportClass = "btn-primary";
  116. if(index > -1) {
  117. exportText = "Deselect";
  118. exportClass = "btn-success";
  119. }
  120. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  121. <p>
  122. ${marker.location.name}
  123. ${badge}
  124. </p>
  125. <div class="form-row">
  126. <div class="col">
  127. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  128. </div>
  129. <div class="col">
  130. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  131. </div>
  132. </div>
  133. </div>`;
  134. infoWindow.setContent(content);
  135. infoWindow.open(map, marker);
  136. }
  137. class AddLocationsToProjectModal {
  138. constructor() {
  139. this.$modal = $("#add-locations-to-project-modal");
  140. this.$modal.on("show.bs.modal", event => this.onShow(event));
  141. this.$form = this.$modal.find("form");
  142. this.$form.on("submit", event => this.onSubmit(event));
  143. this.$projectDropdown = this.$form.find("[name=project_id]");
  144. }
  145. onShow(event) {
  146. this.$form.find(`input[name="location_ids[]"]`).remove();
  147. exportIds.forEach(locationId => {
  148. this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
  149. });
  150. }
  151. onSubmit(event) {
  152. event.preventDefault();
  153. const projectId = this.$projectDropdown.val();
  154. const data = this.$form.serialize();
  155. $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
  156. if(response.success) {
  157. if(response.dupeLocations.length > 0) {
  158. const locationNames = response.dupeLocations.map(location => location.name).join("\n");
  159. alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
  160. }
  161. else {
  162. alert("Locations added to project successfully.");
  163. }
  164. }
  165. this.$modal.modal("hide");
  166. }).catch(error => {
  167. alert(`An error has occurred: ${error}`);
  168. });
  169. }
  170. }