@@ -14,28 +14,40 @@ class Router {
14
14
constructor ( params ) {
15
15
this . component = ko . observable ( )
16
16
this . isNavigating = ko . observable ( true )
17
+ this . routes = Route . createRoutes ( params . routes || { } )
17
18
18
- Router . link ( this , params )
19
-
20
- this . passthrough = params
19
+ Router . link ( this )
21
20
22
21
if ( this . isRoot ) {
22
+ Router . setConfig ( params )
23
+ this . routes . push ( ...Route . createRoutes ( Router . routes ) )
23
24
document . addEventListener ( events . click , Router . onclick )
24
25
window . addEventListener ( events . popstate , Router . onpopstate )
25
- }
26
-
27
- const routes = Object . assign ( this . isRoot ? Router . routes : { } , params . routes )
28
- this . routes = Object . entries ( routes ) . map ( ( [ r , m ] ) => new Route ( r , m ) )
29
- if ( ! this . isRoot && this . $parent . ctx . route . children ) {
26
+ } else if ( this . $parent . ctx . route . children ) {
30
27
this . routes . push ( ...this . $parent . ctx . route . children )
31
28
}
32
- delete params . routes
29
+
30
+ this . passthrough = Object . entries ( params ) . reduce ( ( accum , [ k , v ] ) =>
31
+ k === 'base' || k === 'hashbang' || k === 'routes'
32
+ ? accum
33
+ : Object . assign ( accum , { [ k ] : v } ) ,
34
+ { } )
33
35
34
36
this . update ( this . getPathFromLocation ( ) , false )
35
37
}
36
38
37
- static async update ( ...args ) {
38
- return await routers [ 0 ] . update ( ...args )
39
+ get base ( ) {
40
+ return this . isRoot
41
+ ? ( Router . config . hashbang ? '/#!' : '' ) + Router . config . base
42
+ : this . $parent . base + this . $parent . ctx . pathname
43
+ }
44
+
45
+ get $parent ( ) {
46
+ return routers [ this . depth - 1 ]
47
+ }
48
+
49
+ get $child ( ) {
50
+ return routers [ this . depth + 1 ]
39
51
}
40
52
41
53
async update ( url , args ) {
@@ -152,6 +164,15 @@ class Router {
152
164
}
153
165
}
154
166
167
+ static setConfig ( { base, hashbang } ) {
168
+ if ( base ) {
169
+ Router . config . base = base
170
+ }
171
+ if ( hashbang ) {
172
+ Router . config . hashbang = hashbang
173
+ }
174
+ }
175
+
155
176
static use ( ...fns ) {
156
177
Router . middleware . push ( ...fns )
157
178
}
@@ -160,6 +181,10 @@ class Router {
160
181
Router . plugins . push ( ...fns )
161
182
}
162
183
184
+ static useRoutes ( routes ) {
185
+ Object . assign ( Router . routes , routes )
186
+ }
187
+
163
188
static get ( i ) {
164
189
return routers [ i ]
165
190
}
@@ -172,32 +197,15 @@ class Router {
172
197
return routers [ routers . length - 1 ]
173
198
}
174
199
175
- static link ( router , params ) {
176
- routers . push ( router )
177
- router . depth = routers . length - 1
200
+ static async update ( ...args ) {
201
+ return await routers [ 0 ] . update ( ...args )
202
+ }
203
+
204
+ static link ( router ) {
205
+ router . depth = routers . length
178
206
router . isRoot = router . depth === 0
207
+ routers . push ( router )
179
208
router . $root = routers [ 0 ]
180
-
181
- if ( router . isRoot ) {
182
- if ( params . base ) {
183
- Router . config . base = params . base
184
- delete params . base
185
- }
186
- router . base = Router . config . base
187
- if ( params . hashbang ) {
188
- Router . config . hashbang = params . hashbang
189
- delete params . hashbang
190
- }
191
- if ( Router . config . hashbang ) {
192
- router . base += '/#!'
193
- }
194
- } else {
195
- const $parent = routers [ router . depth - 1 ]
196
- const { ctx : { pathname } , base } = $parent
197
- router . $parent = $parent
198
- router . $parent . $child = router
199
- router . base = base + pathname
200
- }
201
209
}
202
210
203
211
static unlink ( ) {
0 commit comments