feat(router): implement data and resolve

This commit is contained in:
vsavkin
2016-06-27 14:00:07 -07:00
parent e913d9954d
commit f2f1ec0117
11 changed files with 318 additions and 56 deletions

View File

@ -11,8 +11,8 @@ import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import {of } from 'rxjs/observable/of';
import {Route, RouterConfig} from './config';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {Data, ResolveData, Route, RouterConfig} from './config';
import {ActivatedRouteSnapshot, InheritedResolve, RouterStateSnapshot} from './router_state';
import {PRIMARY_OUTLET, Params} from './shared';
import {UrlPathWithParams, UrlSegment, UrlTree, mapChildrenIntoArray} from './url_tree';
import {last, merge} from './utils/collection';
@ -22,13 +22,31 @@ class NoMatch {
constructor(public segment: UrlSegment = null) {}
}
class InheritedFromParent {
constructor(
public parent: InheritedFromParent, public params: Params, public data: Data,
public resolve: InheritedResolve) {}
get allParams(): Params {
return this.parent ? merge(this.parent.allParams, this.params) : this.params;
}
get allData(): Data { return this.parent ? merge(this.parent.allData, this.data) : this.data; }
static get empty(): InheritedFromParent {
return new InheritedFromParent(null, {}, {}, new InheritedResolve(null, {}));
}
}
export function recognize(
rootComponentType: Type, config: RouterConfig, urlTree: UrlTree,
url: string): Observable<RouterStateSnapshot> {
try {
const children = processSegment(config, urlTree.root, {}, PRIMARY_OUTLET);
const children =
processSegment(config, urlTree.root, InheritedFromParent.empty, PRIMARY_OUTLET);
const root = new ActivatedRouteSnapshot(
[], {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1);
[], {}, {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1,
InheritedResolve.empty);
const rootNode = new TreeNode<ActivatedRouteSnapshot>(root, children);
return of (new RouterStateSnapshot(url, rootNode, urlTree.queryParams, urlTree.fragment));
} catch (e) {
@ -43,19 +61,21 @@ export function recognize(
}
}
function processSegment(config: Route[], segment: UrlSegment, extraParams: Params, outlet: string):
TreeNode<ActivatedRouteSnapshot>[] {
function processSegment(
config: Route[], segment: UrlSegment, inherited: InheritedFromParent,
outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
if (segment.pathsWithParams.length === 0 && segment.hasChildren()) {
return processSegmentChildren(config, segment, extraParams);
return processSegmentChildren(config, segment, inherited);
} else {
return processPathsWithParams(config, segment, 0, segment.pathsWithParams, extraParams, outlet);
return processPathsWithParams(config, segment, 0, segment.pathsWithParams, inherited, outlet);
}
}
function processSegmentChildren(
config: Route[], segment: UrlSegment, extraParams: Params): TreeNode<ActivatedRouteSnapshot>[] {
config: Route[], segment: UrlSegment,
inherited: InheritedFromParent): TreeNode<ActivatedRouteSnapshot>[] {
const children = mapChildrenIntoArray(
segment, (child, childOutlet) => processSegment(config, child, extraParams, childOutlet));
segment, (child, childOutlet) => processSegment(config, child, inherited, childOutlet));
checkOutletNameUniqueness(children);
sortActivatedRouteSnapshots(children);
return children;
@ -71,10 +91,10 @@ function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]):
function processPathsWithParams(
config: Route[], segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
extraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
inherited: InheritedFromParent, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
for (let r of config) {
try {
return processPathsWithParamsAgainstRoute(r, segment, pathIndex, paths, extraParams, outlet);
return processPathsWithParamsAgainstRoute(r, segment, pathIndex, paths, inherited, outlet);
} catch (e) {
if (!(e instanceof NoMatch)) throw e;
}
@ -84,32 +104,39 @@ function processPathsWithParams(
function processPathsWithParamsAgainstRoute(
route: Route, rawSegment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
parentExtraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
inherited: InheritedFromParent, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
if (route.redirectTo) throw new NoMatch();
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== outlet) throw new NoMatch();
const newInheritedResolve = new InheritedResolve(inherited.resolve, getResolve(route));
if (route.path === '**') {
const params = paths.length > 0 ? last(paths).parameters : {};
const snapshot = new ActivatedRouteSnapshot(
paths, merge(parentExtraParams, params), outlet, route.component, route,
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1);
paths, merge(inherited.allParams, params), merge(inherited.allData, getData(route)), outlet,
route.component, route, getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1,
newInheritedResolve);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
}
const {consumedPaths, parameters, extraParams, lastChild} =
match(rawSegment, route, paths, parentExtraParams);
const {consumedPaths, parameters, lastChild} = match(rawSegment, route, paths);
const rawSlicedPath = paths.slice(lastChild);
const childConfig = route.children ? route.children : [];
const newInherited = route.component ?
InheritedFromParent.empty :
new InheritedFromParent(inherited, parameters, getData(route), newInheritedResolve);
const {segment, slicedPath} = split(rawSegment, consumedPaths, rawSlicedPath, childConfig);
const snapshot = new ActivatedRouteSnapshot(
consumedPaths, parameters, outlet, route.component, route, getSourceSegment(rawSegment),
getPathIndexShift(rawSegment) + pathIndex + lastChild - 1);
consumedPaths, merge(inherited.allParams, parameters),
merge(inherited.allData, getData(route)), outlet, route.component, route,
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) + pathIndex + lastChild - 1,
newInheritedResolve);
if (slicedPath.length === 0 && segment.hasChildren()) {
const children = processSegmentChildren(childConfig, segment, extraParams);
const children = processSegmentChildren(childConfig, segment, newInherited);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
} else if (childConfig.length === 0 && slicedPath.length === 0) {
@ -117,18 +144,17 @@ function processPathsWithParamsAgainstRoute(
} else {
const children = processPathsWithParams(
childConfig, segment, pathIndex + lastChild, slicedPath, extraParams, PRIMARY_OUTLET);
childConfig, segment, pathIndex + lastChild, slicedPath, newInherited, PRIMARY_OUTLET);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
}
}
function match(
segment: UrlSegment, route: Route, paths: UrlPathWithParams[], parentExtraParams: Params) {
function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
if (route.path === '') {
if (route.terminal && (segment.hasChildren() || paths.length > 0)) {
throw new NoMatch();
} else {
return {consumedPaths: [], lastChild: 0, parameters: {}, extraParams: {}};
return {consumedPaths: [], lastChild: 0, parameters: {}};
}
}
@ -158,10 +184,8 @@ function match(
throw new NoMatch();
}
const parameters = merge(
parentExtraParams, merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters));
const extraParams = route.component ? {} : parameters;
return {consumedPaths, lastChild: currentIndex, parameters, extraParams};
const parameters = merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters);
return {consumedPaths, lastChild: currentIndex, parameters};
}
function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
@ -274,4 +298,12 @@ function emptyPathMatch(segment: UrlSegment, slicedPath: UrlPathWithParams[], r:
function getOutlet(route: Route): string {
return route.outlet ? route.outlet : PRIMARY_OUTLET;
}
function getData(route: Route): Data {
return route.data ? route.data : {};
}
function getResolve(route: Route): ResolveData {
return route.resolve ? route.resolve : {};
}