import { Guid } from 'guid-typescript'; export interface NavBarItemBase { route?: string; icon?: string; svgIcon?: string; title: string; children?: NavBarItemBase[]; id?: string; action?: string; isOutlineIcon?: boolean; isOpened?: boolean; isActive?: boolean; showAlways?: boolean; } export class NavBarItem implements NavBarItemBase { showAlways?: boolean; route?: string; icon?: string; isOutlineIcon?: boolean; svgIcon?: string; title: string = ''; id?: string; parent?: NavBarItem; action?: string; isOpened? = false; isActive? = false; set children(items: NavBarItem[]) { this._children = items; this._hasChildren = items?.length > 0; this._childrenCount = items?.length || 0; } get children(): NavBarItem[] { return this._children; } get childrenCount(): number { return this._childrenCount; } get hasChildren(): boolean { return this._hasChildren; } private _children: NavBarItem[] = []; private _hasChildren = false; private _childrenCount = 0; constructor(item: NavBarItemBase) { Object.assign(this, item); this.id = item.id ?? Guid.create().toString(); this.children = (item.children && item.children.map((t) => { const result = new NavBarItem(t); result.parent = this; return result; })) || []; } contains(item: NavBarItem): boolean | null { if (!item) { return null; } const matchCurrentItem = this.id === item.id; const matchChildItem = this.children && this.children.map((t) => t.contains(item)).filter((t) => !!t)[0]; return (matchCurrentItem || matchChildItem) === true; } }