forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (36 loc) · 923 Bytes
/
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
import { HashHistory } from './history/hash'
import { HTML5History } from './history/html5'
import { supportsPushState } from '../util/env'
import * as dom from '../util/dom'
export function routerMixin (proto) {
proto.route = {}
}
let lastRoute = {}
function updateRender (vm) {
vm.router.normalize()
vm.route = vm.router.parse()
dom.body.setAttribute('data-page', vm.route.file)
}
export function initRouter (vm) {
const config = vm.config
const mode = config.routerMode || 'hash'
let router
if (mode === 'history' && supportsPushState) {
router = new HTML5History(config)
} else {
router = new HashHistory(config)
}
vm.router = router
updateRender(vm)
lastRoute = vm.route
router.onchange(_ => {
updateRender(vm)
vm._updateRender()
if (lastRoute.path === vm.route.path) {
vm.$resetEvents()
return
}
vm.$fetch()
lastRoute = vm.route
})
}