83 lines
3.1 KiB
JavaScript
83 lines
3.1 KiB
JavaScript
function main(page) {
|
|
window.currPage = page;
|
|
window.currOpacity = 1;
|
|
document.getElementById("about-link").attributes.removeNamedItem("href");
|
|
document.getElementById("games-link").attributes.removeNamedItem("href");
|
|
document.getElementById("blog-link").attributes.removeNamedItem("href");
|
|
document.getElementById("about-link").onclick = function () { return openPage("about"); };
|
|
document.getElementById("games-link").onclick = function () { return openPage("games"); };
|
|
document.getElementById("blog-link").onclick = function () { return openPage("blog"); };
|
|
addBlogposts();
|
|
var search = window.location.search;
|
|
if (search != "") {
|
|
search = search.slice(1, -1);
|
|
var parts = search.split("=");
|
|
if (parts[0] == "id") {
|
|
setBlogText(parts[1] + ".html", function () { });
|
|
}
|
|
}
|
|
}
|
|
function openPage(page, new_url) {
|
|
if (new_url === void 0) { new_url = null; }
|
|
if (page == window.currPage) {
|
|
return;
|
|
}
|
|
slideOpacity(function () {
|
|
document.getElementById(window.currPage).className = "hiddenarea";
|
|
window.currPage = page;
|
|
document.getElementById(page).style.opacity = "0";
|
|
document.getElementById(page).className = "";
|
|
var target = new_url || page;
|
|
var targetPg = "/" + target;
|
|
if (target == "about") {
|
|
target = "";
|
|
targetPg = "";
|
|
}
|
|
;
|
|
window.history.pushState(target, target, targetPg + "/");
|
|
slideOpacity(function () {
|
|
}, 1.5);
|
|
}, -1.5);
|
|
}
|
|
function slideOpacity(callback, step) {
|
|
var currElem = document.getElementById(window.currPage);
|
|
var id = setInterval(function () {
|
|
window.currOpacity += 0.02 * step;
|
|
currElem.style.opacity = "" + window.currOpacity;
|
|
if ((window.currOpacity <= 0 && step < 0) || (window.currOpacity >= 1 && step > 0)) {
|
|
clearInterval(id);
|
|
callback();
|
|
}
|
|
}, 2);
|
|
}
|
|
function addBlogposts() {
|
|
window.blogpostData = JSON.parse(window.blogpostDataStr);
|
|
var div = document.getElementById("blog");
|
|
var blogpostData = window.blogpostData.sort(function (a, b) { return b.time - a.time; });
|
|
blogpostData.forEach(function (val) {
|
|
var title = document.createElement("h2");
|
|
title.innerHTML = val.title;
|
|
var timestamp = document.createElement("p");
|
|
timestamp.className += "timestamp";
|
|
timestamp.innerHTML = new Date(val.time).toUTCString();
|
|
title.appendChild(timestamp);
|
|
div.appendChild(title);
|
|
setBlogText(val.source, function () {
|
|
title.onclick = function () { return openPage("blogtext", "blogtext?id=" + val.source.slice(0, -5)); };
|
|
});
|
|
});
|
|
}
|
|
function setBlogText(source, callback) {
|
|
var xmlhttp = new XMLHttpRequest();
|
|
xmlhttp.onreadystatechange = function () {
|
|
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
|
|
if (xmlhttp.status == 200) {
|
|
document.getElementById("blogtext").innerHTML = xmlhttp.responseText;
|
|
callback();
|
|
}
|
|
}
|
|
};
|
|
xmlhttp.open("GET", "/blogposts/" + source, true);
|
|
xmlhttp.send();
|
|
}
|