2015-05-29 14:58:41 -07:00
|
|
|
import {
|
|
|
|
RegExp,
|
|
|
|
RegExpWrapper,
|
|
|
|
RegExpMatcherWrapper,
|
|
|
|
StringWrapper,
|
|
|
|
isPresent,
|
2015-09-10 15:25:36 -07:00
|
|
|
isBlank
|
2015-08-20 14:28:25 -07:00
|
|
|
} from 'angular2/src/core/facade/lang';
|
2015-09-10 15:25:36 -07:00
|
|
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
|
|
|
|
2015-10-07 09:09:43 -07:00
|
|
|
import {Map, MapWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
import {RouteHandler} from './route_handler';
|
|
|
|
import {Url, RootUrl, serializeParams} from './url_parser';
|
|
|
|
import {ComponentInstruction} from './instruction';
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
class TouchMap {
|
2015-10-02 16:47:54 -07:00
|
|
|
map: {[key: string]: string} = {};
|
|
|
|
keys: {[key: string]: boolean} = {};
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-10-02 16:47:54 -07:00
|
|
|
constructor(map: {[key: string]: any}) {
|
2015-07-08 10:57:38 -07:00
|
|
|
if (isPresent(map)) {
|
|
|
|
StringMapWrapper.forEach(map, (value, key) => {
|
|
|
|
this.map[key] = isPresent(value) ? value.toString() : null;
|
|
|
|
this.keys[key] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(key: string): string {
|
|
|
|
StringMapWrapper.delete(this.keys, key);
|
|
|
|
return this.map[key];
|
|
|
|
}
|
|
|
|
|
2015-10-02 16:47:54 -07:00
|
|
|
getUnused(): {[key: string]: any} {
|
|
|
|
var unused: {[key: string]: any} = StringMapWrapper.create();
|
2015-07-08 10:57:38 -07:00
|
|
|
var keys = StringMapWrapper.keys(this.keys);
|
2015-10-07 09:09:43 -07:00
|
|
|
keys.forEach(key => unused[key] = StringMapWrapper.get(this.map, key));
|
2015-07-08 10:57:38 -07:00
|
|
|
return unused;
|
|
|
|
}
|
2015-05-29 14:58:41 -07:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:05:05 -07:00
|
|
|
function normalizeString(obj: any): string {
|
|
|
|
if (isBlank(obj)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return obj.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
interface Segment {
|
2015-07-17 13:36:53 -07:00
|
|
|
name: string;
|
|
|
|
generate(params: TouchMap): string;
|
|
|
|
match(path: string): boolean;
|
|
|
|
}
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
class ContinuationSegment implements Segment {
|
2015-06-29 10:37:55 +02:00
|
|
|
name: string = '';
|
2015-07-17 13:36:53 -07:00
|
|
|
generate(params: TouchMap): string { return ''; }
|
|
|
|
match(path: string): boolean { return true; }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
class StaticSegment implements Segment {
|
|
|
|
name: string = '';
|
|
|
|
constructor(public path: string) {}
|
|
|
|
match(path: string): boolean { return path == this.path; }
|
|
|
|
generate(params: TouchMap): string { return this.path; }
|
|
|
|
}
|
2015-06-29 10:37:55 +02:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
class DynamicSegment implements Segment {
|
2015-06-29 10:37:55 +02:00
|
|
|
constructor(public name: string) {}
|
2015-09-14 11:22:54 -07:00
|
|
|
match(path: string): boolean { return path.length > 0; }
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string {
|
|
|
|
if (!StringMapWrapper.contains(params.map, this.name)) {
|
2015-05-29 14:58:41 -07:00
|
|
|
throw new BaseException(
|
2015-06-30 13:18:51 -07:00
|
|
|
`Route generator for '${this.name}' was not included in parameters passed.`);
|
2015-05-06 18:30:37 -07:00
|
|
|
}
|
2015-07-08 10:57:38 -07:00
|
|
|
return normalizeString(params.get(this.name));
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
class StarSegment implements Segment {
|
2015-06-29 10:37:55 +02:00
|
|
|
constructor(public name: string) {}
|
2015-07-17 13:36:53 -07:00
|
|
|
match(path: string): boolean { return true; }
|
2015-07-08 10:57:38 -07:00
|
|
|
generate(params: TouchMap): string { return normalizeString(params.get(this.name)); }
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-23 12:46:38 +02:00
|
|
|
var paramMatcher = /^:([^\/]+)$/g;
|
|
|
|
var wildcardMatcher = /^\*([^\/]+)$/g;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-10-02 16:47:54 -07:00
|
|
|
function parsePathString(route: string): {[key: string]: any} {
|
2015-04-17 09:59:56 -07:00
|
|
|
// normalize route as not starting with a "/". Recognition will
|
|
|
|
// also normalize.
|
2015-06-17 11:57:38 -07:00
|
|
|
if (StringWrapper.startsWith(route, "/")) {
|
2015-04-17 09:59:56 -07:00
|
|
|
route = StringWrapper.substring(route, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var segments = splitBySlash(route);
|
2015-06-17 11:17:21 -07:00
|
|
|
var results = [];
|
2015-05-15 02:05:57 -07:00
|
|
|
var specificity = 0;
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
// The "specificity" of a path is used to determine which route is used when multiple routes match
|
|
|
|
// a URL.
|
|
|
|
// Static segments (like "/foo") are the most specific, followed by dynamic segments (like
|
|
|
|
// "/:id"). Star segments
|
2015-05-15 02:05:57 -07:00
|
|
|
// add no specificity. Segments at the start of the path are more specific than proceeding ones.
|
2015-05-29 14:58:41 -07:00
|
|
|
// The code below uses place values to combine the different types of segments into a single
|
|
|
|
// integer that we can
|
|
|
|
// sort later. Each static segment is worth hundreds of points of specificity (10000, 9900, ...,
|
|
|
|
// 200), and each
|
2015-05-15 02:05:57 -07:00
|
|
|
// dynamic segment is worth single points of specificity (100, 99, ... 2).
|
|
|
|
if (segments.length > 98) {
|
|
|
|
throw new BaseException(`'${route}' has more than the maximum supported number of segments.`);
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-06-17 11:57:38 -07:00
|
|
|
var limit = segments.length - 1;
|
|
|
|
for (var i = 0; i <= limit; i++) {
|
2015-05-29 14:58:41 -07:00
|
|
|
var segment = segments[i], match;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
if (isPresent(match = RegExpWrapper.firstMatch(paramMatcher, segment))) {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new DynamicSegment(match[1]));
|
2015-05-15 02:05:57 -07:00
|
|
|
specificity += (100 - i);
|
2015-04-17 09:59:56 -07:00
|
|
|
} else if (isPresent(match = RegExpWrapper.firstMatch(wildcardMatcher, segment))) {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new StarSegment(match[1]));
|
2015-06-17 11:57:38 -07:00
|
|
|
} else if (segment == '...') {
|
|
|
|
if (i < limit) {
|
|
|
|
// TODO (matsko): setup a proper error here `
|
|
|
|
throw new BaseException(`Unexpected "..." before the end of the path for "${route}".`);
|
|
|
|
}
|
|
|
|
results.push(new ContinuationSegment());
|
2015-07-17 13:36:53 -07:00
|
|
|
} else {
|
2015-06-17 11:17:21 -07:00
|
|
|
results.push(new StaticSegment(segment));
|
2015-05-15 02:05:57 -07:00
|
|
|
specificity += 100 * (100 - i);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 11:10:52 -07:00
|
|
|
var result = StringMapWrapper.create();
|
|
|
|
StringMapWrapper.set(result, 'segments', results);
|
|
|
|
StringMapWrapper.set(result, 'specificity', specificity);
|
|
|
|
return result;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
// this function is used to determine whether a route config path like `/foo/:id` collides with
|
|
|
|
// `/foo/:name`
|
2015-08-28 11:29:19 -07:00
|
|
|
function pathDslHash(segments: Segment[]): string {
|
2015-07-17 13:36:53 -07:00
|
|
|
return segments.map((segment) => {
|
|
|
|
if (segment instanceof StarSegment) {
|
|
|
|
return '*';
|
|
|
|
} else if (segment instanceof ContinuationSegment) {
|
|
|
|
return '...';
|
|
|
|
} else if (segment instanceof DynamicSegment) {
|
|
|
|
return ':';
|
|
|
|
} else if (segment instanceof StaticSegment) {
|
|
|
|
return segment.path;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join('/');
|
|
|
|
}
|
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
function splitBySlash(url: string): string[] {
|
2015-05-14 15:24:35 +02:00
|
|
|
return url.split('/');
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 02:01:34 -07:00
|
|
|
var RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|=');
|
|
|
|
function assertPath(path: string) {
|
|
|
|
if (StringWrapper.contains(path, '#')) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`);
|
|
|
|
}
|
|
|
|
var illegalCharacter = RegExpWrapper.firstMatch(RESERVED_CHARS, path);
|
|
|
|
if (isPresent(illegalCharacter)) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`);
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
export class PathMatch {
|
|
|
|
constructor(public instruction: ComponentInstruction, public remaining: Url,
|
2015-08-28 11:29:19 -07:00
|
|
|
public remainingAux: Url[]) {}
|
2015-07-17 13:36:53 -07:00
|
|
|
}
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
// represents something like '/foo/:bar'
|
|
|
|
export class PathRecognizer {
|
2015-08-28 11:29:19 -07:00
|
|
|
private _segments: Segment[];
|
2015-05-29 14:58:41 -07:00
|
|
|
specificity: number;
|
2015-06-17 11:57:38 -07:00
|
|
|
terminal: boolean = true;
|
2015-07-17 13:36:53 -07:00
|
|
|
hash: string;
|
2015-09-04 15:09:02 -07:00
|
|
|
private _cache: Map<string, ComponentInstruction> = new Map<string, ComponentInstruction>();
|
2015-08-30 21:20:18 -07:00
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
// TODO: cache component instruction instances by params and by ParsedUrl instance
|
2015-07-21 01:26:43 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
constructor(public path: string, public handler: RouteHandler) {
|
2015-07-17 02:01:34 -07:00
|
|
|
assertPath(path);
|
2015-05-12 14:53:13 -07:00
|
|
|
var parsed = parsePathString(path);
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
this._segments = parsed['segments'];
|
|
|
|
this.specificity = parsed['specificity'];
|
|
|
|
this.hash = pathDslHash(this._segments);
|
2015-06-17 11:57:38 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
var lastSegment = this._segments[this._segments.length - 1];
|
|
|
|
this.terminal = !(lastSegment instanceof ContinuationSegment);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
recognize(beginningSegment: Url): PathMatch {
|
|
|
|
var nextSegment = beginningSegment;
|
|
|
|
var currentSegment: Url;
|
|
|
|
var positionalParams = {};
|
|
|
|
var captured = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this._segments.length; i += 1) {
|
|
|
|
var segment = this._segments[i];
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-08-13 11:09:22 -07:00
|
|
|
currentSegment = nextSegment;
|
2015-06-17 11:57:38 -07:00
|
|
|
if (segment instanceof ContinuationSegment) {
|
2015-07-17 13:36:53 -07:00
|
|
|
break;
|
2015-06-17 11:57:38 -07:00
|
|
|
}
|
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
if (isPresent(currentSegment)) {
|
|
|
|
captured.push(currentSegment.path);
|
2015-08-13 11:09:22 -07:00
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
// the star segment consumes all of the remaining URL, including matrix params
|
|
|
|
if (segment instanceof StarSegment) {
|
|
|
|
positionalParams[segment.name] = currentSegment.toString();
|
|
|
|
nextSegment = null;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-17 13:36:53 -07:00
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
if (segment instanceof DynamicSegment) {
|
|
|
|
positionalParams[segment.name] = currentSegment.path;
|
|
|
|
} else if (!segment.match(currentSegment.path)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-09-14 11:22:54 -07:00
|
|
|
nextSegment = currentSegment.child;
|
|
|
|
} else if (!segment.match('')) {
|
2015-07-17 13:36:53 -07:00
|
|
|
return null;
|
2015-07-21 01:26:43 -07:00
|
|
|
}
|
2015-07-17 13:36:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.terminal && isPresent(nextSegment)) {
|
|
|
|
return null;
|
2015-07-08 10:57:38 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
var urlPath = captured.join('/');
|
|
|
|
|
2015-08-13 11:09:22 -07:00
|
|
|
var auxiliary;
|
|
|
|
var instruction: ComponentInstruction;
|
2015-08-30 21:20:18 -07:00
|
|
|
var urlParams;
|
|
|
|
var allParams;
|
2015-08-13 11:09:22 -07:00
|
|
|
if (isPresent(currentSegment)) {
|
|
|
|
// If this is the root component, read query params. Otherwise, read matrix params.
|
|
|
|
var paramsSegment = beginningSegment instanceof RootUrl ? beginningSegment : currentSegment;
|
2015-07-17 13:36:53 -07:00
|
|
|
|
2015-08-30 21:20:18 -07:00
|
|
|
allParams = isPresent(paramsSegment.params) ?
|
|
|
|
StringMapWrapper.merge(paramsSegment.params, positionalParams) :
|
|
|
|
positionalParams;
|
2015-07-17 13:36:53 -07:00
|
|
|
|
2015-08-30 21:20:18 -07:00
|
|
|
urlParams = serializeParams(paramsSegment.params);
|
2015-07-17 13:36:53 -07:00
|
|
|
|
|
|
|
|
2015-08-13 11:09:22 -07:00
|
|
|
auxiliary = currentSegment.auxiliary;
|
|
|
|
} else {
|
2015-08-30 21:20:18 -07:00
|
|
|
allParams = positionalParams;
|
2015-08-13 11:09:22 -07:00
|
|
|
auxiliary = [];
|
2015-08-30 21:20:18 -07:00
|
|
|
urlParams = [];
|
2015-08-13 11:09:22 -07:00
|
|
|
}
|
2015-08-30 21:20:18 -07:00
|
|
|
instruction = this._getInstruction(urlPath, urlParams, this, allParams);
|
2015-08-13 11:09:22 -07:00
|
|
|
return new PathMatch(instruction, nextSegment, auxiliary);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
|
2015-10-02 16:47:54 -07:00
|
|
|
generate(params: {[key: string]: any}): ComponentInstruction {
|
2015-07-08 10:57:38 -07:00
|
|
|
var paramTokens = new TouchMap(params);
|
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
var path = [];
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
for (var i = 0; i < this._segments.length; i++) {
|
|
|
|
let segment = this._segments[i];
|
|
|
|
if (!(segment instanceof ContinuationSegment)) {
|
|
|
|
path.push(segment.generate(paramTokens));
|
2015-07-08 10:57:38 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 13:36:53 -07:00
|
|
|
var urlPath = path.join('/');
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-07-17 13:36:53 -07:00
|
|
|
var nonPositionalParams = paramTokens.getUnused();
|
|
|
|
var urlParams = serializeParams(nonPositionalParams);
|
2015-07-08 10:57:38 -07:00
|
|
|
|
2015-08-30 21:20:18 -07:00
|
|
|
return this._getInstruction(urlPath, urlParams, this, params);
|
|
|
|
}
|
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
private _getInstruction(urlPath: string, urlParams: string[], _recognizer: PathRecognizer,
|
2015-10-02 16:47:54 -07:00
|
|
|
params: {[key: string]: any}): ComponentInstruction {
|
2015-08-30 21:20:18 -07:00
|
|
|
var hashKey = urlPath + '?' + urlParams.join('?');
|
2015-09-04 15:09:02 -07:00
|
|
|
if (this._cache.has(hashKey)) {
|
|
|
|
return this._cache.get(hashKey);
|
2015-08-30 21:20:18 -07:00
|
|
|
}
|
|
|
|
var instruction = new ComponentInstruction(urlPath, urlParams, _recognizer, params);
|
2015-09-04 15:09:02 -07:00
|
|
|
this._cache.set(hashKey, instruction);
|
2015-08-30 21:20:18 -07:00
|
|
|
|
|
|
|
return instruction;
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|