app-user.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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").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. let marker = locationMarkers[i];
  61. marker.setMap(null);
  62. marker = null;
  63. }
  64. locationMarkers = [];
  65. $("#nearby-locations-list-group").empty();
  66. let map = maps["nearby-locations-map"];
  67. $.get("/api/users/" + userId + "/get-locations", {
  68. "address_id": $("#address-id").val(),
  69. "date_start": $("#date-start").val(),
  70. "date_end": $("#date-end").val()
  71. }, function(response) {
  72. if(!response.success) return false;
  73. if(!response.locations) return false;
  74. let bounds = new google.maps.LatLngBounds;
  75. // User Address
  76. let position = {
  77. lat: response.latitude,
  78. lng: response.longitude
  79. };
  80. let options = {
  81. map: map,
  82. position: position,
  83. title: response.name,
  84. icon: "/img/markers/address.png"
  85. }
  86. bounds.extend(position);
  87. let marker = new google.maps.Marker(options);
  88. locationMarkers.push(marker);
  89. // Locations
  90. for(let i in response.locations) {
  91. let location = response.locations[i];
  92. let position = {
  93. lat: location.latitude,
  94. lng: location.longitude
  95. };
  96. let options = {
  97. map: map,
  98. position: position,
  99. title: location.name
  100. }
  101. if(location.icon) options.icon = "/img/markers/" + location.icon;
  102. else options.icon = "/img/markers/building-red.png";
  103. bounds.extend(position);
  104. let marker = new google.maps.Marker(options);
  105. locationMarkers.push(marker);
  106. if(location.internalId == 0) {
  107. location.internalId = "N/A";
  108. }
  109. // Add to list group
  110. let lgi = $("div[data-nearby-location-list-group-item-template]").clone();
  111. lgi.removeAttr("data-nearby-location-list-group-item-template");
  112. lgi.find("a[data-location-name]").text(location.name).attr("href", "/account/locations/" + location.id);
  113. lgi.find("a[data-customer-name]").text(location.customer.name).attr("href", "/account/customers/" + location.customer.id);
  114. lgi.find("span[data-location-distance]").text(location.distance + " miles");
  115. lgi.find("span[data-location-id]").text(location.id);
  116. lgi.find("span[data-location-internal-id]").text(location.internalId);
  117. lgi.find("small[data-location-address]").text(location.address);
  118. if(location.test) {
  119. let test = location.test;
  120. let tester = test.tester;
  121. let project = test.project;
  122. lgi.find("div[data-test-container]").removeClass("d-none");
  123. lgi.find("div[data-test-id-container] a").text(test.id).attr("href", "/account/tests/" + test.id);
  124. lgi.find("div[data-test-project-name-container] a").text(project.name).attr("href", "/account/projects/" + project.id);
  125. if(tester) {
  126. lgi.find("div[data-test-user-name-container] a").text(tester.name).attr("href", "/account/users/" + tester.id);
  127. let passText = "";
  128. let passClass = "";
  129. if(test.pass === null) {
  130. passText = "Pending";
  131. passClass = "warning";
  132. lgi.find("div[data-test-due-dts-container]").removeClass("d-none").find("span").text(test.formattedDueDts);
  133. if(test.pastDue) {
  134. lgi.find("div[data-test-due-dts-container]").append(" <span class=\"badge badge-danger\">Overdue</span>");
  135. }
  136. }
  137. else if(test.pass === 0) {
  138. passText = "Fail";
  139. passClass = "danger";
  140. }
  141. else if(test.pass === 1) {
  142. passText = "Pass";
  143. passClass = "success";
  144. }
  145. if(test.pass !== null) {
  146. lgi.find("div[data-test-start-dts-container]").removeClass("d-none").find("span").text(test.formattedStartDts);
  147. }
  148. lgi.find("div[data-test-pass-container] span").addClass("text-" + passClass).text(passText);
  149. }
  150. }
  151. lgi.appendTo("#nearby-locations-list-group").show();
  152. addMarkerClickListenerForLocation(marker, location);
  153. }
  154. map.fitBounds(bounds);
  155. });
  156. }
  157. function addMarkerClickListenerForLocation(m, l) {
  158. let map = maps["map"];
  159. m.addListener("click", function() {
  160. let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
  161. if(l.internalId) {
  162. content += "<div class=\"small\"><strong>Internal ID:</strong> " + l.internalId + "</div>";
  163. }
  164. content += "<div class=\"small\"><strong>Location ID:</strong> " + l.id + "</div>";
  165. if(l.test) {
  166. content += '<hr />';
  167. content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + l.test.id + '" target="_blank">' + l.test.id + '</a></div>';
  168. if(l.test.formattedStartDts) {
  169. content += '<div class="small"><strong>Date/Time:</strong> ' + l.test.formattedStartDts + '</div>';
  170. }
  171. if(l.test.tester) {
  172. content += '<div class="small"><strong>Tester ID:</strong> ' + l.test.tester.id + '</div>';
  173. content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + l.test.tester.id + '" target="_blank">' + l.test.tester.name + '</a></div>';
  174. }
  175. let pastDueBadge = '';
  176. if(l.test.pastDue) {
  177. pastDueBadge = '<span class="badge badge-lg badge-primary">Overdue</span>';
  178. }
  179. if(l.test.formattedDueDts) {
  180. content += '<div class="small"><strong>Due:</strong> ' + l.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
  181. }
  182. }
  183. infoWindow.setContent(content);
  184. infoWindow.open(map, m);
  185. });
  186. }