12
12
const Cls = ( app . Router = class Router {
13
13
static initClass ( ) {
14
14
$ . extend ( this . prototype , Events ) ;
15
-
15
+
16
16
this . routes = [
17
- [ '*' , ' before' ] ,
18
- [ '/' , ' root' ] ,
19
- [ ' /settings' , ' settings' ] ,
20
- [ ' /offline' , ' offline' ] ,
21
- [ ' /about' , ' about' ] ,
22
- [ ' /news' , ' news' ] ,
23
- [ ' /help' , ' help' ] ,
24
- [ ' /:doc-:type/' , ' type' ] ,
25
- [ ' /:doc/' , ' doc' ] ,
26
- [ ' /:doc/:path(*)' , ' entry' ] ,
27
- [ '*' , ' notFound' ]
17
+ [ "*" , " before" ] ,
18
+ [ "/" , " root" ] ,
19
+ [ " /settings" , " settings" ] ,
20
+ [ " /offline" , " offline" ] ,
21
+ [ " /about" , " about" ] ,
22
+ [ " /news" , " news" ] ,
23
+ [ " /help" , " help" ] ,
24
+ [ " /:doc-:type/" , " type" ] ,
25
+ [ " /:doc/" , " doc" ] ,
26
+ [ " /:doc/:path(*)" , " entry" ] ,
27
+ [ "*" , " notFound" ] ,
28
28
] ;
29
29
}
30
30
@@ -45,16 +45,16 @@ const Cls = (app.Router = class Router {
45
45
46
46
triggerRoute ( name ) {
47
47
this . trigger ( name , this . context ) ;
48
- this . trigger ( ' after' , name , this . context ) ;
48
+ this . trigger ( " after" , name , this . context ) ;
49
49
}
50
50
51
51
before ( context , next ) {
52
52
let res ;
53
53
const previousContext = this . context ;
54
54
this . context = context ;
55
- this . trigger ( ' before' , context ) ;
55
+ this . trigger ( " before" , context ) ;
56
56
57
- if ( res = next ( ) ) {
57
+ if ( ( res = next ( ) ) ) {
58
58
this . context = previousContext ;
59
59
return res ;
60
60
} else {
@@ -64,10 +64,14 @@ const Cls = (app.Router = class Router {
64
64
65
65
doc ( context , next ) {
66
66
let doc ;
67
- if ( doc = app . docs . findBySlug ( context . params . doc ) || app . disabledDocs . findBySlug ( context . params . doc ) ) {
67
+ if (
68
+ ( doc =
69
+ app . docs . findBySlug ( context . params . doc ) ||
70
+ app . disabledDocs . findBySlug ( context . params . doc ) )
71
+ ) {
68
72
context . doc = doc ;
69
73
context . entry = doc . toEntry ( ) ;
70
- this . triggerRoute ( ' entry' ) ;
74
+ this . triggerRoute ( " entry" ) ;
71
75
return ;
72
76
} else {
73
77
return next ( ) ;
@@ -78,10 +82,13 @@ const Cls = (app.Router = class Router {
78
82
let type ;
79
83
const doc = app . docs . findBySlug ( context . params . doc ) ;
80
84
81
- if ( type = doc != null ? doc . types . findBy ( 'slug' , context . params . type ) : undefined ) {
85
+ if (
86
+ ( type =
87
+ doc != null ? doc . types . findBy ( "slug" , context . params . type ) : undefined )
88
+ ) {
82
89
context . doc = doc ;
83
90
context . type = type ;
84
- this . triggerRoute ( ' type' ) ;
91
+ this . triggerRoute ( " type" ) ;
85
92
return ;
86
93
} else {
87
94
return next ( ) ;
@@ -91,111 +98,144 @@ const Cls = (app.Router = class Router {
91
98
entry ( context , next ) {
92
99
let entry ;
93
100
const doc = app . docs . findBySlug ( context . params . doc ) ;
94
- if ( ! doc ) { return next ( ) ; }
95
- let {
96
- path
97
- } = context . params ;
98
- const {
99
- hash
100
- } = context ;
101
-
102
- if ( entry = doc . findEntryByPathAndHash ( path , hash ) ) {
101
+ if ( ! doc ) {
102
+ return next ( ) ;
103
+ }
104
+ let { path } = context . params ;
105
+ const { hash } = context ;
106
+
107
+ if ( ( entry = doc . findEntryByPathAndHash ( path , hash ) ) ) {
103
108
context . doc = doc ;
104
109
context . entry = entry ;
105
- this . triggerRoute ( ' entry' ) ;
110
+ this . triggerRoute ( " entry" ) ;
106
111
return ;
107
- } else if ( path . slice ( - 6 ) === ' /index' ) {
112
+ } else if ( path . slice ( - 6 ) === " /index" ) {
108
113
path = path . substr ( 0 , path . length - 6 ) ;
109
- if ( entry = doc . findEntryByPathAndHash ( path , hash ) ) { return entry . fullPath ( ) ; }
114
+ if ( ( entry = doc . findEntryByPathAndHash ( path , hash ) ) ) {
115
+ return entry . fullPath ( ) ;
116
+ }
110
117
} else {
111
118
path = `${ path } /index` ;
112
- if ( entry = doc . findEntryByPathAndHash ( path , hash ) ) { return entry . fullPath ( ) ; }
119
+ if ( ( entry = doc . findEntryByPathAndHash ( path , hash ) ) ) {
120
+ return entry . fullPath ( ) ;
121
+ }
113
122
}
114
123
115
124
return next ( ) ;
116
125
}
117
126
118
127
root ( ) {
119
- if ( app . isSingleDoc ( ) ) { return '/' ; }
120
- this . triggerRoute ( 'root' ) ;
128
+ if ( app . isSingleDoc ( ) ) {
129
+ return "/" ;
130
+ }
131
+ this . triggerRoute ( "root" ) ;
121
132
}
122
133
123
134
settings ( context ) {
124
- if ( app . isSingleDoc ( ) ) { return `/#/${ context . path } ` ; }
125
- this . triggerRoute ( 'settings' ) ;
135
+ if ( app . isSingleDoc ( ) ) {
136
+ return `/#/${ context . path } ` ;
137
+ }
138
+ this . triggerRoute ( "settings" ) ;
126
139
}
127
140
128
- offline ( context ) {
129
- if ( app . isSingleDoc ( ) ) { return `/#/${ context . path } ` ; }
130
- this . triggerRoute ( 'offline' ) ;
141
+ offline ( context ) {
142
+ if ( app . isSingleDoc ( ) ) {
143
+ return `/#/${ context . path } ` ;
144
+ }
145
+ this . triggerRoute ( "offline" ) ;
131
146
}
132
147
133
148
about ( context ) {
134
- if ( app . isSingleDoc ( ) ) { return `/#/${ context . path } ` ; }
135
- context . page = 'about' ;
136
- this . triggerRoute ( 'page' ) ;
149
+ if ( app . isSingleDoc ( ) ) {
150
+ return `/#/${ context . path } ` ;
151
+ }
152
+ context . page = "about" ;
153
+ this . triggerRoute ( "page" ) ;
137
154
}
138
155
139
156
news ( context ) {
140
- if ( app . isSingleDoc ( ) ) { return `/#/${ context . path } ` ; }
141
- context . page = 'news' ;
142
- this . triggerRoute ( 'page' ) ;
157
+ if ( app . isSingleDoc ( ) ) {
158
+ return `/#/${ context . path } ` ;
159
+ }
160
+ context . page = "news" ;
161
+ this . triggerRoute ( "page" ) ;
143
162
}
144
163
145
164
help ( context ) {
146
- if ( app . isSingleDoc ( ) ) { return `/#/${ context . path } ` ; }
147
- context . page = 'help' ;
148
- this . triggerRoute ( 'page' ) ;
165
+ if ( app . isSingleDoc ( ) ) {
166
+ return `/#/${ context . path } ` ;
167
+ }
168
+ context . page = "help" ;
169
+ this . triggerRoute ( "page" ) ;
149
170
}
150
171
151
172
notFound ( context ) {
152
- this . triggerRoute ( ' notFound' ) ;
173
+ this . triggerRoute ( " notFound" ) ;
153
174
}
154
175
155
176
isIndex ( ) {
156
- return ( ( this . context != null ? this . context . path : undefined ) === '/' ) || ( app . isSingleDoc ( ) && __guard__ ( this . context != null ? this . context . entry : undefined , x => x . isIndex ( ) ) ) ;
177
+ return (
178
+ ( this . context != null ? this . context . path : undefined ) === "/" ||
179
+ ( app . isSingleDoc ( ) &&
180
+ __guard__ ( this . context != null ? this . context . entry : undefined , ( x ) =>
181
+ x . isIndex ( ) ,
182
+ ) )
183
+ ) ;
157
184
}
158
185
159
186
isSettings ( ) {
160
- return ( this . context != null ? this . context . path : undefined ) === '/settings' ;
187
+ return (
188
+ ( this . context != null ? this . context . path : undefined ) === "/settings"
189
+ ) ;
161
190
}
162
191
163
192
setInitialPath ( ) {
164
193
// Remove superfluous forward slashes at the beginning of the path
165
194
let path ;
166
- if ( ( path = location . pathname . replace ( / ^ \/ { 2 , } / g, '/' ) ) !== location . pathname ) {
195
+ if (
196
+ ( path = location . pathname . replace ( / ^ \/ { 2 , } / g, "/" ) ) !== location . pathname
197
+ ) {
167
198
page . replace ( path + location . search + location . hash , null , true ) ;
168
199
}
169
200
170
- if ( location . pathname === '/' ) {
171
- if ( path = this . getInitialPathFromHash ( ) ) {
201
+ if ( location . pathname === "/" ) {
202
+ if ( ( path = this . getInitialPathFromHash ( ) ) ) {
172
203
page . replace ( path + location . search , null , true ) ;
173
- } else if ( path = this . getInitialPathFromCookie ( ) ) {
204
+ } else if ( ( path = this . getInitialPathFromCookie ( ) ) ) {
174
205
page . replace ( path + location . search + location . hash , null , true ) ;
175
206
}
176
207
}
177
208
}
178
209
179
210
getInitialPathFromHash ( ) {
180
211
try {
181
- return __guard__ ( ( new RegExp ( "#/(.+)" ) ) . exec ( decodeURIComponent ( location . hash ) ) , x => x [ 1 ] ) ;
212
+ return __guard__ (
213
+ new RegExp ( "#/(.+)" ) . exec ( decodeURIComponent ( location . hash ) ) ,
214
+ ( x ) => x [ 1 ] ,
215
+ ) ;
182
216
} catch ( error ) { }
183
217
}
184
218
185
219
getInitialPathFromCookie ( ) {
186
220
let path ;
187
- if ( path = Cookies . get ( ' initial_path' ) ) {
188
- Cookies . expire ( ' initial_path' ) ;
221
+ if ( ( path = Cookies . get ( " initial_path" ) ) ) {
222
+ Cookies . expire ( " initial_path" ) ;
189
223
return path ;
190
224
}
191
225
}
192
226
193
227
replaceHash ( hash ) {
194
- page . replace ( location . pathname + location . search + ( hash || '' ) , null , true ) ;
228
+ page . replace (
229
+ location . pathname + location . search + ( hash || "" ) ,
230
+ null ,
231
+ true ,
232
+ ) ;
195
233
}
196
234
} ) ;
197
235
Cls . initClass ( ) ;
198
236
199
237
function __guard__ ( value , transform ) {
200
- return ( typeof value !== 'undefined' && value !== null ) ? transform ( value ) : undefined ;
201
- }
238
+ return typeof value !== "undefined" && value !== null
239
+ ? transform ( value )
240
+ : undefined ;
241
+ }
0 commit comments