91 lines
2.9 KiB
JavaScript
91 lines
2.9 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { GisState } from '../state.js';
|
|
import { TECH_LAYERS } from "../map.js";
|
|
|
|
const LEGEND_DEF = [
|
|
"puntatore",
|
|
"icona",
|
|
];
|
|
|
|
export default class extends Controller {
|
|
static targets = ['bar', 'legend'];
|
|
|
|
openView() {
|
|
const map = GisState.map;
|
|
// Remove all other layers first!
|
|
for (const key of Object.keys(GisState.layers)) {
|
|
if (GisState.layers[key] instanceof L.Layer && key !== 'geometries') {
|
|
map.removeLayer(GisState.layers[key]);
|
|
}
|
|
}
|
|
/**
|
|
* @type {string[]}
|
|
*/
|
|
const currentTechs = [...TECH_LAYERS[GisState.currentBView], ...LEGEND_DEF];
|
|
this.barTarget.classList.remove('is-hidden');
|
|
|
|
/**
|
|
* @type {HTMLDivElement}
|
|
*/
|
|
const legend = this.legendTarget;
|
|
const items = legend.querySelectorAll('li');
|
|
|
|
for (let item of items) {
|
|
if (!currentTechs.includes(item.id)) item.classList.add('is-hidden');
|
|
}
|
|
|
|
legend.classList.remove('is-hidden');
|
|
map._container.classList.add('bview-active');
|
|
}
|
|
|
|
closeView(event) {
|
|
// Don't do anything if ESC keyboard event
|
|
// is not fired from the building view
|
|
if (event instanceof KeyboardEvent && !GisState.bViewActive) return;
|
|
|
|
this.reset();
|
|
this.barTarget.classList.add('is-hidden');
|
|
this.legendTarget.classList.add('is-hidden');
|
|
GisState.bViewActive = false;
|
|
}
|
|
|
|
reset() {
|
|
// Restore previous map state before closing view
|
|
const map = GisState.map;
|
|
// Hard-coded...
|
|
map.removeLayer(GisState.baseLayers['Limiti amministrativi']);
|
|
|
|
const walls = GisState.layers.walls;
|
|
|
|
// Re-add all layers first (except techniques)! (TODO: Should this be filtered by map state??)
|
|
for (const key of Object.keys(GisState.layers)) {
|
|
if (GisState.layers[key] instanceof L.Layer
|
|
&& key !== 'geometries'
|
|
&& key !== 'buildingTechs') {
|
|
map.addLayer(GisState.layers[key]);
|
|
}
|
|
}
|
|
// map.removeLayer(GisState.layers.walls) doesn't work, i.e.
|
|
// it doesn't remove the layer group (fails silently)
|
|
walls.eachLayer(layer => map.removeLayer(layer));
|
|
|
|
map.addLayer(GisState.currentMapState.baseLayer);
|
|
map.setMinZoom(GisState.currentMapState.minZoom);
|
|
map.setMaxZoom(GisState.currentMapState.maxZoom);
|
|
map.setView(
|
|
GisState.currentMapState.center,
|
|
GisState.currentMapState.zoom
|
|
);
|
|
map._container.classList.remove('bview-active');
|
|
/**
|
|
* @type {HTMLDivElement}
|
|
*/
|
|
const legend = this.legendTarget;
|
|
const items = legend.querySelectorAll('li');
|
|
|
|
for (let item of items) {
|
|
item.classList.remove('is-hidden');
|
|
}
|
|
}
|
|
}
|