| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- var addressesInitialised = false;
- var locationsInitialised = false;
- var addressMarkers = [];
- var locationMarkers = [];
- var infoWindow = null;
- $(function() {
- $("[data-toggle=tab]").on("shown.bs.tab", function(event) {
- const $tab = $(event.currentTarget);
- if(event.target.id === "addresses-tab" && addressesInitialised === false) {
- initialiseAddresses();
- }
- if(event.target.id === "tests-tab") {
- refreshTestLocations();
- }
- if(event.target.id === "nearby-locations-tab") {
- refreshNearbyLocations();
- }
- const id = $tab.attr("id");
- $tab.removeAttr("id");
- window.location.hash = id;
- $tab.attr("id", id);
- });
- if(window.location.hash) {
- const id = window.location.hash.substring(1);
- $(`#${id}`).tab("show");
- }
- $("#address-id").on("change", function() {
- refreshNearbyLocations();
- });
- $("#date-range").daterangepicker({
- locale: {
- format: "DD/MM/YYYY"
- }
- }, function(start, end, label) {
- var startValue = start.format('YYYY-MM-DD');
- var endValue = end.format('YYYY-MM-DD');
- $("#date-start").val(startValue);
- $("#date-end").val(endValue);
- refreshNearbyLocations();
- });
- $("#pending-tests").on("change", () => refreshNearbyLocations());
- $("#delete-address-form button[type=submit]").on("click", event => {
- const confirmed = confirm("Are you sure you want to delete this address?");
- if(!confirmed) {
- event.preventDefault();
- }
- });
- });
- function initialiseAddresses() {
- let map = maps["map"];
- $.get("/api/users/" + userId + "/get-addresses", {
- }, function(response) {
- if(!response.success) return false;
- if(!response.addresses) return false;
- let bounds = new google.maps.LatLngBounds;
- for(let i in response.addresses) {
- let address = response.addresses[i];
- let position = {
- lat: address.latitude,
- lng: address.longitude
- };
- bounds.extend(position);
- let marker = new google.maps.Marker({
- map: map,
- position: position,
- title: address.name,
- icon: "/img/markers/address.png"
- });
- }
- map.fitBounds(bounds);
- });
- }
- function clearMaps() {
- if(infoWindow === null) {
- infoWindow = new google.maps.InfoWindow();
- }
- locationMarkers.forEach(marker => {
- marker.setMap(null);
- marker = null;
- });
- locationMarkers = [];
- }
- function refreshTestLocations() {
- clearMaps();
- let map = maps["assigned-tests-map"];
- if(!map) {
- return;
- }
- $.get("/api/users/" + userId + "/get-assigned-locations", response => {
- if(!response.success) return false;
- if(!response.locations) return false;
- let bounds = new google.maps.LatLngBounds();
- // Extend bounds for locations
- response.locations.forEach(location => {
- bounds.extend({
- lat: location.latitude,
- lng: location.longitude
- });
- });
- // Add locations to map
- addLocationsToMap(response.locations, map);
- map.fitBounds(bounds);
- });
- }
- function refreshNearbyLocations() {
- clearMaps();
- $("#nearby-locations-list-group").empty();
- let map = maps["nearby-locations-map"];
- $.get("/api/users/" + userId + "/get-locations", {
- address_id: $("#address-id").val(),
- date_start: $("#date-start").val(),
- date_end: $("#date-end").val(),
- pending_tests: $("#pending-tests").is(":checked") ? 1 : 0
- }, function(response) {
- if(!response.success) return false;
- if(!response.locations) return false;
- let bounds = new google.maps.LatLngBounds();
- // User Address
- let position = {
- lat: response.latitude,
- lng: response.longitude
- };
- bounds.extend(position);
- let marker = new google.maps.Marker({
- map: map,
- position: position,
- title: response.name,
- icon: "/img/markers/address.png"
- });
- locationMarkers.push(marker);
- // Extend bounds for locations
- response.locations.forEach(location => {
- bounds.extend({
- lat: location.latitude,
- lng: location.longitude
- });
- });
- // Add locations to map
- addLocationsToMap(response.locations, map);
- // Add locations to nearby locations list group
- response.locations.forEach(location => {
- // Add to list group
- let lgi = $("div[data-nearby-location-list-group-item-template]").clone();
- lgi.removeAttr("data-nearby-location-list-group-item-template");
- lgi.find("a[data-location-name]").text(location.name).attr("href", "/account/locations/" + location.id);
- lgi.find("a[data-customer-name]").text(location.customer.name).attr("href", "/account/customers/" + location.customer.id);
- lgi.find("span[data-location-distance]").text(location.distance + " miles");
- lgi.find("span[data-location-id]").text(location.id);
- lgi.find("span[data-location-internal-id]").text(location.internalId);
- lgi.find("small[data-location-address]").text(location.address);
- if(location.test) {
- let test = location.test;
- let tester = test.tester;
- let project = test.project;
- lgi.find("div[data-test-container]").removeClass("d-none");
- lgi.find("div[data-test-id-container] a").text(test.id).attr("href", "/account/tests/" + test.id);
- lgi.find("div[data-test-project-name-container] a").text(project.name).attr("href", "/account/projects/" + project.id);
- if(tester) {
- lgi.find("div[data-test-user-name-container] a").text(tester.name).attr("href", "/account/users/" + tester.id);
- let passText = "";
- let passClass = "";
- if(test.pass === null) {
- passText = "Pending";
- passClass = "warning";
- lgi.find("div[data-test-due-dts-container]").removeClass("d-none").find("span").text(test.formattedDueDts);
- if(test.pastDue) {
- lgi.find("div[data-test-due-dts-container]").append(" <span class=\"badge badge-danger\">Overdue</span>");
- }
- }
- else if(test.pass === 0) {
- passText = "Fail";
- passClass = "danger";
- }
- else if(test.pass === 1) {
- passText = "Pass";
- passClass = "success";
- }
- if(test.pass !== null) {
- lgi.find("div[data-test-start-dts-container]").removeClass("d-none").find("span").text(test.formattedStartDts);
- }
- lgi.find("div[data-test-pass-container] span").addClass("text-" + passClass).text(passText);
- }
- }
- lgi.appendTo("#nearby-locations-list-group").show();
- });
- map.fitBounds(bounds);
- });
- }
- function addLocationsToMap(locations, map) {
- locations.forEach(location => {
- let position = {
- lat: location.latitude,
- lng: location.longitude
- };
- let options = {
- map: map,
- position: position,
- title: location.name
- };
- if(location.icon) {
- options.icon = "/img/markers/" + location.icon;
- }
- else {
- options.icon = "/img/markers/building-red.png";
- }
- let marker = new google.maps.Marker(options);
- locationMarkers.push(marker);
- if(location.internalId == 0) {
- location.internalId = "N/A";
- }
- addMarkerClickListenerForLocation(marker, location);
- });
- }
- function addMarkerClickListenerForLocation(marker, location) {
- marker.addListener("click", function() {
- let content = "<strong><a href=\"/account/locations/" + location.id + "\" target=\"_blank\">" + location.name + "</a></strong><br />";
- if(location.internalId) {
- content += "<div class=\"small\"><strong>Internal ID:</strong> " + location.internalId + "</div>";
- }
- content += "<div class=\"small\"><strong>Location ID:</strong> " + location.id + "</div>";
- if(location.test) {
- content += '<hr />';
- content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + location.test.id + '" target="_blank">' + location.test.id + '</a></div>';
- content += '<div class="small"><strong>Project:</strong> <a href="/account/projects/' + location.test.project.id + '" target="_blank">' + location.test.project.name + '</a></div>';
- if(location.test.formattedStartDts) {
- content += '<div class="small"><strong>Date/Time:</strong> ' + location.test.formattedStartDts + '</div>';
- }
- if(location.test.tester) {
- content += '<div class="small"><strong>Tester ID:</strong> ' + location.test.tester.id + '</div>';
- content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + location.test.tester.id + '" target="_blank">' + location.test.tester.name + '</a></div>';
- }
- let pastDueBadge = '';
- if(location.test.pastDue) {
- pastDueBadge = '<span class="badge badge-lg badge-primary">Overdue</span>';
- }
- if(location.test.formattedDueDts) {
- content += '<div class="small"><strong>Due:</strong> ' + location.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
- }
- }
- infoWindow.setContent(content);
- infoWindow.open(marker.getMap(), marker);
- });
- }
|