forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.06 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
import { supportsPushState } from '../util/env';
import * as dom from '../util/dom';
import { noop } from '../util/core';
import { HashHistory } from './history/hash';
import { HTML5History } from './history/html5';
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;
// eslint-disable-next-line no-unused-vars
router.onchange(params => {
updateRender(vm);
vm._updateRender();
if (lastRoute.path === vm.route.path) {
vm.$resetEvents(params.source);
return;
}
vm.$fetch(noop, vm.$resetEvents.bind(vm, params.source));
lastRoute = vm.route;
});
}