@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
57 lines (56 loc) • 1.9 kB
JavaScript
import { setPropertyByPath } from '../utils/pathUtils.js';
/**
* Manages the toggling of state properties based on specified paths.
* Every paths leading to boolean value will be initially set to false,
* then activities one will deactivate the others.
*/
export default class StateToggleManager {
eventsCallbacks = [];
//private readonly togglePaths: string[];
stateManager;
panels = [];
constructor(elements, stateManager) {
this.panels = elements;
this.stateManager = stateManager;
this.watchToggle();
}
get state() {
return this.stateManager.state;
}
/**
* To call to destroy properly the component.
*/
destroy() {
this.stateManager.unsubscribe(this.eventsCallbacks);
}
/**
* Set to false every state leaded by toggle paths except the specified one.
* Use the given value on the specified one.
*/
toggle(panel, isVisible) {
const panelsToHide = this.panels.filter((p) => p.panelTogglePath !== panel.panelTogglePath);
for (const panelToHide of panelsToHide) {
setPropertyByPath(this.state, panelToHide.panelTogglePath, false);
panelToHide.togglePanel(false);
}
setPropertyByPath(this.state, panel.panelTogglePath, isVisible);
panel.togglePanel(isVisible);
}
/**
* Set to false every state leaded by toggle paths.
*/
deactivateAll() {
for (const panel of this.panels) {
setPropertyByPath(this.state, panel.panelTogglePath, false);
}
}
/**
* Watches for changes in togglePaths and invokes the toggle method when a change is detected.
* @private
**/
watchToggle() {
for (const panel of this.panels) {
this.stateManager.subscribe(panel.panelTogglePath, (_, visible) => this.toggle(panel, visible));
}
}
}