From a3d86fed4b217bda123f4e700ff7ca72a1529b0c Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 6 Jan 2022 13:40:40 +0300 Subject: [PATCH] feat: create abstarct menu service --- .../src/lib/services/abstract-menu.service.ts | 60 +++++++++++++++++++ .../src/lib/services/nav-items.service.ts | 48 +-------------- 2 files changed, 63 insertions(+), 45 deletions(-) create mode 100644 npm/ng-packs/packages/theme-shared/src/lib/services/abstract-menu.service.ts diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/abstract-menu.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/abstract-menu.service.ts new file mode 100644 index 0000000000..2c3d904a1b --- /dev/null +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/abstract-menu.service.ts @@ -0,0 +1,60 @@ +import { BehaviorSubject, Observable } from 'rxjs'; +import { NavItem } from '../models/nav-item'; + +export abstract class AbstractMenuService { + protected abstract baseClass; + + protected _items$ = new BehaviorSubject([]); + + get items(): T[] { + return this._items$.value; + } + + get items$(): Observable { + return this._items$.asObservable(); + } + + addItems(newItems: T[]) { + const items = [...this.items]; + newItems.forEach(item => { + const index = items.findIndex(i => i.id === item.id); + const data = new this.baseClass(item); + + if (index > -1) { + items[index] = data; + return; + } + + items.push(data); + }); + items.sort(this.sortItems); + this._items$.next(items); + } + + removeItem(id: string | number) { + const index = this.items.findIndex(item => item.id === id); + + if (index < 0) 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 < 0) return; + + const items = [...this.items]; + items[index] = new this.baseClass({ ...items[index], ...item }); + items.sort(this.sortItems); + this._items$.next(items); + } + + private sortItems(a: T, b: T) { + if (!a.order) return 1; + if (!b.order) return -1; + + return a.order - b.order; + } +} 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 ca361a2940..6aacdd279b 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 @@ -1,50 +1,8 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, Observable } from 'rxjs'; import { NavItem } from '../models/nav-item'; +import { AbstractMenuService } from './abstract-menu.service'; @Injectable({ providedIn: 'root' }) -export class NavItemsService { - private _items$ = new BehaviorSubject([]); - - get items(): NavItem[] { - return this._items$.value; - } - - get items$(): Observable { - return this._items$.asObservable(); - } - - 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 < 0) 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 < 0) return; - - const items = [...this.items]; - items[index] = new NavItem({ ...items[index], ...item }); - items.sort(sortItems); - this._items$.next(items); - } -} - -function sortItems(a: NavItem, b: NavItem) { - if (!a.order) return 1; - if (!b.order) return -1; - - return a.order - b.order; +export class NavItemsService extends AbstractMenuService { + protected baseClass = NavItem; }