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(" Overdue"); } } 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 = "" + location.name + "
"; if(location.internalId) { content += "
Internal ID: " + location.internalId + "
"; } content += "
Location ID: " + location.id + "
"; if(location.test) { content += '
'; content += '
Test ID: ' + location.test.id + '
'; content += '
Project: ' + location.test.project.name + '
'; if(location.test.formattedStartDts) { content += '
Date/Time: ' + location.test.formattedStartDts + '
'; } if(location.test.tester) { content += '
Tester ID: ' + location.test.tester.id + '
'; content += '
Tester Name: ' + location.test.tester.name + '
'; } let pastDueBadge = ''; if(location.test.pastDue) { pastDueBadge = 'Overdue'; } if(location.test.formattedDueDts) { content += '
Due: ' + location.test.formattedDueDts + ' ' + pastDueBadge + '
'; } } infoWindow.setContent(content); infoWindow.open(marker.getMap(), marker); }); }