app-locations.js 5.3 KB

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