mfss-positioning/static/openLayers/map/map-layer-switcher.js

110 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class LayerSwitcher {
constructor(mapCore, target) {
this.mapCore = mapCore;
this.target = target;
this.initLayerSwitcher()
}
initLayerSwitcher() {
this.layerSwitcherControl = new ol.control.LayerShop({
minibar: true,
target: this.target
})
this.mapCore.map.getLayers().on('change:length', () => {
this.saveLayerVisibility();
});
this.mapCore.map.getLayers().getArray().forEach(layer => {
layer.on('change:visible', () => {
this.saveLayerVisibility();
});
});
this.addVisibilityListeners(this.mapCore.map.getLayers().getArray());
this.restoreLayerVisibility();
// Монтирование компонента
this.mapCore.addControl(this.layerSwitcherControl)
this.addBlockVisibleListener()
}
saveLayerVisibility() {
const layers = this.mapCore.map.getLayers().getArray();
const visibilityState = {};
function traverseLayers(layers, path) {
layers.forEach((layer, index) => {
if (layer instanceof ol.layer.Group) {
visibilityState[`${path}.group${index}`] = layer.getVisible();
traverseLayers(layer.getLayers().getArray(), `${path}.group${index}`);
} else {
visibilityState[`${path}.layer${index}`] = layer.getVisible();
}
});
}
traverseLayers(layers, 'root');
localStorage.setItem('layerVisibility', JSON.stringify(visibilityState));
}
restoreLayerVisibility() {
const visibilityState = JSON.parse(localStorage.getItem('layerVisibility'));
if (!visibilityState) return;
const layers = this.mapCore.map.getLayers().getArray();
// Рекурсивная функция для восстановления видимости
function traverseLayers(layers, path) {
layers.forEach((layer, index) => {
const layerKey = `${path}.layer${index}`;
const groupKey = `${path}.group${index}`;
if (layer instanceof ol.layer.Group) {
if (visibilityState[groupKey] !== undefined) {
layer.setVisible(visibilityState[groupKey]);
}
traverseLayers(layer.getLayers().getArray(), groupKey);
} else {
// Если это обычный слой, восстанавливаем его видимость
if (visibilityState[layerKey] !== undefined) {
layer.setVisible(visibilityState[layerKey]);
}
}
});
}
traverseLayers(layers, 'root');
}
addVisibilityListeners(layers) {
layers.forEach(layer => {
layer.on('change:visible', () => {
this.saveLayerVisibility();
});
if (layer instanceof ol.layer.Group) {
// Рекурсивно добавляем обработчики для слоев внутри группы
this.addVisibilityListeners(layer.getLayers().getArray());
}
});
}
addBlockVisibleListener() {
const layersBtn = document.getElementById('layers-btn')
layersBtn.addEventListener("click", (event) => {
if (this.target.style.display === "block") {
this.target.style.display = "none";
} else {
this.target.style.display = "block";
}
layersBtn.classList.toggle("selected");
});
}
addButton(control, placement) {
this.layerSwitcherControl.addControl(control, placement)
}
}