aboutsummaryrefslogtreecommitdiff
path: root/views/js/themes.js
blob: ccdd2774eb82d2db5f41e6bdb778fc01e07b3bd2 (plain)
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
function setCookie(key, value, age) {
	document.cookie = key + "=" + encodeURIComponent(value) + ";sameSite=strict;max-age=" + (60 * 60 * 24 * age) + ";path=/";
}

function getCookie(key) {
	if (document.cookie.length != 0) {
		return document.cookie.split('; ').find(row => row.startsWith(key)).split('=')[1];
	}
	return "";
}

function setTheme(name) {
	for (let i = 0, tags = document.getElementsByTagName("link"); i < tags.length; i++) {
		if (tags[i].type === "text/css" && tags[i].title) {
			tags[i].disabled = !(tags[i].title === name);
		}
	}

	setCookie("theme", name, 3650);
}

function applyTheme() {
	// HACK: disable all of the themes first. this for some reason makes things work.
	for (let i = 0, tags = document.getElementsByTagName("link"); i < tags.length; i++) {
		if (tags[i].type === "text/css" && tags[i].title) {
			tags[i].disabled = true;
		}
	}
	let theme = getCookie("theme") || "default";
	setTheme(theme);

	// reflect this in the switcher
	let switcher = document.getElementById("themeSwitcher");
	for(var i = 0; i < switcher.options.length; i++) {
		if (switcher.options[i].value === theme) {
			switcher.selectedIndex = i;
			break;
		}
	}
}