var markerGroups = []; var infoWindow = false; $(function() { // On customer select $("a[data-customer-id]").on("click", function() { let customerId = $(this).data("customer-id"); let key = "customer-" + customerId; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { if(markerGroups[key].hasOwnProperty(i)) { let m = markerGroups[key][i]; m.setMap(null); } } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); $(this).find("small").text(""); } else { loadCustomerLocations(customerId); } return false; }); // On project select $("a[data-project-id]").on("click", function() { let projectId = $(this).data("project-id"); let key = "project-" + projectId; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { if(markerGroups[key].hasOwnProperty(i)) { let m = markerGroups[key][i]; m.setMap(null); } } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); } else { loadProjectLocations(projectId); } return false; }); // On all addresses select $("a[data-all-addresses]").on("click", function() { let key = "all-addresses"; if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { if(markerGroups[key].hasOwnProperty(i)) { let m = markerGroups[key][i]; m.setMap(null); } } } let selected = $(this).data("selected"); if(selected) { $(this).data("selected", false); $(this).find(".badge").removeClass("badge-success").text(""); } else { loadAllAddresses(); } return false; }); // Filters $("[data-filter-target]").on("input", event => { const $element = $(event.currentTarget); const value = $element.val(); const $targetElement = $($element.data("filter-target")); $targetElement.children().each((index, element) => { const filterValue = $(element).data("filter-value"); let method = "show"; if(value.length > 0) { method = filterValue.indexOf(value.toLowerCase()) !== -1 ? "show" : "hide"; } $(element)[method](); }); }); }); function loadCustomerLocations(customerId) { if(infoWindow === false) { infoWindow = new google.maps.InfoWindow(); } let minDate = $("#min-date").val(); let maxDate = $("#max-date").val(); let $customerItem = $("a[data-customer-id=" + customerId + "]"); /** * @property {Object} response * @property {Array} response.locations * @property {String} response.testPeriod * @property {Boolean} response.success */ $.get("/account/map-overview/get-customer-locations", { customer_id: customerId, min_date: minDate, max_date: maxDate, submit: $("#submit").val() }, function(response) { if(!response.success) return false; let key = "customer-" + customerId; markerGroups[key] = []; let count = response.locations.length; for(let i = 0; i < count; ++i) { let marker = addLocationMarker(response.locations[i]); markerGroups[key].push(marker); } $customerItem.data("selected", true); $customerItem.find(".badge").addClass("badge-danger").text(response.locations.length); $customerItem.find("small").text(response.testPeriod); }); } function loadProjectLocations(projectId) { if(infoWindow === false) { infoWindow = new google.maps.InfoWindow(); } let minDate = $("#min-date").val(); let maxDate = $("#max-date").val(); let $projectItem = $("a[data-project-id=" + projectId + "]"); /** * @property {Object} response * @property {Array} response.locations * @property {Boolean} response.success */ $.get("/account/map-overview/get-project-locations", { project_id: projectId, min_date: minDate, max_date: maxDate, submit: $("#submit").val() }, function(response) { if(!response.success) return false; let key = "project-" + projectId; markerGroups[key] = []; let count = response.locations.length; for(let i = 0; i < count; ++i) { let marker = addLocationMarker(response.locations[i]); markerGroups[key].push(marker); } $projectItem.data("selected", true); $projectItem.find(".badge").addClass("badge-danger").text(response.locations.length); }); } function addLocationMarker(location) { let map = maps["map"]; let position = { lat: location.latitude, lng: location.longitude }; var options = { position: position, title: location.name, map: map }; if(location.icon) { options.icon = "/img/markers/" + location.icon; } else { if(location.doNotTest) options.icon = "/img/markers/do_not_test.png"; if(location.closed) options.icon = "/img/markers/closed.png"; } infoWindow.setContent(""); let marker = new google.maps.Marker(options); addMarkerClickListenerForLocation(marker, location); return marker; } function loadAllAddresses() { if(infoWindow === false) { infoWindow = new google.maps.InfoWindow(); } /** * @property {Object} response * @property {Array} response.addresses * @property {Boolean} response.success */ let $allAddresses = $("a[data-all-addresses]"); $.get("/api/addresses/get-all", { submit: $("#submit").val() }, function(response) { if(!response.success) return false; let map = maps["map"]; let key = "all-addresses"; markerGroups[key] = []; let addressCount = response.addresses.length; for(let i = 0; i < addressCount; ++i) { let address = response.addresses[i]; let position = { lat: address.latitude, lng: address.longitude }; var options = { position: position, title: address.name, map: map }; let marker = new google.maps.Marker(options); addMarkerClickListenerForAddress(marker, address); markerGroups[key].push(marker); } $allAddresses.data("selected", true); $allAddresses.find(".badge").addClass("badge-danger").text(response.addresses.length); }); } function addMarkerClickListenerForLocation(marker, location) { let map = maps["map"]; marker.addListener("click", function() { let content = "
" + location.name + "
"; if(location.doNotTest) { content += "Do Not Test"; } if(location.closed) { content += "Closed"; } if(location.internalId) { content += "
Internal ID: " + location.internalId + "
"; } content += "
Location ID: " + location.id + "
"; if(location.test) { content += '
'; content += '
Test ID: ' + location.test.id + '
'; 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 = 'Past Due'; } if(location.test.formattedDueDts) { content += '
Due: ' + location.test.formattedDueDts + ' ' + pastDueBadge + '
'; } } if(typeof location.testCount !== "undefined") { content += '
'; content += `
Test Count: ${location.testCount}
`; } infoWindow.setContent(content); infoWindow.open(map, marker); }); } function addMarkerClickListenerForAddress(marker, address) { let map = maps["map"]; marker.addListener("click", function() { let dateLastTest = address.user.dateLastTest; if(dateLastTest === null) { dateLastTest = "Never"; } const content = `
${address.name}
ID${address.user.id}
Score${address.user.score}
Last Test${dateLastTest}
Registered${address.user.dateRegistered}
`; infoWindow.setContent(content); infoWindow.open(map, marker); }); }