UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

66 lines (65 loc) 2.68 kB
import GirafeSingleton from '../../base/GirafeSingleton'; import GroupLayer from '../../models/layers/grouplayer'; import ThemeLayer from '../../models/layers/themelayer'; import StateManager from '../state/statemanager'; export default class OrderingManager extends GirafeSingleton { get state() { return this.stateManager.state; } constructor(type) { super(type); Object.defineProperty(this, "stateManager", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "timeoutId", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.stateManager = StateManager.getInstance(); this.registerEvents(); } registerEvents() { this.stateManager.subscribe('layers.layersList', () => this.reorderLayers()); this.stateManager.subscribe(/layers\.layersList\..*\.children/, () => this.reorderLayers()); this.stateManager.subscribe(/layers\.layersList\..*\.order/, () => this.reorderLayers()); } /** * This method do a reorder of all layers present in the treeview * in order to keep a order attribut corresponding to what the use wants. * This order will be used by the map component to calculate the list of layers in the right order */ reorderLayers() { // Use a debouncing to prevent multiple execution of this method // If multiple order attributes are modified at the same time. if (this.timeoutId) { clearTimeout(this.timeoutId); } this.timeoutId = setTimeout(() => { const orderedLayers = this.getSortedLayers(this.state.layers.layersList); const counter = { index: 1000 }; this.reorderLayersRecursively(orderedLayers, counter); }); } reorderLayersRecursively(orderedLayers, counter) { // Traverse the layertree in depth first to renumber all the elements for (const layer of orderedLayers) { console.log(`Setting layer ${layer.name} to order=${counter.index}`); layer.order = counter.index++; if (layer instanceof GroupLayer || layer instanceof ThemeLayer) { const orderedChilds = this.getSortedLayers(layer.children); this.reorderLayersRecursively(orderedChilds, counter); } } } getSortedLayers(layers) { const orderedLayers = layers.slice().sort((l1, l2) => { return l1.order - l2.order; }); return orderedLayers; } }