This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
276 lines (239 loc) · 7.56 KB
/
common.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//#region Hamburger Menu
document.addEventListener("DOMContentLoaded", () => {
const leftHamMenuButton = document.getElementById("left-hamMenu-button"),
leftHamMenuBack = document.getElementById("left-hamMenu-back"),
rightHamMenuButton = document.getElementById("right-hamMenu-button"),
rightHamMenuBack = document.getElementById("right-hamMenu-back"),
HamMenu = document.getElementById("hamMenu");
function menuClick() {
this.classList.toggle("active");
((this === leftHamMenuButton) ? leftHamMenuBack : rightHamMenuBack).classList.toggle("active");
HamMenu.classList.toggle("showing");
}
leftHamMenuButton.addEventListener("click", menuClick);
rightHamMenuButton.addEventListener("click", menuClick);
(function () {
const height = 56;
let offset = 0,
lastPosition = 0,
ticking = false;
function onScroll() {
if (lastPosition > height) {
if (lastPosition > offset) {
leftHamMenuButton.classList.add("left-disappear-animation");
rightHamMenuButton.classList.add("right-disappear-animation");
leftHamMenuBack.classList.add("left-disappear-animation");
rightHamMenuBack.classList.add("right-disappear-animation");
console.log("disappear!");
} else {
leftHamMenuButton.classList.remove("left-disappear-animation");
rightHamMenuButton.classList.remove("right-disappear-animation");
leftHamMenuBack.classList.remove("left-disappear-animation");
rightHamMenuBack.classList.remove("right-disappear-animation");
console.log("appear!");
}
offset = lastPosition;
}
}
window.addEventListener('scroll', function () {
lastPosition = window.scrollY;
if (!ticking) {
window.requestAnimationFrame(function () {
onScroll(lastPosition);
ticking = false;
});
ticking = true;
}
});
})();
});
//#endregion
//#region Page Loader
document.addEventListener("DOMContentLoaded", () => loadPage(undefined, false));
window.addEventListener("popstate", () => loadPage(undefined, false));
// specific functions
/**
* @param {HashChangeEvent|undefined} ev
* @returns 普通の読み込み動作で事足りる場合には true が返ってきますので、うまい具合にしてください。
*/
function loadPage(url = location.href, shouldAddHistory = true) {
url = transURL(url, getBaseURL(location.href));
if (url.startsWith(location.origin)) {
const pageID = getURLQuery(url)["page"];
loadSpecificPage(pageID);
if (shouldAddHistory)
history.pushState(undefined, "", url);
return false;
} else {
return true;
}
}
function loadSpecificPage(pageID) {
console.groupCollapsed(`page loading`);
console.log(`query == '${pageID}'`);
if (!pageID) {
console.log("loading landing page");
loadArticle("pages/landing/article.html", "KSS PC Club / Menu");
} else if (pageID == "404") {
show404();
} else if (pageID.startsWith("works-")) {
const tmp = pageID.split("-");
tmp.shift();
const workID = tmp.join("-");
console.log(`loading works page associated to '${workID}'`);
loadArticle("pages/works/" + workID + ".html", loadWorksTitles(workID));
} else {
show404();
}
console.groupEnd();
}
const loadWorksTitles = (() => {
let map = undefined;
const promise = fetch("pages/works/index.json")
.then(res => res.json())
.then(json => {
map = json;
});
return (workID) => {
const getTitle = () => map[workID] && map[workID].title ? "KSS PC club / " + map[workID].title : undefined;
if (map) return Promise.resolve(getTitle());
else return promise.then(() => getTitle());
}
})();
function loadArticle(url, title) {
return fetch(url)
.then(res => {
if (res.ok) {
return res.text();
} else {
throw res.statusText;
}
})
.then(html => showArticle(html, getBaseURL(url), title))
.catch(err => {
console.error("@loadArticle", err);
show404();
})
}
function show404() {
document.getElementById("main-contents").classList.add("hide");
document.getElementById("error-page").classList.remove("hide");
}
/**
* @param {string} html
* @param {string} baseURL
* @param {string|Promise<string>} title
*/
function showArticle(html, baseURL, title) {
document.getElementById("main-contents").classList.remove("hide");
document.getElementById("error-page").classList.add("hide");
// Set Contents
/** @param {HTMLElement} elem */
const modifier = elem => elem.childNodes.forEach(child => {
const tag = child.tagName;
if (!tag) return; // text node
if (tag == "SCRIPT") {
const newElem = cloneHTMLElem(child); // これをしないとスクリプトが再度読み込みされないみたいなので。
elem.replaceChild(newElem, child);
child = newElem; // 次の処理のために。
}
const transAttrURL = attr => {
if (!child.getAttribute) return;
const attrValue = child.getAttribute(attr);
if (!attrValue) return;
child.setAttribute(attr, transURL(attrValue, baseURL));
}
transAttrURL("href");
transAttrURL("src");
if (tag == "A") {
const href = child.getAttribute("href");
if (href && !href.startsWith("#")) {
// child.setAttribute("href_url", href); // For debug
// child.setAttribute("href", "#");
child.addEventListener("click", e => {
if (!loadPage(href)) {
e.preventDefault();
}
});
}
}
modifier(child);
});
const div = document.createElement("div");
div.innerHTML = html;
modifier(div);
const container = document.getElementById("main-contents");
container.innerHTML = "";
div.childNodes.forEach(_ => container.appendChild(_));
// Set Title
const setTitle = title => {
if (title === undefined) {
console.warn("Page title was not provided.");
title = "KSS PC club";
}
document.title = title;
};
if (typeof title == "string" || typeof title == "undefined") {
setTitle(title);
} else { // title is a Promise
title.then(setTitle);
}
}
// generic functions
/**
* @param {HTMLElement} node
* @returns {HTMLElement}
*/
function cloneHTMLElem(node) {
const tag = node.tagName;
const newElem = document.createElement(tag);
for (let i = 0; i < child.attributes.length; i++) {
const attr = child.attributes.item(i);
newElem.setAttribute(attr.nodeName, attr.nodeValue);
}
newElem.innerHTML = child.innerHTML;
}
/**
* @param {string} url
* @param {string} baseURL
*/
function transURL(url, baseURL) {
if (!baseURL.endsWith("/")) {
console.warn("baseURLは、末尾に'/'がついていてほしい", "baseURL: " + baseURL);
baseURL += "/";
}
if (url.indexOf(":") >= 0) return url; // absolute path
if (url.startsWith("#")) return url; // hash
if (url.startsWith("/")) return "." + url; // absolute path
return baseURL + url;
}
/**
* https://hoge.com/fuga/hhu/piyo.html -> https://hoge.com/fuga/hhu/
* @param {string} url
*/
function getBaseURL(url) {
if (url.endsWith("/")) return url;
const frag = url.split("/");
frag.pop(); // filename
return frag.join("/") + "/";
}
function getURLQuery(url = location.href) {
let arg = {};
let pair = url2searchQuery(url).split('&');
pair.forEach((V) => {
let kv = V.split('=');
arg[kv[0]] = kv[1];
});
return arg;
}
function url2searchQuery(url) {
const tmp = url.split("#")[0].split("?");
tmp.shift();
return tmp.join("?");
}
//#endregion
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("global-header").addEventListener("click", () => {
loadPage(location.origin); // TODO: 正しいか?
});
});