app-user.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 listGroupItem = $("div[data-nearby-location-list-group-item-template]").clone();
  98. listGroupItem.removeAttr("data-nearby-location-list-group-item-template");
  99. listGroupItem.find("a[data-location-name]").text(location.name);
  100. listGroupItem.find("span[data-location-distance]").text(location.distance + " miles");
  101. listGroupItem.find("span[data-location-id]").text(location.id);
  102. listGroupItem.find("span[data-location-internal-id]").text(location.internalId);
  103. listGroupItem.find("small[data-location-address]").text(location.address);
  104. listGroupItem.appendTo("#nearby-locations-list-group").show();
  105. addMarkerClickListenerForLocation(marker, location);
  106. }
  107. map.fitBounds(bounds);
  108. });
  109. }
  110. function addMarkerClickListenerForLocation(m, l) {
  111. let map = maps["map"];
  112. m.addListener("click", function() {
  113. let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
  114. if(l.internalId) {
  115. content += "<div class=\"small\"><strong>Internal ID:</strong> " + l.internalId + "</div>";
  116. }
  117. content += "<div class=\"small\"><strong>Location ID:</strong> " + l.id + "</div>";
  118. if(l.test) {
  119. content += '<hr />';
  120. content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + l.test.id + '" target="_blank">' + l.test.id + '</a></div>';
  121. if(l.test.formattedStartDts) {
  122. content += '<div class="small"><strong>Date/Time:</strong> ' + l.test.formattedStartDts + '</div>';
  123. }
  124. if(l.test.tester) {
  125. content += '<div class="small"><strong>Tester ID:</strong> ' + l.test.tester.id + '</div>';
  126. content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + l.test.tester.id + '" target="_blank">' + l.test.tester.name + '</a></div>';
  127. }
  128. let pastDueBadge = '';
  129. if(l.test.pastDue) {
  130. pastDueBadge = '<span class="badge badge-lg badge-primary">Past Due</span>';
  131. }
  132. if(l.test.formattedDueDts) {
  133. content += '<div class="small"><strong>Due:</strong> ' + l.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
  134. }
  135. }
  136. infoWindow.setContent(content);
  137. infoWindow.open(map, m);
  138. });
  139. }