app-locations.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
  63. });
  64. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  65. event.preventDefault();
  66. const $form = $("#locations-export-form");
  67. const action = $(event.currentTarget).attr("data-action");
  68. $form.find("input[name='ids[]']").remove();
  69. $form.find("input[name='action']").val(action);
  70. for(const i in exportIds) {
  71. if(!exportIds.hasOwnProperty(i)) continue;
  72. let locationId = exportIds[i];
  73. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  74. }
  75. $form.trigger("submit");
  76. });
  77. const $createProjectButton = $("#create-project-button");
  78. $createProjectButton.on("click", function(event) {
  79. event.preventDefault();
  80. if(exportIds.length === 0) return;
  81. const $form = $("#locations-create-project-form");
  82. $form.find("input[name='location_ids']").remove();
  83. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  84. $form.trigger("submit");
  85. });
  86. const $addLocationsToProjectButton = $("#add-locations-to-project-button");
  87. new AddLocationsToProjectModal();
  88. }
  89. function displayInfoWindow(marker) {
  90. const locationId = parseInt(marker.location.id);
  91. const index = exportIds.indexOf(locationId);
  92. const archived = marker.location.archived;
  93. let badge = "";
  94. if(archived) {
  95. badge = `<span class="badge badge-secondary">Archived</span>`;
  96. }
  97. let exportText = "Select";
  98. let exportClass = "btn-primary";
  99. if(index > -1) {
  100. exportText = "Deselect";
  101. exportClass = "btn-success";
  102. }
  103. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  104. <p>
  105. ${marker.location.name}
  106. ${badge}
  107. </p>
  108. <div class="form-row">
  109. <div class="col">
  110. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  111. </div>
  112. <div class="col">
  113. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  114. </div>
  115. </div>
  116. </div>`;
  117. infoWindow.setContent(content);
  118. infoWindow.open(map, marker);
  119. }
  120. class AddLocationsToProjectModal {
  121. constructor() {
  122. this.$modal = $("#add-locations-to-project-modal");
  123. this.$modal.on("show.bs.modal", event => this.onShow(event));
  124. this.$form = this.$modal.find("form");
  125. this.$form.on("submit", event => this.onSubmit(event));
  126. this.$projectDropdown = this.$form.find("[name=project_id]");
  127. }
  128. onShow(event) {
  129. this.$form.find(`input[name="location_ids[]"]`).remove();
  130. exportIds.forEach(locationId => {
  131. this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
  132. });
  133. }
  134. onSubmit(event) {
  135. event.preventDefault();
  136. const projectId = this.$projectDropdown.val();
  137. const data = this.$form.serialize();
  138. $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
  139. if(response.success) {
  140. if(response.dupeLocations.length > 0) {
  141. const locationNames = response.dupeLocations.map(location => location.name).join("\n");
  142. alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
  143. }
  144. else {
  145. alert("Locations added to project successfully.");
  146. }
  147. }
  148. console.log(response);
  149. this.$modal.modal("hide");
  150. }).catch(error => {
  151. console.error(error);
  152. });
  153. }
  154. }