-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (58 loc) · 1.6 KB
/
index.js
File metadata and controls
72 lines (58 loc) · 1.6 KB
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
import * as dom from '../util/dom.js';
import { noop } from '../util/core.js';
import { HashHistory } from './history/hash.js';
import { HTML5History } from './history/html5.js';
/**
* @typedef {{
* path: string
* query: Record<string, string>;
* file: string;
* response: {}
* }} Route
*/
/** @type {Partial<Route>} */
let lastRoute = {};
/** @typedef {import('../Docsify.js').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Router(Base) {
return class Router extends Base {
/** @type {Partial<Route>} */
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') {
router = new HTML5History(config);
} else {
router = new HashHistory(config);
}
this.router = router;
this.updateRender();
lastRoute = this.route;
router.onchange(params => {
this.updateRender();
this._updateRender();
dom.restoreFocusAfterNavigation();
if (lastRoute.path === this.route.path) {
this.onNavigate(params.source);
return;
}
// Fetch new page content
this.$fetch(noop, () => {
this.onNavigate(params.source);
dom.restoreFocusAfterNavigation();
});
lastRoute = this.route;
});
}
};
}