From c2662d8442b455212ae44176a3e87a17bb99103a Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 21 Jul 2020 10:58:29 +0300 Subject: [PATCH 1/3] feat: convert NavItem to class --- .../packages/theme-shared/src/lib/models/nav-item.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/models/nav-item.ts b/npm/ng-packs/packages/theme-shared/src/lib/models/nav-item.ts index ea708fdde6..7fcb154cbf 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/models/nav-item.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/models/nav-item.ts @@ -1,6 +1,6 @@ import { Type } from '@angular/core'; -export interface NavItem { +export class NavItem { id: string | number; component?: Type; html?: string; @@ -8,4 +8,8 @@ export interface NavItem { order?: number; requiredPolicy?: string; visible?: () => boolean; + constructor(props: Partial) { + props = { ...props, visible: props.visible || (() => true) }; + Object.assign(this, props); + } } From 05e631c87e43aa6f8f5a76868f1c0d2de3bb54d6 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Tue, 21 Jul 2020 10:59:17 +0300 Subject: [PATCH 2/3] feat: create new NavItem instance on add and patch --- .../src/lib/services/nav-items.service.ts | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts index 92b7c26059..d79f293032 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts @@ -14,37 +14,31 @@ export class NavItemsService { return this._items$.asObservable(); } - addItems(items: NavItem[]) { - this._items$.next( - [ - ...this.items, - ...items.map(item => - typeof item.visible === 'undefined' ? { ...item, visible: () => true } : item, - ), - ].sort(sortItems), - ); + addItems(newItems: NavItem[]) { + const items = [...this.items]; + newItems.forEach(item => items.push(new NavItem(item))); + items.sort(sortItems); + this._items$.next(items); } removeItem(id: string | number) { const index = this.items.findIndex(item => item.id === id); - if (index > -1) { - this._items$.next([...this.items.slice(0, index), ...this.items.slice(index + 1)]); - } + if (index < -1) return; + + const items = [...this.items.slice(0, index), ...this.items.slice(index + 1)]; + this._items$.next(items); } patchItem(id: string | number, item: Partial>) { const index = this.items.findIndex(i => i.id === id); - if (index > -1) { - this._items$.next( - [ - ...this.items.slice(0, index), - { ...this.items[index], ...item }, - ...this.items.slice(index + 1), - ].sort(sortItems), - ); - } + if (index < -1) return; + + const items = [...this.items]; + items[index] = new NavItem({ ...items[index], ...item }); + items.sort(sortItems); + this._items$.next(items); } } From 61797e99bb837069982498eb454e29b84d74394d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levent=20Arman=20=C3=96zak?= Date: Tue, 21 Jul 2020 11:02:11 +0300 Subject: [PATCH 3/3] Update nav-items.service.ts --- .../theme-shared/src/lib/services/nav-items.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts index d79f293032..ca361a2940 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/nav-items.service.ts @@ -24,7 +24,7 @@ export class NavItemsService { removeItem(id: string | number) { const index = this.items.findIndex(item => item.id === id); - if (index < -1) return; + if (index < 0) return; const items = [...this.items.slice(0, index), ...this.items.slice(index + 1)]; this._items$.next(items); @@ -33,7 +33,7 @@ export class NavItemsService { patchItem(id: string | number, item: Partial>) { const index = this.items.findIndex(i => i.id === id); - if (index < -1) return; + if (index < 0) return; const items = [...this.items]; items[index] = new NavItem({ ...items[index], ...item });