@@ -4,61 +4,56 @@ import { noop } from '../util/core';
4
4
import { getAndActive } from '../event/sidebar' ;
5
5
import { get } from './ajax' ;
6
6
7
- function loadNested ( path , qs , file , next , vm , first ) {
8
- path = first ? path : path . replace ( / \/ $ / , '' ) ;
9
- path = getParentPath ( path ) ;
10
-
11
- if ( ! path ) {
12
- return ;
13
- }
14
-
15
- get (
16
- vm . router . getFile ( path + file ) + qs ,
17
- false ,
18
- vm . config . requestHeaders
19
- ) . then ( next , _ => loadNested ( path , qs , file , next , vm ) ) ;
20
- }
21
-
22
- let last ;
23
-
24
- const abort = ( ) => last && last . abort && last . abort ( ) ;
25
- const request = ( url , hasbar , requestHeaders ) => {
26
- abort ( ) ;
27
- last = get ( url , true , requestHeaders ) ;
28
- return last ;
29
- } ;
30
-
31
- const get404Path = ( path , config ) => {
32
- const { notFoundPage, ext } = config ;
33
- const defaultPath = '_404' + ( ext || '.md' ) ;
34
- let key ;
35
- let path404 ;
36
-
37
- switch ( typeof notFoundPage ) {
38
- case 'boolean' :
39
- path404 = defaultPath ;
40
- break ;
41
- case 'string' :
42
- path404 = notFoundPage ;
43
- break ;
7
+ export function fetchMixin ( Base = class { } ) {
8
+ return class extends Base {
9
+ constructor ( ) {
10
+ super ( ) ;
11
+ this . _lastRequest = null ;
12
+ }
44
13
45
- case 'object' :
46
- key = Object . keys ( notFoundPage )
47
- . sort ( ( a , b ) => b . length - a . length )
48
- . find ( key => path . match ( new RegExp ( '^' + key ) ) ) ;
14
+ _abort ( ) {
15
+ return (
16
+ this . _lastRequest &&
17
+ this . _lastRequest . abort &&
18
+ this . _lastRequest . abort ( )
19
+ ) ;
20
+ }
49
21
50
- path404 = ( key && notFoundPage [ key ] ) || defaultPath ;
51
- break ;
22
+ _request ( url , hasbar , requestHeaders ) {
23
+ this . _abort ( ) ;
24
+ this . _lastRequest = get ( url , true , requestHeaders ) ;
25
+ return this . _lastRequest ;
26
+ }
52
27
53
- default :
54
- break ;
55
- }
28
+ _get404Path ( path , config ) {
29
+ const { notFoundPage, ext } = config ;
30
+ const defaultPath = '_404' + ( ext || '.md' ) ;
31
+ let key ;
32
+ let path404 ;
33
+
34
+ switch ( typeof notFoundPage ) {
35
+ case 'boolean' :
36
+ path404 = defaultPath ;
37
+ break ;
38
+ case 'string' :
39
+ path404 = notFoundPage ;
40
+ break ;
41
+
42
+ case 'object' :
43
+ key = Object . keys ( notFoundPage )
44
+ . sort ( ( a , b ) => b . length - a . length )
45
+ . find ( key => path . match ( new RegExp ( '^' + key ) ) ) ;
46
+
47
+ path404 = ( key && notFoundPage [ key ] ) || defaultPath ;
48
+ break ;
49
+
50
+ default :
51
+ break ;
52
+ }
56
53
57
- return path404 ;
58
- } ;
54
+ return path404 ;
55
+ }
59
56
60
- export function fetchMixin ( Base = class { } ) {
61
- return class extends Base {
62
57
_loadSideAndNav ( path , qs , loadSidebar , cb ) {
63
58
return ( ) => {
64
59
if ( ! loadSidebar ) {
@@ -71,7 +66,7 @@ export function fetchMixin(Base = class {}) {
71
66
} ;
72
67
73
68
// Load sidebar
74
- loadNested ( path , qs , loadSidebar , fn , this , true ) ;
69
+ this . _loadNested ( path , qs , loadSidebar , fn , true ) ;
75
70
} ;
76
71
}
77
72
@@ -82,7 +77,7 @@ export function fetchMixin(Base = class {}) {
82
77
// Abort last request
83
78
84
79
const file = this . router . getFile ( path ) ;
85
- const req = request ( file + qs , true , requestHeaders ) ;
80
+ const req = this . _request ( file + qs , true , requestHeaders ) ;
86
81
87
82
// Current page is html
88
83
this . isHTML = / \. h t m l $ / g. test ( file ) ;
@@ -102,16 +97,30 @@ export function fetchMixin(Base = class {}) {
102
97
103
98
// Load nav
104
99
loadNavbar &&
105
- loadNested (
100
+ this . _loadNested (
106
101
path ,
107
102
qs ,
108
103
loadNavbar ,
109
104
text => this . _renderNav ( text ) ,
110
- this ,
111
105
true
112
106
) ;
113
107
}
114
108
109
+ _loadNested ( path , qs , file , next , first ) {
110
+ path = first ? path : path . replace ( / \/ $ / , '' ) ;
111
+ path = getParentPath ( path ) ;
112
+
113
+ if ( ! path ) {
114
+ return ;
115
+ }
116
+
117
+ get (
118
+ this . router . getFile ( path + file ) + qs ,
119
+ false ,
120
+ this . config . requestHeaders
121
+ ) . then ( next , _ => this . _loadNested ( path , qs , file , next ) ) ;
122
+ }
123
+
115
124
_fetchCover ( ) {
116
125
const { coverpage, requestHeaders } = this . config ;
117
126
const query = this . route . query ;
@@ -180,7 +189,7 @@ export function fetchMixin(Base = class {}) {
180
189
}
181
190
182
191
const newPath = path . replace ( new RegExp ( `^/${ local } ` ) , '' ) ;
183
- const req = request ( newPath + qs , true , requestHeaders ) ;
192
+ const req = this . _request ( newPath + qs , true , requestHeaders ) ;
184
193
185
194
req . then (
186
195
( text , opt ) =>
@@ -208,9 +217,9 @@ export function fetchMixin(Base = class {}) {
208
217
209
218
const fnLoadSideAndNav = this . _loadSideAndNav ( path , qs , loadSidebar , cb ) ;
210
219
if ( notFoundPage ) {
211
- const path404 = get404Path ( path , this . config ) ;
220
+ const path404 = this . _get404Path ( path , this . config ) ;
212
221
213
- request ( this . router . getFile ( path404 ) , true , requestHeaders ) . then (
222
+ this . _request ( this . router . getFile ( path404 ) , true , requestHeaders ) . then (
214
223
( text , opt ) => this . _renderMain ( text , opt , fnLoadSideAndNav ) ,
215
224
( ) => this . _renderMain ( null , { } , fnLoadSideAndNav )
216
225
) ;
@@ -220,24 +229,24 @@ export function fetchMixin(Base = class {}) {
220
229
this . _renderMain ( null , { } , fnLoadSideAndNav ) ;
221
230
return false ;
222
231
}
223
- } ;
224
- }
225
232
226
- export function initFetch ( vm ) {
227
- const { loadSidebar } = vm . config ;
233
+ initFetch ( ) {
234
+ const { loadSidebar } = this . config ;
228
235
229
- // Server-Side Rendering
230
- if ( vm . rendered ) {
231
- const activeEl = getAndActive ( vm . router , '.sidebar-nav' , true , true ) ;
232
- if ( loadSidebar && activeEl ) {
233
- activeEl . parentNode . innerHTML += window . __SUB_SIDEBAR__ ;
234
- }
236
+ // Server-Side Rendering
237
+ if ( this . rendered ) {
238
+ const activeEl = getAndActive ( this . router , '.sidebar-nav' , true , true ) ;
239
+ if ( loadSidebar && activeEl ) {
240
+ activeEl . parentNode . innerHTML += window . __SUB_SIDEBAR__ ;
241
+ }
235
242
236
- vm . _bindEventOnRendered ( activeEl ) ;
237
- vm . $resetEvents ( ) ;
238
- vm . callHook ( 'doneEach' ) ;
239
- vm . callHook ( 'ready' ) ;
240
- } else {
241
- vm . $fetch ( _ => vm . callHook ( 'ready' ) ) ;
242
- }
243
+ this . _bindEventOnRendered ( activeEl ) ;
244
+ this . $resetEvents ( ) ;
245
+ this . callHook ( 'doneEach' ) ;
246
+ this . callHook ( 'ready' ) ;
247
+ } else {
248
+ this . $fetch ( _ => this . callHook ( 'ready' ) ) ;
249
+ }
250
+ }
251
+ } ;
243
252
}
0 commit comments