let infoWindow = false; let exportIds = []; let markerMap = {}; // noinspection JSUnusedGlobalSymbols function getMarkerKey(locationId) { return `marker-${locationId}`; } function initialiseLocations() { if(!infoWindow) { infoWindow = new google.maps.InfoWindow(); } $.get(`/api/customers/${customerId}/get-locations`, { id: $("#id").val(), internal_id: $("#internal-id").val(), name: $("#name").val(), legal_entity_name: $("#legal-entity-name").val(), brand_name: $("#brand-name").val(), region_name: $("#region-name").val(), area_manager_name: $("#area-manager-name").val(), operations_manager_name: $("#operations-manager-name").val(), gaming_manager_name: $("#gaming-manager-name").val(), senior_gaming_manager_name: $("#senior-gaming-manager-name").val(), include_archived: $("#include-archived").prop("checked") ? 1 : 0, include_closed: $("#include-closed").prop("checked") ? 1 : 0, filter: $("#filter").val(), min_date: $("#min-date").val(), max_date: $("#max-date").val(), }, function(response) { if(!response.success) return false; if(!response.locations) return false; let map = maps["map"]; let bounds = new google.maps.LatLngBounds; let locationCount = response.locations.length; const $markerCountSpan = $("#map-marker-count"); if(locationCount > 0) { $markerCountSpan.text(locationCount); } else { $markerCountSpan.text(""); } response.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/building-red.png"; } else { options.icon = "/img/markers/" + location.icon; } bounds.extend(position); let key = getMarkerKey(location.id); let marker = new google.maps.Marker(options); marker.location = location; marker.originalIcon = options.icon; marker.addListener("click", function() { displayInfoWindow(this); }); markerMap[key] = marker; }); map.fitBounds(bounds); }); $(document).on("click", "a[data-export-location-id]", function(event) { event.preventDefault(); let locationId = parseInt($(event.currentTarget).attr("data-export-location-id")); let index = exportIds.indexOf(locationId); let key = getMarkerKey(locationId); let marker = markerMap[key]; if(index === -1) { exportIds.push(locationId); marker.setOptions({ icon: "/img/markers/building-green.png" }); } else { exportIds.splice(index, 1); marker.setOptions({ icon: marker.originalIcon }); } displayInfoWindow(marker); $createProjectButton.prop("disabled", (exportIds.length === 0)); $addLocationsToProjectButton.prop("disabled", (exportIds.length === 0)); }); $("#export-csv-button, #export-kml-button").on("click", function(event) { event.preventDefault(); const $form = $("#locations-export-form"); const action = $(event.currentTarget).attr("data-action"); $form.find("input[name='ids[]']").remove(); $form.find("input[name='action']").val(action); for(const i in exportIds) { if(!exportIds.hasOwnProperty(i)) continue; let locationId = exportIds[i]; $form.append(""); } $form.trigger("submit"); }); const $createProjectButton = $("#create-project-button"); $createProjectButton.on("click", function(event) { event.preventDefault(); if(exportIds.length === 0) return; const $form = $("#locations-create-project-form"); $form.find("input[name='location_ids']").remove(); $form.append(""); $form.trigger("submit"); }); const $addLocationsToProjectButton = $("#add-locations-to-project-button"); new AddLocationsToProjectModal(); $(".btn-map").on("click", event => { const locationId = $(event.currentTarget).data("location-id"); const key = getMarkerKey(locationId); const marker = markerMap[key]; const map = maps.map; const center = marker.getPosition(); map.setCenter(center); map.setZoom(14); displayInfoWindow(markerMap[key]); }); } function displayInfoWindow(marker) { const escapeText = (text) => { const span = document.createElement("span"); span.innerText = text; return span.innerHTML; }; const locationId = parseInt(marker.location.id); const index = exportIds.indexOf(locationId); const location = marker.location; const badges = []; const lines = []; if(location.doNotTest) { badges.push(`Do Not Test`); } if(location.archived) { badges.push(`Archived`); } if(location.closed) { badges.push(`Closed`); } lines.push({title: "Internal ID", text: location.internalId}); lines.push({title: "Location ID", text: location.id}); if(location.test) { lines.push({divider: true}); lines.push({title: "Test ID", html: `${location.test.id}`}); if(location.test.formattedStartDts) { lines.push({title: "Date/Time", text: location.test.formattedStartDts}); } if(location.test.tester) { const { id, name } = location.test.tester; lines.push({title: "Tester ID", text: id}); lines.push({title: "Tester Name", html: `${name}`}); } if(location.test.pastDue) { badges.push(`Past Due`); } if(location.test.formattedDueDts) { lines.push({title: "Due", text: location.test.formattedDueDts}); } if(location.test) { lines.push({title: "Result", text: location.test.resultText}); } } let linesHtml = ""; lines.forEach(line => { if(line.divider) { linesHtml += "
\n"; } if(line.title) { const titleHtml = escapeText(line.title); let contentHtml = line.html; if(line.text) { contentHtml = escapeText(line.text); } linesHtml += `
${titleHtml}: ${contentHtml}
`; } }); let exportText = "Select"; let exportClass = "btn-primary"; if(index > -1) { exportText = "Deselect"; exportClass = "btn-success"; } const content = `
${marker.location.name}
${badges.join(" ")}

${linesHtml}
`; infoWindow.setContent(content); infoWindow.open(map, marker); } class AddLocationsToProjectModal { constructor() { this.$modal = $("#add-locations-to-project-modal"); this.$modal.on("show.bs.modal", event => this.onShow(event)); this.$form = this.$modal.find("form"); this.$form.on("submit", event => this.onSubmit(event)); this.$projectDropdown = this.$form.find("[name=project_id]"); } onShow() { this.$form.find(`input[name="location_ids[]"]`).remove(); exportIds.forEach(locationId => { this.$form.append(``); }); } onSubmit(event) { event.preventDefault(); const projectId = this.$projectDropdown.val(); const data = this.$form.serialize(); $.post(`/account/projects/${projectId}/add-locations`, data).then(response => { if(response.success) { if(response.dupeLocations.length > 0) { const locationNames = response.dupeLocations.map(location => location.name).join("\n"); alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`); } else { alert("Locations added to project successfully."); } } this.$modal.modal("hide"); }).catch(error => { alert(`An error has occurred: ${error}`); }); } }