Skip to content

Commit

Permalink
refactor(router): clean up RouterLinkActive (angular#13273)
Browse files Browse the repository at this point in the history
PR Close angular#13273
  • Loading branch information
Dzmitry Shylovich authored and juleskremer committed Aug 24, 2017
1 parent 12908e3 commit a09cbbf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
22 changes: 9 additions & 13 deletions modules/@angular/router/src/directives/router_link_active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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 {NavigationEnd, Router} from '../router';
Expand Down Expand Up @@ -82,7 +82,7 @@ import {RouterLink, RouterLinkWithHref} from './router_link';
exportAs: 'routerLinkActive',
})
export class RouterLinkActive implements OnChanges,
OnDestroy, AfterContentInit {
OnDestroy, AfterContentInit {
@ContentChildren(RouterLink, {descendants: true}) links: QueryList<RouterLink>;
@ContentChildren(RouterLinkWithHref, {descendants: true})
linksWithHrefs: QueryList<RouterLinkWithHref>;
Expand All @@ -103,22 +103,18 @@ export class RouterLinkActive implements OnChanges,
get isActive(): boolean { return this.hasActiveLink(); }

ngAfterContentInit(): void {
this.links.changes.subscribe(s => this.update());
this.linksWithHrefs.changes.subscribe(s => this.update());
this.links.changes.subscribe(_ => this.update());
this.linksWithHrefs.changes.subscribe(_ => this.update());
this.update();
}

@Input()
set routerLinkActive(data: string[]|string) {
if (Array.isArray(data)) {
this.classes = <any>data;
} else {
this.classes = data.split(' ');
}
this.classes = (Array.isArray(data) ? data : data.split(' ')).filter(c => !!c);
}

ngOnChanges(changes: {}): any { this.update(); }
ngOnDestroy(): any { this.subscription.unsubscribe(); }
ngOnChanges(changes: {}): void { this.update(); }
ngOnDestroy(): void { this.subscription.unsubscribe(); }

private update(): void {
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
Expand All @@ -133,11 +129,11 @@ export class RouterLinkActive implements OnChanges,

private isLinkActive(router: Router): (link: (RouterLink|RouterLinkWithHref)) => boolean {
return (link: RouterLink | RouterLinkWithHref) =>
router.isActive(link.urlTree, this.routerLinkActiveOptions.exact);
router.isActive(link.urlTree, this.routerLinkActiveOptions.exact);
}

private hasActiveLink(): boolean {
return this.links.some(this.isLinkActive(this.router)) ||
this.linksWithHrefs.some(this.isLinkActive(this.router));
this.linksWithHrefs.some(this.isLinkActive(this.router));
}
}
6 changes: 3 additions & 3 deletions tools/public_api_guard/router/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ export declare class RouterLinkActive implements OnChanges, OnDestroy, AfterCont
routerLinkActiveOptions: {
exact: boolean;
};
constructor(router: Router, element: ElementRef, renderer: Renderer);
constructor(router: Router, element: ElementRef, renderer: Renderer, cdr: ChangeDetectorRef);
ngAfterContentInit(): void;
ngOnChanges(changes: {}): any;
ngOnDestroy(): any;
ngOnChanges(changes: {}): void;
ngOnDestroy(): void;
}

/** @stable */
Expand Down

0 comments on commit a09cbbf

Please sign in to comment.