app-user.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. var addressesInitialised = false;
  2. var locationsInitialised = false;
  3. var addressMarkers = [];
  4. var locationMarkers = [];
  5. var infoWindow = false;
  6. $(function() {
  7. $("[data-toggle=tab]").on("shown.bs.tab", function(event) {
  8. if(event.target.id === "addresses-tab" && addressesInitialised === false) {
  9. initialiseAddresses();
  10. }
  11. if(event.target.id === "nearby-locations-tab" && locationsInitialised === false) {
  12. initialiseLocations();
  13. }
  14. });
  15. $("#address-id").on("change", function() {
  16. initialiseLocations();
  17. });
  18. $("#date-range").daterangepicker({
  19. locale: {
  20. format: "DD/MM/YYYY"
  21. }
  22. }, function(start, end, label) {
  23. var startValue = start.format('YYYY-MM-DD');
  24. var endValue = end.format('YYYY-MM-DD');
  25. $("#date-start").val(startValue);
  26. $("#date-end").val(endValue);
  27. initialiseLocations();
  28. });
  29. });
  30. function initialiseAddresses() {
  31. let map = maps["map"];
  32. $.get("/api/users/" + userId + "/get-addresses", {
  33. }, function(response) {
  34. if(!response.success) return false;
  35. if(!response.addresses) return false;
  36. let bounds = new google.maps.LatLngBounds;
  37. for(let i in response.addresses) {
  38. let address = response.addresses[i];
  39. let position = {
  40. lat: address.latitude,
  41. lng: address.longitude
  42. };
  43. let options = {
  44. map: map,
  45. position: position,
  46. title: address.name,
  47. icon: "/img/markers/address.png"
  48. };
  49. bounds.extend(position);
  50. let marker = new google.maps.Marker(options);
  51. }
  52. map.fitBounds(bounds);
  53. });
  54. }
  55. function initialiseLocations() {
  56. if(infoWindow === false) {
  57. infoWindow = new google.maps.InfoWindow();
  58. }
  59. for(var i in locationMarkers) {
  60. if(!locationMarkers.hasOwnProperty(i)) continue;
  61. let marker = locationMarkers[i];
  62. marker.setMap(null);
  63. marker = null;
  64. }
  65. locationMarkers = [];
  66. $("#nearby-locations-list-group").empty();
  67. let map = maps["nearby-locations-map"];
  68. $.get("/api/users/" + userId + "/get-locations", {
  69. "address_id": $("#address-id").val(),
  70. "date_start": $("#date-start").val(),
  71. "date_end": $("#date-end").val()
  72. }, function(response) {
  73. if(!response.success) return false;
  74. if(!response.locations) return false;
  75. let bounds = new google.maps.LatLngBounds;
  76. // User Address
  77. let position = {
  78. lat: response.latitude,
  79. lng: response.longitude
  80. };
  81. let options = {
  82. map: map,
  83. position: position,
  84. title: response.name,
  85. icon: "/img/markers/address.png"
  86. };
  87. bounds.extend(position);
  88. let marker = new google.maps.Marker(options);
  89. locationMarkers.push(marker);
  90. // Locations
  91. for(let i in response.locations) {
  92. if(!response.locations.hasOwnProperty(i)) continue;
  93. let location = response.locations[i];
  94. let position = {
  95. lat: location.latitude,
  96. lng: location.longitude
  97. };
  98. let options = {
  99. map: map,
  100. position: position,
  101. title: location.name
  102. };
  103. if(location.icon) options.icon = "/img/markers/" + location.icon;
  104. else options.icon = "/img/markers/building-red.png";
  105. bounds.extend(position);
  106. let marker = new google.maps.Marker(options);
  107. locationMarkers.push(marker);
  108. if(location.internalId == 0) {
  109. location.internalId = "N/A";
  110. }
  111. // Add to list group
  112. let lgi = $("div[data-nearby-location-list-group-item-template]").clone();
  113. lgi.removeAttr("data-nearby-location-list-group-item-template");
  114. lgi.find("a[data-location-name]").text(location.name).attr("href", "/account/locations/" + location.id);
  115. lgi.find("a[data-customer-name]").text(location.customer.name).attr("href", "/account/customers/" + location.customer.id);
  116. lgi.find("span[data-location-distance]").text(location.distance + " miles");
  117. lgi.find("span[data-location-id]").text(location.id);
  118. lgi.find("span[data-location-internal-id]").text(location.internalId);
  119. lgi.find("small[data-location-address]").text(location.address);
  120. if(location.test) {
  121. let test = location.test;
  122. let tester = test.tester;
  123. let project = test.project;
  124. lgi.find("div[data-test-container]").removeClass("d-none");
  125. lgi.find("div[data-test-id-container] a").text(test.id).attr("href", "/account/tests/" + test.id);
  126. lgi.find("div[data-test-project-name-container] a").text(project.name).attr("href", "/account/projects/" + project.id);
  127. if(tester) {
  128. lgi.find("div[data-test-user-name-container] a").text(tester.name).attr("href", "/account/users/" + tester.id);
  129. let passText = "";
  130. let passClass = "";
  131. if(test.pass === null) {
  132. passText = "Pending";
  133. passClass = "warning";
  134. lgi.find("div[data-test-due-dts-container]").removeClass("d-none").find("span").text(test.formattedDueDts);
  135. if(test.pastDue) {
  136. lgi.find("div[data-test-due-dts-container]").append(" <span class=\"badge badge-danger\">Overdue</span>");
  137. }
  138. }
  139. else if(test.pass === 0) {
  140. passText = "Fail";
  141. passClass = "danger";
  142. }
  143. else if(test.pass === 1) {
  144. passText = "Pass";
  145. passClass = "success";
  146. }
  147. if(test.pass !== null) {
  148. lgi.find("div[data-test-start-dts-container]").removeClass("d-none").find("span").text(test.formattedStartDts);
  149. }
  150. lgi.find("div[data-test-pass-container] span").addClass("text-" + passClass).text(passText);
  151. }
  152. }
  153. lgi.appendTo("#nearby-locations-list-group").show();
  154. addMarkerClickListenerForLocation(marker, location);
  155. }
  156. map.fitBounds(bounds);
  157. });
  158. }
  159. function addMarkerClickListenerForLocation(m, l) {
  160. let map = maps["map"];
  161. m.addListener("click", function() {
  162. let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
  163. if(l.internalId) {
  164. content += "<div class=\"small\"><strong>Internal ID:</strong> " + l.internalId + "</div>";
  165. }
  166. content += "<div class=\"small\"><strong>Location ID:</strong> " + l.id + "</div>";
  167. if(l.test) {
  168. content += '<hr />';
  169. content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + l.test.id + '" target="_blank">' + l.test.id + '</a></div>';
  170. if(l.test.formattedStartDts) {
  171. content += '<div class="small"><strong>Date/Time:</strong> ' + l.test.formattedStartDts + '</div>';
  172. }
  173. if(l.test.tester) {
  174. content += '<div class="small"><strong>Tester ID:</strong> ' + l.test.tester.id + '</div>';
  175. content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + l.test.tester.id + '" target="_blank">' + l.test.tester.name + '</a></div>';
  176. }
  177. let pastDueBadge = '';
  178. if(l.test.pastDue) {
  179. pastDueBadge = '<span class="badge badge-lg badge-primary">Overdue</span>';
  180. }
  181. if(l.test.formattedDueDts) {
  182. content += '<div class="small"><strong>Due:</strong> ' + l.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
  183. }
  184. }
  185. infoWindow.setContent(content);
  186. infoWindow.open(map, m);
  187. });
  188. }