%PDF- %PDF-
Server IP : 37.220.80.31 / Your IP : 18.117.165.82 Web Server : Apache/2.4.52 (Ubuntu) System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 User : www-root ( 1010) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/js/ui/sidepanel/menu/src/ |
Upload File : |
import {Tag, Type} from 'main.core'; import {EventEmitter, BaseEvent} from 'main.core.events'; import {Item, type ItemOptions} from './item.js'; type CollectionOptions = { items: Array<ItemOptions>; }; export class Collection extends EventEmitter { #list: Array<Item> = []; #node: HTMLElement; #sync: boolean = false; constructor(options: CollectionOptions = {}) { super(); this.setEventNamespace('ui:sidepanel:menu:collection'); this.setItems(options.items); } #addSilent(itemOptions: ItemOptions): Item { if (itemOptions.active) { itemOptions.active = !this.hasActive(); } else { itemOptions.active = false; } const item = new Item(itemOptions); this.#list.push(item); item.subscribe('change:active', () => { if (item.isActive() && item.getCollection().isEmpty()) { this.syncActive(item); } }); item.subscribe('sync:active', () => this.syncActive(item)); item.subscribe('click', data => this.emit('click', data)); item.subscribe('change', () => setTimeout(() => this.render(), 0)); return item; } setActiveFirstItem(): void { const item = this.list()[0]; if (!item) { return; } item.setActive(true); item.getCollection().setActiveFirstItem(); } getActiveItem(): Item { return this.list().filter(item => item.isActive())[0]; } syncActive(excludeItem): Collection { if (this.#sync) { return this; } this.#sync = true; this.list() .filter(otherItem => otherItem !== excludeItem) .forEach(otherItem => { otherItem.getCollection().isEmpty() ? otherItem.setActive(false) : otherItem.getCollection().syncActive(otherItem) }) ; this.emit('sync:active'); this.#sync = false; return this; } add(itemOptions: ItemOptions): Item { const item = this.#addSilent(itemOptions); this.emit('change'); if (this.#node) { this.render(); } return item; } setItems(items: Array<ItemOptions> = []): Collection { this.#list = items.map(itemOptions => this.#addSilent(itemOptions)); this.emit('change'); if (this.#node) { this.render(); } return this; } list(): Array<Item> { return this.#list; } isEmpty(): boolean { return this.list().length === 0; } hasActive(recursively: boolean = true): boolean { const has = this.list().some(item => item.isActive()); if (has) { return true; } if (!recursively) { return false; } return this.list().some(item => item.getCollection().hasActive()); } render(): HTMLElement { if (!this.#node) { this.#node = Tag.render`<div class="ui-sidepanel-menu-items"></div>`; } this.#node.innerHTML = ''; this.#list.forEach((item: Item) => this.#node.appendChild(item.render())); return this.#node; } }