2016-05-01 13:45:14 -07:00
|
|
|
import {RouteSegment, UrlSegment, Tree, TreeNode, rootNode, UrlTree, RouteTree} from './segments';
|
2016-04-22 12:04:56 -07:00
|
|
|
import {RoutesMetadata, RouteMetadata} from './metadata/metadata';
|
2016-05-02 10:36:58 -07:00
|
|
|
import {Type, isBlank, isPresent, stringify} from './facade/lang';
|
|
|
|
|
import {ListWrapper, StringMapWrapper} from './facade/collection';
|
|
|
|
|
import {PromiseWrapper} from './facade/promise';
|
2016-04-28 17:50:03 -07:00
|
|
|
import {BaseException} from '@angular/core';
|
|
|
|
|
import {ComponentResolver} from '@angular/core';
|
2016-04-25 16:57:15 -07:00
|
|
|
import {DEFAULT_OUTLET_NAME} from './constants';
|
2016-04-28 17:50:03 -07:00
|
|
|
import {reflector} from '@angular/core';
|
2016-04-22 12:04:56 -07:00
|
|
|
|
2016-05-03 16:45:30 -07:00
|
|
|
export function recognize(componentResolver: ComponentResolver, rootComponent: Type,
|
2016-05-01 13:45:14 -07:00
|
|
|
url: UrlTree): Promise<RouteTree> {
|
2016-05-04 10:45:48 -07:00
|
|
|
let matched = new _MatchResult(rootComponent, [url.root], {}, rootNode(url).children, []);
|
2016-05-01 13:45:14 -07:00
|
|
|
return _constructSegment(componentResolver, matched).then(roots => new RouteTree(roots[0]));
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-03 16:45:30 -07:00
|
|
|
function _recognize(componentResolver: ComponentResolver, parentComponent: Type,
|
2016-04-25 16:57:15 -07:00
|
|
|
url: TreeNode<UrlSegment>): Promise<TreeNode<RouteSegment>[]> {
|
2016-05-03 16:45:30 -07:00
|
|
|
let metadata = _readMetadata(parentComponent); // should read from the factory instead
|
2016-04-28 18:33:48 -07:00
|
|
|
if (isBlank(metadata)) {
|
|
|
|
|
throw new BaseException(
|
2016-05-03 16:45:30 -07:00
|
|
|
`Component '${stringify(parentComponent)}' does not have route configuration`);
|
2016-04-28 18:33:48 -07:00
|
|
|
}
|
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 =
|
2016-05-03 16:45:30 -07:00
|
|
|
_recognizeMany(componentResolver, parentComponent, match.aux).then(_checkOutletNameUniqueness);
|
2016-04-25 16:57:15 -07:00
|
|
|
return PromiseWrapper.all([main, aux]).then(ListWrapper.flatten);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 16:45:30 -07:00
|
|
|
function _recognizeMany(componentResolver: ComponentResolver, parentComponent: Type,
|
2016-04-25 16:57:15 -07:00
|
|
|
urls: TreeNode<UrlSegment>[]): Promise<TreeNode<RouteSegment>[]> {
|
2016-05-03 16:45:30 -07:00
|
|
|
let recognized = urls.map(u => _recognize(componentResolver, parentComponent, u));
|
2016-04-25 16:57:15 -07:00
|
|
|
return PromiseWrapper.all(recognized).then(ListWrapper.flatten);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _constructSegment(componentResolver: ComponentResolver,
|
|
|
|
|
matched: _MatchResult): Promise<TreeNode<RouteSegment>[]> {
|
2016-04-28 18:33:48 -07:00
|
|
|
return componentResolver.resolveComponent(matched.component)
|
2016-04-22 12:04:56 -07:00
|
|
|
.then(factory => {
|
2016-04-30 18:08:26 -07:00
|
|
|
let urlOutlet = matched.consumedUrlSegments.length === 0 ||
|
|
|
|
|
isBlank(matched.consumedUrlSegments[0].outlet) ?
|
|
|
|
|
DEFAULT_OUTLET_NAME :
|
|
|
|
|
matched.consumedUrlSegments[0].outlet;
|
|
|
|
|
|
|
|
|
|
let segment = new RouteSegment(matched.consumedUrlSegments, matched.parameters, urlOutlet,
|
2016-05-03 16:45:30 -07:00
|
|
|
factory.componentType, factory);
|
2016-04-22 12:04:56 -07:00
|
|
|
|
2016-04-27 15:36:11 -07:00
|
|
|
if (matched.leftOverUrl.length > 0) {
|
2016-05-03 16:45:30 -07:00
|
|
|
return _recognizeMany(componentResolver, factory.componentType, 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-05-03 16:45:30 -07:00
|
|
|
return _recognizeLeftOvers(componentResolver, factory.componentType)
|
2016-04-28 18:33:48 -07:00
|
|
|
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _recognizeLeftOvers(componentResolver: ComponentResolver,
|
2016-05-03 16:45:30 -07:00
|
|
|
parentComponent: Type): Promise<TreeNode<RouteSegment>[]> {
|
|
|
|
|
return componentResolver.resolveComponent(parentComponent)
|
2016-04-28 18:33:48 -07:00
|
|
|
.then(factory => {
|
2016-05-03 16:45:30 -07:00
|
|
|
let metadata = _readMetadata(factory.componentType);
|
2016-04-28 18:33:48 -07:00
|
|
|
if (isBlank(metadata)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let r = (<any[]>metadata.routes).filter(r => r.path == "" || r.path == "/");
|
|
|
|
|
if (r.length === 0) {
|
|
|
|
|
return PromiseWrapper.resolve([]);
|
|
|
|
|
} else {
|
|
|
|
|
return _recognizeLeftOvers(componentResolver, r[0].component)
|
|
|
|
|
.then(children => {
|
|
|
|
|
return componentResolver.resolveComponent(r[0].component)
|
|
|
|
|
.then(factory => {
|
|
|
|
|
let segment =
|
2016-05-02 14:47:59 -07:00
|
|
|
new RouteSegment([], {}, DEFAULT_OUTLET_NAME, r[0].component, factory);
|
2016-04-28 18:33:48 -07:00
|
|
|
return [new TreeNode<RouteSegment>(segment, children)];
|
|
|
|
|
});
|
|
|
|
|
});
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-28 18:33:48 -07:00
|
|
|
let availableRoutes = metadata.routes.map(r => `'${r.path}'`).join(", ");
|
|
|
|
|
throw new BaseException(
|
|
|
|
|
`Cannot match any routes. Current segment: '${url.value}'. Available routes: [${availableRoutes}].`);
|
2016-04-22 12:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-25 16:57:15 -07:00
|
|
|
function _matchWithParts(route: RouteMetadata, url: TreeNode<UrlSegment>): _MatchResult {
|
2016-04-28 18:33:48 -07:00
|
|
|
let path = route.path.startsWith("/") ? route.path.substring(1) : route.path;
|
2016-04-30 18:08:26 -07:00
|
|
|
|
|
|
|
|
if (path == "*") {
|
|
|
|
|
return new _MatchResult(route.component, [], null, [], []);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 18:33:48 -07:00
|
|
|
let parts = 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
|
|
|
let p = lastSegment.value.parameters;
|
2016-05-03 18:49:59 -07:00
|
|
|
let parameters = <{[key: string]: string}>StringMapWrapper.merge(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
|
|
|
|
2016-04-28 18:33:48 -07:00
|
|
|
return new _MatchResult(route.component, consumedUrlSegments, parameters, lastSegment.children,
|
2016-04-27 15:36:11 -07:00
|
|
|
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-05-03 16:45:30 -07:00
|
|
|
constructor(public component: Type|string, 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);
|
2016-04-28 18:33:48 -07:00
|
|
|
return ListWrapper.first(metadata);
|
2016-04-28 17:50:03 -07:00
|
|
|
}
|