11import { RouteRecognizer } from './route_recognizer' ;
22import { Instruction , noopInstruction } from './instruction' ;
33import { List , ListWrapper , Map , MapWrapper , StringMap , StringMapWrapper } from 'angular2/src/facade/collection' ;
4- import { isPresent , isBlank , isType , StringWrapper } from 'angular2/src/facade/lang' ;
4+ import { isPresent , isBlank , isType , StringWrapper , CONST } from 'angular2/src/facade/lang' ;
55import { RouteConfig } from './route_config' ;
66import { reflector } from 'angular2/src/reflection/reflection' ;
77
8+ export const rootHostComponent = 'ROOT_HOST' ;
9+
810export class RouteRegistry {
911 _rules :Map < any , RouteRecognizer > ;
1012
1113 constructor ( ) {
1214 this . _rules = MapWrapper . create ( ) ;
1315 }
1416
15- config ( parentComponent , path :string , component :any , alias :string = null ) {
16- if ( parentComponent === 'app' ) {
17- parentComponent = '/' ;
18- }
17+ config ( parentComponent , config ) {
1918
2019 var recognizer :RouteRecognizer ;
2120 if ( MapWrapper . contains ( this . _rules , parentComponent ) ) {
@@ -25,16 +24,14 @@ export class RouteRegistry {
2524 MapWrapper . set ( this . _rules , parentComponent , recognizer ) ;
2625 }
2726
28- this . _configFromComponent ( component ) ;
29-
30- //TODO: support sibling components
31- var components = StringMapWrapper . create ( ) ;
32- StringMapWrapper . set ( components , 'default' , component ) ;
27+ config = normalizeConfig ( config ) ;
3328
34- var handler = StringMapWrapper . create ( ) ;
35- StringMapWrapper . set ( handler , 'components' , components ) ;
29+ var components = StringMapWrapper . get ( config , 'components' ) ;
30+ StringMapWrapper . forEach ( components , ( component , _ ) => {
31+ this . _configFromComponent ( component ) ;
32+ } ) ;
3633
37- recognizer . addConfig ( path , handler , alias ) ;
34+ recognizer . addConfig ( config [ ' path' ] , config , config [ ' alias' ] ) ;
3835 }
3936
4037 _configFromComponent ( component ) {
@@ -53,7 +50,9 @@ export class RouteRegistry {
5350 var annotation = annotations [ i ] ;
5451
5552 if ( annotation instanceof RouteConfig ) {
56- this . config ( component , annotation . path , annotation . component ) ;
53+ ListWrapper . forEach ( annotation . configs , ( config ) => {
54+ this . config ( component , config ) ;
55+ } )
5756 }
5857 }
5958 }
@@ -62,7 +61,7 @@ export class RouteRegistry {
6261
6362 // TODO: make recognized context a class
6463 // TODO: change parentComponent into parentContext
65- recognize ( url :string , parentComponent = '/' ) {
64+ recognize ( url :string , parentComponent = rootHostComponent ) {
6665 var componentRecognizer = MapWrapper . get ( this . _rules , parentComponent ) ;
6766 if ( isBlank ( componentRecognizer ) ) {
6867 return null ;
@@ -106,7 +105,7 @@ export class RouteRegistry {
106105
107106 generate ( name :string , params :any ) {
108107 //TODO: implement for hierarchical routes
109- var componentRecognizer = MapWrapper . get ( this . _rules , '/' ) ;
108+ var componentRecognizer = MapWrapper . get ( this . _rules , rootHostComponent ) ;
110109 if ( isPresent ( componentRecognizer ) ) {
111110 return componentRecognizer . generate ( name , params ) ;
112111 }
@@ -127,3 +126,29 @@ function handlerToLeafInstructions(context, parentComponent) {
127126 matchedUrl : context [ 'matchedUrl' ]
128127 } ) ;
129128}
129+
130+ // given:
131+ // { component: Foo }
132+ // mutates the config to:
133+ // { components: { default: Foo } }
134+ function normalizeConfig ( config :StringMap ) {
135+ if ( StringMapWrapper . contains ( config , 'component' ) ) {
136+ var component = StringMapWrapper . get ( config , 'component' ) ;
137+ var components = StringMapWrapper . create ( ) ;
138+ StringMapWrapper . set ( components , 'default' , component ) ;
139+
140+ var newConfig = StringMapWrapper . create ( ) ;
141+ StringMapWrapper . set ( newConfig , 'components' , components ) ;
142+
143+ StringMapWrapper . forEach ( config , ( value , key ) => {
144+ if ( ! StringWrapper . equals ( key , 'component' ) && ! StringWrapper . equals ( key , 'components' ) ) {
145+ StringMapWrapper . set ( newConfig , key , value ) ;
146+ }
147+ } ) ;
148+
149+ return newConfig ;
150+ } else if ( ! StringMapWrapper . contains ( config , 'components' ) ) {
151+ throw new Error ( 'Config does not include a "component" or "components" key.' ) ;
152+ }
153+ return config ;
154+ }
0 commit comments