app-user.js 6.2 KB

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