2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-09-27 20:12:25 -04:00
|
|
|
import {Type} from '@angular/core';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2017-03-17 13:09:42 -04:00
|
|
|
import {map} from 'rxjs/operator/map';
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
import {Data, ResolveData, Route} from './config';
|
2017-03-17 13:09:42 -04:00
|
|
|
import {PRIMARY_OUTLET, ParamMap, Params, convertToParamMap} from './shared';
|
2016-10-28 18:17:00 -04:00
|
|
|
import {UrlSegment, UrlSegmentGroup, UrlTree, equalSegments} from './url_tree';
|
2017-03-26 10:15:10 -04:00
|
|
|
import {shallowEqual, shallowEqualArrays} from './utils/collection';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {Tree, TreeNode} from './utils/tree';
|
|
|
|
|
2017-03-17 13:09:42 -04:00
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-09-10 19:53:09 -04:00
|
|
|
* @whatItDoes Represents the state of the router.
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @howToUse
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-09-10 19:53:09 -04:00
|
|
|
* @Component({templateUrl:'template.html'})
|
2016-05-24 16:41:37 -04:00
|
|
|
* class MyComponent {
|
|
|
|
* constructor(router: Router) {
|
2016-09-10 19:53:09 -04:00
|
|
|
* const state: RouterState = router.routerState;
|
|
|
|
* const root: ActivatedRoute = state.root;
|
|
|
|
* const child = root.firstChild;
|
|
|
|
* const id: Observable<string> = child.params.map(p => p.id);
|
|
|
|
* //...
|
2016-05-24 16:41:37 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @description
|
|
|
|
* RouterState is a tree of activated routes. Every node in this tree knows about the "consumed" URL
|
|
|
|
* segments,
|
|
|
|
* the extracted parameters, and the resolved data.
|
|
|
|
*
|
|
|
|
* See {@link ActivatedRoute} for more information.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-05-24 16:41:37 -04:00
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
export class RouterState extends Tree<ActivatedRoute> {
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-09-10 19:53:09 -04:00
|
|
|
constructor(
|
|
|
|
root: TreeNode<ActivatedRoute>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The current snapshot of the router state */
|
2016-09-10 19:53:09 -04:00
|
|
|
public snapshot: RouterStateSnapshot) {
|
2016-05-23 19:14:23 -04:00
|
|
|
super(root);
|
2016-08-02 17:34:00 -04:00
|
|
|
setRouterStateSnapshot<RouterState, ActivatedRoute>(this, root);
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
2016-06-09 17:11:54 -04:00
|
|
|
|
2016-06-09 17:33:09 -04:00
|
|
|
toString(): string { return this.snapshot.toString(); }
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
export function createEmptyState(urlTree: UrlTree, rootComponent: Type<any>): RouterState {
|
2016-06-14 17:55:59 -04:00
|
|
|
const snapshot = createEmptyStateSnapshot(urlTree, rootComponent);
|
2016-07-25 15:15:07 -04:00
|
|
|
const emptyUrl = new BehaviorSubject([new UrlSegment('', {})]);
|
2016-05-23 19:14:23 -04:00
|
|
|
const emptyParams = new BehaviorSubject({});
|
2016-06-27 17:00:07 -04:00
|
|
|
const emptyData = new BehaviorSubject({});
|
2016-05-23 19:14:23 -04:00
|
|
|
const emptyQueryParams = new BehaviorSubject({});
|
2016-06-08 14:13:41 -04:00
|
|
|
const fragment = new BehaviorSubject('');
|
2016-06-27 17:00:07 -04:00
|
|
|
const activated = new ActivatedRoute(
|
2016-08-02 18:31:56 -04:00
|
|
|
emptyUrl, emptyParams, emptyQueryParams, fragment, emptyData, PRIMARY_OUTLET, rootComponent,
|
|
|
|
snapshot.root);
|
2016-06-02 17:44:57 -04:00
|
|
|
activated.snapshot = snapshot.root;
|
2016-08-02 18:31:56 -04:00
|
|
|
return new RouterState(new TreeNode<ActivatedRoute>(activated, []), snapshot);
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
export function createEmptyStateSnapshot(
|
2016-08-10 21:21:28 -04:00
|
|
|
urlTree: UrlTree, rootComponent: Type<any>): RouterStateSnapshot {
|
2016-06-01 16:29:02 -04:00
|
|
|
const emptyParams = {};
|
2016-06-27 17:00:07 -04:00
|
|
|
const emptyData = {};
|
2016-06-01 16:29:02 -04:00
|
|
|
const emptyQueryParams = {};
|
2016-06-08 14:13:41 -04:00
|
|
|
const fragment = '';
|
|
|
|
const activated = new ActivatedRouteSnapshot(
|
2016-08-02 18:31:56 -04:00
|
|
|
[], emptyParams, emptyQueryParams, fragment, emptyData, PRIMARY_OUTLET, rootComponent, null,
|
2016-10-25 17:33:18 -04:00
|
|
|
urlTree.root, -1, {});
|
2016-08-02 18:31:56 -04:00
|
|
|
return new RouterStateSnapshot('', new TreeNode<ActivatedRouteSnapshot>(activated, []));
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-09-10 19:53:09 -04:00
|
|
|
* @whatItDoes Contains the information about a route associated with a component loaded in an
|
|
|
|
* outlet.
|
2016-12-09 13:44:46 -05:00
|
|
|
* An `ActivatedRoute` can also be used to traverse the router state tree.
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @howToUse
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-12-09 13:44:46 -05:00
|
|
|
* @Component({...})
|
2016-05-24 16:41:37 -04:00
|
|
|
* class MyComponent {
|
|
|
|
* constructor(route: ActivatedRoute) {
|
|
|
|
* const id: Observable<string> = route.params.map(p => p.id);
|
2016-12-09 13:44:46 -05:00
|
|
|
* const url: Observable<string> = route.url.map(segments => segments.join(''));
|
|
|
|
* // route.data includes both `data` and `resolve`
|
|
|
|
* const user = route.data.map(d => d.user);
|
2016-05-24 16:41:37 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-05-24 16:41:37 -04:00
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
export class ActivatedRoute {
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The current snapshot of this route */
|
|
|
|
snapshot: ActivatedRouteSnapshot;
|
2016-06-02 17:44:57 -04:00
|
|
|
/** @internal */
|
|
|
|
_futureSnapshot: ActivatedRouteSnapshot;
|
2016-08-02 17:34:00 -04:00
|
|
|
/** @internal */
|
|
|
|
_routerState: RouterState;
|
2017-03-17 13:09:42 -04:00
|
|
|
/** @internal */
|
|
|
|
_paramMap: Observable<ParamMap>;
|
|
|
|
/** @internal */
|
|
|
|
_queryParamMap: Observable<ParamMap>;
|
2016-08-02 17:34:00 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of the URL segments matched by this route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public url: Observable<UrlSegment[]>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of the matrix parameters scoped to this route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public params: Observable<Params>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of the query parameters shared by all the routes */
|
2016-09-10 19:53:09 -04:00
|
|
|
public queryParams: Observable<Params>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of the URL fragment shared by all the routes */
|
2016-09-10 19:53:09 -04:00
|
|
|
public fragment: Observable<string>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of the static and resolved data of this route. */
|
2016-09-10 19:53:09 -04:00
|
|
|
public data: Observable<Data>,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The outlet name of the route. It's a constant */
|
2016-09-10 19:53:09 -04:00
|
|
|
public outlet: string,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The component of the route. It's a constant */
|
|
|
|
// TODO(vsavkin): remove |string
|
|
|
|
public component: Type<any>|string, futureSnapshot: ActivatedRouteSnapshot) {
|
2016-06-02 17:44:57 -04:00
|
|
|
this._futureSnapshot = futureSnapshot;
|
|
|
|
}
|
2016-06-09 17:11:54 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The configuration used to match this route */
|
2016-07-28 19:39:01 -04:00
|
|
|
get routeConfig(): Route { return this._futureSnapshot.routeConfig; }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The root of the router state */
|
2016-08-07 23:09:04 -04:00
|
|
|
get root(): ActivatedRoute { return this._routerState.root; }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The parent of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get parent(): ActivatedRoute { return this._routerState.parent(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The first child of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get firstChild(): ActivatedRoute { return this._routerState.firstChild(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The children of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get children(): ActivatedRoute[] { return this._routerState.children(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The path from the root of the router state tree to this route */
|
2016-08-02 17:34:00 -04:00
|
|
|
get pathFromRoot(): ActivatedRoute[] { return this._routerState.pathFromRoot(this); }
|
|
|
|
|
2017-03-17 13:09:42 -04:00
|
|
|
get paramMap(): Observable<ParamMap> {
|
|
|
|
if (!this._paramMap) {
|
|
|
|
this._paramMap = map.call(this.params, (p: Params): ParamMap => convertToParamMap(p));
|
|
|
|
}
|
|
|
|
return this._paramMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
get queryParamMap(): Observable<ParamMap> {
|
|
|
|
if (!this._queryParamMap) {
|
|
|
|
this._queryParamMap =
|
|
|
|
map.call(this.queryParams, (p: Params): ParamMap => convertToParamMap(p));
|
|
|
|
}
|
|
|
|
return this._queryParamMap;
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:11:54 -04:00
|
|
|
toString(): string {
|
|
|
|
return this.snapshot ? this.snapshot.toString() : `Future(${this._futureSnapshot})`;
|
|
|
|
}
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-10-25 17:33:18 -04:00
|
|
|
export type Inherited = {
|
2016-12-09 13:44:46 -05:00
|
|
|
params: Params,
|
|
|
|
data: Data,
|
|
|
|
resolve: Data,
|
2016-12-27 17:55:58 -05:00
|
|
|
};
|
2016-06-27 17:00:07 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-12-27 17:55:58 -05:00
|
|
|
export function inheritedParamsDataResolve(route: ActivatedRouteSnapshot): Inherited {
|
|
|
|
const pathToRoot = route.pathFromRoot;
|
|
|
|
|
|
|
|
let inhertingStartingFrom = pathToRoot.length - 1;
|
|
|
|
|
|
|
|
while (inhertingStartingFrom >= 1) {
|
|
|
|
const current = pathToRoot[inhertingStartingFrom];
|
|
|
|
const parent = pathToRoot[inhertingStartingFrom - 1];
|
|
|
|
// current route is an empty path => inherits its parent's params and data
|
|
|
|
if (current.routeConfig && current.routeConfig.path === '') {
|
|
|
|
inhertingStartingFrom--;
|
|
|
|
|
|
|
|
// parent is componentless => current route should inherit its params and data
|
|
|
|
} else if (!parent.component) {
|
|
|
|
inhertingStartingFrom--;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
break;
|
2016-10-25 17:33:18 -04:00
|
|
|
}
|
2016-12-27 17:55:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return pathToRoot.slice(inhertingStartingFrom).reduce((res, curr) => {
|
2017-03-26 10:15:10 -04:00
|
|
|
const params = {...res.params, ...curr.params};
|
|
|
|
const data = {...res.data, ...curr.data};
|
|
|
|
const resolve = {...res.resolve, ...curr._resolvedData};
|
2016-12-27 17:55:58 -05:00
|
|
|
return {params, data, resolve};
|
|
|
|
}, <any>{params: {}, data: {}, resolve: {}});
|
|
|
|
}
|
2016-10-25 17:33:18 -04:00
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
/**
|
2016-09-10 19:53:09 -04:00
|
|
|
* @whatItDoes Contains the information about a route associated with a component loaded in an
|
|
|
|
* outlet
|
|
|
|
* at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router
|
|
|
|
* state tree.
|
2016-06-01 20:55:21 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @howToUse
|
2016-06-01 20:55:21 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-09-10 19:53:09 -04:00
|
|
|
* @Component({templateUrl:'./my-component.html'})
|
2016-06-01 20:55:21 -04:00
|
|
|
* class MyComponent {
|
|
|
|
* constructor(route: ActivatedRoute) {
|
|
|
|
* const id: string = route.snapshot.params.id;
|
2016-09-10 19:53:09 -04:00
|
|
|
* const url: string = route.snapshot.url.join('');
|
|
|
|
* const user = route.snapshot.data.user;
|
2016-06-01 20:55:21 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-01 20:55:21 -04:00
|
|
|
*/
|
|
|
|
export class ActivatedRouteSnapshot {
|
2016-06-01 16:29:02 -04:00
|
|
|
/** @internal **/
|
2016-06-15 12:14:41 -04:00
|
|
|
_routeConfig: Route;
|
2016-06-02 14:30:38 -04:00
|
|
|
/** @internal **/
|
2016-07-25 15:15:07 -04:00
|
|
|
_urlSegment: UrlSegmentGroup;
|
2016-06-20 14:48:08 -04:00
|
|
|
/** @internal */
|
2016-06-14 17:55:59 -04:00
|
|
|
_lastPathIndex: number;
|
2016-06-27 17:00:07 -04:00
|
|
|
/** @internal */
|
2016-10-25 17:33:18 -04:00
|
|
|
_resolve: ResolveData;
|
|
|
|
/** @internal */
|
|
|
|
_resolvedData: Data;
|
2016-08-02 17:34:00 -04:00
|
|
|
/** @internal */
|
|
|
|
_routerState: RouterStateSnapshot;
|
2017-03-17 13:09:42 -04:00
|
|
|
/** @internal */
|
|
|
|
_paramMap: ParamMap;
|
|
|
|
/** @internal */
|
|
|
|
_queryParamMap: ParamMap;
|
2016-08-02 17:34:00 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The URL segments matched by this route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public url: UrlSegment[],
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The matrix parameters scoped to this route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public params: Params,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The query parameters shared by all the routes */
|
2016-09-10 19:53:09 -04:00
|
|
|
public queryParams: Params,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The URL fragment shared by all the routes */
|
2016-09-10 19:53:09 -04:00
|
|
|
public fragment: string,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The static and resolved data of this route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public data: Data,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The outlet name of the route */
|
2016-09-10 19:53:09 -04:00
|
|
|
public outlet: string,
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The component of the route */
|
2016-08-10 21:21:28 -04:00
|
|
|
public component: Type<any>|string, routeConfig: Route, urlSegment: UrlSegmentGroup,
|
2016-10-25 17:33:18 -04:00
|
|
|
lastPathIndex: number, resolve: ResolveData) {
|
2016-06-01 16:29:02 -04:00
|
|
|
this._routeConfig = routeConfig;
|
2016-06-14 17:55:59 -04:00
|
|
|
this._urlSegment = urlSegment;
|
|
|
|
this._lastPathIndex = lastPathIndex;
|
2016-06-27 17:00:07 -04:00
|
|
|
this._resolve = resolve;
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
2016-06-09 17:11:54 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The configuration used to match this route */
|
2016-07-28 19:39:01 -04:00
|
|
|
get routeConfig(): Route { return this._routeConfig; }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The root of the router state */
|
2016-08-07 23:09:04 -04:00
|
|
|
get root(): ActivatedRouteSnapshot { return this._routerState.root; }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The parent of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get parent(): ActivatedRouteSnapshot { return this._routerState.parent(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The first child of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get firstChild(): ActivatedRouteSnapshot { return this._routerState.firstChild(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The children of this route in the router state tree */
|
2016-08-02 17:34:00 -04:00
|
|
|
get children(): ActivatedRouteSnapshot[] { return this._routerState.children(this); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The path from the root of the router state tree to this route */
|
2016-08-02 17:34:00 -04:00
|
|
|
get pathFromRoot(): ActivatedRouteSnapshot[] { return this._routerState.pathFromRoot(this); }
|
|
|
|
|
2017-03-17 13:09:42 -04:00
|
|
|
get paramMap(): ParamMap {
|
|
|
|
if (!this._paramMap) {
|
|
|
|
this._paramMap = convertToParamMap(this.params);
|
|
|
|
}
|
|
|
|
return this._paramMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
get queryParamMap(): ParamMap {
|
|
|
|
if (!this._queryParamMap) {
|
|
|
|
this._queryParamMap = convertToParamMap(this.queryParams);
|
|
|
|
}
|
|
|
|
return this._queryParamMap;
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:11:54 -04:00
|
|
|
toString(): string {
|
2016-12-09 13:44:46 -05:00
|
|
|
const url = this.url.map(segment => segment.toString()).join('/');
|
2016-06-09 17:11:54 -04:00
|
|
|
const matched = this._routeConfig ? this._routeConfig.path : '';
|
|
|
|
return `Route(url:'${url}', path:'${matched}')`;
|
|
|
|
}
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
/**
|
2016-09-10 19:53:09 -04:00
|
|
|
* @whatItDoes Represents the state of the router at a moment in time.
|
2016-06-01 20:55:21 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @howToUse
|
2016-06-01 20:55:21 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-09-10 19:53:09 -04:00
|
|
|
* @Component({templateUrl:'template.html'})
|
2016-06-01 20:55:21 -04:00
|
|
|
* class MyComponent {
|
|
|
|
* constructor(router: Router) {
|
2016-09-10 19:53:09 -04:00
|
|
|
* const state: RouterState = router.routerState;
|
|
|
|
* const snapshot: RouterStateSnapshot = state.snapshot;
|
|
|
|
* const root: ActivatedRouteSnapshot = snapshot.root;
|
|
|
|
* const child = root.firstChild;
|
|
|
|
* const id: Observable<string> = child.params.map(p => p.id);
|
|
|
|
* //...
|
2016-06-01 20:55:21 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-09-10 19:53:09 -04:00
|
|
|
* @description
|
|
|
|
* RouterStateSnapshot is a tree of activated route snapshots. Every node in this tree knows about
|
|
|
|
* the "consumed" URL segments, the extracted parameters, and the resolved data.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-01 20:55:21 -04:00
|
|
|
*/
|
|
|
|
export class RouterStateSnapshot extends Tree<ActivatedRouteSnapshot> {
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @internal */
|
2016-09-10 19:53:09 -04:00
|
|
|
constructor(
|
|
|
|
/** The url from which this snapshot was created */
|
|
|
|
public url: string, root: TreeNode<ActivatedRouteSnapshot>) {
|
2016-06-01 16:29:02 -04:00
|
|
|
super(root);
|
2016-08-02 17:34:00 -04:00
|
|
|
setRouterStateSnapshot<RouterStateSnapshot, ActivatedRouteSnapshot>(this, root);
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
2016-06-09 17:11:54 -04:00
|
|
|
|
2016-06-09 17:33:09 -04:00
|
|
|
toString(): string { return serializeNode(this._root); }
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
|
|
|
|
2016-08-02 17:34:00 -04:00
|
|
|
function setRouterStateSnapshot<U, T extends{_routerState: U}>(state: U, node: TreeNode<T>): void {
|
|
|
|
node.value._routerState = state;
|
|
|
|
node.children.forEach(c => setRouterStateSnapshot(state, c));
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:11:54 -04:00
|
|
|
function serializeNode(node: TreeNode<ActivatedRouteSnapshot>): string {
|
|
|
|
const c = node.children.length > 0 ? ` { ${node.children.map(serializeNode).join(", ")} } ` : '';
|
|
|
|
return `${node.value}${c}`;
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:44:57 -04:00
|
|
|
/**
|
|
|
|
* The expectation is that the activate route is created with the right set of parameters.
|
|
|
|
* So we push new values into the observables only when they are not the initial values.
|
|
|
|
* And we detect that by checking if the snapshot field is set.
|
|
|
|
*/
|
|
|
|
export function advanceActivatedRoute(route: ActivatedRoute): void {
|
2016-06-24 15:23:42 -04:00
|
|
|
if (route.snapshot) {
|
2016-12-27 18:57:22 -05:00
|
|
|
const currentSnapshot = route.snapshot;
|
|
|
|
route.snapshot = route._futureSnapshot;
|
|
|
|
if (!shallowEqual(currentSnapshot.queryParams, route._futureSnapshot.queryParams)) {
|
2016-08-02 18:31:56 -04:00
|
|
|
(<any>route.queryParams).next(route._futureSnapshot.queryParams);
|
|
|
|
}
|
2016-12-27 18:57:22 -05:00
|
|
|
if (currentSnapshot.fragment !== route._futureSnapshot.fragment) {
|
2016-08-02 18:31:56 -04:00
|
|
|
(<any>route.fragment).next(route._futureSnapshot.fragment);
|
|
|
|
}
|
2016-12-27 18:57:22 -05:00
|
|
|
if (!shallowEqual(currentSnapshot.params, route._futureSnapshot.params)) {
|
2016-06-24 15:23:42 -04:00
|
|
|
(<any>route.params).next(route._futureSnapshot.params);
|
|
|
|
}
|
2016-12-27 18:57:22 -05:00
|
|
|
if (!shallowEqualArrays(currentSnapshot.url, route._futureSnapshot.url)) {
|
2016-06-24 15:23:42 -04:00
|
|
|
(<any>route.url).next(route._futureSnapshot.url);
|
|
|
|
}
|
2017-03-23 13:43:14 -04:00
|
|
|
if (!shallowEqual(currentSnapshot.data, route._futureSnapshot.data)) {
|
2016-10-31 16:11:36 -04:00
|
|
|
(<any>route.data).next(route._futureSnapshot.data);
|
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
} else {
|
|
|
|
route.snapshot = route._futureSnapshot;
|
2016-08-02 18:31:56 -04:00
|
|
|
|
|
|
|
// this is for resolved data
|
2016-06-27 17:00:07 -04:00
|
|
|
(<any>route.data).next(route._futureSnapshot.data);
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
2016-06-27 15:27:23 -04:00
|
|
|
}
|
2016-10-28 18:17:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
export function equalParamsAndUrlSegments(
|
|
|
|
a: ActivatedRouteSnapshot, b: ActivatedRouteSnapshot): boolean {
|
2016-12-14 07:21:45 -05:00
|
|
|
const equalUrlParams = shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
|
|
|
|
const parentsMismatch = !a.parent !== !b.parent;
|
|
|
|
|
|
|
|
return equalUrlParams && !parentsMismatch &&
|
|
|
|
(!a.parent || equalParamsAndUrlSegments(a.parent, b.parent));
|
2017-03-23 13:43:14 -04:00
|
|
|
}
|