From 46d6e8d1915433f282b9dded96fc0871ea39002c Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Sun, 5 Mar 2017 21:25:41 +0000 Subject: [PATCH] fix(aio): reimplement side-nav --- aio/src/app/app.component.ts | 6 +- .../layout/nav-item/nav-item.component.html | 27 +++++++++ .../layout/nav-item/nav-item.component.scss | 4 +- .../app/layout/nav-item/nav-item.component.ts | 60 ++++++++----------- .../app/layout/nav-menu/nav-menu.component.ts | 7 ++- 5 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 aio/src/app/layout/nav-item/nav-item.component.html diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index b867988f2f..0eb7cd6050 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -1,7 +1,7 @@ import { Component, ViewChild, OnInit } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { DocumentService, DocumentContents } from 'app/documents/document.service'; -import { NavigationService, NavigationViews } from 'app/navigation/navigation.service'; +import { NavigationService, NavigationViews, NavigationNode } from 'app/navigation/navigation.service'; import { SearchService, QueryResults } from 'app/search/search.service'; @Component({ @@ -19,7 +19,7 @@ import { SearchService, QueryResults } from 'app/search/search.service'; - +
@@ -93,11 +93,13 @@ export class AppComponent implements OnInit { currentDocument: Observable; navigationViews: Observable; + selectedNodes: Observable; searchResults: Observable; constructor(documentService: DocumentService, navigationService: NavigationService, private searchService: SearchService) { this.currentDocument = documentService.currentDocument; this.navigationViews = navigationService.navigationViews; + this.selectedNodes = navigationService.activeNodes; this.searchResults = searchService.searchResults; } diff --git a/aio/src/app/layout/nav-item/nav-item.component.html b/aio/src/app/layout/nav-item/nav-item.component.html new file mode 100644 index 0000000000..8599bc242e --- /dev/null +++ b/aio/src/app/layout/nav-item/nav-item.component.html @@ -0,0 +1,27 @@ + + + \ No newline at end of file diff --git a/aio/src/app/layout/nav-item/nav-item.component.scss b/aio/src/app/layout/nav-item/nav-item.component.scss index 19ba2a0b86..e842864073 100644 --- a/aio/src/app/layout/nav-item/nav-item.component.scss +++ b/aio/src/app/layout/nav-item/nav-item.component.scss @@ -80,7 +80,7 @@ a.vertical-menu { display: none; } -.material-icons.active { +.material-icons.expanded { display: inline-block; position: absolute; top: 6px; @@ -91,7 +91,7 @@ a.vertical-menu { display: none; } -.heading-children.active { +.heading-children.expanded { display: block; } diff --git a/aio/src/app/layout/nav-item/nav-item.component.ts b/aio/src/app/layout/nav-item/nav-item.component.ts index f74935756e..d147468319 100644 --- a/aio/src/app/layout/nav-item/nav-item.component.ts +++ b/aio/src/app/layout/nav-item/nav-item.component.ts @@ -1,50 +1,42 @@ -import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core'; -import { NavigationService, NavigationNode } from 'app/navigation/navigation.service'; +import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; +import { NavigationNode } from 'app/navigation/navigation.service'; @Component({ selector: 'aio-nav-item', - template: ` - `, - styles: ['nav-item.component.scss'], - // we don't expect the inputs to change - changeDetection: ChangeDetectionStrategy.OnPush + templateUrl: 'nav-item.component.html', + styleUrls: ['nav-item.component.scss'] }) -export class NavItemComponent implements OnInit { +export class NavItemComponent implements OnChanges { + @Input() selectedNodes: NavigationNode[]; @Input() node: NavigationNode; @Input() level = 1; - isActive: boolean; - + isExpanded = false; + isSelected = false; classes: {[index: string]: boolean }; - constructor(navigation: NavigationService) { - navigation.activeNodes.subscribe(nodes => { - this.classes['active'] = nodes.indexOf(this.node) !== -1; - }); + ngOnChanges(changes: SimpleChanges) { + if (changes['selectedNodes'] || changes['node']) { + this.isSelected = this.selectedNodes.indexOf(this.node) !== -1; + } + this.setClasses(); } - ngOnInit() { + setClasses() { this.classes = { ['level-' + this.level]: true, - active: false, - heading: !!this.node.children + expanded: this.isExpanded, + selected: this.isSelected }; } + + itemClicked() { + this.isExpanded = true; + this.isSelected = !!this.node; + } + + headerClicked() { + this.isExpanded = !this.isExpanded; + this.setClasses(); + } } diff --git a/aio/src/app/layout/nav-menu/nav-menu.component.ts b/aio/src/app/layout/nav-menu/nav-menu.component.ts index 9847082236..509f168360 100644 --- a/aio/src/app/layout/nav-menu/nav-menu.component.ts +++ b/aio/src/app/layout/nav-menu/nav-menu.component.ts @@ -3,12 +3,13 @@ import { NavigationNode } from 'app/navigation/navigation.service'; @Component({ selector: 'aio-nav-menu', - template: ``, - // we don't expect the inputs to change - changeDetection: ChangeDetectionStrategy.OnPush + template: `` }) export class NavMenuComponent { + @Input() + selectedNodes: NavigationNode[]; + @Input() nodes: NavigationNode[]; }