feat(router): add isActive to router

This commit is contained in:
vsavkin
2016-07-28 17:59:05 -07:00
parent 2fdb39e60a
commit 5162fb6d52
3 changed files with 21 additions and 7 deletions

View File

@ -10,7 +10,7 @@ import {AfterContentInit, ContentChildren, Directive, ElementRef, Input, OnChang
import {Subscription} from 'rxjs/Subscription';
import {NavigationEnd, Router} from '../router';
import {UrlTree, containsTree} from '../url_tree';
import {UrlTree} from '../url_tree';
import {RouterLink, RouterLinkWithHref} from './router_link';
@ -97,18 +97,17 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
private update(): void {
if (!this.links || !this.linksWithHrefs || !this.router.navigated) return;
const currentUrlTree = this.router.parseUrl(this.router.url);
const isActiveLinks = this.reduceList(currentUrlTree, this.links);
const isActiveLinksWithHrefs = this.reduceList(currentUrlTree, this.linksWithHrefs);
const isActiveLinks = this.reduceList(this.links);
const isActiveLinksWithHrefs = this.reduceList(this.linksWithHrefs);
this.classes.forEach(
c => this.renderer.setElementClass(
this.element.nativeElement, c, isActiveLinks || isActiveLinksWithHrefs));
}
private reduceList(currentUrlTree: UrlTree, q: QueryList<any>): boolean {
private reduceList(q: QueryList<any>): boolean {
return q.reduce(
(res: boolean, link: any) =>
res || containsTree(currentUrlTree, link.urlTree, this.routerLinkActiveOptions.exact),
res || this.router.isActive(link.urlTree, this.routerLinkActiveOptions.exact),
false);
}
}