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 {Type} from '@angular/core';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-06-15 19:45:19 -04:00
|
|
|
import {Observer} from 'rxjs/Observer';
|
2016-06-08 19:14:26 -04:00
|
|
|
import {of } from 'rxjs/observable/of';
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-07-06 19:19:52 -04:00
|
|
|
import {Data, ResolveData, Route, Routes} from './config';
|
2016-06-27 17:00:07 -04:00
|
|
|
import {ActivatedRouteSnapshot, InheritedResolve, RouterStateSnapshot} from './router_state';
|
2016-06-19 17:44:20 -04:00
|
|
|
import {PRIMARY_OUTLET, Params} from './shared';
|
2016-06-14 17:55:59 -04:00
|
|
|
import {UrlPathWithParams, UrlSegment, UrlTree, mapChildrenIntoArray} from './url_tree';
|
|
|
|
import {last, merge} from './utils/collection';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {TreeNode} from './utils/tree';
|
2016-05-23 19:14:23 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
class NoMatch {
|
|
|
|
constructor(public segment: UrlSegment = null) {}
|
|
|
|
}
|
2016-06-06 13:15:23 -04:00
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
class InheritedFromParent {
|
|
|
|
constructor(
|
2016-07-08 17:23:23 -04:00
|
|
|
public parent: InheritedFromParent, public snapshot: ActivatedRouteSnapshot,
|
|
|
|
public params: Params, public data: Data, public resolve: InheritedResolve) {}
|
2016-06-27 17:00:07 -04:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2016-07-08 17:23:23 -04:00
|
|
|
static empty(snapshot: ActivatedRouteSnapshot): InheritedFromParent {
|
|
|
|
return new InheritedFromParent(null, snapshot, {}, {}, new InheritedResolve(null, {}));
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:19:52 -04:00
|
|
|
export function recognize(rootComponentType: Type, config: Routes, urlTree: UrlTree, url: string):
|
|
|
|
Observable<RouterStateSnapshot> {
|
2016-05-26 19:51:44 -04:00
|
|
|
try {
|
2016-06-27 17:00:07 -04:00
|
|
|
const children =
|
2016-07-08 17:23:23 -04:00
|
|
|
processSegment(config, urlTree.root, InheritedFromParent.empty(null), PRIMARY_OUTLET);
|
2016-06-14 17:55:59 -04:00
|
|
|
const root = new ActivatedRouteSnapshot(
|
2016-07-14 16:33:05 -04:00
|
|
|
[], Object.freeze({}), {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1,
|
2016-06-27 17:00:07 -04:00
|
|
|
InheritedResolve.empty);
|
2016-06-14 17:55:59 -04:00
|
|
|
const rootNode = new TreeNode<ActivatedRouteSnapshot>(root, children);
|
2016-07-14 20:29:01 -04:00
|
|
|
return of (new RouterStateSnapshot(
|
|
|
|
url, rootNode, Object.freeze(urlTree.queryParams), urlTree.fragment));
|
2016-06-08 14:13:41 -04:00
|
|
|
} catch (e) {
|
2016-06-14 17:55:59 -04:00
|
|
|
if (e instanceof NoMatch) {
|
2016-06-08 14:13:41 -04:00
|
|
|
return new Observable<RouterStateSnapshot>(
|
2016-06-15 19:45:19 -04:00
|
|
|
(obs: Observer<RouterStateSnapshot>) =>
|
|
|
|
obs.error(new Error(`Cannot match any routes: '${e.segment}'`)));
|
2016-06-06 13:15:23 -04:00
|
|
|
} else {
|
2016-06-15 19:45:19 -04:00
|
|
|
return new Observable<RouterStateSnapshot>(
|
|
|
|
(obs: Observer<RouterStateSnapshot>) => obs.error(e));
|
2016-06-06 13:15:23 -04:00
|
|
|
}
|
2016-05-26 19:51:44 -04:00
|
|
|
}
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
function processSegment(
|
|
|
|
config: Route[], segment: UrlSegment, inherited: InheritedFromParent,
|
|
|
|
outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
|
2016-06-19 17:44:20 -04:00
|
|
|
if (segment.pathsWithParams.length === 0 && segment.hasChildren()) {
|
2016-06-27 17:00:07 -04:00
|
|
|
return processSegmentChildren(config, segment, inherited);
|
2016-06-14 17:55:59 -04:00
|
|
|
} else {
|
2016-06-27 17:00:07 -04:00
|
|
|
return processPathsWithParams(config, segment, 0, segment.pathsWithParams, inherited, outlet);
|
2016-06-14 17:55:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processSegmentChildren(
|
2016-06-27 17:00:07 -04:00
|
|
|
config: Route[], segment: UrlSegment,
|
|
|
|
inherited: InheritedFromParent): TreeNode<ActivatedRouteSnapshot>[] {
|
2016-06-14 17:55:59 -04:00
|
|
|
const children = mapChildrenIntoArray(
|
2016-06-27 17:00:07 -04:00
|
|
|
segment, (child, childOutlet) => processSegment(config, child, inherited, childOutlet));
|
2016-06-02 14:30:38 -04:00
|
|
|
checkOutletNameUniqueness(children);
|
2016-06-14 17:55:59 -04:00
|
|
|
sortActivatedRouteSnapshots(children);
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
|
|
|
|
nodes.sort((a, b) => {
|
2016-06-02 14:30:38 -04:00
|
|
|
if (a.value.outlet === PRIMARY_OUTLET) return -1;
|
|
|
|
if (b.value.outlet === PRIMARY_OUTLET) return 1;
|
2016-06-07 12:50:35 -04:00
|
|
|
return a.value.outlet.localeCompare(b.value.outlet);
|
2016-06-02 14:30:38 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
function processPathsWithParams(
|
|
|
|
config: Route[], segment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
|
2016-06-27 17:00:07 -04:00
|
|
|
inherited: InheritedFromParent, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
|
2016-06-14 17:55:59 -04:00
|
|
|
for (let r of config) {
|
2016-06-06 13:15:23 -04:00
|
|
|
try {
|
2016-06-27 17:00:07 -04:00
|
|
|
return processPathsWithParamsAgainstRoute(r, segment, pathIndex, paths, inherited, outlet);
|
2016-06-06 13:15:23 -04:00
|
|
|
} catch (e) {
|
2016-06-14 17:55:59 -04:00
|
|
|
if (!(e instanceof NoMatch)) throw e;
|
2016-06-06 13:15:23 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 17:55:59 -04:00
|
|
|
throw new NoMatch(segment);
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
function processPathsWithParamsAgainstRoute(
|
2016-06-24 14:17:17 -04:00
|
|
|
route: Route, rawSegment: UrlSegment, pathIndex: number, paths: UrlPathWithParams[],
|
2016-06-27 17:00:07 -04:00
|
|
|
inherited: InheritedFromParent, outlet: string): TreeNode<ActivatedRouteSnapshot>[] {
|
2016-06-14 17:55:59 -04:00
|
|
|
if (route.redirectTo) throw new NoMatch();
|
2016-06-19 17:44:20 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== outlet) throw new NoMatch();
|
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
const newInheritedResolve = new InheritedResolve(inherited.resolve, getResolve(route));
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
if (route.path === '**') {
|
|
|
|
const params = paths.length > 0 ? last(paths).parameters : {};
|
2016-06-19 17:44:20 -04:00
|
|
|
const snapshot = new ActivatedRouteSnapshot(
|
2016-07-14 20:29:01 -04:00
|
|
|
paths, Object.freeze(merge(inherited.allParams, params)),
|
|
|
|
merge(inherited.allData, getData(route)), outlet, route.component, route,
|
|
|
|
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) - 1, newInheritedResolve);
|
2016-06-24 14:17:17 -04:00
|
|
|
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
2016-06-06 13:15:23 -04:00
|
|
|
|
2016-07-08 17:23:23 -04:00
|
|
|
const {consumedPaths, parameters, lastChild} =
|
|
|
|
match(rawSegment, route, paths, inherited.snapshot);
|
2016-06-24 14:17:17 -04:00
|
|
|
const rawSlicedPath = paths.slice(lastChild);
|
2016-07-06 14:02:16 -04:00
|
|
|
const childConfig = getChildConfig(route);
|
2016-06-14 17:55:59 -04:00
|
|
|
|
2016-06-24 14:17:17 -04:00
|
|
|
const {segment, slicedPath} = split(rawSegment, consumedPaths, rawSlicedPath, childConfig);
|
|
|
|
|
|
|
|
const snapshot = new ActivatedRouteSnapshot(
|
2016-07-14 16:33:05 -04:00
|
|
|
consumedPaths, Object.freeze(merge(inherited.allParams, parameters)),
|
2016-06-27 17:00:07 -04:00
|
|
|
merge(inherited.allData, getData(route)), outlet, route.component, route,
|
|
|
|
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) + pathIndex + lastChild - 1,
|
|
|
|
newInheritedResolve);
|
2016-06-14 17:55:59 -04:00
|
|
|
|
2016-07-08 17:23:23 -04:00
|
|
|
const newInherited = route.component ?
|
|
|
|
InheritedFromParent.empty(snapshot) :
|
|
|
|
new InheritedFromParent(inherited, snapshot, parameters, getData(route), newInheritedResolve);
|
|
|
|
|
2016-06-24 14:17:17 -04:00
|
|
|
if (slicedPath.length === 0 && segment.hasChildren()) {
|
2016-06-27 17:00:07 -04:00
|
|
|
const children = processSegmentChildren(childConfig, segment, newInherited);
|
2016-06-24 14:17:17 -04:00
|
|
|
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
|
|
|
|
|
|
|
|
} else if (childConfig.length === 0 && slicedPath.length === 0) {
|
|
|
|
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
|
2016-06-14 17:55:59 -04:00
|
|
|
|
|
|
|
} else {
|
2016-06-24 14:17:17 -04:00
|
|
|
const children = processPathsWithParams(
|
2016-06-27 17:00:07 -04:00
|
|
|
childConfig, segment, pathIndex + lastChild, slicedPath, newInherited, PRIMARY_OUTLET);
|
2016-06-24 14:17:17 -04:00
|
|
|
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 14:02:16 -04:00
|
|
|
function getChildConfig(route: Route): Route[] {
|
|
|
|
if (route.children) {
|
|
|
|
return route.children;
|
2016-07-06 19:19:52 -04:00
|
|
|
} else if (route.loadChildren) {
|
2016-07-06 14:02:16 -04:00
|
|
|
return (<any>route)._loadedConfig.routes;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 17:23:23 -04:00
|
|
|
function match(
|
|
|
|
segment: UrlSegment, route: Route, paths: UrlPathWithParams[], parent: ActivatedRouteSnapshot) {
|
2016-06-16 17:14:09 -04:00
|
|
|
if (route.path === '') {
|
2016-06-27 23:10:36 -04:00
|
|
|
if ((route.terminal || route.pathMatch === 'full') &&
|
|
|
|
(segment.hasChildren() || paths.length > 0)) {
|
2016-06-14 17:55:59 -04:00
|
|
|
throw new NoMatch();
|
|
|
|
} else {
|
2016-07-08 17:23:23 -04:00
|
|
|
const params = parent ? parent.params : {};
|
|
|
|
return {consumedPaths: [], lastChild: 0, parameters: params};
|
2016-06-14 17:55:59 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 19:14:23 -04:00
|
|
|
|
2016-06-16 17:14:09 -04:00
|
|
|
const path = route.path;
|
2016-06-14 17:55:59 -04:00
|
|
|
const parts = path.split('/');
|
2016-06-15 12:14:41 -04:00
|
|
|
const posParameters: {[key: string]: any} = {};
|
2016-06-15 19:45:19 -04:00
|
|
|
const consumedPaths: UrlPathWithParams[] = [];
|
2016-06-14 17:55:59 -04: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 19:14:23 -04:00
|
|
|
}
|
2016-06-14 17:55:59 -04:00
|
|
|
consumedPaths.push(current);
|
|
|
|
currentIndex++;
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:10:36 -04:00
|
|
|
if ((route.terminal || route.pathMatch === 'full') &&
|
|
|
|
(segment.hasChildren() || currentIndex < paths.length)) {
|
2016-06-14 17:55:59 -04:00
|
|
|
throw new NoMatch();
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
const parameters = merge(posParameters, consumedPaths[consumedPaths.length - 1].parameters);
|
|
|
|
return {consumedPaths, lastChild: currentIndex, parameters};
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]): void {
|
2016-06-15 19:45:19 -04:00
|
|
|
const names: {[k: string]: ActivatedRouteSnapshot} = {};
|
2016-06-14 17:55:59 -04:00
|
|
|
nodes.forEach(n => {
|
|
|
|
let routeWithSameOutletName = names[n.value.outlet];
|
|
|
|
if (routeWithSameOutletName) {
|
2016-06-15 19:45:19 -04:00
|
|
|
const p = routeWithSameOutletName.url.map(s => s.toString()).join('/');
|
2016-06-14 17:55:59 -04: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-06-24 14:17:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSourceSegment(segment: UrlSegment): UrlSegment {
|
|
|
|
let s = segment;
|
|
|
|
while (s._sourceSegment) {
|
|
|
|
s = s._sourceSegment;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPathIndexShift(segment: UrlSegment): number {
|
|
|
|
let s = segment;
|
|
|
|
let res = 0;
|
|
|
|
while (s._sourceSegment) {
|
|
|
|
s = s._sourceSegment;
|
|
|
|
res += segment._pathIndexShift;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function split(
|
|
|
|
segment: UrlSegment, consumedPaths: UrlPathWithParams[], slicedPath: UrlPathWithParams[],
|
|
|
|
config: Route[]) {
|
|
|
|
if (slicedPath.length > 0 &&
|
|
|
|
containsEmptyPathMatchesWithNamedOutlets(segment, slicedPath, config)) {
|
|
|
|
const s = new UrlSegment(
|
|
|
|
consumedPaths,
|
|
|
|
createChildrenForEmptyPaths(
|
|
|
|
segment, consumedPaths, config, new UrlSegment(slicedPath, segment.children)));
|
|
|
|
s._sourceSegment = segment;
|
|
|
|
s._pathIndexShift = 0;
|
|
|
|
return {segment: s, slicedPath: []};
|
|
|
|
|
|
|
|
} else if (slicedPath.length === 0 && containsEmptyPathMatches(segment, slicedPath, config)) {
|
|
|
|
const s = new UrlSegment(
|
|
|
|
segment.pathsWithParams,
|
|
|
|
addEmptyPathsToChildrenIfNeeded(segment, slicedPath, config, segment.children));
|
|
|
|
s._sourceSegment = segment;
|
|
|
|
s._pathIndexShift = 0;
|
|
|
|
return {segment: s, slicedPath};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return {segment, slicedPath};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addEmptyPathsToChildrenIfNeeded(
|
|
|
|
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[],
|
|
|
|
children: {[name: string]: UrlSegment}): {[name: string]: UrlSegment} {
|
|
|
|
const res: {[name: string]: UrlSegment} = {};
|
|
|
|
for (let r of routes) {
|
|
|
|
if (emptyPathMatch(segment, slicedPath, r) && !children[getOutlet(r)]) {
|
|
|
|
const s = new UrlSegment([], {});
|
|
|
|
s._sourceSegment = segment;
|
|
|
|
s._pathIndexShift = segment.pathsWithParams.length;
|
|
|
|
res[getOutlet(r)] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return merge(children, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createChildrenForEmptyPaths(
|
|
|
|
segment: UrlSegment, consumedPaths: UrlPathWithParams[], routes: Route[],
|
|
|
|
primarySegment: UrlSegment): {[name: string]: UrlSegment} {
|
|
|
|
const res: {[name: string]: UrlSegment} = {};
|
|
|
|
res[PRIMARY_OUTLET] = primarySegment;
|
|
|
|
primarySegment._sourceSegment = segment;
|
|
|
|
primarySegment._pathIndexShift = consumedPaths.length;
|
|
|
|
|
|
|
|
for (let r of routes) {
|
|
|
|
if (r.path === '') {
|
|
|
|
const s = new UrlSegment([], {});
|
|
|
|
s._sourceSegment = segment;
|
|
|
|
s._pathIndexShift = consumedPaths.length;
|
|
|
|
res[getOutlet(r)] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function containsEmptyPathMatchesWithNamedOutlets(
|
|
|
|
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
|
|
|
|
return routes
|
|
|
|
.filter(r => emptyPathMatch(segment, slicedPath, r) && getOutlet(r) !== PRIMARY_OUTLET)
|
|
|
|
.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function containsEmptyPathMatches(
|
|
|
|
segment: UrlSegment, slicedPath: UrlPathWithParams[], routes: Route[]): boolean {
|
|
|
|
return routes.filter(r => emptyPathMatch(segment, slicedPath, r)).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyPathMatch(segment: UrlSegment, slicedPath: UrlPathWithParams[], r: Route): boolean {
|
2016-06-27 23:10:36 -04:00
|
|
|
if ((segment.hasChildren() || slicedPath.length > 0) && (r.terminal || r.pathMatch === 'full'))
|
|
|
|
return false;
|
2016-06-24 14:17:17 -04:00
|
|
|
return r.path === '' && r.redirectTo === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOutlet(route: Route): string {
|
|
|
|
return route.outlet ? route.outlet : PRIMARY_OUTLET;
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getData(route: Route): Data {
|
|
|
|
return route.data ? route.data : {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResolve(route: Route): ResolveData {
|
|
|
|
return route.resolve ? route.resolve : {};
|
2016-05-26 19:51:44 -04:00
|
|
|
}
|