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-06-08 14:13:41 -04:00
|
|
|
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
|
|
|
|
|
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from './router_state';
|
|
|
|
import {TreeNode} from './utils/tree';
|
2016-06-01 16:29:28 -04:00
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
export function createRouterState(curr: RouterStateSnapshot, prevState: RouterState): RouterState {
|
2016-06-02 14:40:47 -04:00
|
|
|
const root = createNode(curr._root, prevState ? prevState._root : undefined);
|
2016-06-01 16:29:28 -04:00
|
|
|
const queryParams = prevState ? prevState.queryParams : new BehaviorSubject(curr.queryParams);
|
|
|
|
const fragment = prevState ? prevState.fragment : new BehaviorSubject(curr.fragment);
|
2016-06-01 17:32:15 -04:00
|
|
|
return new RouterState(root, queryParams, fragment, curr);
|
2016-06-01 16:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
function createNode(curr: TreeNode<ActivatedRouteSnapshot>, prevState?: TreeNode<ActivatedRoute>):
|
|
|
|
TreeNode<ActivatedRoute> {
|
2016-06-01 20:55:21 -04:00
|
|
|
if (prevState && equalRouteSnapshots(prevState.value.snapshot, curr.value)) {
|
2016-06-01 16:29:28 -04:00
|
|
|
const value = prevState.value;
|
2016-06-02 17:44:57 -04:00
|
|
|
value._futureSnapshot = curr.value;
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
const children = createOrReuseChildren(curr, prevState);
|
2016-06-01 16:29:28 -04:00
|
|
|
return new TreeNode<ActivatedRoute>(value, children);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const value = createActivatedRoute(curr.value);
|
|
|
|
const children = curr.children.map(c => createNode(c));
|
|
|
|
return new TreeNode<ActivatedRoute>(value, children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
function createOrReuseChildren(
|
|
|
|
curr: TreeNode<ActivatedRouteSnapshot>, prevState: TreeNode<ActivatedRoute>) {
|
2016-06-01 16:29:28 -04:00
|
|
|
return curr.children.map(child => {
|
2016-06-24 18:37:46 -04:00
|
|
|
for (const p of prevState.children) {
|
|
|
|
if (equalRouteSnapshots(p.value.snapshot, child.value)) {
|
|
|
|
return createNode(child, p);
|
|
|
|
}
|
2016-06-01 16:29:28 -04:00
|
|
|
}
|
2016-06-24 18:37:46 -04:00
|
|
|
return createNode(child);
|
2016-06-01 16:29:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
function createActivatedRoute(c: ActivatedRouteSnapshot) {
|
|
|
|
return new ActivatedRoute(
|
2016-06-14 17:55:59 -04:00
|
|
|
new BehaviorSubject(c.url), new BehaviorSubject(c.params), c.outlet, c.component, c);
|
2016-06-01 16:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
function equalRouteSnapshots(a: ActivatedRouteSnapshot, b: ActivatedRouteSnapshot): boolean {
|
2016-06-01 16:29:28 -04:00
|
|
|
return a._routeConfig === b._routeConfig;
|
|
|
|
}
|