let infoWindow = false; let exportIds = []; let markerMap = {}; // noinspection JSUnusedGlobalSymbols function initialiseLocations() { if(!infoWindow) { infoWindow = new google.maps.InfoWindow(); } const includeArchived = $("#include-archived").prop("checked") ? 1 : 0; const includeClosed = $("#include-closed").prop("checked") ? 1 : 0; $.get(`/api/customers/${customerId}/get-locations`, { id: $("#id").val(), internal_id: $("#internal-id").val(), name: $("#name").val(), legal_entity_name: $("#legal-entity-name").val(), include_archived: includeArchived, include_closed: includeClosed }, function(response) { if(!response.success) return false; if(!response.locations) return false; let map = maps["map"]; let bounds = new google.maps.LatLngBounds; 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"; } bounds.extend(position); let key = "marker-" + location.id; let marker = new google.maps.Marker(options); marker.location = location; marker.addListener("click", function() { displayInfoWindow(this); }); markerMap[key] = marker; }); map.fitBounds(bounds); }); $(document).on("click", "a[data-export-location-id]", function(event) { let locationId = parseInt($(event.currentTarget).attr("data-export-location-id")); let index = exportIds.indexOf(locationId); let key = "marker-" + 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: "/img/markers/building-red.png" }); } 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(); } function displayInfoWindow(marker) { const locationId = parseInt(marker.location.id); const index = exportIds.indexOf(locationId); const archived = marker.location.archived; let badge = ""; if(archived) { badge = `Archived`; } let exportText = "Select"; let exportClass = "btn-primary"; if(index > -1) { exportText = "Deselect"; exportClass = "btn-success"; } const content = `

${marker.location.name} ${badge}

`; 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(event) { 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}`); }); } }