2015-07-17 13:36:53 -07:00
|
|
|
import {AsyncRoute, AuxRoute, Route, Redirect, RouteDefinition} from './route_config_decorator';
|
2015-07-13 16:12:48 -07:00
|
|
|
import {ComponentDefinition} from './route_definition';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {isType, Type} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
2015-09-10 15:25:36 -07:00
|
|
|
|
2015-07-13 16:12:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a JS Object that represents... returns a corresponding Route, AsyncRoute, or Redirect
|
|
|
|
*/
|
|
|
|
export function normalizeRouteConfig(config: RouteDefinition): RouteDefinition {
|
2015-07-17 13:36:53 -07:00
|
|
|
if (config instanceof Route || config instanceof Redirect || config instanceof AsyncRoute ||
|
|
|
|
config instanceof AuxRoute) {
|
2015-07-13 16:12:48 -07:00
|
|
|
return <RouteDefinition>config;
|
|
|
|
}
|
|
|
|
|
2015-10-08 12:27:55 -07:00
|
|
|
if ((+!!config.component) + (+!!config.redirectTo) + (+!!config.loader) != 1) {
|
2015-07-13 16:12:48 -07:00
|
|
|
throw new BaseException(
|
2015-07-17 13:36:53 -07:00
|
|
|
`Route config should contain exactly one "component", "loader", or "redirectTo" property.`);
|
2015-07-13 16:12:48 -07:00
|
|
|
}
|
2015-10-25 15:00:27 +05:30
|
|
|
if (config.as && config.name) {
|
|
|
|
throw new BaseException(`Route config should contain exactly one "as" or "name" property.`);
|
|
|
|
}
|
|
|
|
if (config.as) {
|
|
|
|
config.name = config.as;
|
|
|
|
}
|
2015-10-08 12:27:55 -07:00
|
|
|
if (config.loader) {
|
2015-10-25 15:00:27 +05:30
|
|
|
return new AsyncRoute({path: config.path, loader: config.loader, name: config.name});
|
2015-10-08 12:27:55 -07:00
|
|
|
}
|
2015-10-30 17:05:30 -07:00
|
|
|
if (config.aux) {
|
|
|
|
return new AuxRoute({path: config.aux, component:<Type>config.component, name: config.name});
|
|
|
|
}
|
2015-07-13 16:12:48 -07:00
|
|
|
if (config.component) {
|
|
|
|
if (typeof config.component == 'object') {
|
|
|
|
let componentDefinitionObject = <ComponentDefinition>config.component;
|
|
|
|
if (componentDefinitionObject.type == 'constructor') {
|
|
|
|
return new Route({
|
|
|
|
path: config.path,
|
|
|
|
component:<Type>componentDefinitionObject.constructor,
|
2015-10-25 15:00:27 +05:30
|
|
|
name: config.name
|
2015-07-13 16:12:48 -07:00
|
|
|
});
|
|
|
|
} else if (componentDefinitionObject.type == 'loader') {
|
|
|
|
return new AsyncRoute(
|
2015-10-25 15:00:27 +05:30
|
|
|
{path: config.path, loader: componentDefinitionObject.loader, name: config.name});
|
2015-07-13 16:12:48 -07:00
|
|
|
} else {
|
|
|
|
throw new BaseException(
|
2015-07-17 13:36:53 -07:00
|
|
|
`Invalid component type "${componentDefinitionObject.type}". Valid types are "constructor" and "loader".`);
|
2015-07-13 16:12:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Route(<{
|
|
|
|
path: string;
|
|
|
|
component: Type;
|
2015-10-25 15:00:27 +05:30
|
|
|
name?: string;
|
2015-07-13 16:12:48 -07:00
|
|
|
}>config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.redirectTo) {
|
|
|
|
return new Redirect({path: config.path, redirectTo: config.redirectTo});
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
2015-09-18 15:41:02 -07:00
|
|
|
|
|
|
|
export function assertComponentExists(component: Type, path: string): void {
|
|
|
|
if (!isType(component)) {
|
|
|
|
throw new BaseException(`Component for route "${path}" is not defined, or is not a class.`);
|
|
|
|
}
|
|
|
|
}
|