UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

123 lines (122 loc) 4.44 kB
// SPDX-License-Identifier: Apache-2.0 import BaseLayer from './baselayer.js'; import LayerTimeFormatter from '../../tools/time/layertimeformatter.js'; import LayerWms from './layerwms.js'; /* * This class is a used in the state of the application, which will be accessed behind a javascript proxy. * This means that each modification made to its properties must come from outside, * because they have to be made through the proxy, so that the modification can be listen. * Therefore, this class must not contain any method which is updating a value directly * For example, any method doing <this.xxx = value> is forbidden here, because the modification be known from the proxy */ class GroupLayer extends BaseLayer { isExclusiveGroup; isExpanded; _isMixed; activeState = 'off'; timeOptions; timeAttribute; timeRestriction; children = []; constructor(id, name, order, options) { super(id, name, order, options); this.isExpanded = options?.isDefaultExpanded || false; this.isExclusiveGroup = options?.isExclusiveGroup ?? false; this._isMixed = options?.isMixed; this.timeOptions = options?.time; this.timeAttribute = options?.timeAttribute; this.setDefaultTimeRestriction(); } clone() { const options = { isDefaultChecked: this.isDefaultChecked, metadataUrl: this.metadataUrl, disclaimer: this.disclaimer, isDefaultExpanded: this.isExpanded, isExclusiveGroup: this.isExclusiveGroup, isMixed: this._isMixed, time: this.timeOptions, timeAttribute: this.timeAttribute }; const clonedObject = new GroupLayer(this.id, this.name, this.order, options); clonedObject.activeState = this.activeState; clonedObject.timeRestriction = this.timeRestriction; // Clone children for (const child of this.children) { const clonedChild = child.clone(); clonedChild.parent = clonedObject; clonedObject.children.push(clonedChild); } return clonedObject; } get active() { return this.activeState === 'on'; } get inactive() { return this.activeState === 'off'; } get semiActive() { return this.activeState === 'semi'; } get hasTimeRestriction() { return !!this.timeRestriction; } get hasGrandChildren() { for (const child of this.children) { if (child.children?.length) { return true; } } return false; } setDefaultTimeRestriction() { if (this.timeOptions) { const timeFormatter = new LayerTimeFormatter(this.timeOptions); this.timeRestriction = timeFormatter.getFormattedDefault(); } } /** * Checks if all the children are LayerWms with the same ogcServer, opacity * time */ get isMixed() { // It was explicitly set to true, it can't be unmixed if (this._isMixed) { return this._isMixed; } // If any child is not a GroupLayer or LayerWms, return true immediately for (const child of this.children) { if (!(child instanceof GroupLayer) && !(child instanceof LayerWms)) { return true; } } // Collect all LayerWms children and grandchildren recursively const layerWmsList = []; function collectLayerWms(layer) { if (layer instanceof LayerWms) { layerWmsList.push(layer); } else if (layer instanceof GroupLayer) { for (const child of layer.children) { collectLayerWms(child); } } } for (const child of this.children) { collectLayerWms(child); } // Check if all LayerWms have the same properties if (layerWmsList.length > 0) { const first = layerWmsList[0]; const allSame = layerWmsList.every((lw) => lw.opacity === first.opacity && lw.ogcServer === first.ogcServer && lw.filter === first.filter && lw.timeRestriction === first.timeRestriction); if (allSame) { return false; } } return true; } } export default GroupLayer;