@@ -3,8 +3,176 @@ var invariant = require('react/lib/invariant');
3
3
var warning = require ( 'react/lib/warning' ) ;
4
4
var PathUtils = require ( './PathUtils' ) ;
5
5
6
+ var _currentRoute ;
7
+
6
8
class Route {
7
9
10
+ /**
11
+ * Creates and returns a new route. Options may be a URL pathname string
12
+ * with placeholders for named params or an object with any of the following
13
+ * properties:
14
+ *
15
+ * - name The name of the route. This is used to lookup a
16
+ * route relative to its parent route and should be
17
+ * unique among all child routes of the same parent
18
+ * - path A URL pathname string with optional placeholders
19
+ * that specify the names of params to extract from
20
+ * the URL when the path matches. Defaults to `/${name}`
21
+ * when there is a name given, or the path of the parent
22
+ * route, or /
23
+ * - ignoreScrollBehavior True to make this route (and all descendants) ignore
24
+ * the scroll behavior of the router
25
+ * - isDefault True to make this route the default route among all
26
+ * its siblings
27
+ * - isNotFound True to make this route the "not found" route among
28
+ * all its siblings
29
+ * - onEnter A transition hook that will be called when the
30
+ * router is going to enter this route
31
+ * - onLeave A transition hook that will be called when the
32
+ * router is going to leave this route
33
+ * - handler A React component that will be rendered when
34
+ * this route is active
35
+ * - parentRoute The parent route to use for this route. This option
36
+ * is automatically supplied when creating routes inside
37
+ * the callback to another invocation of createRoute. You
38
+ * only ever need to use this when declaring routes
39
+ * independently of one another to manually piece together
40
+ * the route hierarchy
41
+ *
42
+ * The callback may be used to structure your route hierarchy. Any call to
43
+ * createRoute, createDefaultRoute, createNotFoundRoute, or createRedirect
44
+ * inside the callback automatically uses this route as its parent.
45
+ */
46
+ static createRoute ( options , callback ) {
47
+ options = options || { } ;
48
+
49
+ if ( typeof options === 'string' )
50
+ options = { path : options } ;
51
+
52
+ var parentRoute = _currentRoute ;
53
+
54
+ if ( parentRoute ) {
55
+ warning (
56
+ options . parentRoute == null || options . parentRoute === parentRoute ,
57
+ 'You should not use parentRoute with createRoute inside another route\'s child callback; it is ignored'
58
+ ) ;
59
+ } else {
60
+ parentRoute = options . parentRoute ;
61
+ }
62
+
63
+ var name = options . name ;
64
+ var path = options . path || name ;
65
+
66
+ if ( path ) {
67
+ if ( PathUtils . isAbsolute ( path ) ) {
68
+ if ( parentRoute ) {
69
+ invariant (
70
+ parentRoute . paramNames . length === 0 ,
71
+ 'You cannot nest path "%s" inside "%s"; the parent requires URL parameters' ,
72
+ path , parentRoute . path
73
+ ) ;
74
+ }
75
+ } else if ( parentRoute ) {
76
+ // Relative paths extend their parent.
77
+ path = PathUtils . join ( parentRoute . path , path ) ;
78
+ } else {
79
+ path = '/' + path ;
80
+ }
81
+ } else {
82
+ path = parentRoute ? parentRoute . path : '/' ;
83
+ }
84
+
85
+ if ( options . isNotFound && ! ( / \* $ / ) . test ( path ) )
86
+ path += '*' ; // Auto-append * to the path of not found routes.
87
+
88
+ var route = new Route (
89
+ name ,
90
+ path ,
91
+ options . ignoreScrollBehavior ,
92
+ options . isDefault ,
93
+ options . isNotFound ,
94
+ options . onEnter ,
95
+ options . onLeave ,
96
+ options . handler
97
+ ) ;
98
+
99
+ if ( parentRoute ) {
100
+ if ( route . isDefault ) {
101
+ invariant (
102
+ parentRoute . defaultRoute == null ,
103
+ '%s may not have more than one default route' ,
104
+ parentRoute
105
+ ) ;
106
+
107
+ parentRoute . defaultRoute = route ;
108
+ } else if ( route . isNotFound ) {
109
+ invariant (
110
+ parentRoute . notFoundRoute == null ,
111
+ '%s may not have more than one not found route' ,
112
+ parentRoute
113
+ ) ;
114
+
115
+ parentRoute . notFoundRoute = route ;
116
+ }
117
+
118
+ parentRoute . appendChild ( route ) ;
119
+ }
120
+
121
+ // Any routes created in the callback
122
+ // use this route as their parent.
123
+ if ( typeof callback === 'function' ) {
124
+ var currentRoute = _currentRoute ;
125
+ _currentRoute = route ;
126
+ callback . call ( route , route ) ;
127
+ _currentRoute = currentRoute ;
128
+ }
129
+
130
+ return route ;
131
+ }
132
+
133
+ /**
134
+ * Creates and returns a route that is rendered when its parent matches
135
+ * the current URL.
136
+ */
137
+ static createDefaultRoute ( options ) {
138
+ return Route . createRoute (
139
+ assign ( { } , options , { isDefault : true } )
140
+ ) ;
141
+ }
142
+
143
+ /**
144
+ * Creates and returns a route that is rendered when its parent matches
145
+ * the current URL but none of its siblings do.
146
+ */
147
+ static createNotFoundRoute ( options ) {
148
+ return Route . createRoute (
149
+ assign ( { } , options , { isNotFound : true } )
150
+ ) ;
151
+ }
152
+
153
+ /**
154
+ * Creates and returns a route that automatically redirects the transition
155
+ * to another route. In addition to the normal options to createRoute, this
156
+ * function accepts the following options:
157
+ *
158
+ * - from An alias for the `path` option. Defaults to *
159
+ * - to The path/route/route name to redirect to
160
+ * - params The params to use in the redirect URL. Defaults
161
+ * to using the current params
162
+ * - query The query to use in the redirect URL. Defaults
163
+ * to using the current query
164
+ */
165
+ static createRedirect ( options ) {
166
+ return Route . createRoute (
167
+ assign ( { } , options , {
168
+ path : options . path || options . from || '*' ,
169
+ onEnter : function ( transition , params , query ) {
170
+ transition . redirect ( options . to , options . params || params , options . query || query ) ;
171
+ }
172
+ } )
173
+ ) ;
174
+ }
175
+
8
176
constructor ( name , path , ignoreScrollBehavior , isDefault , isNotFound , onEnter , onLeave , handler ) {
9
177
this . name = name ;
10
178
this . path = path ;
@@ -45,172 +213,4 @@ class Route {
45
213
46
214
}
47
215
48
- var _currentRoute ;
49
-
50
- /**
51
- * Creates and returns a new route. Options may be a URL pathname string
52
- * with placeholders for named params or an object with any of the following
53
- * properties:
54
- *
55
- * - name The name of the route. This is used to lookup a
56
- * route relative to its parent route and should be
57
- * unique among all child routes of the same parent
58
- * - path A URL pathname string with optional placeholders
59
- * that specify the names of params to extract from
60
- * the URL when the path matches. Defaults to `/${name}`
61
- * when there is a name given, or the path of the parent
62
- * route, or /
63
- * - ignoreScrollBehavior True to make this route (and all descendants) ignore
64
- * the scroll behavior of the router
65
- * - isDefault True to make this route the default route among all
66
- * its siblings
67
- * - isNotFound True to make this route the "not found" route among
68
- * all its siblings
69
- * - onEnter A transition hook that will be called when the
70
- * router is going to enter this route
71
- * - onLeave A transition hook that will be called when the
72
- * router is going to leave this route
73
- * - handler A React component that will be rendered when
74
- * this route is active
75
- * - parentRoute The parent route to use for this route. This option
76
- * is automatically supplied when creating routes inside
77
- * the callback to another invocation of createRoute. You
78
- * only ever need to use this when declaring routes
79
- * independently of one another to manually piece together
80
- * the route hierarchy
81
- *
82
- * The callback may be used to structure your route hierarchy. Any call to
83
- * createRoute, createDefaultRoute, createNotFoundRoute, or createRedirect
84
- * inside the callback automatically uses this route as its parent.
85
- */
86
- Route . createRoute = function ( options , callback ) {
87
- options = options || { } ;
88
-
89
- if ( typeof options === 'string' )
90
- options = { path : options } ;
91
-
92
- var parentRoute = _currentRoute ;
93
-
94
- if ( parentRoute ) {
95
- warning (
96
- options . parentRoute == null || options . parentRoute === parentRoute ,
97
- 'You should not use parentRoute with createRoute inside another route\'s child callback; it is ignored'
98
- ) ;
99
- } else {
100
- parentRoute = options . parentRoute ;
101
- }
102
-
103
- var name = options . name ;
104
- var path = options . path || name ;
105
-
106
- if ( path ) {
107
- if ( PathUtils . isAbsolute ( path ) ) {
108
- if ( parentRoute ) {
109
- invariant (
110
- parentRoute . paramNames . length === 0 ,
111
- 'You cannot nest path "%s" inside "%s"; the parent requires URL parameters' ,
112
- path , parentRoute . path
113
- ) ;
114
- }
115
- } else if ( parentRoute ) {
116
- // Relative paths extend their parent.
117
- path = PathUtils . join ( parentRoute . path , path ) ;
118
- } else {
119
- path = '/' + path ;
120
- }
121
- } else {
122
- path = parentRoute ? parentRoute . path : '/' ;
123
- }
124
-
125
- if ( options . isNotFound && ! ( / \* $ / ) . test ( path ) )
126
- path += '*' ; // Auto-append * to the path of not found routes.
127
-
128
- var route = new Route (
129
- name ,
130
- path ,
131
- options . ignoreScrollBehavior ,
132
- options . isDefault ,
133
- options . isNotFound ,
134
- options . onEnter ,
135
- options . onLeave ,
136
- options . handler
137
- ) ;
138
-
139
- if ( parentRoute ) {
140
- if ( route . isDefault ) {
141
- invariant (
142
- parentRoute . defaultRoute == null ,
143
- '%s may not have more than one default route' ,
144
- parentRoute
145
- ) ;
146
-
147
- parentRoute . defaultRoute = route ;
148
- } else if ( route . isNotFound ) {
149
- invariant (
150
- parentRoute . notFoundRoute == null ,
151
- '%s may not have more than one not found route' ,
152
- parentRoute
153
- ) ;
154
-
155
- parentRoute . notFoundRoute = route ;
156
- }
157
-
158
- parentRoute . appendChild ( route ) ;
159
- }
160
-
161
- // Any routes created in the callback
162
- // use this route as their parent.
163
- if ( typeof callback === 'function' ) {
164
- var currentRoute = _currentRoute ;
165
- _currentRoute = route ;
166
- callback . call ( route , route ) ;
167
- _currentRoute = currentRoute ;
168
- }
169
-
170
- return route ;
171
- } ;
172
-
173
- /**
174
- * Creates and returns a route that is rendered when its parent matches
175
- * the current URL.
176
- */
177
- Route . createDefaultRoute = function ( options ) {
178
- return Route . createRoute (
179
- assign ( { } , options , { isDefault : true } )
180
- ) ;
181
- } ;
182
-
183
- /**
184
- * Creates and returns a route that is rendered when its parent matches
185
- * the current URL but none of its siblings do.
186
- */
187
- Route . createNotFoundRoute = function ( options ) {
188
- return Route . createRoute (
189
- assign ( { } , options , { isNotFound : true } )
190
- ) ;
191
- } ;
192
-
193
- /**
194
- * Creates and returns a route that automatically redirects the transition
195
- * to another route. In addition to the normal options to createRoute, this
196
- * function accepts the following options:
197
- *
198
- * - from An alias for the `path` option. Defaults to *
199
- * - to The path/route/route name to redirect to
200
- * - params The params to use in the redirect URL. Defaults
201
- * to using the current params
202
- * - query The query to use in the redirect URL. Defaults
203
- * to using the current query
204
- */
205
- Route . createRedirect = function ( options ) {
206
- return Route . createRoute (
207
- assign ( { } , options , {
208
- path : options . path || options . from || '*' ,
209
- onEnter : function ( transition , params , query ) {
210
- transition . redirect ( options . to , options . params || params , options . query || query ) ;
211
- }
212
- } )
213
- ) ;
214
- } ;
215
-
216
216
module . exports = Route ;
0 commit comments