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);
}
}

View File

@ -31,7 +31,7 @@ import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader';
import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state';
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 {TreeNode} from './utils/tree';
@ -305,6 +305,18 @@ export class Router {
*/
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> {
const id = ++this.navigationId;
this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url)));

View File

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