13
modules/angular2/src/router/route_config.js
vendored
13
modules/angular2/src/router/route_config.js
vendored
@ -1,13 +1,18 @@
|
||||
import {CONST} from 'angular2/src/facade/lang';
|
||||
import {List} from 'angular2/src/facade/collection';
|
||||
import {List, Map} from 'angular2/src/facade/collection';
|
||||
|
||||
/**
|
||||
* You use the RouteConfig annotation to ...
|
||||
* You use the RouteConfig annotation to add routes to a component.
|
||||
*
|
||||
* Supported keys:
|
||||
* - `path` (required)
|
||||
* - `component` or `components` (requires exactly one of these)
|
||||
* - `as` (optional)
|
||||
*/
|
||||
export class RouteConfig {
|
||||
configs;
|
||||
configs:List<Map>;
|
||||
@CONST()
|
||||
constructor(configs:List) {
|
||||
constructor(configs:List<Map>) {
|
||||
this.configs = configs;
|
||||
}
|
||||
}
|
||||
|
18
modules/angular2/src/router/route_registry.js
vendored
18
modules/angular2/src/router/route_registry.js
vendored
@ -1,12 +1,10 @@
|
||||
import {RouteRecognizer} from './route_recognizer';
|
||||
import {Instruction, noopInstruction} from './instruction';
|
||||
import {List, ListWrapper, Map, MapWrapper, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {isPresent, isBlank, isType, StringWrapper, CONST} from 'angular2/src/facade/lang';
|
||||
import {isPresent, isBlank, isType, StringWrapper, BaseException} from 'angular2/src/facade/lang';
|
||||
import {RouteConfig} from './route_config';
|
||||
import {reflector} from 'angular2/src/reflection/reflection';
|
||||
|
||||
export const rootHostComponent = 'ROOT_HOST';
|
||||
|
||||
export class RouteRegistry {
|
||||
_rules:Map<any, RouteRecognizer>;
|
||||
|
||||
@ -28,13 +26,13 @@ export class RouteRegistry {
|
||||
|
||||
var components = StringMapWrapper.get(config, 'components');
|
||||
StringMapWrapper.forEach(components, (component, _) => {
|
||||
this._configFromComponent(component);
|
||||
this.configFromComponent(component);
|
||||
});
|
||||
|
||||
recognizer.addConfig(config['path'], config, config['alias']);
|
||||
}
|
||||
|
||||
_configFromComponent(component) {
|
||||
configFromComponent(component) {
|
||||
if (!isType(component)) {
|
||||
return;
|
||||
}
|
||||
@ -59,9 +57,7 @@ export class RouteRegistry {
|
||||
}
|
||||
|
||||
|
||||
// TODO: make recognized context a class
|
||||
// TODO: change parentComponent into parentContext
|
||||
recognize(url:string, parentComponent = rootHostComponent) {
|
||||
recognize(url:string, parentComponent) {
|
||||
var componentRecognizer = MapWrapper.get(this._rules, parentComponent);
|
||||
if (isBlank(componentRecognizer)) {
|
||||
return null;
|
||||
@ -103,9 +99,9 @@ export class RouteRegistry {
|
||||
return null;
|
||||
}
|
||||
|
||||
generate(name:string, params:any) {
|
||||
generate(name:string, params:any, hostComponent) {
|
||||
//TODO: implement for hierarchical routes
|
||||
var componentRecognizer = MapWrapper.get(this._rules, rootHostComponent);
|
||||
var componentRecognizer = MapWrapper.get(this._rules, hostComponent);
|
||||
if (isPresent(componentRecognizer)) {
|
||||
return componentRecognizer.generate(name, params);
|
||||
}
|
||||
@ -148,7 +144,7 @@ function normalizeConfig(config:StringMap) {
|
||||
|
||||
return newConfig;
|
||||
} else if (!StringMapWrapper.contains(config, 'components')) {
|
||||
throw new Error('Config does not include a "component" or "components" key.');
|
||||
throw new BaseException('Config does not include a "component" or "components" key.');
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
23
modules/angular2/src/router/router.js
vendored
23
modules/angular2/src/router/router.js
vendored
@ -1,8 +1,8 @@
|
||||
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
import {isBlank, Type} from 'angular2/src/facade/lang';
|
||||
|
||||
import {RouteRegistry, rootHostComponent} from './route_registry';
|
||||
import {RouteRegistry} from './route_registry';
|
||||
import {Pipeline} from './pipeline';
|
||||
import {Instruction} from './instruction';
|
||||
import {RouterOutlet} from './router_outlet';
|
||||
@ -42,7 +42,6 @@ export class Router {
|
||||
this._registry = registry;
|
||||
this._pipeline = pipeline;
|
||||
this._subject = new EventEmitter();
|
||||
//this._location.subscribe((url) => this.navigate(url));
|
||||
}
|
||||
|
||||
|
||||
@ -86,10 +85,8 @@ export class Router {
|
||||
*
|
||||
*/
|
||||
config(config:any) {
|
||||
|
||||
//TODO: use correct check
|
||||
if (config instanceof List) {
|
||||
path.forEach((configObject) => {
|
||||
config.forEach((configObject) => {
|
||||
// TODO: this is a hack
|
||||
this._registry.config(this.hostComponent, configObject);
|
||||
})
|
||||
@ -172,7 +169,7 @@ export class Router {
|
||||
* Given a URL, returns an instruction representing the component graph
|
||||
*/
|
||||
recognize(url:string) {
|
||||
return this._registry.recognize(url);
|
||||
return this._registry.recognize(url, this.hostComponent);
|
||||
}
|
||||
|
||||
|
||||
@ -192,17 +189,15 @@ export class Router {
|
||||
* Generate a URL from a component name and optional map of parameters. The URL is relative to the app's base href.
|
||||
*/
|
||||
generate(name:string, params:any) {
|
||||
return this._registry.generate(name, params);
|
||||
}
|
||||
|
||||
static getRoot():Router {
|
||||
return new RootRouter(new Pipeline(), new Location());
|
||||
return this._registry.generate(name, params, this.hostComponent);
|
||||
}
|
||||
}
|
||||
|
||||
export class RootRouter extends Router {
|
||||
constructor(pipeline:Pipeline, location:Location) {
|
||||
super(new RouteRegistry(), pipeline, location, null, rootHostComponent);
|
||||
constructor(registry:RouteRegistry, pipeline:Pipeline, location:Location, hostComponent:Type) {
|
||||
super(registry, pipeline, location, null, hostComponent);
|
||||
this._location.subscribe((url) => this.navigate(url));
|
||||
this._registry.configFromComponent(hostComponent);
|
||||
this.navigate(location.path());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user