mirror of https://github.com/abpframework/abp.git
2 changed files with 63 additions and 45 deletions
@ -0,0 +1,60 @@ |
|||||
|
import { BehaviorSubject, Observable } from 'rxjs'; |
||||
|
import { NavItem } from '../models/nav-item'; |
||||
|
|
||||
|
export abstract class AbstractMenuService<T extends NavItem> { |
||||
|
protected abstract baseClass; |
||||
|
|
||||
|
protected _items$ = new BehaviorSubject<T[]>([]); |
||||
|
|
||||
|
get items(): T[] { |
||||
|
return this._items$.value; |
||||
|
} |
||||
|
|
||||
|
get items$(): Observable<T[]> { |
||||
|
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<Omit<T, 'id'>>) { |
||||
|
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; |
||||
|
} |
||||
|
} |
||||
@ -1,50 +1,8 @@ |
|||||
import { Injectable } from '@angular/core'; |
import { Injectable } from '@angular/core'; |
||||
import { BehaviorSubject, Observable } from 'rxjs'; |
|
||||
import { NavItem } from '../models/nav-item'; |
import { NavItem } from '../models/nav-item'; |
||||
|
import { AbstractMenuService } from './abstract-menu.service'; |
||||
|
|
||||
@Injectable({ providedIn: 'root' }) |
@Injectable({ providedIn: 'root' }) |
||||
export class NavItemsService { |
export class NavItemsService extends AbstractMenuService<NavItem> { |
||||
private _items$ = new BehaviorSubject<NavItem[]>([]); |
protected baseClass = NavItem; |
||||
|
|
||||
get items(): NavItem[] { |
|
||||
return this._items$.value; |
|
||||
} |
|
||||
|
|
||||
get items$(): Observable<NavItem[]> { |
|
||||
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<Omit<NavItem, 'id'>>) { |
|
||||
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; |
|
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue