UNPKG

jcore-ui

Version:

jcore-ui - components for building an interface

73 lines 2.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const EventEmitter_1 = __importDefault(require("../utils/EventEmitter")); const JDom_1 = require("./JDom"); class Component { constructor(props) { /** * Если пришел массив HTMLElement, * то для каждого вызываем экземпляр компонента * и записываем их в массив components */ if (Array.isArray(props.$element) || props.$element instanceof NodeList) { let array = Array.from(props.$element); if (array.length) { this.components = array.map(el => { if (this._mount || JDom_1.$(el).hasAttr('data-mount')) { return; } ; return new props.Component(Object.assign(props, { $element: el })); }).filter(cpm => cpm); } return this; } ; /** * Если не передали HTMLElement, * то делаем поиск по селектору элемента и вызываем для него экземпляр компонента */ if (!props.$element) { return new props.Component(Object.assign(Object.assign({}, props), { $element: JDom_1.$(props.selectors.element).element })); } ; this.$element = JDom_1.$(props.$element); if (!this.$element) { // throw Error(`Не найден HTMLElement ${this.constructor.name}`) console.log(Error(`Не найден HTMLElement ${this.constructor.name}`)); } ; this.options = { mount: true, name: this.$element.dataset.name || props.options && props.options.name || '' }; this.on = { mount: () => { }, render: () => { }, destroy: () => { }, unmount: () => { }, }; this.selectors = props.selectors; this.emitter = props.emitter || new EventEmitter_1.default(); this.options = Object.assign(this.options, props.options || {}); this.on = Object.assign(this.on, props.on || {}); } mount() { this.$element.attr('data-mount', 'true'); this._mount = true; } getByName(components, name) { return components.filter(component => component.options.name === name)[0]; } unmount() { this._mount = false; this.$element.attr('data-mount', null); } } exports.default = Component; //# sourceMappingURL=Component.js.map