2016-06-08 11:13:41 -07:00
|
|
|
import {Type} from '@angular/core';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-06-15 16:45:19 -07:00
|
|
|
import {Observer} from 'rxjs/Observer';
|
2016-06-08 16:14:26 -07:00
|
|
|
import {of } from 'rxjs/observable/of';
|
2016-06-08 11:13:41 -07:00
|
|
|
|
|
|
|
import {Route, RouterConfig} from './config';
|
|
|
|
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
|
2016-06-19 14:44:20 -07:00
|
|
|
import {PRIMARY_OUTLET, Params} from './shared';
|
2016-06-14 14:55:59 -07:00
|
|
|
import {UrlPathWithParams, UrlSegment, UrlTree, mapChildrenIntoArray} from './url_tree';
|
|
|
|
import {last, merge} from './utils/collection';
|
2016-06-08 11:13:41 -07:00
|
|
|
import {TreeNode} from './utils/tree';
|
2016-05-23 16:14:23 -07:00
|
|
|
|
2016-06-14 14:55:59 -07:00
|
|
|
class NoMatch {
|
|
|
|
constructor(public segment: UrlSegment = null) {}
|
|
|
|
}
|
2016-06-06 10:15:23 -07:00
|
|
|
|
2016-06-08 11:13:41 -07:00
|
|
|
export function recognize(
|
2016-06-14 14:55:59 -07:00
|
|
|
rootComponentType: Type, config: RouterConfig, urlTree: UrlTree,
|
|
|
|
url: string): Observable<RouterStateSnapshot> {
|
2016-05-26 16:51:44 -07:00
|
|
|
try {
|
2016-06-19 14:44:20 -07:00
|
|
|
const children = processSegment(config, urlTree.root, {}, PRIMARY_OUTLET);
|
2016-06-14 14:55:59 -07:00
|
|
|
const root = new ActivatedRouteSnapshot(
|
|
|
|
[], {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1);
|
|
|
|
const rootNode = new TreeNode<ActivatedRouteSnapshot>(root, children);
|
|
|
|
return of (new RouterStateSnapshot(url, rootNode, urlTree.queryParams, urlTree.fragment));
|
2016-06-08 11:13:41 -07:00
|
|
|
} catch (e) {
|
2016-06-14 14:55:59 -07:00
|
|
|
if (e instanceof NoMatch) {
|
2016-06-08 11:13:41 -07:00
|
|
|
return new Observable<RouterStateSnapshot>(
|
2016-06-15 16:45:19 -07:00
|
|
|
(obs: Observer<RouterStateSnapshot>) =>
|
|
|
|
obs.error(new Error(`Cannot match any routes: '${e.segment}'`)));
|
2016-06-06 10:15:23 -07:00
|
|
|
} else {
|
2016-06-15 16:45:19 -07:00
|
|
|
return new Observable<RouterStateSnapshot>(
|
|
|
|
(obs: Observer<RouterStateSnapshot>) => obs.error(e));
|
2016-06-06 10:15:23 -07:00
|
|
|
}
|
2016-05-26 16:51:44 -07:00
|
|
|
}
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-19 14:44:20 -07:00
|
|
|
function processSegment(config: Route[], segment: UrlSegment, extraParams: Params, outlet: string):
|
|
|
|
TreeNode<ActivatedRouteSnapshot>[] {
|
|
|
|
if (segment.pathsWithParams.length === 0 && segment.hasChildren()) {
|
|
|
|
return processSegmentChildren(config, segment, extraParams);
|
2016-06-14 14:55:59 -07:00
|
|
|
} else {
|
2016-06-19 14:44:20 -07:00
|
|
|
return [processPathsWithParams(
|
|
|
|
config, segment, 0, segment.pathsWithParams, extraParams, outlet)];
|
2016-06-14 14:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processSegmentChildren(
|
2016-06-19 14:44:20 -07:00
|
|
|
config: Route[], segment: UrlSegment, extraParams: Params): TreeNode<ActivatedRouteSnapshot>[] {
|
2016-06-14 14:55:59 -07:00
|
|
|
const children = mapChildrenIntoArray(
|
2016-06-19 14:44:20 -07:00
|
|
|
segment, (child, childOutlet) => processSegment(config, child, extraParams, childOutlet));
|
2016-06-02 11:30:38 -07:00
|
|
|
checkOutletNameUniqueness(children);
|
2016-06-14 14:55:59 -07:00
|
|
|
sortActivatedRouteSnapshots(children);
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
|
|
|
|
nodes.sort((a, b) => {
|
2016-06-02 11:30:38 -07:00
|
|
|
if (a.value.outlet === PRIMARY_OUTLET) return -1;
|
|
|
|
if (b.value.outlet === PRIMARY_OUTLET) return 1;
|
2016-06-07 09:50:35 -07:00
|
|
|
return a.value.outlet.localeCompare(b.value.outlet);
|
2016-06-02 11:30:38 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:55:59 -07:00
|
|
|
function processPathsWithParams(
|
|
|
|
config: Route[], segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
|
2016-06-19 14:44:20 -07:00
|
|
|
extraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot> {
|
2016-06-14 14:55:59 -07:00
|
|
|
for (let r of config) {
|
2016-06-06 10:15:23 -07:00
|
|
|
try {
|
2016-06-19 14:44:20 -07:00
|
|
|
return processPathsWithParamsAgainstRoute(r, segment, pathIndex, paths, extraParams, outlet);
|
2016-06-06 10:15:23 -07:00
|
|
|
} catch (e) {
|
2016-06-14 14:55:59 -07:00
|
|
|
if (!(e instanceof NoMatch)) throw e;
|
2016-06-06 10:15:23 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 14:55:59 -07:00
|
|
|
throw new NoMatch(segment);
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:55:59 -07:00
|
|
|
function processPathsWithParamsAgainstRoute(
|
|
|
|
route: Route, segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
|
2016-06-19 14:44:20 -07:00
|
|
|
parentExtraParams: Params, outlet: string): TreeNode<ActivatedRouteSnapshot> {
|
2016-06-14 14:55:59 -07:00
|
|
|
if (route.redirectTo) throw new NoMatch();
|
2016-06-19 14:44:20 -07:00
|
|
|
|
2016-06-14 14:55:59 -07:00
|
|
|
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== outlet) throw new NoMatch();
|
|
|
|
|
|
|
|
if (route.path === '**') {
|
|
|
|
const params = paths.length > 0 ? last(paths).parameters : {};
|
2016-06-19 14:44:20 -07:00
|
|
|
const snapshot = new ActivatedRouteSnapshot(
|
|
|
|
paths, merge(parentExtraParams, params), outlet, route.component, route, segment, -1);
|
2016-06-14 14:55:59 -07:00
|
|
|
return new TreeNode<ActivatedRouteSnapshot>(snapshot, []);
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
2016-06-06 10:15:23 -07:00
|
|
|
|
2016-06-19 14:44:20 -07:00
|
|
|
const {consumedPaths, parameters, extraParams, lastChild} =
|
|
|
|
match(segment, route, paths, parentExtraParams);
|
2016-06-14 14:55:59 -07:00
|
|
|
const snapshot = new ActivatedRouteSnapshot(
|
|
|
|
consumedPaths, parameters, outlet, route.component, route, segment,
|
|
|
|
pathIndex + lastChild - 1);
|
|
|
|
const slicedPath = paths.slice(lastChild);
|
|
|
|
const childConfig = route.children ? route.children : [];
|
|
|
|
|
|
|
|
if (childConfig.length === 0 && slicedPath.length === 0) {
|
|
|
|
return new TreeNode<ActivatedRouteSnapshot>(snapshot, []);
|
|
|
|
|
|
|
|
// TODO: check that the right segment is present
|
2016-06-19 14:44:20 -07:00
|
|
|
} else if (slicedPath.length === 0 && segment.hasChildren()) {
|
|
|
|
const children = processSegmentChildren(childConfig, segment, extraParams);
|
2016-06-14 14:55:59 -07:00
|
|
|
return new TreeNode<ActivatedRouteSnapshot>(snapshot, children);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const child = processPathsWithParams(
|
2016-06-19 14:44:20 -07:00
|
|
|
childConfig, segment, pathIndex + lastChild, slicedPath, extraParams, PRIMARY_OUTLET);
|
2016-06-14 14:55:59 -07:00
|
|
|
return new TreeNode<ActivatedRouteSnapshot>(snapshot, [child]);
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 14:44:20 -07:00
|
|
|
function match(
|
|
|
|
segment: UrlSegment, route: Route, paths: UrlPathWithParams[], parentExtraParams: Params) {
|
2016-06-16 14:14:09 -07:00
|
|
|
if (route.path === '') {
|
2016-06-19 14:44:20 -07:00
|
|
|
if (route.terminal && (segment.hasChildren() || paths.length > 0)) {
|
2016-06-14 14:55:59 -07:00
|
|
|
throw new NoMatch();
|
|
|
|
} else {
|
2016-06-19 14:44:20 -07:00
|
|
|
return {consumedPaths: [], lastChild: 0, parameters: {}, extraParams: {}};
|
2016-06-14 14:55:59 -07:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 16:14:23 -07:00
|
|
|
|
2016-06-16 14:14:09 -07:00
|
|
|
const path = route.path;
|
2016-06-14 14:55:59 -07:00
|
|
|
const parts = path.split('/');
|
2016-06-15 09:14:41 -07:00
|
|
|
const posParameters: {[key: string]: any} = {};
|
2016-06-15 16:45:19 -07:00
|
|
|
const consumedPaths: UrlPathWithParams[] = [];
|
2016-06-14 14:55:59 -07:00
|
|
|
|
|
|
|
let currentIndex = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
|
|
|
if (currentIndex >= paths.length) throw new NoMatch();
|
|
|
|
const current = paths[currentIndex];
|
|
|
|
|
|
|
|
const p = parts[i];
|
|
|
|
const isPosParam = p.startsWith(':');
|
|
|
|
|
|
|
|
if (!isPosParam && p !== current.path) throw new NoMatch();
|
|
|
|
if (isPosParam) {
|
|
|
|
posParameters[p.substring(1)] = current.path;
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
2016-06-14 14:55:59 -07:00
|
|
|
consumedPaths.push(current);
|
|
|
|
currentIndex++;
|
|
|
|
}
|
|
|
|
|
2016-06-19 14:44:20 -07:00
|
|
|
if (route.terminal && (segment.hasChildren() || currentIndex < paths.length)) {
|
2016-06-14 14:55:59 -07:00
|
|
|
throw new NoMatch();
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-19 14:44:20 -07:00
|
|
|
const parameters = merge(
|
|
|
|
parentExtraParams, merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters));
|
|
|
|
const extraParams = route.component ? {} : parameters;
|
|
|
|
return {consumedPaths, lastChild: currentIndex, parameters, extraParams};
|
2016-05-23 16:14:23 -07:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:55:59 -07:00
|
|
|
function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
|
2016-06-15 16:45:19 -07:00
|
|
|
const names: {[k: string]: ActivatedRouteSnapshot} = {};
|
2016-06-14 14:55:59 -07:00
|
|
|
nodes.forEach(n => {
|
|
|
|
let routeWithSameOutletName = names[n.value.outlet];
|
|
|
|
if (routeWithSameOutletName) {
|
2016-06-15 16:45:19 -07:00
|
|
|
const p = routeWithSameOutletName.url.map(s => s.toString()).join('/');
|
2016-06-14 14:55:59 -07:00
|
|
|
const c = n.value.url.map(s => s.toString()).join('/');
|
|
|
|
throw new Error(`Two segments cannot have the same outlet name: '${p}' and '${c}'.`);
|
|
|
|
}
|
|
|
|
names[n.value.outlet] = n.value;
|
|
|
|
});
|
2016-05-26 16:51:44 -07:00
|
|
|
}
|