Persist project display options and toggle map tools

This commit is contained in:
livio committed 2026-09-21 14:27:45 +02:00
1 parent 06050b05f5
commit 32a36c98ec
6 files changed
+177 -52

No files matched your search

+83 -42
View File
@@ -12,7 +12,6 @@ const lmeState = {
importedMeasurements: null,
drawings: null,
drawingMeasurements: null,
quickMeasureControl: null,
drawControl: null,
activeDrawHandler: null,
drawEventsBound: false,
@@ -144,7 +143,6 @@ function init() {
refreshCadastreLayer();
bindFileImport();
bindDrawingEvents();
addQuickMeasureControl();
if (!lmeState.resizeBound) {
window.addEventListener("resize", resize);
@@ -950,11 +948,7 @@ function addProjectMarker(name) {
setInfo(`POI aggiunto: ${label}`);
}
function addQuickMeasureControl() {
if (!lmeState.map || lmeState.quickMeasureControl) {
return;
}
function configureDrawLabels() {
L.drawLocal.draw.toolbar.buttons.polygon = "Misura area";
L.drawLocal.draw.toolbar.buttons.polyline = "Misura distanza";
L.drawLocal.draw.toolbar.buttons.circle = "Misura cerchio";
@@ -969,32 +963,6 @@ function addQuickMeasureControl() {
L.drawLocal.draw.handlers.polyline.tooltip.cont = "Tocca per aggiungere un tratto.";
L.drawLocal.draw.handlers.polyline.tooltip.end = "Tocca l'ultimo punto per terminare.";
L.drawLocal.draw.handlers.circle.tooltip.start = "Tocca il centro e trascina per il raggio.";
const orangeArea = { color: "#f97316", weight: 4, fillColor: "#fb923c", fillOpacity: 0.2 };
lmeState.quickMeasureControl = new L.Control.Draw({
position: "topright",
edit: false,
draw: {
circle: { metric: true, feet: false, showRadius: true, shapeOptions: orangeArea },
circlemarker: false,
marker: false,
polygon: {
metric: true,
feet: false,
showArea: true,
allowIntersection: false,
shapeOptions: orangeArea
},
polyline: {
metric: true,
feet: false,
showLength: true,
shapeOptions: { color: "#f97316", weight: 4, opacity: 0.95 }
},
rectangle: false
}
});
lmeState.map.addControl(lmeState.quickMeasureControl);
}
function bindDrawingEvents() {
@@ -1036,21 +1004,34 @@ function enableDrawTools() {
return;
}
configureDrawLabels();
const orangeArea = { color: "#f97316", weight: 4, fillColor: "#fb923c", fillOpacity: 0.2 };
lmeState.drawControl = new L.Control.Draw({
position: "topright",
edit: { featureGroup: lmeState.drawings },
draw: {
circle: false,
circle: { metric: true, feet: false, showRadius: true, shapeOptions: orangeArea },
circlemarker: false,
marker: true,
polygon: false,
polyline: false,
rectangle: { metric: true, shapeOptions: { color: "#f97316", weight: 4, fillOpacity: 0.2 } }
polygon: {
metric: true,
feet: false,
showArea: true,
allowIntersection: false,
shapeOptions: orangeArea
},
polyline: {
metric: true,
feet: false,
showLength: true,
shapeOptions: { color: "#f97316", weight: 4, opacity: 0.95 }
},
rectangle: { metric: true, shapeOptions: orangeArea }
}
});
lmeState.map.addControl(lmeState.drawControl);
bindDrawingEvents();
setInfo("Barra completa attiva: rettangolo, POI, modifica e cancellazione.");
setInfo("Barra strumenti attiva: misure, disegni, POI, modifica e cancellazione.");
}
function startMeasurementDrawing(mode) {
@@ -1377,7 +1358,7 @@ function formatNumber(value, digits) {
function createProjectSnapshot(includeCadastralParcels) {
const center = lmeState.map.getCenter();
return JSON.stringify({
schemaVersion: 2,
schemaVersion: 3,
view: {
latitude: center.lat,
longitude: center.lng,
@@ -1387,6 +1368,7 @@ function createProjectSnapshot(includeCadastralParcels) {
imported: lmeState.imported.toGeoJSON(),
importedSheets: lmeState.importedSheets.toGeoJSON(),
importedSheetsVisible: lmeState.importedSheetsVisible,
displayOptions: captureDisplayOptions(),
selectedParcels: includeCadastralParcels
? lmeState.selectedParcelsLayer.toGeoJSON()
: { type: "FeatureCollection", features: [] },
@@ -1460,7 +1442,12 @@ function loadProjectSnapshot(raw, projectName) {
}),
onEachFeature: bindStoredFeaturePopup
}).addTo(lmeState.importedSheets);
lmeState.importedSheetsVisible = data.schemaVersion >= 2 && Boolean(data.importedSheetsVisible);
const projectDisplayOptions = data.schemaVersion >= 3 && data.displayOptions
? data.displayOptions
: null;
lmeState.importedSheetsVisible = projectDisplayOptions
? Boolean(projectDisplayOptions.importedSheetsVisible)
: data.schemaVersion >= 2 && Boolean(data.importedSheetsVisible);
applyImportedSheetVisibility();
L.geoJSON(data.selectedParcels || emptyFeatureCollection(), {
style: { color: "#ef4444", weight: 4, opacity: 1, fillColor: "#fbbf24", fillOpacity: 0.26 },
@@ -1477,6 +1464,9 @@ function loadProjectSnapshot(raw, projectName) {
lmeState.gpsTrailPoints = Array.isArray(data.trail) ? data.trail : [];
lmeState.gpsTrail.setLatLngs(lmeState.gpsTrailPoints);
if (projectDisplayOptions) {
applyProjectDisplayOptions(projectDisplayOptions);
}
refreshImportedMeasurements();
refreshDrawingMeasurements();
@@ -1507,13 +1497,60 @@ function loadProjectSnapshot(raw, projectName) {
? ` ${importedParts.sheets.features.length} elementi del foglio sono nascosti.`
: "";
setInfo(`Progetto caricato: ${projectName || "senza nome"}.${hiddenSheets}`);
return lmeState.importedSheetsVisible;
return captureDisplayOptions();
} catch (error) {
setInfo(`Progetto non leggibile: ${error.message}`);
throw error;
}
}
function captureDisplayOptions() {
return {
baseLayer: lmeState.currentBaseName,
cadastreOpacity: lmeState.cadastreOpacity,
cadastreLayers: [...lmeState.cadastreLayers],
includeKmlSheetLayer: lmeState.includeKmlSheetLayer,
importedSheetsVisible: lmeState.importedSheetsVisible,
showImportedMeasurements: lmeState.showImportedMeasurements,
showDrawingMeasurements: lmeState.showDrawingMeasurements,
drawToolsVisible: Boolean(lmeState.drawControl),
gpsPositionVisible: lmeState.gpsPositionVisible,
trailEnabled: lmeState.gpsTrailEnabled
};
}
function applyProjectDisplayOptions(options) {
const supportedCadastreLayers = new Set([
"CP.CadastralParcel",
"codice_plla",
"CP.CadastralZoning",
"fabbricati",
"strade",
"acque"
]);
if (Array.isArray(options.cadastreLayers)) {
lmeState.cadastreLayers = options.cadastreLayers.filter(layer => supportedCadastreLayers.has(layer));
}
const opacity = Number(options.cadastreOpacity);
if (Number.isFinite(opacity)) {
lmeState.cadastreOpacity = Math.max(0, Math.min(1, opacity));
}
lmeState.includeKmlSheetLayer = Boolean(options.includeKmlSheetLayer);
lmeState.importedSheetsVisible = Boolean(options.importedSheetsVisible);
lmeState.showImportedMeasurements = Boolean(options.showImportedMeasurements);
lmeState.showDrawingMeasurements = options.showDrawingMeasurements !== false;
applyImportedSheetVisibility();
refreshCadastreLayer();
if (typeof options.baseLayer === "string") {
setBaseLayer(options.baseLayer);
}
setGpsPositionVisible(options.gpsPositionVisible !== false);
toggleTrail(Boolean(options.trailEnabled));
setDrawToolsVisible(Boolean(options.drawToolsVisible));
}
function partitionStoredImportedFeatures(imported, storedSheets) {
const parcelFeatures = [];
const sheetFeatures = [...(storedSheets?.features || [])];
@@ -1748,7 +1785,11 @@ function htmlToText(value) {
}
function setCadastreOpacity(value) {
lmeState.cadastreOpacity = Number(value || 0.75);
const opacity = Number(value);
if (!Number.isFinite(opacity)) {
return;
}
lmeState.cadastreOpacity = Math.max(0, Math.min(1, opacity));
if (lmeState.cadastre) {
lmeState.cadastre.setOpacity(lmeState.cadastreOpacity);
}