var addressesInitialised = false; var locationsInitialised = false; var addressMarkers = []; var locationMarkers = []; var infoWindow = false; $(function() { $("[data-toggle=tab]").on("shown.bs.tab", function(event) { if(event.target.id == "addresses-tab" && addressesInitialised == false) { initialiseAddresses(); } if(event.target.id == "nearby-locations-tab" && locationsInitialised == false) { initialiseLocations(); } }); $("#address-id").change(function() { initialiseLocations(); }); }); 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 }; let options = { map: map, position: position, title: address.name, icon: "/img/markers/address.png" } bounds.extend(position); let marker = new google.maps.Marker(options); } map.fitBounds(bounds); }); } function initialiseLocations() { if(infoWindow == false) { infoWindow = new google.maps.InfoWindow(); } for(var i in locationMarkers) { let marker = locationMarkers[i]; marker.setMap(null); marker = null; } locationMarkers = []; $("#nearby-locations-list-group").empty(); let map = maps["nearby-locations-map"]; $.get("/api/users/" + userId + "/get-locations", { "address_id": $("#address-id").val() }, 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 }; let options = { map: map, position: position, title: response.name, icon: "/img/markers/address.png" } bounds.extend(position); let marker = new google.maps.Marker(options); locationMarkers.push(marker); // Locations for(let i in response.locations) { let location = response.locations[i]; 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"; bounds.extend(position); let marker = new google.maps.Marker(options); locationMarkers.push(marker); if(location.internalId == 0) { location.internalId = "N/A"; } // 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(); addMarkerClickListenerForLocation(marker, location); } map.fitBounds(bounds); }); } function addMarkerClickListenerForLocation(m, l) { let map = maps["map"]; m.addListener("click", function() { let content = "" + l.name + "
"; if(l.internalId) { content += "
Internal ID: " + l.internalId + "
"; } content += "
Location ID: " + l.id + "
"; if(l.test) { content += '
'; content += '
Test ID: ' + l.test.id + '
'; if(l.test.formattedStartDts) { content += '
Date/Time: ' + l.test.formattedStartDts + '
'; } if(l.test.tester) { content += '
Tester ID: ' + l.test.tester.id + '
'; content += '
Tester Name: ' + l.test.tester.name + '
'; } let pastDueBadge = ''; if(l.test.pastDue) { pastDueBadge = 'Overdue'; } if(l.test.formattedDueDts) { content += '
Due: ' + l.test.formattedDueDts + ' ' + pastDueBadge + '
'; } } infoWindow.setContent(content); infoWindow.open(map, m); }); }