| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- 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 = "<div><strong><a href=\"/account/locations/" + location.id + "\" target=\"_blank\">" + location.name + "</a></strong></div>";
- if(location.doNotTest) {
- content += "<span class=\"badge badge-warning\">Do Not Test</span>";
- }
- if(location.closed) {
- content += "<span class=\"badge badge-danger\">Closed</span>";
- }
- 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>';
- 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">Past Due</span>';
- }
- if(location.test.formattedDueDts) {
- content += '<div class="small"><strong>Due:</strong> ' + location.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
- }
- if(location.test) {
- content += '<div class="small"><strong>Result:</strong> ' + location.test.resultText + '</div>';
- }
- }
- if(typeof location.testCount !== "undefined") {
- content += '<hr />';
- content += `<div class="small"><strong>Test Count:</strong> ${location.testCount}</div>`;
- }
- 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 = `
- <div class="text-center mb-2">
- <strong><a href="/account/users/${address.userId}" target="_blank">${address.name}</a></strong>
- </div>
- <table class="table small table-sm mb-0">
- <tr><th>ID</th><td class="text-right">${address.user.id}</td></tr>
- <tr><th>Score</th><td class="text-right">${address.user.score}</td></tr>
- <tr><th>Last Test</th><td class="text-right">${dateLastTest}</td></tr>
- <tr><th>Registered</th><td class="text-right">${address.user.dateRegistered}</td></tr>
- </table>
- `;
- infoWindow.setContent(content);
- infoWindow.open(map, marker);
- });
- }
|