feat(router): add isActive to router
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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)));
|
||||
|
Reference in New Issue
Block a user