forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (55 loc) · 1.52 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
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';
/**
* @typedef {{
* path?: string
* }} Route
*/
/** @type {Route} */
let lastRoute = {};
/** @typedef {import('../Docsify').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Router(Base) {
return class Router extends Base {
/** @param {any[]} args */
constructor(...args) {
super(...args);
this.route = {};
}
updateRender() {
this.router.normalize();
this.route = this.router.parse();
dom.body.setAttribute('data-page', this.route.file);
}
initRouter() {
const config = this.config;
const mode = config.routerMode || 'hash';
let router;
if (mode === 'history' && supportsPushState) {
router = new HTML5History(config);
} else {
router = new HashHistory(config);
}
this.router = router;
this.updateRender();
lastRoute = this.route;
// eslint-disable-next-line no-unused-vars
router.onchange(params => {
this.updateRender();
this._updateRender();
if (lastRoute.path === this.route.path) {
this.$resetEvents(params.source);
return;
}
this.$fetch(noop, this.$resetEvents.bind(this, params.source));
lastRoute = this.route;
});
}
};
}