angular-cn/modules/@angular/router/src/recognize.ts

172 lines
6.2 KiB
TypeScript
Raw Normal View History

2016-06-08 14:13:41 -04:00
import {Type} from '@angular/core';
import {Observable} from 'rxjs/Observable';
2016-06-08 19:14:26 -04:00
import {of } from 'rxjs/observable/of';
2016-06-08 14:13:41 -04:00
2016-06-08 19:14:26 -04:00
import {match} from './apply_redirects';
2016-06-08 14:13:41 -04:00
import {Route, RouterConfig} from './config';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {PRIMARY_OUTLET} from './shared';
import {UrlSegment, UrlTree} from './url_tree';
2016-06-08 19:14:26 -04:00
import {first, flatten, forEach, 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
class CannotRecognize {}
2016-06-08 14:13:41 -04:00
export function recognize(
rootComponentType: Type, config: RouterConfig, url: UrlTree): Observable<RouterStateSnapshot> {
try {
2016-06-08 14:13:41 -04:00
const match = new MatchResult(
rootComponentType, config, [url.root], {}, url._root.children, [], PRIMARY_OUTLET, null,
url.root);
const roots = constructActivatedRoute(match);
2016-06-08 19:14:26 -04:00
return of (new RouterStateSnapshot(roots[0], url.queryParams, url.fragment));
2016-06-08 14:13:41 -04:00
} catch (e) {
if (e instanceof CannotRecognize) {
2016-06-08 14:13:41 -04:00
return new Observable<RouterStateSnapshot>(
obs => obs.error(new Error('Cannot match any routes')));
} else {
return new Observable<RouterStateSnapshot>(obs => obs.error(e));
}
}
2016-05-23 19:14:23 -04:00
}
function constructActivatedRoute(match: MatchResult): TreeNode<ActivatedRouteSnapshot>[] {
const activatedRoute = createActivatedRouteSnapshot(match);
2016-06-02 14:30:38 -04:00
const children = match.leftOverUrl.length > 0 ?
2016-06-08 14:13:41 -04:00
recognizeMany(match.children, match.leftOverUrl) :
recognizeLeftOvers(match.children, match.lastUrlSegment);
2016-06-02 14:30:38 -04:00
checkOutletNameUniqueness(children);
children.sort((a, b) => {
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
});
return [new TreeNode<ActivatedRouteSnapshot>(activatedRoute, children)];
}
2016-06-08 14:13:41 -04:00
function recognizeLeftOvers(
config: Route[], lastUrlSegment: UrlSegment): TreeNode<ActivatedRouteSnapshot>[] {
2016-06-02 14:30:38 -04:00
if (!config) return [];
const mIndex = matchIndex(config, [], lastUrlSegment);
return mIndex ? constructActivatedRoute(mIndex) : [];
2016-05-23 19:14:23 -04:00
}
2016-06-08 14:13:41 -04:00
function recognizeMany(
config: Route[], urls: TreeNode<UrlSegment>[]): TreeNode<ActivatedRouteSnapshot>[] {
return flatten(urls.map(url => recognizeOne(config, url)));
2016-05-23 19:14:23 -04:00
}
function createActivatedRouteSnapshot(match: MatchResult): ActivatedRouteSnapshot {
2016-06-08 14:13:41 -04:00
return new ActivatedRouteSnapshot(
match.consumedUrlSegments, match.parameters, match.outlet, match.component, match.route,
match.lastUrlSegment);
2016-05-23 19:14:23 -04:00
}
2016-06-08 14:13:41 -04:00
function recognizeOne(
config: Route[], url: TreeNode<UrlSegment>): TreeNode<ActivatedRouteSnapshot>[] {
2016-06-08 19:14:26 -04:00
const matches = matchNode(config, url);
2016-06-08 14:13:41 -04:00
for (let match of matches) {
try {
const primary = constructActivatedRoute(match);
const secondary = recognizeMany(config, match.secondary);
const res = primary.concat(secondary);
checkOutletNameUniqueness(res);
return res;
} catch (e) {
2016-06-08 14:13:41 -04:00
if (!(e instanceof CannotRecognize)) {
throw e;
}
}
}
throw new CannotRecognize();
2016-05-23 19:14:23 -04:00
}
2016-06-08 14:13:41 -04:00
function checkOutletNameUniqueness(nodes: TreeNode<ActivatedRouteSnapshot>[]):
TreeNode<ActivatedRouteSnapshot>[] {
2016-05-23 19:14:23 -04:00
let names = {};
nodes.forEach(n => {
let routeWithSameOutletName = names[n.value.outlet];
if (routeWithSameOutletName) {
2016-06-08 14:13:41 -04:00
const p = routeWithSameOutletName.urlSegments.map(s => s.toString()).join('/');
const c = n.value.urlSegments.map(s => s.toString()).join('/');
2016-05-23 19:14:23 -04:00
throw new Error(`Two segments cannot have the same outlet name: '${p}' and '${c}'.`);
}
names[n.value.outlet] = n.value;
});
return nodes;
}
2016-06-08 19:14:26 -04:00
function matchNode(config: Route[], url: TreeNode<UrlSegment>): MatchResult[] {
const res = [];
2016-05-23 19:14:23 -04:00
for (let r of config) {
const m = matchWithParts(r, url);
if (m) {
res.push(m);
} else if (r.index) {
res.push(createIndexMatch(r, [url], url.value));
}
2016-05-23 19:14:23 -04:00
}
return res;
}
2016-06-08 14:13:41 -04:00
function createIndexMatch(
r: Route, leftOverUrls: TreeNode<UrlSegment>[], lastUrlSegment: UrlSegment): MatchResult {
const outlet = r.outlet ? r.outlet : PRIMARY_OUTLET;
const children = r.children ? r.children : [];
2016-06-08 14:13:41 -04:00
return new MatchResult(
r.component, children, [], lastUrlSegment.parameters, leftOverUrls, [], outlet, r,
lastUrlSegment);
2016-05-23 19:14:23 -04:00
}
2016-06-08 14:13:41 -04:00
function matchIndex(
config: Route[], leftOverUrls: TreeNode<UrlSegment>[], lastUrlSegment: UrlSegment): MatchResult|
null {
2016-05-23 19:14:23 -04:00
for (let r of config) {
if (r.index) {
return createIndexMatch(r, leftOverUrls, lastUrlSegment);
2016-05-23 19:14:23 -04:00
}
}
return null;
}
2016-06-08 14:13:41 -04:00
function matchWithParts(route: Route, url: TreeNode<UrlSegment>): MatchResult|null {
2016-05-23 19:14:23 -04:00
if (!route.path) return null;
if ((route.outlet ? route.outlet : PRIMARY_OUTLET) !== url.value.outlet) return null;
2016-05-23 19:14:23 -04:00
2016-06-08 14:13:41 -04:00
const path = route.path.startsWith('/') ? route.path.substring(1) : route.path;
if (path === '**') {
2016-05-23 19:14:23 -04:00
const consumedUrl = [];
2016-06-08 14:13:41 -04:00
let u: TreeNode<UrlSegment>|null = url;
2016-05-23 19:14:23 -04:00
while (u) {
consumedUrl.push(u.value);
u = first(u.children);
}
const last = consumedUrl[consumedUrl.length - 1];
2016-06-08 14:13:41 -04:00
return new MatchResult(
route.component, [], consumedUrl, last.parameters, [], [], PRIMARY_OUTLET, route, last);
2016-05-23 19:14:23 -04:00
}
2016-06-08 19:14:26 -04:00
const m = match(route, url);
if (!m) return null;
const {consumedUrlSegments, lastSegment, lastParent, positionalParamSegments} = m;
2016-05-23 19:14:23 -04:00
const p = lastSegment.value.parameters;
2016-06-08 19:14:26 -04:00
const posParams = {};
forEach(positionalParamSegments, (v, k) => { posParams[k] = v.path; });
const parameters = <{[key: string]: string}>merge(p, posParams);
2016-05-23 19:14:23 -04:00
const secondarySubtrees = lastParent ? lastParent.children.slice(1) : [];
const children = route.children ? route.children : [];
const outlet = route.outlet ? route.outlet : PRIMARY_OUTLET;
2016-06-08 14:13:41 -04:00
return new MatchResult(
route.component, children, consumedUrlSegments, parameters, lastSegment.children,
secondarySubtrees, outlet, route, lastSegment.value);
2016-05-23 19:14:23 -04:00
}
class MatchResult {
2016-06-08 14:13:41 -04:00
constructor(
public component: Type|string, public children: Route[],
public consumedUrlSegments: UrlSegment[], public parameters: {[key: string]: string},
public leftOverUrl: TreeNode<UrlSegment>[], public secondary: TreeNode<UrlSegment>[],
public outlet: string, public route: Route|null, public lastUrlSegment: UrlSegment) {}
}