-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (104 loc) · 3.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class View {
constructor(title, view_file, view_css=undefined) {
this.title = title;
this.view_file = view_file;
this.view_css = view_css;
}
setTitle() {
document.title = this.title;
}
async getHTML() {
let view_html = await (await fetch(this.view_file)).text();
return view_html;
}
async getCSS() {
let view_css = '';
if (this.view_css) {
view_css = await (await fetch(this.view_css)).text();
}
return view_css;
}
}
class Navigation {
constructor(routes) {
this.routes = routes;
}
async router() {
console.log(this.routes);
const potentialMatches = this.routes.map(route => {
return {
route: route,
isMatch: location.pathname === route.path
}
});
let match = potentialMatches.find(potentialMatch => potentialMatch.isMatch);
// handle 404.
if (!match) {
match = {
route: this.routes[0],
isMatch: true
}
this.navigate(match.route.path);
}
match.route.view.setTitle();
document.querySelector('#khonshu-view').innerHTML = await match.route.view.getCSS() + await match.route.view.getHTML();
await match.route.actions.map(async (action) => await action());
}
navigate(url) {
history.pushState(null, null, url);
this.router();
}
run() {
document.addEventListener('DOMContentLoaded', () => {
document.body.addEventListener('click', e => {
if (e.target.matches('[khonshu-link]')) {
e.preventDefault();
this.navigate(e.target.href);
} else if (e.target.parentElement.matches('[khonshu-link]')) {
e.preventDefault();
this.navigate(e.target.parentElement.href);
}
});
this.router();
});
window.onpopstate = this.router;
}
}
class ChangeThemer {
constructor(light_icon, dark_icon) {
this.dark = 'dark';
this.light = 'light';
this.key_name = 'theme';
this.body = document.querySelector('body');
this.icon = document.querySelector('#khonshu-theme-icon');
this.icon_name = {light: light_icon, dark: dark_icon};
}
load_theme() {
this.theme = localStorage.getItem(this.key_name);
if (this.theme == null) {
this.theme = this.body.getAttribute(this.key_name);
localStorage.setItem(this.key_name, this.theme);
}
this.body.setAttribute(this.key_name, this.theme);
this.icon.className = this.icon_name[this.theme];
}
change_theme() {
if (this.theme === this.dark) {
localStorage.setItem(this.key_name, this.light);
} else {
localStorage.setItem(this.key_name, this.dark);
}
this.load_theme();
}
}
class ListRender {
render(data, target, template_handler, optional=null, show_spinner=false) {
data.forEach(async (item, idx) => {
const to_render = await template_handler(item, idx, optional);
if (to_render) {
target.appendChild(document.importNode(to_render.content, true));
}
});
}
}
export const Khonshu = {Navigation: Navigation, View: View, ChangeThemer: ChangeThemer, ListRender: ListRender};