Skip to content

Commit

Permalink
feat(menu): add collapseAll function (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnixaa committed Jun 8, 2018
1 parent 0861d17 commit 2942bfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/framework/theme/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ export class NbMenuComponent implements OnInit, AfterViewInit, OnDestroy {
data.listener.next({ tag: this.tag, item: this.getSelectedItem(this.items) });
});

this.menuInternalService
.onCollapseAll()
.pipe(
takeWhile(() => this.alive),
filter((data: { tag: string }) => this.compareTag(data.tag)),
)
.subscribe((data: { tag: string }) => {
this.collapseAll();
});

this.router.events
.pipe(
takeWhile(() => this.alive),
Expand Down Expand Up @@ -327,6 +337,10 @@ export class NbMenuComponent implements OnInit, AfterViewInit, OnDestroy {
}
}

private collapseAll() {
this.menuInternalService.collapseAll(this.items, this.tag);
}

private getHomeItem(items: NbMenuItem[]): NbMenuItem {
for (const item of items) {
if (item.home) {
Expand Down
13 changes: 13 additions & 0 deletions src/framework/theme/components/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const getSelectedItem$
const itemSelect$ = new ReplaySubject<NbMenuBag>(1);
const itemHover$ = new ReplaySubject<NbMenuBag>(1);
const submenuToggle$ = new ReplaySubject<NbMenuBag>(1);
const collapseAll$ = new ReplaySubject<{ tag: string }>(1);

// TODO: check if we need both URL and LINK
/**
Expand Down Expand Up @@ -113,6 +114,14 @@ export class NbMenuService {
addItems$.next({ tag, items });
}

/**
* Collapses all menu items
* @param {string} tag
*/
collapseAll(tag?: string) {
collapseAll$.next({ tag });
}

/**
* Navigate to the home menu item
* @param {string} tag
Expand Down Expand Up @@ -194,6 +203,10 @@ export class NbMenuInternalService {
return navigateHome$.pipe(share());
}

onCollapseAll(): Observable<{ tag: string }> {
return collapseAll$.pipe(share());
}

onGetSelectedItem(): Observable<{ tag: string; listener: BehaviorSubject<NbMenuBag> }> {
return getSelectedItem$.pipe(share());
}
Expand Down

0 comments on commit 2942bfd

Please sign in to comment.