2015-04-17 09:59:56 -07:00
|
|
|
|
import {RouteRecognizer} from './route_recognizer';
|
|
|
|
|
import {Instruction, noopInstruction} from './instruction';
|
|
|
|
|
import {List, ListWrapper, Map, MapWrapper, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-05-01 05:53:38 -07:00
|
|
|
|
import {isPresent, isBlank, isType, StringWrapper, BaseException} from 'angular2/src/facade/lang';
|
2015-05-04 15:39:14 -07:00
|
|
|
|
import {RouteConfig} from './route_config_impl';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
import {reflector} from 'angular2/src/reflection/reflection';
|
|
|
|
|
|
|
|
|
|
export class RouteRegistry {
|
|
|
|
|
_rules:Map<any, RouteRecognizer>;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this._rules = MapWrapper.create();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 15:47:12 -07:00
|
|
|
|
config(parentComponent, config) {
|
2015-05-01 15:50:12 -07:00
|
|
|
|
if (!StringMapWrapper.contains(config, 'path')) {
|
|
|
|
|
throw new BaseException('Route config does not contain "path"');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!StringMapWrapper.contains(config, 'component') &&
|
2015-05-03 20:25:26 -07:00
|
|
|
|
!StringMapWrapper.contains(config, 'components') &&
|
|
|
|
|
!StringMapWrapper.contains(config, 'redirectTo')) {
|
|
|
|
|
throw new BaseException('Route config does not contain "component," "components," or "redirectTo"');
|
2015-05-01 15:50:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
|
var recognizer:RouteRecognizer;
|
|
|
|
|
if (MapWrapper.contains(this._rules, parentComponent)) {
|
|
|
|
|
recognizer = MapWrapper.get(this._rules, parentComponent);
|
|
|
|
|
} else {
|
|
|
|
|
recognizer = new RouteRecognizer();
|
|
|
|
|
MapWrapper.set(this._rules, parentComponent, recognizer);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 15:47:12 -07:00
|
|
|
|
config = normalizeConfig(config);
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
2015-05-03 20:25:26 -07:00
|
|
|
|
if (StringMapWrapper.contains(config, 'redirectTo')) {
|
|
|
|
|
recognizer.addRedirect(StringMapWrapper.get(config, 'path'), StringMapWrapper.get(config, 'redirectTo'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 15:47:12 -07:00
|
|
|
|
var components = StringMapWrapper.get(config, 'components');
|
|
|
|
|
StringMapWrapper.forEach(components, (component, _) => {
|
2015-05-01 05:53:38 -07:00
|
|
|
|
this.configFromComponent(component);
|
2015-04-29 15:47:12 -07:00
|
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
2015-05-03 20:36:36 -07:00
|
|
|
|
recognizer.addConfig(config['path'], config, config['as']);
|
2015-04-17 09:59:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-01 05:53:38 -07:00
|
|
|
|
configFromComponent(component) {
|
2015-04-17 09:59:56 -07:00
|
|
|
|
if (!isType(component)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't read the annotations from a type more than once –
|
|
|
|
|
// this prevents an infinite loop if a component routes recursively.
|
|
|
|
|
if (MapWrapper.contains(this._rules, component)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var annotations = reflector.annotations(component);
|
|
|
|
|
if (isPresent(annotations)) {
|
|
|
|
|
for (var i=0; i<annotations.length; i++) {
|
|
|
|
|
var annotation = annotations[i];
|
|
|
|
|
|
|
|
|
|
if (annotation instanceof RouteConfig) {
|
2015-04-29 15:47:12 -07:00
|
|
|
|
ListWrapper.forEach(annotation.configs, (config) => {
|
|
|
|
|
this.config(component, config);
|
|
|
|
|
})
|
2015-04-17 09:59:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-05-01 05:53:38 -07:00
|
|
|
|
recognize(url:string, parentComponent) {
|
2015-04-17 09:59:56 -07:00
|
|
|
|
var componentRecognizer = MapWrapper.get(this._rules, parentComponent);
|
|
|
|
|
if (isBlank(componentRecognizer)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 14:53:13 -07:00
|
|
|
|
var componentSolutions = componentRecognizer.recognize(url);
|
|
|
|
|
var fullSolutions = ListWrapper.create();
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
2015-05-12 14:53:13 -07:00
|
|
|
|
for(var i = 0; i < componentSolutions.length; i++) {
|
|
|
|
|
var candidate = componentSolutions[i];
|
2015-04-17 09:59:56 -07:00
|
|
|
|
if (candidate['unmatchedUrl'].length == 0) {
|
2015-05-12 14:53:13 -07:00
|
|
|
|
ListWrapper.push(fullSolutions, handlerToLeafInstructions(candidate, parentComponent));
|
|
|
|
|
} else {
|
|
|
|
|
var children = StringMapWrapper.create(),
|
|
|
|
|
allMapped = true;
|
|
|
|
|
|
|
|
|
|
StringMapWrapper.forEach(candidate['handler']['components'], (component, name) => {
|
|
|
|
|
if (!allMapped) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var childInstruction = this.recognize(candidate['unmatchedUrl'], component);
|
|
|
|
|
if (isPresent(childInstruction)) {
|
|
|
|
|
childInstruction.params = candidate['params'];
|
|
|
|
|
children[name] = childInstruction;
|
|
|
|
|
} else {
|
|
|
|
|
allMapped = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
2015-05-12 14:53:13 -07:00
|
|
|
|
if (allMapped) {
|
|
|
|
|
ListWrapper.push(fullSolutions, new Instruction({
|
|
|
|
|
component: parentComponent,
|
|
|
|
|
children: children,
|
|
|
|
|
matchedUrl: candidate['matchedUrl'],
|
|
|
|
|
parentCost: candidate['cost']
|
|
|
|
|
}));
|
2015-04-17 09:59:56 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 14:53:13 -07:00
|
|
|
|
if (fullSolutions.length > 0) {
|
|
|
|
|
ListWrapper.sort(fullSolutions, (a, b) => a.cost < b.cost ? -1 : 1);
|
|
|
|
|
return fullSolutions[0];
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-17 09:59:56 -07:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-01 05:53:38 -07:00
|
|
|
|
generate(name:string, params:any, hostComponent) {
|
2015-04-17 09:59:56 -07:00
|
|
|
|
//TODO: implement for hierarchical routes
|
2015-05-01 05:53:38 -07:00
|
|
|
|
var componentRecognizer = MapWrapper.get(this._rules, hostComponent);
|
2015-04-17 09:59:56 -07:00
|
|
|
|
if (isPresent(componentRecognizer)) {
|
|
|
|
|
return componentRecognizer.generate(name, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handlerToLeafInstructions(context, parentComponent) {
|
|
|
|
|
var children = StringMapWrapper.create();
|
|
|
|
|
StringMapWrapper.forEach(context['handler']['components'], (component, outletName) => {
|
|
|
|
|
children[outletName] = new Instruction({
|
|
|
|
|
component: component,
|
2015-05-12 14:53:13 -07:00
|
|
|
|
params: context['params'],
|
|
|
|
|
parentCost: 0
|
2015-04-17 09:59:56 -07:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return new Instruction({
|
|
|
|
|
component: parentComponent,
|
|
|
|
|
children: children,
|
2015-05-12 14:53:13 -07:00
|
|
|
|
matchedUrl: context['matchedUrl'],
|
|
|
|
|
parentCost: context['cost']
|
2015-04-17 09:59:56 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
2015-04-29 15:47:12 -07:00
|
|
|
|
|
|
|
|
|
// given:
|
|
|
|
|
// { component: Foo }
|
|
|
|
|
// mutates the config to:
|
|
|
|
|
// { components: { default: Foo } }
|
|
|
|
|
function normalizeConfig(config:StringMap) {
|
|
|
|
|
if (StringMapWrapper.contains(config, 'component')) {
|
|
|
|
|
var component = StringMapWrapper.get(config, 'component');
|
|
|
|
|
var components = StringMapWrapper.create();
|
|
|
|
|
StringMapWrapper.set(components, 'default', component);
|
|
|
|
|
|
|
|
|
|
var newConfig = StringMapWrapper.create();
|
|
|
|
|
StringMapWrapper.set(newConfig, 'components', components);
|
|
|
|
|
|
|
|
|
|
StringMapWrapper.forEach(config, (value, key) => {
|
|
|
|
|
if (!StringWrapper.equals(key, 'component') && !StringWrapper.equals(key, 'components')) {
|
|
|
|
|
StringMapWrapper.set(newConfig, key, value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return newConfig;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}
|