UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

83 lines (82 loc) 2.71 kB
/*! * NENT 2022 */ import { Component, Element, Prop } from '@stencil/core'; import { actionBus, eventBus } from '../../services/actions'; import { debugIf } from '../../services/common/logging'; import { commonState, onCommonStateChange, } from '../../services/common/state'; import { resolveChildElementXAttributes } from '../../services/data/elements'; import { addDataProvider, removeDataProvider, } from '../../services/data/factory'; import { DATA_EVENTS } from '../../services/data/interfaces'; import { ElementsActionListener } from './services/actions'; import { ElementsDataProvider } from './services/provider'; /** * This element enables element manipulation through the n-actions element. * Add it to the page to perform actions like 'add-css', toggle * attributes or to execute functions on the DOM without code. * * @system elements * @extension actions */ export class Elements { constructor() { /** * Turn on debug statements for load, update and render events. */ this.debug = false; } componentWillLoad() { debugIf(this.debug, `n-elements: initialized`); this.listener = new ElementsActionListener(); this.listener.initialize(window, actionBus, eventBus); commonState.elementsEnabled = true; if (commonState.dataEnabled) { this.subscribeToDataEvents(); } else { const dispose = onCommonStateChange('dataEnabled', (enabled) => { if (enabled) { this.subscribeToDataEvents(); } dispose(); }); } } subscribeToDataEvents() { this.dataSubscription = eventBus.on(DATA_EVENTS.DataChanged, () => { debugIf(this.debug, `n-elements: data changed `); resolveChildElementXAttributes(this.el.ownerDocument.body); }); this.provider = new ElementsDataProvider(this.el.ownerDocument, this.listener); addDataProvider('elements', this.provider); } disconnectedCallback() { var _a; this.listener.destroy(); commonState.elementsEnabled = false; (_a = this.dataSubscription) === null || _a === void 0 ? void 0 : _a.call(this); removeDataProvider('elements'); } static get is() { return "n-elements"; } static get properties() { return { "debug": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Turn on debug statements for load, update and render events." }, "attribute": "debug", "reflect": false, "defaultValue": "false" } }; } static get elementRef() { return "el"; } }