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

View File

@ -31,7 +31,7 @@ import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader';
import {RouterOutletMap} from './router_outlet_map'; import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state'; import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state';
import {PRIMARY_OUTLET, Params} from './shared'; import {PRIMARY_OUTLET, Params} from './shared';
import {UrlSerializer, UrlTree, createEmptyUrlTree} from './url_tree'; import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
import {andObservables, forEach, merge, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection'; import {andObservables, forEach, merge, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection';
import {TreeNode} from './utils/tree'; import {TreeNode} from './utils/tree';
@ -305,6 +305,18 @@ export class Router {
*/ */
parseUrl(url: string): UrlTree { return this.urlSerializer.parse(url); } parseUrl(url: string): UrlTree { return this.urlSerializer.parse(url); }
/**
* Returns if the url is activated or not.
*/
isActive(url: string|UrlTree, exact: boolean): boolean {
if (url instanceof UrlTree) {
return containsTree(this.currentUrlTree, url, exact);
} else {
const urlTree = this.urlSerializer.parse(url);
return containsTree(this.currentUrlTree, urlTree, exact);
}
}
private scheduleNavigation(url: UrlTree, preventPushState: boolean): Promise<boolean> { private scheduleNavigation(url: UrlTree, preventPushState: boolean): Promise<boolean> {
const id = ++this.navigationId; const id = ++this.navigationId;
this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url))); this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url)));

View File

@ -4,6 +4,7 @@ export declare class ActivatedRoute {
data: Observable<Data>; data: Observable<Data>;
outlet: string; outlet: string;
params: Observable<Params>; params: Observable<Params>;
routeConfig: Route;
snapshot: ActivatedRouteSnapshot; snapshot: ActivatedRouteSnapshot;
url: Observable<UrlSegment[]>; url: Observable<UrlSegment[]>;
toString(): string; toString(): string;
@ -15,6 +16,7 @@ export declare class ActivatedRouteSnapshot {
data: Data; data: Data;
outlet: string; outlet: string;
params: Params; params: Params;
routeConfig: Route;
url: UrlSegment[]; url: UrlSegment[];
toString(): string; toString(): string;
} }
@ -157,6 +159,7 @@ export declare class Router {
createUrlTree(commands: any[], {relativeTo, queryParams, fragment, preserveQueryParams, preserveFragment}?: NavigationExtras): UrlTree; createUrlTree(commands: any[], {relativeTo, queryParams, fragment, preserveQueryParams, preserveFragment}?: NavigationExtras): UrlTree;
dispose(): void; dispose(): void;
initialNavigation(): void; initialNavigation(): void;
isActive(url: string | UrlTree, exact: boolean): boolean;
navigate(commands: any[], extras?: NavigationExtras): Promise<boolean>; navigate(commands: any[], extras?: NavigationExtras): Promise<boolean>;
navigateByUrl(url: string | UrlTree): Promise<boolean>; navigateByUrl(url: string | UrlTree): Promise<boolean>;
parseUrl(url: string): UrlTree; parseUrl(url: string): UrlTree;