2016-04-25 16:57:15 -07:00
|
|
|
import {RouteSegment, UrlSegment, Tree, TreeNode, rootNode} from './segments';
|
2016-04-22 12:04:56 -07:00
|
|
|
import {RoutesMetadata, RouteMetadata} from './metadata/metadata';
|
2016-04-25 16:57:15 -07:00
|
|
|
import {Type, isBlank, isPresent, stringify} from 'angular2/src/facade/lang';
|
|
|
|
|
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2016-04-22 12:04:56 -07:00
|
|
|
import {PromiseWrapper} from 'angular2/src/facade/promise';
|
|
|
|
|
import {BaseException} from 'angular2/src/facade/exceptions';
|
|
|
|
|
import {ComponentResolver} from 'angular2/core';
|
2016-04-25 16:57:15 -07:00
|
|
|
import {DEFAULT_OUTLET_NAME} from './constants';
|
2016-04-22 12:04:56 -07:00
|
|
|
import {reflector} from 'angular2/src/core/reflection/reflection';
|
|
|
|
|
|
|
|
|
|
export function recognize(componentResolver: ComponentResolver, type: Type,
|
|
|
|
|
url: Tree<UrlSegment>): Promise<Tree<RouteSegment>> {
|
2016-04-25 16:57:15 -07:00
|
|
|
return componentResolver.resolveComponent(type).then(factory => {
|
|
|
|
|
let segment =
|
|
|
|
|
new RouteSegment([url.root], url.root.parameters, DEFAULT_OUTLET_NAME, type, factory);
|
|
|
|
|
return _recognizeMany(componentResolver, type, rootNode(url).children)
|
|
|
|
|
.then(children => new Tree<RouteSegment>(new TreeNode<RouteSegment>(segment, children)));
|
|
|
|
|
});
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
function _recognize(componentResolver: ComponentResolver, parentType: Type,
|
|
|
|
|
url: TreeNode<UrlSegment>): Promise<TreeNode<RouteSegment>[]> {
|
|
|
|
|
let metadata = _readMetadata(parentType); // should read from the factory instead
|
2016-04-22 12:04:56 -07:00
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
let match;
|
2016-04-22 12:04:56 -07:00
|
|
|
try {
|
2016-04-25 16:57:15 -07:00
|
|
|
match = _match(metadata, url);
|
2016-04-22 12:04:56 -07:00
|
|
|
} catch (e) {
|
|
|
|
|
return PromiseWrapper.reject(e, null);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
let main = _constructSegment(componentResolver, match);
|
|
|
|
|
let aux =
|
|
|
|
|
_recognizeMany(componentResolver, parentType, match.aux).then(_checkOutletNameUniqueness);
|
|
|
|
|
return PromiseWrapper.all([main, aux]).then(ListWrapper.flatten);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _recognizeMany(componentResolver: ComponentResolver, parentType: Type,
|
|
|
|
|
urls: TreeNode<UrlSegment>[]): Promise<TreeNode<RouteSegment>[]> {
|
|
|
|
|
let recognized = urls.map(u => _recognize(componentResolver, parentType, u));
|
|
|
|
|
return PromiseWrapper.all(recognized).then(ListWrapper.flatten);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _constructSegment(componentResolver: ComponentResolver,
|
|
|
|
|
matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> {
|
2016-04-22 12:04:56 -07:00
|
|
|
return componentResolver.resolveComponent(matched.route.component)
|
|
|
|
|
.then(factory => {
|
2016-04-27 15:36:11 -07:00
|
|
|
let urlOutlet = matched.consumedUrlSegments[0].outlet;
|
2016-04-25 16:57:15 -07:00
|
|
|
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters,
|
2016-04-27 15:36:11 -07:00
|
|
|
isBlank(urlOutlet) ? DEFAULT_OUTLET_NAME : urlOutlet,
|
2016-04-22 12:04:56 -07:00
|
|
|
matched.route.component, factory);
|
|
|
|
|
|
2016-04-27 15:36:11 -07:00
|
|
|
if (matched.leftOverUrl.length > 0) {
|
|
|
|
|
return _recognizeMany(componentResolver, matched.route.component, matched.leftOverUrl)
|
2016-04-25 16:57:15 -07:00
|
|
|
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
|
2016-04-22 12:04:56 -07:00
|
|
|
} else {
|
2016-04-25 16:57:15 -07:00
|
|
|
return [new TreeNode<RouteSegment>(segment, [])];
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
function _match(metadata: RoutesMetadata, url: TreeNode<UrlSegment>): _MatchResult {
|
2016-04-22 12:04:56 -07:00
|
|
|
for (let r of metadata.routes) {
|
2016-04-25 16:57:15 -07:00
|
|
|
let matchingResult = _matchWithParts(r, url);
|
2016-04-22 12:04:56 -07:00
|
|
|
if (isPresent(matchingResult)) {
|
|
|
|
|
return matchingResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new BaseException("Cannot match any routes");
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _MatchResult {
|
2016-04-22 12:04:56 -07:00
|
|
|
let parts = route.path.split("/");
|
2016-04-25 16:57:15 -07:00
|
|
|
let positionalParams = {};
|
2016-04-22 12:04:56 -07:00
|
|
|
let consumedUrlSegments = [];
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
let lastParent: TreeNode<UrlSegment> = null;
|
|
|
|
|
let lastSegment: TreeNode<UrlSegment> = null;
|
|
|
|
|
|
|
|
|
|
let current = url;
|
2016-04-22 12:04:56 -07:00
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
2016-04-27 15:36:11 -07:00
|
|
|
if (isBlank(current)) return null;
|
|
|
|
|
|
2016-04-22 12:04:56 -07:00
|
|
|
let p = parts[i];
|
2016-04-25 16:57:15 -07:00
|
|
|
let isLastSegment = i === parts.length - 1;
|
|
|
|
|
let isLastParent = i === parts.length - 2;
|
|
|
|
|
let isPosParam = p.startsWith(":");
|
|
|
|
|
|
|
|
|
|
if (!isPosParam && p != current.value.segment) return null;
|
|
|
|
|
if (isLastSegment) {
|
|
|
|
|
lastSegment = current;
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
2016-04-25 16:57:15 -07:00
|
|
|
if (isLastParent) {
|
|
|
|
|
lastParent = current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isPosParam) {
|
|
|
|
|
positionalParams[p.substring(1)] = current.value.segment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consumedUrlSegments.push(current.value);
|
|
|
|
|
|
|
|
|
|
current = ListWrapper.first(current.children);
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
2016-04-25 16:57:15 -07:00
|
|
|
|
2016-04-27 15:36:11 -07:00
|
|
|
if (isPresent(current) && isBlank(current.value.segment)) {
|
|
|
|
|
lastParent = lastSegment;
|
|
|
|
|
lastSegment = current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let p = lastSegment.value.parameters;
|
|
|
|
|
let parameters =
|
|
|
|
|
<{[key: string]: string}>StringMapWrapper.merge(isBlank(p) ? {} : p, positionalParams);
|
2016-04-25 16:57:15 -07:00
|
|
|
let axuUrlSubtrees = isPresent(lastParent) ? lastParent.children.slice(1) : [];
|
2016-04-27 15:36:11 -07:00
|
|
|
|
|
|
|
|
return new _MatchResult(route, consumedUrlSegments, parameters, lastSegment.children,
|
|
|
|
|
axuUrlSubtrees);
|
2016-04-25 16:57:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _checkOutletNameUniqueness(nodes: TreeNode<RouteSegment>[]): TreeNode<RouteSegment>[] {
|
|
|
|
|
let names = {};
|
|
|
|
|
nodes.forEach(n => {
|
|
|
|
|
let segmentWithSameOutletName = names[n.value.outlet];
|
|
|
|
|
if (isPresent(segmentWithSameOutletName)) {
|
|
|
|
|
let p = segmentWithSameOutletName.stringifiedUrlSegments;
|
|
|
|
|
let c = n.value.stringifiedUrlSegments;
|
|
|
|
|
throw new BaseException(`Two segments cannot have the same outlet name: '${p}' and '${c}'.`);
|
|
|
|
|
}
|
|
|
|
|
names[n.value.outlet] = n.value;
|
|
|
|
|
});
|
|
|
|
|
return nodes;
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
class _MatchResult {
|
2016-04-22 12:04:56 -07:00
|
|
|
constructor(public route: RouteMetadata, public consumedUrlSegments: UrlSegment[],
|
2016-04-27 15:36:11 -07:00
|
|
|
public parameters: {[key: string]: string},
|
|
|
|
|
public leftOverUrl: TreeNode<UrlSegment>[], public aux: TreeNode<UrlSegment>[]) {}
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _readMetadata(componentType: Type) {
|
|
|
|
|
let metadata = reflector.annotations(componentType).filter(f => f instanceof RoutesMetadata);
|
|
|
|
|
if (metadata.length === 0) {
|
|
|
|
|
throw new BaseException(
|
|
|
|
|
`Component '${stringify(componentType)}' does not have route configuration`);
|
|
|
|
|
}
|
|
|
|
|
return metadata[0];
|
|
|
|
|
}
|