-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
55 lines (44 loc) · 1.13 KB
/
router.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
export class Router {
static #router = null;
constructor(default_page = "") {
if (Router.#router != null) return Router.#router;
Router.#router = this;
this.current_page = default_page;
this.pages = [];
window.addEventListener("hashchange", this.#pageChange);
}
addPages(...ids) {
ids.forEach((p) => this.pages.push(p));
}
#pageChange = (e) => {
const page_id = location.hash.replace("#", "");
this.render(page_id);
};
render(page_id) {
if (!this.pages.includes(page_id)) return;
for (let p of this.pages) {
const p_div = document.querySelector(`#${p}`);
if (p_div) p_div.style.display = p === page_id ? "" : "none";
}
}
goTo(page_id) {
location.hash === `#${page_id}`
? this.render(page_id)
: (location.hash = page_id);
}
}
export class PageBase {
router = new Router();
#pid;
page_name;
constructor(page_name) {
this.#pid = page_name;
this.page_name = page_name;
this.router.addPages(this.#pid);
//Convert page name into CSS id format
this.dom = new DOM(`#${this.#pid}`);
}
show() {
this.router.goTo(this.#pid);
}
}