2016-06-01 17:32:15 -04:00
|
|
|
import { ComponentResolver, ReflectiveInjector, Type, Injector } from '@angular/core';
|
2016-05-24 16:23:27 -04:00
|
|
|
import { Location } from '@angular/common';
|
|
|
|
import { UrlSerializer } from './url_serializer';
|
|
|
|
import { RouterOutletMap } from './router_outlet_map';
|
|
|
|
import { recognize } from './recognize';
|
2016-06-01 16:34:48 -04:00
|
|
|
import { resolve } from './resolve';
|
|
|
|
import { createRouterState } from './create_router_state';
|
|
|
|
import { TreeNode } from './utils/tree';
|
2016-05-26 19:51:56 -04:00
|
|
|
import { UrlTree, createEmptyUrlTree } from './url_tree';
|
|
|
|
import { PRIMARY_OUTLET, Params } from './shared';
|
2016-06-01 17:32:15 -04:00
|
|
|
import { createEmptyState, RouterState, RouterStateCandidate, ActivatedRoute, ActivatedRouteCandidate} from './router_state';
|
2016-05-24 16:23:27 -04:00
|
|
|
import { RouterConfig } from './config';
|
|
|
|
import { RouterOutlet } from './directives/router_outlet';
|
2016-05-26 19:51:56 -04:00
|
|
|
import { createUrlTree } from './create_url_tree';
|
2016-06-01 17:32:15 -04:00
|
|
|
import { forEach, and, shallowEqual } from './utils/collection';
|
2016-05-26 19:51:56 -04:00
|
|
|
import { Observable } from 'rxjs/Observable';
|
2016-05-24 16:23:27 -04:00
|
|
|
import { Subscription } from 'rxjs/Subscription';
|
2016-05-26 19:51:56 -04:00
|
|
|
import 'rxjs/add/operator/map';
|
2016-06-01 17:32:15 -04:00
|
|
|
import 'rxjs/add/operator/scan';
|
2016-05-26 19:51:56 -04:00
|
|
|
import 'rxjs/add/operator/mergeMap';
|
2016-06-01 17:32:15 -04:00
|
|
|
import {of} from 'rxjs/observable/of';
|
|
|
|
import {forkJoin} from 'rxjs/observable/forkJoin';
|
2016-05-26 19:51:56 -04:00
|
|
|
|
|
|
|
export interface NavigationExtras { relativeTo?: ActivatedRoute; queryParameters?: Params; fragment?: string; }
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* The `Router` is responsible for mapping URLs to components.
|
|
|
|
*/
|
2016-05-24 16:23:27 -04:00
|
|
|
export class Router {
|
2016-05-26 19:51:56 -04:00
|
|
|
private currentUrlTree: UrlTree;
|
|
|
|
private currentRouterState: RouterState;
|
2016-05-24 16:23:27 -04:00
|
|
|
private config: RouterConfig;
|
|
|
|
private locationSubscription: Subscription;
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-01 17:32:15 -04:00
|
|
|
constructor(private rootComponentType:Type, private resolver: ComponentResolver, private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap, private location: Location, private injector: Injector) {
|
2016-05-26 19:51:56 -04:00
|
|
|
this.currentUrlTree = createEmptyUrlTree();
|
2016-06-01 20:57:53 -04:00
|
|
|
this.currentRouterState = createEmptyState(rootComponentType);
|
2016-05-24 16:23:27 -04:00
|
|
|
this.setUpLocationChangeListener();
|
|
|
|
this.navigateByUrl(this.location.path());
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* Returns the current route state.
|
|
|
|
*/
|
|
|
|
get routerState(): RouterState {
|
2016-05-26 19:51:56 -04:00
|
|
|
return this.currentRouterState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current url tree.
|
|
|
|
*/
|
|
|
|
get urlTree(): UrlTree {
|
|
|
|
return this.currentUrlTree;
|
2016-05-24 16:41:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate based on the provided url. This navigation is always absolute.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.navigateByUrl("/team/33/user/11");
|
|
|
|
* ```
|
|
|
|
*/
|
2016-05-26 19:51:56 -04:00
|
|
|
navigateByUrl(url: string): Observable<void> {
|
2016-05-24 16:23:27 -04:00
|
|
|
const urlTree = this.urlSerializer.parse(url);
|
2016-05-26 19:51:56 -04:00
|
|
|
return this.runNavigate(urlTree, false);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* Resets the configuration used for navigation and generating links.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.resetConfig([
|
2016-05-26 19:51:56 -04:00
|
|
|
* { path: 'team/:id', component: TeamCmp, children: [
|
|
|
|
* { path: 'simple', component: SimpleCmp },
|
|
|
|
* { path: 'user/:name', component: UserCmp }
|
2016-05-24 16:41:37 -04:00
|
|
|
* ] }
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*/
|
2016-05-24 16:23:27 -04:00
|
|
|
resetConfig(config: RouterConfig): void {
|
|
|
|
this.config = config;
|
|
|
|
}
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-05-24 17:33:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
dispose(): void { this.locationSubscription.unsubscribe(); }
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
/**
|
|
|
|
* Applies an array of commands to the current url tree and creates
|
|
|
|
* a new url tree.
|
|
|
|
*
|
|
|
|
* When given an activate route, applies the given commands starting from the route.
|
|
|
|
* When not given a route, applies the given command starting from the root.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // create /team/33/user/11
|
|
|
|
* router.createUrlTree(['/team', 33, 'user', 11]);
|
|
|
|
*
|
|
|
|
* // create /team/33;expand=true/user/11
|
|
|
|
* router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);
|
|
|
|
*
|
|
|
|
* // you can collapse static fragments like this
|
|
|
|
* router.createUrlTree(['/team/33/user', userId]);
|
|
|
|
*
|
|
|
|
* // assuming the current url is `/team/33/user/11` and the route points to `user/11`
|
|
|
|
*
|
|
|
|
* // navigate to /team/33/user/11/details
|
|
|
|
* router.createUrlTree(['details'], {relativeTo: route});
|
|
|
|
*
|
|
|
|
* // navigate to /team/33/user/22
|
|
|
|
* router.createUrlTree(['../22'], {relativeTo: route});
|
|
|
|
*
|
|
|
|
* // navigate to /team/44/user/22
|
|
|
|
* router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
createUrlTree(commands: any[], {relativeTo, queryParameters, fragment}: NavigationExtras = {}): UrlTree {
|
|
|
|
const a = relativeTo ? relativeTo : this.routerState.root;
|
|
|
|
return createUrlTree(a, this.currentUrlTree, commands, queryParameters, fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate based on the provided array of commands and a starting point.
|
|
|
|
* If no starting route is provided, the navigation is absolute.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.navigate(['team', 33, 'team', '11], {relativeTo: route});
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
navigate(commands: any[], extras: NavigationExtras = {}): Observable<void> {
|
|
|
|
return this.runNavigate(this.createUrlTree(commands, extras));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes a {@link UrlTree} into a string.
|
|
|
|
*/
|
|
|
|
serializeUrl(url: UrlTree): string { return this.urlSerializer.serialize(url); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string into a {@link UrlTree}.
|
|
|
|
*/
|
|
|
|
parseUrl(url: string): UrlTree { return this.urlSerializer.parse(url); }
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
private setUpLocationChangeListener(): void {
|
|
|
|
this.locationSubscription = <any>this.location.subscribe((change) => {
|
2016-05-26 19:51:56 -04:00
|
|
|
this.runNavigate(this.urlSerializer.parse(change['url']), change['pop'])
|
2016-05-24 16:23:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
private runNavigate(url:UrlTree, pop?:boolean):Observable<any> {
|
2016-06-01 16:34:48 -04:00
|
|
|
let state;
|
|
|
|
const r = recognize(this.rootComponentType, this.config, url).mergeMap((newRouterStateCandidate) => {
|
|
|
|
return resolve(this.resolver, newRouterStateCandidate);
|
|
|
|
|
|
|
|
}).map((routerStateCandidate) => {
|
2016-06-01 17:32:15 -04:00
|
|
|
return createRouterState(routerStateCandidate, this.currentRouterState);
|
2016-06-01 16:34:48 -04:00
|
|
|
|
|
|
|
}).map((newState:RouterState) => {
|
|
|
|
state = newState;
|
2016-06-01 17:32:15 -04:00
|
|
|
|
|
|
|
}).mergeMap(_ => {
|
|
|
|
return new GuardChecks(state.candidate, this.currentRouterState.candidate, this.injector).check(this.outletMap);
|
2016-06-01 16:34:48 -04:00
|
|
|
});
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
r.subscribe((shouldActivate) => {
|
|
|
|
if (!shouldActivate) return;
|
|
|
|
new ActivateRoutes(state, this.currentRouterState).activate(this.outletMap);
|
2016-06-01 16:34:48 -04:00
|
|
|
|
|
|
|
this.currentUrlTree = url;
|
|
|
|
this.currentRouterState = state;
|
|
|
|
|
|
|
|
if (!pop) {
|
|
|
|
this.location.go(this.urlSerializer.serialize(url));
|
|
|
|
}
|
2016-05-26 19:51:56 -04:00
|
|
|
});
|
|
|
|
return r;
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
class GuardChecks {
|
|
|
|
private checks = [];
|
|
|
|
constructor(private future: RouterStateCandidate, private curr: RouterStateCandidate, private injector: Injector) {}
|
|
|
|
|
|
|
|
check(parentOutletMap: RouterOutletMap): Observable<boolean> {
|
|
|
|
const futureRoot = this.future._root;
|
|
|
|
const currRoot = this.curr ? this.curr._root : null;
|
|
|
|
this.traverseChildRoutes(futureRoot, currRoot, parentOutletMap);
|
|
|
|
if (this.checks.length === 0) return of(true);
|
|
|
|
return forkJoin(this.checks.map(s => this.runCanActivate(s))).map(and);
|
|
|
|
}
|
|
|
|
|
|
|
|
private traverseChildRoutes(futureNode: TreeNode<ActivatedRouteCandidate>,
|
|
|
|
currNode: TreeNode<ActivatedRouteCandidate> | null,
|
|
|
|
outletMap: RouterOutletMap): void {
|
|
|
|
const prevChildren = nodeChildrenAsMap(currNode);
|
|
|
|
futureNode.children.forEach(c => {
|
|
|
|
this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap);
|
|
|
|
delete prevChildren[c.value.outlet];
|
|
|
|
});
|
|
|
|
forEach(prevChildren, (v, k) => this.deactivateOutletAndItChildren(outletMap._outlets[k]));
|
|
|
|
}
|
|
|
|
|
|
|
|
traverseRoutes(futureNode: TreeNode<ActivatedRouteCandidate>, currNode: TreeNode<ActivatedRouteCandidate> | null,
|
|
|
|
parentOutletMap: RouterOutletMap): void {
|
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
|
|
|
|
|
|
|
if (future === curr) {
|
|
|
|
if (!shallowEqual(future.params, curr.params)) {
|
|
|
|
this.checks.push(future);
|
|
|
|
}
|
|
|
|
this.traverseChildRoutes(futureNode, currNode, null);
|
|
|
|
} else {
|
|
|
|
this.deactivateOutletAndItChildren(null);
|
|
|
|
this.checks.push(future);
|
|
|
|
this.traverseChildRoutes(futureNode, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private deactivateOutletAndItChildren(outlet: RouterOutlet): void {}
|
|
|
|
|
|
|
|
private runCanActivate(future: ActivatedRouteCandidate): Observable<boolean> {
|
|
|
|
const canActivate = future._routeConfig ? future._routeConfig.canActivate : null;
|
|
|
|
if (!canActivate || canActivate.length === 0) return of(true);
|
|
|
|
return forkJoin(canActivate.map(c => {
|
|
|
|
const guard = this.injector.get(c);
|
|
|
|
return of(guard(future, this.future));
|
|
|
|
})).map(and);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
class ActivateRoutes {
|
2016-06-01 17:32:15 -04:00
|
|
|
constructor(private futureState: RouterState, private currState: RouterState) {}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-06-01 16:34:48 -04:00
|
|
|
activate(parentOutletMap: RouterOutletMap):void {
|
|
|
|
const futureRoot = this.futureState._root;
|
|
|
|
const currRoot = this.currState ? this.currState._root : null;
|
2016-06-01 17:32:15 -04:00
|
|
|
|
|
|
|
pushQueryParamsAndFragment(this.futureState);
|
|
|
|
this.activateChildRoutes(futureRoot, currRoot, parentOutletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private activateChildRoutes(futureNode: TreeNode<ActivatedRoute>,
|
|
|
|
currNode: TreeNode<ActivatedRoute> | null,
|
2016-06-01 16:34:48 -04:00
|
|
|
outletMap: RouterOutletMap): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
const prevChildren = nodeChildrenAsMap(currNode);
|
2016-06-01 17:32:15 -04:00
|
|
|
futureNode.children.forEach(c => {
|
|
|
|
this.activateRoutes(c, prevChildren[c.value.outlet], outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
delete prevChildren[c.value.outlet];
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-05-24 16:23:27 -04:00
|
|
|
forEach(prevChildren, (v, k) => this.deactivateOutletAndItChildren(outletMap._outlets[k]));
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
activateRoutes(futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute> | null,
|
2016-06-01 16:34:48 -04:00
|
|
|
parentOutletMap: RouterOutletMap): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
|
|
|
const outlet = getOutlet(parentOutletMap, futureNode.value);
|
|
|
|
|
|
|
|
if (future === curr) {
|
2016-06-01 17:32:15 -04:00
|
|
|
pushValues(future);
|
|
|
|
this.activateChildRoutes(futureNode, currNode, outlet.outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
} else {
|
|
|
|
this.deactivateOutletAndItChildren(outlet);
|
|
|
|
const outletMap = new RouterOutletMap();
|
2016-06-01 17:32:15 -04:00
|
|
|
this.activateNewRoutes(outletMap, future, outlet);
|
|
|
|
this.activateChildRoutes(futureNode, null, outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
private activateNewRoutes(outletMap: RouterOutletMap, future: ActivatedRoute, outlet: RouterOutlet): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
const resolved = ReflectiveInjector.resolve([
|
|
|
|
{provide: ActivatedRoute, useValue: future},
|
|
|
|
{provide: RouterOutletMap, useValue: outletMap}
|
|
|
|
]);
|
2016-06-01 17:32:15 -04:00
|
|
|
outlet.activate(future.candidate._resolvedComponentFactory, resolved, outletMap);
|
|
|
|
pushValues(future);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private deactivateOutletAndItChildren(outlet: RouterOutlet): void {
|
|
|
|
if (outlet && outlet.isActivated) {
|
|
|
|
forEach(outlet.outletMap._outlets, (v, k) => this.deactivateOutletAndItChildren(v));
|
|
|
|
outlet.deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
function pushValues(route: ActivatedRoute): void {
|
|
|
|
if (!shallowEqual(route.candidate.params, (<any>route.params).value)) {
|
|
|
|
(<any>route.urlSegments).next(route.candidate.urlSegments);
|
|
|
|
(<any>route.params).next(route.candidate.params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pushQueryParamsAndFragment(state: RouterState): void {
|
|
|
|
if (!shallowEqual(state.candidate.queryParams, (<any>state.queryParams).value)) {
|
|
|
|
(<any>state.queryParams).next(state.candidate.queryParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.candidate.fragment !== (<any>state.fragment).value) {
|
|
|
|
(<any>state.fragment).next(state.candidate.fragment);
|
|
|
|
}
|
2016-06-01 16:34:48 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
function nodeChildrenAsMap(node: TreeNode<any>|null) {
|
2016-05-24 16:23:27 -04:00
|
|
|
return node ?
|
|
|
|
node.children.reduce(
|
|
|
|
(m, c) => {
|
|
|
|
m[c.value.outlet] = c;
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
{}) :
|
|
|
|
{};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOutlet(outletMap: RouterOutletMap, route: ActivatedRoute): RouterOutlet {
|
|
|
|
let outlet = outletMap._outlets[route.outlet];
|
|
|
|
if (!outlet) {
|
|
|
|
if (route.outlet === PRIMARY_OUTLET) {
|
|
|
|
throw new Error(`Cannot find primary outlet`);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Cannot find the outlet ${route.outlet}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outlet;
|
|
|
|
}
|