@@ -2,7 +2,10 @@ import {
22 RegExp ,
33 RegExpWrapper ,
44 StringWrapper ,
5+ isBlank ,
56 isPresent ,
7+ isType ,
8+ isStringMap ,
69 BaseException
710} from 'angular2/src/facade/lang' ;
811import {
@@ -14,7 +17,10 @@ import {
1417 StringMapWrapper
1518} from 'angular2/src/facade/collection' ;
1619
17- import { PathRecognizer , ContinuationSegment } from './path_recognizer' ;
20+ import { PathRecognizer } from './path_recognizer' ;
21+ import { RouteHandler } from './route_handler' ;
22+ import { AsyncRouteHandler } from './async_route_handler' ;
23+ import { SyncRouteHandler } from './sync_route_handler' ;
1824
1925/**
2026 * `RouteRecognizer` is responsible for recognizing routes for a single component.
@@ -33,7 +39,8 @@ export class RouteRecognizer {
3339 this . redirects . set ( path , target ) ;
3440 }
3541
36- addConfig ( path : string , handler : any , alias : string = null ) : boolean {
42+ addConfig ( path : string , handlerObj : any , alias : string = null ) : boolean {
43+ var handler = configObjToHandler ( handlerObj [ 'component' ] ) ;
3744 var recognizer = new PathRecognizer ( path , handler ) ;
3845 MapWrapper . forEach ( this . matchers , ( matcher , _ ) => {
3946 if ( recognizer . regex . toString ( ) == matcher . regex . toString ( ) ) {
@@ -65,28 +72,21 @@ export class RouteRecognizer {
6572 if ( path == url ) {
6673 url = target ;
6774 }
68- } else if ( StringWrapper . startsWith ( url , path ) ) {
69- url = target + StringWrapper . substring ( url , path . length ) ;
75+ } else if ( url . startsWith ( path ) ) {
76+ url = target + url . substring ( path . length ) ;
7077 }
7178 } ) ;
7279
7380 MapWrapper . forEach ( this . matchers , ( pathRecognizer , regex ) => {
7481 var match ;
7582 if ( isPresent ( match = RegExpWrapper . firstMatch ( regex , url ) ) ) {
76- // TODO(btford): determine a good generic way to deal with terminal matches
7783 var matchedUrl = '/' ;
7884 var unmatchedUrl = '' ;
7985 if ( url != '/' ) {
8086 matchedUrl = match [ 0 ] ;
81- unmatchedUrl = StringWrapper . substring ( url , match [ 0 ] . length ) ;
87+ unmatchedUrl = url . substring ( match [ 0 ] . length ) ;
8288 }
83- solutions . push ( new RouteMatch ( {
84- specificity : pathRecognizer . specificity ,
85- handler : pathRecognizer . handler ,
86- params : pathRecognizer . parseParams ( url ) ,
87- matchedUrl : matchedUrl ,
88- unmatchedUrl : unmatchedUrl
89- } ) ) ;
89+ solutions . push ( new RouteMatch ( pathRecognizer , matchedUrl , unmatchedUrl ) ) ;
9090 }
9191 } ) ;
9292
@@ -95,30 +95,39 @@ export class RouteRecognizer {
9595
9696 hasRoute ( name : string ) : boolean { return this . names . has ( name ) ; }
9797
98- generate ( name : string , params : any ) : string {
99- var pathRecognizer = this . names . get ( name ) ;
100- return isPresent ( pathRecognizer ) ? pathRecognizer . generate ( params ) : null ;
98+ generate ( name : string , params : any ) : StringMap < string , any > {
99+ var pathRecognizer : PathRecognizer = this . names . get ( name ) ;
100+ if ( isBlank ( pathRecognizer ) ) {
101+ return null ;
102+ }
103+ var url = pathRecognizer . generate ( params ) ;
104+ return { url, 'nextComponent' : pathRecognizer . handler . componentType } ;
101105 }
102106}
103107
104108export class RouteMatch {
105- specificity : number ;
106- handler : StringMap < string , any > ;
107- params : StringMap < string , string > ;
108- matchedUrl : string ;
109- unmatchedUrl : string ;
109+ constructor ( public recognizer : PathRecognizer , public matchedUrl : string ,
110+ public unmatchedUrl : string ) { }
111+
112+ params ( ) : StringMap < string , string > { return this . recognizer . parseParams ( this . matchedUrl ) ; }
113+ }
110114
111- constructor ( { specificity, handler, params, matchedUrl, unmatchedUrl} : {
112- specificity ?: number ,
113- handler ?: StringMap < string , any > ,
114- params ?: StringMap < string , string > ,
115- matchedUrl ?: string ,
116- unmatchedUrl ?: string
117- } = { } ) {
118- this . specificity = specificity ;
119- this . handler = handler ;
120- this . params = params ;
121- this . matchedUrl = matchedUrl ;
122- this . unmatchedUrl = unmatchedUrl ;
115+ function configObjToHandler ( config : any ) : RouteHandler {
116+ if ( isType ( config ) ) {
117+ return new SyncRouteHandler ( config ) ;
118+ } else if ( isStringMap ( config ) ) {
119+ if ( isBlank ( config [ 'type' ] ) ) {
120+ throw new BaseException (
121+ `Component declaration when provided as a map should include a 'type' property` ) ;
122+ }
123+ var componentType = config [ 'type' ] ;
124+ if ( componentType == 'constructor' ) {
125+ return new SyncRouteHandler ( config [ 'constructor' ] ) ;
126+ } else if ( componentType == 'loader' ) {
127+ return new AsyncRouteHandler ( config [ 'loader' ] ) ;
128+ } else {
129+ throw new BaseException ( `oops` ) ;
130+ }
123131 }
132+ throw new BaseException ( `Unexpected component "${ config } ".` ) ;
124133}
0 commit comments