var markerGroups = []; var infoWindow = false; const clearMarkers = key => { if(typeof markerGroups[key] !== "undefined") { for(let i in markerGroups[key]) { if(markerGroups[key].hasOwnProperty(i)) { let m = markerGroups[key][i]; m.setMap(null); } } } }; $(function() { $("#hide-inactive-testers, #include-archived").on("change", () => loadAllAddresses()); const $customerRows = $("a[data-customer-id]"); // On customer select $customerRows.on("click", function() { let customerId = $(this).data("customer-id"); let key = "customer-" + customerId; clearMarkers(key); 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; clearMarkers(key); 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() { clearMarkers("all-addresses"); 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](); }); }); const filter = $("input[name=filter]:checked").val(); if(filter) { $customerRows.each((index, element) => { setTimeout(() => { $(element).trigger("click"); }, index * 250); }) } const $toggleAllCustomers = $("#toggle-all-customers"); $toggleAllCustomers.on("click", event => { event.preventDefault(); const $firstRow = $customerRows.eq(0); const selected = $firstRow.data("selected"); if(!selected) { let loadCount = 0; $customerRows.each((index, element) => { const $row = $(element); const selected = $row.data("selected"); if(!selected) { setTimeout(() => { $row.trigger("click"); }, loadCount * 250); loadCount++; } }); } else { $customerRows.each((index, element) => { const $row = $(element); const selected = $row.data("selected"); if(selected) { $row.trigger("click"); } }); } }); }); 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, filter: $("input[name=filter]:checked").val(), 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 */ clearMarkers("all-addresses"); const $allAddresses = $("a[data-all-addresses]"); const hideInactive = $("#hide-inactive-testers").prop("checked"); const includeArchived = $("#include-archived").prop("checked") ? 1 : 0; $.get("/api/addresses/get-all", { submit: $("#submit").val(), include_archived: includeArchived }, function(response) { if(!response.success) return false; const map = maps["map"]; const key = "all-addresses"; markerGroups[key] = []; let count = 0; response.addresses.forEach(address => { const user = address.user; let icon = "user-red.png"; if(user.daysSinceLastTest !== null) { if(user.daysSinceLastTest > 90) { icon = "user-red.png"; } else if(user.daysSinceLastTest > 60) { icon = "user-grey.png"; } else if(user.daysSinceLastTest > 30) { icon = "user-orange.png"; } else { icon = "user-green.png"; } } if(hideInactive && icon === "user-red.png") { return; } let marker = new google.maps.Marker({ position: { lat: address.latitude, lng: address.longitude }, title: address.name, map: map, icon: `/img/markers/${icon}` }); addMarkerClickListenerForAddress(marker, address); markerGroups[key].push(marker); count++; }); $allAddresses.data("selected", true); $allAddresses.find(".badge").addClass("badge-danger").text(count); }); } 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(location.test) { content += '
Result: ' + location.test.resultText + '
'; } } 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); }); }