1
0

app-user.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. var addressesInitialised = false;
  2. var locationsInitialised = false;
  3. var addressMarkers = [];
  4. var locationMarkers = [];
  5. var infoWindow = null;
  6. $(function() {
  7. $("[data-toggle=tab]").on("shown.bs.tab", function(event) {
  8. const $tab = $(event.currentTarget);
  9. if(event.target.id === "addresses-tab" && addressesInitialised === false) {
  10. initialiseAddresses();
  11. }
  12. if(event.target.id === "tests-tab") {
  13. refreshTestLocations();
  14. }
  15. if(event.target.id === "nearby-locations-tab") {
  16. refreshNearbyLocations();
  17. }
  18. const id = $tab.attr("id");
  19. $tab.removeAttr("id");
  20. window.location.hash = id;
  21. $tab.attr("id", id);
  22. });
  23. if(window.location.hash) {
  24. const id = window.location.hash.substring(1);
  25. $(`#${id}`).tab("show");
  26. }
  27. $("#address-id").on("change", function() {
  28. refreshNearbyLocations();
  29. });
  30. $("#date-range").daterangepicker({
  31. locale: {
  32. format: "DD/MM/YYYY"
  33. }
  34. }, function(start, end, label) {
  35. var startValue = start.format('YYYY-MM-DD');
  36. var endValue = end.format('YYYY-MM-DD');
  37. $("#date-start").val(startValue);
  38. $("#date-end").val(endValue);
  39. refreshNearbyLocations();
  40. });
  41. $("#pending-tests").on("change", () => refreshNearbyLocations());
  42. $("#delete-address-form button[type=submit]").on("click", event => {
  43. const confirmed = confirm("Are you sure you want to delete this address?");
  44. if(!confirmed) {
  45. event.preventDefault();
  46. }
  47. });
  48. });
  49. function initialiseAddresses() {
  50. let map = maps["map"];
  51. $.get("/api/users/" + userId + "/get-addresses", {
  52. }, function(response) {
  53. if(!response.success) return false;
  54. if(!response.addresses) return false;
  55. let bounds = new google.maps.LatLngBounds;
  56. for(let i in response.addresses) {
  57. let address = response.addresses[i];
  58. let position = {
  59. lat: address.latitude,
  60. lng: address.longitude
  61. };
  62. bounds.extend(position);
  63. let marker = new google.maps.Marker({
  64. map: map,
  65. position: position,
  66. title: address.name,
  67. icon: "/img/markers/address.png"
  68. });
  69. }
  70. map.fitBounds(bounds);
  71. });
  72. }
  73. function clearMaps() {
  74. if(infoWindow === null) {
  75. infoWindow = new google.maps.InfoWindow();
  76. }
  77. locationMarkers.forEach(marker => {
  78. marker.setMap(null);
  79. marker = null;
  80. });
  81. locationMarkers = [];
  82. }
  83. function refreshTestLocations() {
  84. clearMaps();
  85. let map = maps["assigned-tests-map"];
  86. if(!map) {
  87. return;
  88. }
  89. $.get("/api/users/" + userId + "/get-assigned-locations", response => {
  90. if(!response.success) return false;
  91. if(!response.locations) return false;
  92. let bounds = new google.maps.LatLngBounds();
  93. // Extend bounds for locations
  94. response.locations.forEach(location => {
  95. bounds.extend({
  96. lat: location.latitude,
  97. lng: location.longitude
  98. });
  99. });
  100. // Add locations to map
  101. addLocationsToMap(response.locations, map);
  102. map.fitBounds(bounds);
  103. });
  104. }
  105. function refreshNearbyLocations() {
  106. clearMaps();
  107. $("#nearby-locations-list-group").empty();
  108. let map = maps["nearby-locations-map"];
  109. $.get("/api/users/" + userId + "/get-locations", {
  110. address_id: $("#address-id").val(),
  111. date_start: $("#date-start").val(),
  112. date_end: $("#date-end").val(),
  113. pending_tests: $("#pending-tests").is(":checked") ? 1 : 0
  114. }, function(response) {
  115. if(!response.success) return false;
  116. if(!response.locations) return false;
  117. let bounds = new google.maps.LatLngBounds();
  118. // User Address
  119. let position = {
  120. lat: response.latitude,
  121. lng: response.longitude
  122. };
  123. bounds.extend(position);
  124. let marker = new google.maps.Marker({
  125. map: map,
  126. position: position,
  127. title: response.name,
  128. icon: "/img/markers/address.png"
  129. });
  130. locationMarkers.push(marker);
  131. // Extend bounds for locations
  132. response.locations.forEach(location => {
  133. bounds.extend({
  134. lat: location.latitude,
  135. lng: location.longitude
  136. });
  137. });
  138. // Add locations to map
  139. addLocationsToMap(response.locations, map);
  140. // Add locations to nearby locations list group
  141. response.locations.forEach(location => {
  142. // Add to list group
  143. let lgi = $("div[data-nearby-location-list-group-item-template]").clone();
  144. lgi.removeAttr("data-nearby-location-list-group-item-template");
  145. lgi.find("a[data-location-name]").text(location.name).attr("href", "/account/locations/" + location.id);
  146. lgi.find("a[data-customer-name]").text(location.customer.name).attr("href", "/account/customers/" + location.customer.id);
  147. lgi.find("span[data-location-distance]").text(location.distance + " miles");
  148. lgi.find("span[data-location-id]").text(location.id);
  149. lgi.find("span[data-location-internal-id]").text(location.internalId);
  150. lgi.find("small[data-location-address]").text(location.address);
  151. if(location.test) {
  152. let test = location.test;
  153. let tester = test.tester;
  154. let project = test.project;
  155. lgi.find("div[data-test-container]").removeClass("d-none");
  156. lgi.find("div[data-test-id-container] a").text(test.id).attr("href", "/account/tests/" + test.id);
  157. lgi.find("div[data-test-project-name-container] a").text(project.name).attr("href", "/account/projects/" + project.id);
  158. if(tester) {
  159. lgi.find("div[data-test-user-name-container] a").text(tester.name).attr("href", "/account/users/" + tester.id);
  160. let passText = "";
  161. let passClass = "";
  162. if(test.pass === null) {
  163. passText = "Pending";
  164. passClass = "warning";
  165. lgi.find("div[data-test-due-dts-container]").removeClass("d-none").find("span").text(test.formattedDueDts);
  166. if(test.pastDue) {
  167. lgi.find("div[data-test-due-dts-container]").append(" <span class=\"badge badge-danger\">Overdue</span>");
  168. }
  169. }
  170. else if(test.pass === 0) {
  171. passText = "Fail";
  172. passClass = "danger";
  173. }
  174. else if(test.pass === 1) {
  175. passText = "Pass";
  176. passClass = "success";
  177. }
  178. if(test.pass !== null) {
  179. lgi.find("div[data-test-start-dts-container]").removeClass("d-none").find("span").text(test.formattedStartDts);
  180. }
  181. lgi.find("div[data-test-pass-container] span").addClass("text-" + passClass).text(passText);
  182. }
  183. }
  184. lgi.appendTo("#nearby-locations-list-group").show();
  185. });
  186. map.fitBounds(bounds);
  187. });
  188. }
  189. function addLocationsToMap(locations, map) {
  190. locations.forEach(location => {
  191. let position = {
  192. lat: location.latitude,
  193. lng: location.longitude
  194. };
  195. let options = {
  196. map: map,
  197. position: position,
  198. title: location.name
  199. };
  200. if(location.icon) {
  201. options.icon = "/img/markers/" + location.icon;
  202. }
  203. else {
  204. options.icon = "/img/markers/building-red.png";
  205. }
  206. let marker = new google.maps.Marker(options);
  207. locationMarkers.push(marker);
  208. if(location.internalId == 0) {
  209. location.internalId = "N/A";
  210. }
  211. addMarkerClickListenerForLocation(marker, location);
  212. });
  213. }
  214. function addMarkerClickListenerForLocation(marker, location) {
  215. marker.addListener("click", function() {
  216. let content = "<strong><a href=\"/account/locations/" + location.id + "\" target=\"_blank\">" + location.name + "</a></strong><br />";
  217. if(location.internalId) {
  218. content += "<div class=\"small\"><strong>Internal ID:</strong> " + location.internalId + "</div>";
  219. }
  220. content += "<div class=\"small\"><strong>Location ID:</strong> " + location.id + "</div>";
  221. if(location.test) {
  222. content += '<hr />';
  223. content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + location.test.id + '" target="_blank">' + location.test.id + '</a></div>';
  224. content += '<div class="small"><strong>Project:</strong> <a href="/account/projects/' + location.test.project.id + '" target="_blank">' + location.test.project.name + '</a></div>';
  225. if(location.test.formattedStartDts) {
  226. content += '<div class="small"><strong>Date/Time:</strong> ' + location.test.formattedStartDts + '</div>';
  227. }
  228. if(location.test.tester) {
  229. content += '<div class="small"><strong>Tester ID:</strong> ' + location.test.tester.id + '</div>';
  230. content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + location.test.tester.id + '" target="_blank">' + location.test.tester.name + '</a></div>';
  231. }
  232. let pastDueBadge = '';
  233. if(location.test.pastDue) {
  234. pastDueBadge = '<span class="badge badge-lg badge-primary">Overdue</span>';
  235. }
  236. if(location.test.formattedDueDts) {
  237. content += '<div class="small"><strong>Due:</strong> ' + location.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
  238. }
  239. }
  240. infoWindow.setContent(content);
  241. infoWindow.open(marker.getMap(), marker);
  242. });
  243. }