refactor(router): clean up RouterLinkActive (#13273)

PR Close #13273
This commit is contained in:
Dzmitry Shylovich 2016-12-06 17:19:50 +03:00 committed by Miško Hevery
parent be6c95ad03
commit 1a92e3d406
2 changed files with 12 additions and 16 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {AfterContentInit, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, QueryList, Renderer} from '@angular/core'; import {AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, QueryList, Renderer} from '@angular/core';
import {Subscription} from 'rxjs/Subscription'; import {Subscription} from 'rxjs/Subscription';
import {NavigationEnd, Router} from '../router'; import {NavigationEnd, Router} from '../router';
@ -82,7 +82,7 @@ import {RouterLink, RouterLinkWithHref} from './router_link';
exportAs: 'routerLinkActive', exportAs: 'routerLinkActive',
}) })
export class RouterLinkActive implements OnChanges, export class RouterLinkActive implements OnChanges,
OnDestroy, AfterContentInit { OnDestroy, AfterContentInit {
@ContentChildren(RouterLink, {descendants: true}) links: QueryList<RouterLink>; @ContentChildren(RouterLink, {descendants: true}) links: QueryList<RouterLink>;
@ContentChildren(RouterLinkWithHref, {descendants: true}) @ContentChildren(RouterLinkWithHref, {descendants: true})
linksWithHrefs: QueryList<RouterLinkWithHref>; linksWithHrefs: QueryList<RouterLinkWithHref>;
@ -103,22 +103,18 @@ export class RouterLinkActive implements OnChanges,
get isActive(): boolean { return this.hasActiveLink(); } get isActive(): boolean { return this.hasActiveLink(); }
ngAfterContentInit(): void { ngAfterContentInit(): void {
this.links.changes.subscribe(s => this.update()); this.links.changes.subscribe(_ => this.update());
this.linksWithHrefs.changes.subscribe(s => this.update()); this.linksWithHrefs.changes.subscribe(_ => this.update());
this.update(); this.update();
} }
@Input() @Input()
set routerLinkActive(data: string[]|string) { set routerLinkActive(data: string[]|string) {
if (Array.isArray(data)) { this.classes = (Array.isArray(data) ? data : data.split(' ')).filter(c => !!c);
this.classes = <any>data;
} else {
this.classes = data.split(' ');
}
} }
ngOnChanges(changes: {}): any { this.update(); } ngOnChanges(changes: {}): void { this.update(); }
ngOnDestroy(): any { this.subscription.unsubscribe(); } ngOnDestroy(): void { this.subscription.unsubscribe(); }
private update(): void { private update(): void {
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return; if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
@ -133,11 +129,11 @@ export class RouterLinkActive implements OnChanges,
private isLinkActive(router: Router): (link: (RouterLink|RouterLinkWithHref)) => boolean { private isLinkActive(router: Router): (link: (RouterLink|RouterLinkWithHref)) => boolean {
return (link: RouterLink | RouterLinkWithHref) => return (link: RouterLink | RouterLinkWithHref) =>
router.isActive(link.urlTree, this.routerLinkActiveOptions.exact); router.isActive(link.urlTree, this.routerLinkActiveOptions.exact);
} }
private hasActiveLink(): boolean { private hasActiveLink(): boolean {
return this.links.some(this.isLinkActive(this.router)) || return this.links.some(this.isLinkActive(this.router)) ||
this.linksWithHrefs.some(this.isLinkActive(this.router)); this.linksWithHrefs.some(this.isLinkActive(this.router));
} }
} }

View File

@ -262,10 +262,10 @@ export declare class RouterLinkActive implements OnChanges, OnDestroy, AfterCont
routerLinkActiveOptions: { routerLinkActiveOptions: {
exact: boolean; exact: boolean;
}; };
constructor(router: Router, element: ElementRef, renderer: Renderer); constructor(router: Router, element: ElementRef, renderer: Renderer, cdr: ChangeDetectorRef);
ngAfterContentInit(): void; ngAfterContentInit(): void;
ngOnChanges(changes: {}): any; ngOnChanges(changes: {}): void;
ngOnDestroy(): any; ngOnDestroy(): void;
} }
/** @stable */ /** @stable */