forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (95 loc) Β· 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { get } from './ajax'
import { callHook } from '../init/lifecycle'
import { getParentPath, stringifyQuery } from '../router/util'
import { noop } from '../util/core'
import { getAndActive } from '../event/sidebar'
function loadNested (path, qs, file, next, vm, first) {
path = first ? path : path.replace(/\/$/, '')
path = getParentPath(path)
if (!path) return
get(vm.router.getFile(path + file) + qs).then(next, _ =>
loadNested(path, qs, file, next, vm)
)
}
export function fetchMixin (proto) {
let last
proto._fetch = function (cb = noop) {
const { path, query } = this.route
const qs = stringifyQuery(query, ['id'])
const { loadNavbar, loadSidebar } = this.config
// Abort last request
last && last.abort && last.abort()
last = get(this.router.getFile(path) + qs, true)
// Current page is html
this.isHTML = /\.html$/g.test(path)
const loadSideAndNav = () => {
if (!loadSidebar) return cb()
const fn = result => {
this._renderSidebar(result)
cb()
}
// Load sidebar
loadNested(path, qs, loadSidebar, fn, this, true)
}
// Load main content
last.then(
(text, opt) => {
this._renderMain(text, opt)
loadSideAndNav()
},
_ => {
this._renderMain(null)
loadSideAndNav()
}
)
// Load nav
loadNavbar &&
loadNested(
path,
qs,
loadNavbar,
text => this._renderNav(text),
this,
true
)
}
proto._fetchCover = function () {
const { coverpage } = this.config
const query = this.route.query
const root = getParentPath(this.route.path)
const path = this.router.getFile(root + coverpage)
if (this.route.path !== '/' || !coverpage) {
this._renderCover()
return
}
this.coverIsHTML = /\.html$/g.test(path)
get(path + stringifyQuery(query, ['id'])).then(text =>
this._renderCover(text)
)
}
proto.$fetch = function (cb = noop) {
this._fetchCover()
this._fetch(result => {
this.$resetEvents()
callHook(this, 'doneEach')
cb()
})
}
}
export function initFetch (vm) {
const { loadSidebar } = vm.config
// server-client renderer
if (vm.rendered) {
const activeEl = getAndActive(vm.router, '.sidebar-nav', true, true)
if (loadSidebar && activeEl) {
activeEl.parentNode.innerHTML += window.__SUB_SIDEBAR__
}
vm._bindEventOnRendered(activeEl)
vm._fetchCover()
vm.$resetEvents()
callHook(vm, 'doneEach')
callHook(vm, 'ready')
} else {
vm.$fetch(_ => callHook(vm, 'ready'))
}
}