1
0

app-locations.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. $.get(`/api/customers/${customerId}/get-locations`, {
  13. id: $("#id").val(),
  14. internal_id: $("#internal-id").val(),
  15. name: $("#name").val(),
  16. legal_entity_name: $("#legal-entity-name").val(),
  17. brand_name: $("#brand-name").val(),
  18. region_name: $("#region-name").val(),
  19. area_manager_name: $("#area-manager-name").val(),
  20. operations_manager_name: $("#operations-manager-name").val(),
  21. gaming_manager_name: $("#gaming-manager-name").val(),
  22. senior_gaming_manager_name: $("#senior-gaming-manager-name").val(),
  23. include_archived: $("#include-archived").prop("checked") ? 1 : 0,
  24. include_closed: $("#include-closed").prop("checked") ? 1 : 0,
  25. filter: $("#filter").val(),
  26. min_date: $("#min-date").val(),
  27. max_date: $("#max-date").val(),
  28. }, function(response) {
  29. if(!response.success) return false;
  30. if(!response.locations) return false;
  31. let map = maps["map"];
  32. let bounds = new google.maps.LatLngBounds;
  33. let locationCount = response.locations.length;
  34. const $markerCountSpan = $("#map-marker-count");
  35. if(locationCount > 0) {
  36. $markerCountSpan.text(locationCount);
  37. }
  38. else {
  39. $markerCountSpan.text("");
  40. }
  41. response.locations.forEach(location => {
  42. let position = {
  43. lat: location.latitude,
  44. lng: location.longitude
  45. };
  46. let options = {
  47. map: map,
  48. position: position,
  49. title: location.name
  50. };
  51. if(!location.icon) {
  52. options.icon = "/img/markers/building-red.png";
  53. }
  54. else {
  55. options.icon = "/img/markers/" + location.icon;
  56. }
  57. bounds.extend(position);
  58. let key = getMarkerKey(location.id);
  59. let marker = new google.maps.Marker(options);
  60. marker.location = location;
  61. marker.originalIcon = options.icon;
  62. marker.addListener("click", function() {
  63. displayInfoWindow(this);
  64. });
  65. markerMap[key] = marker;
  66. });
  67. map.fitBounds(bounds);
  68. });
  69. $(document).on("click", "a[data-export-location-id]", function(event) {
  70. event.preventDefault();
  71. let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
  72. let index = exportIds.indexOf(locationId);
  73. let key = getMarkerKey(locationId);
  74. let marker = markerMap[key];
  75. if(index === -1) {
  76. exportIds.push(locationId);
  77. marker.setOptions({
  78. icon: "/img/markers/building-green.png"
  79. });
  80. }
  81. else {
  82. exportIds.splice(index, 1);
  83. marker.setOptions({
  84. icon: marker.originalIcon
  85. });
  86. }
  87. displayInfoWindow(marker);
  88. $createProjectButton.prop("disabled", (exportIds.length === 0));
  89. $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
  90. });
  91. $("#export-csv-button, #export-kml-button").on("click", function(event) {
  92. event.preventDefault();
  93. const $form = $("#locations-export-form");
  94. const action = $(event.currentTarget).attr("data-action");
  95. $form.find("input[name='ids[]']").remove();
  96. $form.find("input[name='action']").val(action);
  97. for(const i in exportIds) {
  98. if(!exportIds.hasOwnProperty(i)) continue;
  99. let locationId = exportIds[i];
  100. $form.append("<input type=\"hidden\" name=\"ids[]\" value=\"" + locationId + "\" />");
  101. }
  102. $form.trigger("submit");
  103. });
  104. const $createProjectButton = $("#create-project-button");
  105. $createProjectButton.on("click", function(event) {
  106. event.preventDefault();
  107. if(exportIds.length === 0) return;
  108. const $form = $("#locations-create-project-form");
  109. $form.find("input[name='location_ids']").remove();
  110. $form.append("<input type=\"hidden\" name=\"location_ids\" value=\"" + exportIds.join(", ") + "\" />");
  111. $form.trigger("submit");
  112. });
  113. const $addLocationsToProjectButton = $("#add-locations-to-project-button");
  114. new AddLocationsToProjectModal();
  115. $(".btn-map").on("click", event => {
  116. const locationId = $(event.currentTarget).data("location-id");
  117. const key = getMarkerKey(locationId);
  118. const marker = markerMap[key];
  119. const map = maps.map;
  120. const center = marker.getPosition();
  121. map.setCenter(center);
  122. map.setZoom(14);
  123. displayInfoWindow(markerMap[key]);
  124. });
  125. }
  126. function displayInfoWindow(marker) {
  127. const escapeText = (text) => {
  128. const span = document.createElement("span");
  129. span.innerText = text;
  130. return span.innerHTML;
  131. };
  132. const locationId = parseInt(marker.location.id);
  133. const index = exportIds.indexOf(locationId);
  134. const location = marker.location;
  135. const badges = [];
  136. const lines = [];
  137. if(location.doNotTest) {
  138. badges.push(`<span class="badge badge-warning">Do Not Test</span>`);
  139. }
  140. if(location.archived) {
  141. badges.push(`<span class="badge badge-secondary">Archived</span>`);
  142. }
  143. if(location.closed) {
  144. badges.push(`<span class="badge badge-danger">Closed</span>`);
  145. }
  146. lines.push({title: "Internal ID", text: location.internalId});
  147. lines.push({title: "Location ID", text: location.id});
  148. if(location.test) {
  149. lines.push({divider: true});
  150. lines.push({title: "Test ID", html: `<a href="/account/tests/${location.test.id}" target="_blank">${location.test.id}</a>`});
  151. if(location.test.formattedStartDts) {
  152. lines.push({title: "Date/Time", text: location.test.formattedStartDts});
  153. }
  154. if(location.test.tester) {
  155. const { id, name } = location.test.tester;
  156. lines.push({title: "Tester ID", text: id});
  157. lines.push({title: "Tester Name", html: `<a href="/account/users/${id}" target="_blank">${name}</a>`});
  158. }
  159. if(location.test.pastDue) {
  160. badges.push(`<span class="badge badge-danger">Past Due</span>`);
  161. }
  162. if(location.test.formattedDueDts) {
  163. lines.push({title: "Due", text: location.test.formattedDueDts});
  164. }
  165. if(location.test) {
  166. lines.push({title: "Result", text: location.test.resultText});
  167. }
  168. }
  169. let linesHtml = "";
  170. lines.forEach(line => {
  171. if(line.divider) {
  172. linesHtml += "<hr />\n";
  173. }
  174. if(line.title) {
  175. const titleHtml = escapeText(line.title);
  176. let contentHtml = line.html;
  177. if(line.text) {
  178. contentHtml = escapeText(line.text);
  179. }
  180. linesHtml += `<div class="small"><strong>${titleHtml}:</strong> ${contentHtml}</div>`;
  181. }
  182. });
  183. let exportText = "Select";
  184. let exportClass = "btn-primary";
  185. if(index > -1) {
  186. exportText = "Deselect";
  187. exportClass = "btn-success";
  188. }
  189. const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
  190. <div class="mb-1">
  191. ${marker.location.name}<br />
  192. ${badges.join(" ")}
  193. </div>
  194. <hr />
  195. ${linesHtml}
  196. <hr />
  197. <div class="form-row">
  198. <div class="col">
  199. <a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
  200. </div>
  201. <div class="col">
  202. <a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
  203. </div>
  204. </div>
  205. </div>`;
  206. infoWindow.setContent(content);
  207. infoWindow.open(map, marker);
  208. }
  209. class AddLocationsToProjectModal {
  210. constructor() {
  211. this.$modal = $("#add-locations-to-project-modal");
  212. this.$modal.on("show.bs.modal", event => this.onShow(event));
  213. this.$form = this.$modal.find("form");
  214. this.$form.on("submit", event => this.onSubmit(event));
  215. this.$projectDropdown = this.$form.find("[name=project_id]");
  216. }
  217. onShow() {
  218. this.$form.find(`input[name="location_ids[]"]`).remove();
  219. exportIds.forEach(locationId => {
  220. this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
  221. });
  222. }
  223. onSubmit(event) {
  224. event.preventDefault();
  225. const projectId = this.$projectDropdown.val();
  226. const data = this.$form.serialize();
  227. $.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
  228. if(response.success) {
  229. if(response.dupeLocations.length > 0) {
  230. const locationNames = response.dupeLocations.map(location => location.name).join("\n");
  231. alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
  232. }
  233. else {
  234. alert("Locations added to project successfully.");
  235. }
  236. }
  237. this.$modal.modal("hide");
  238. }).catch(error => {
  239. alert(`An error has occurred: ${error}`);
  240. });
  241. }
  242. }