UNPKG

@devexperts/dxcharts-lite

Version:
137 lines (136 loc) 4.27 kB
/* * Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** * Base class for chart elements. Contains lifecycle support, utility methods. * * Chart entity state transition: INITIAL -> ACTIVE <-> DEACTIVATED -> DISPOSED */ export class ChartBaseElement { constructor() { this.subscriptions = []; this._state = 'initial'; // substitute entities which cascade activate/deactivate this.entities = []; } /** * This method is used to activate a protected feature. * It does not take any arguments and does not return anything. */ doActivate() { } /** * This method is used to unsubscribe from all events. * It clears the subscriptions array. * @returns {void} */ doDeactivate() { // Unsubscribe from all events this.subscriptions.forEach(unsub => unsub()); this.subscriptions = []; } /** * Enables the functionality of an object. * If the object is not currently active, it sets the state to 'deactivated' and activates it. * @returns {void} * @deprecated use `ChartBaseElement.activate()` instead */ enable() { if (this._state !== 'active') { this._state = 'deactivated'; this.activate(); } } /** * Disables the current object if it is not already disabled. * If the object is not disabled, it will be deactivated and its state will be set to 'disabled'. * @returns {void} * @deprecated use `ChartBaseElement.deactivate()` instead */ disable() { if (this._state !== 'disabled') { this.deactivate(); this._state = 'disabled'; } } /** * Activates the entity and all its child entities. * If the entity is already active, it does nothing. * If the entity is disabled, it does nothing. * @returns {void} */ activate() { if (this._state === 'disabled') { return; } if (this._state !== 'active') { this.doActivate(); this._state = 'active'; } this.entities.forEach(comp => comp.activate()); } /** * Deactivates the entity and all its child entities. * If the entity is already disabled, it does nothing. * If the entity is not yet deactivated, it calls the doDeactivate method and sets the state to 'deactivated'. * Finally, it deactivates all child entities. * @returns {void} */ deactivate() { if (this._state === 'disabled') { return; } if (this._state !== 'deactivated') { this.doDeactivate(); this._state = 'deactivated'; } this.entities.forEach(comp => comp.deactivate()); } /** * Returns the current state of the ChartEnitity instance. * @returns {ChartEntityState} The current state of the ChartEnitity instance. */ getState() { return this._state; } /** * Adds default subscription * @param fn - an unsubscriber function * @protected */ addSubscription(fn) { this.subscriptions.push(fn); } /** * Adds rxjs subscription * @param subscription * @protected */ addRxSubscription(subscription) { if (subscription) { this.subscriptions.push(subscription.unsubscribe.bind(subscription)); } } /** * Adds a new entity to the entities array and activates it if the parent entity is active. * * @param {Entity} entity - The entity to be added to the entities array. * @returns {void} */ addChildEntity(entity) { this.entities.push(entity); if (this._state === 'active') { entity.activate(); } } /** * Removes a entity from the entities array. * * @param {ChartEntity} entity - The entity to be removed. * @returns {void} */ removeChildEntity(entity) { this.entities = this.entities.filter(c => c !== entity); } }