website/assets/script/theme.js

77 lines
2.5 KiB
JavaScript

// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0
// Enable transitions between themes without reloading page
var Themes = {
AUTO: "auto",
LIGHT: "light",
DARK: "dark"
};
window.addEventListener("load", function() {
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
// Add trees
if (!document.querySelector(".tree")) {
for (var x = 0; x < width; x += 240) {
var left = x + Math.floor(Math.random() * 240 / 2);
var bottom = Math.floor(Math.random() * 150);
for (var i = 0; i < 2; i++) {
var theme = ["light", "dark"][i];
var img = document.createElement("img");
img.src = "/assets/img/tree_" + theme + (window.SVGElement ? ".svg" : ".png");
img.className = theme + " tree";
img.height = 300;
img.alt = "";
img.style.position = "fixed";
img.style.left = left + "px";
img.style.bottom = bottom + "px";
document.body.appendChild(img);
}
}
}
// Add click listener to theme buttons
var themeBtns = document.getElementsByClassName("theme-btn");
for (var i = 0; i < themeBtns.length; i++) {
themeBtns[i].onclick = function(e) {
var event = e || window.event;
var target = event.target || event.srcElement;
event.returnValue = false;
if (event.preventDefault) event.preventDefault();
// Ignore if already selected
if (target.className.indexOf("selected") > -1) return;
// Get new theme
var theme = target.getAttribute("data-theme");
// Apply theme changes
if (theme === Themes.AUTO) {
document.cookie = "theme=; expires=Tue, 19 Jan 2038 03:14:08 GMT";
} else {
document.cookie = "theme=" + theme + "; expires=Tue, 19 Jan 2038 03:14:08 GMT"
}
// Enable / disable the right stylesheets
var themeStyles = document.getElementsByClassName("theme-style");
for (var i = 0; i < themeStyles.length; i++) {
themeStyles[i].disabled = true;
if (themeStyles[i].getAttribute("data-theme") === theme) themeStyles[i].disabled = false;
}
// Force redrawing of containers because of a bug in IE8
var containers = document.getElementsByClassName("container");
for (var i = 0; i < containers.length; i++) containers[i].className += "";
}
}
}, false);
// @license-end