@@ -2,7 +2,10 @@ import {
2
2
RegExp ,
3
3
RegExpWrapper ,
4
4
StringWrapper ,
5
+ isBlank ,
5
6
isPresent ,
7
+ isType ,
8
+ isStringMap ,
6
9
BaseException
7
10
} from 'angular2/src/facade/lang' ;
8
11
import {
@@ -14,7 +17,10 @@ import {
14
17
StringMapWrapper
15
18
} from 'angular2/src/facade/collection' ;
16
19
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' ;
18
24
19
25
/**
20
26
* `RouteRecognizer` is responsible for recognizing routes for a single component.
@@ -33,7 +39,8 @@ export class RouteRecognizer {
33
39
this . redirects . set ( path , target ) ;
34
40
}
35
41
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' ] ) ;
37
44
var recognizer = new PathRecognizer ( path , handler ) ;
38
45
MapWrapper . forEach ( this . matchers , ( matcher , _ ) => {
39
46
if ( recognizer . regex . toString ( ) == matcher . regex . toString ( ) ) {
@@ -65,28 +72,21 @@ export class RouteRecognizer {
65
72
if ( path == url ) {
66
73
url = target ;
67
74
}
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 ) ;
70
77
}
71
78
} ) ;
72
79
73
80
MapWrapper . forEach ( this . matchers , ( pathRecognizer , regex ) => {
74
81
var match ;
75
82
if ( isPresent ( match = RegExpWrapper . firstMatch ( regex , url ) ) ) {
76
- // TODO(btford): determine a good generic way to deal with terminal matches
77
83
var matchedUrl = '/' ;
78
84
var unmatchedUrl = '' ;
79
85
if ( url != '/' ) {
80
86
matchedUrl = match [ 0 ] ;
81
- unmatchedUrl = StringWrapper . substring ( url , match [ 0 ] . length ) ;
87
+ unmatchedUrl = url . substring ( match [ 0 ] . length ) ;
82
88
}
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 ) ) ;
90
90
}
91
91
} ) ;
92
92
@@ -95,30 +95,39 @@ export class RouteRecognizer {
95
95
96
96
hasRoute ( name : string ) : boolean { return this . names . has ( name ) ; }
97
97
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 } ;
101
105
}
102
106
}
103
107
104
108
export 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
+ }
110
114
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
+ }
123
131
}
132
+ throw new BaseException ( `Unexpected component "${ config } ".` ) ;
124
133
}
0 commit comments