-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroute.js
209 lines (174 loc) · 6.55 KB
/
route.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const loading_animation_html = `
<div class="text-center align-items-center">
<div class="spinner-border text-secondary" role="status" style="width: 10rem; height: 10rem;">
<span class="visually-hidden">Loading...</span>
</div>
</div>
`;
function sameOrigin(a, b) {
// https://stackoverflow.com/questions/31374766/javascript-how-to-check-if-a-url-is-same-origin-as-current-page
const urlA = new URL(a);
const urlB = new URL(b);
return urlA.origin === urlB.origin;
}
function get_form_data(obj) {
const fd = new FormData();
for (let i in obj) {
append_form_data(obj[i], fd, i)
}
return fd
}
function append_form_data(data, form_data, key) {
if ((typeof data === 'object' && data !== null && !(data instanceof File)) || Array.isArray(data)) {
for (const i in data) {
if ((typeof data[i] === 'object' && data[i] !== null) || Array.isArray(data[i])) {
append_form_data(data[i], form_data, key + `[${i}]`)
} else {
form_data.append(key + `[${i}]`, data[i])
}
}
} else {
form_data.append(key, data)
}
}
function post(url, form_data) {
let res = fetch(url, {
method: 'POST',
body: get_form_data(form_data)
});
return res;
}
function get(url) {
let res = fetch(url, {
method: 'GET'
});
return res;
}
function run_script(script) {
// https://www.cnblogs.com/libin-1/p/6565458.html
const new_script = document.createElement('script');
new_script.innerHTML = script.innerHTML;
const src = script.getAttribute('src');
if (src) {
new_script.setAttribute('src', src);
}
document.head.appendChild(new_script);
document.head.removeChild(new_script);
}
var route = new function() {
this.session_id = null;
this.curr_url = null;
this.prev_url = null;
this.main_navbar = null;
this.init_ui = function() {
let session_id = document.getElementById("indexjs").getAttribute('session_id');
this.main_navbar = document.getElementById('main-navbar');
this.main_navbar.querySelector('.nav-link.logout').addEventListener('click', () => {
post('/board/be/login', {
reqtype: 'logout',
session_id: session_id,
}).then(_ => {
location.href = '/board/info/';
});
});
if (session_id != '') {
this.session_id = parseInt(session_id);
this.main_navbar.querySelector('.nav-link.logout').style.display = 'block';
} else {
this.main_navbar.querySelector('.nav-link.login').style.display = 'block';
}
}
this.go = (url) => {
window.history.pushState(null, document.title, url);
this.update(1);
}
this.reload = () => {
this.update(1);
}
this.update = function(mode) {
function PoPState() {
this.prev_url = location.href;
let parts = location.href.split('/');
let page = parts[4];
if (page === undefined || page.length === 0 || page === 'index') {
page = 'info';
}
let req_path = parts[4];
for (let i = 5; i < parts.length - 1; i++) {
req_path += `/${parts[i]}`;
}
if (page === 'info') {
req_path = 'info';
}
let args = '';
parts = parts[parts.length - 1].match(/\?([^#]+)/);
if (parts == null) {
args = `cache=${new Date().getTime()}`;
} else {
args = `${parts[1]}&cache=${new Date().getTime()}`
}
routerView.innerHTML = loading_animation_html;
let request_url = `/board/be/${req_path}?${args}`;
get(request_url).then(response => {
if (!response.ok) return "";
return response.text();
}).then(html => {
let callback = () => {
if (page.length !== 0) {
// There will only be one li.page, so we can just use querySelector to change classlist of a.
// If this code throw exception, your code must have bug.
let node = null;
node = route.main_navbar.querySelector('li > a.active');
if (node !== null) node.classList.remove('active');
node = route.main_navbar.querySelector(`li.${page} > a`);
if (node !== null) node.classList.add('active');
}
routerView.querySelectorAll('a[href]').forEach(el => {
el.addEventListener('click', event => {
if (sameOrigin(el.getAttribute('href'), localStorage.href)) {
event.preventDefault();
history.pushState(null, '', el.getAttribute('href'));
PoPState();
}
});
});
const scripts = routerView.querySelectorAll('script');
for (let script of scripts) {
run_script(script);
}
};
routerView.innerHTML = html;
if (document.readyState === 'complete' || document.readyState !== 'loading') {
setTimeout(function() {
callback();
}, 0);
} else {
let handler = function() {
document.removeEventListener('DOMContentLoaded', handler, false);
callback();
}
document.addEventListener('DOMContentLoaded', handler, false);
}
});
}
let routerView = document.getElementById('routerView');
if (mode == 1) {
PoPState();
return;
}
document.addEventListener('DOMContentLoaded', onLoad);
window.addEventListener('popstate', PoPState);
function onLoad() {
PoPState();
let links = document.querySelectorAll('li a[href]');
links.forEach(link => {
link.addEventListener('click', event => {
event.preventDefault();
history.pushState(null, '', link.getAttribute('href'))
PoPState()
})
})
}
onLoad();
}
}