-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
executable file
·69 lines (61 loc) · 1.78 KB
/
theme.js
File metadata and controls
executable file
·69 lines (61 loc) · 1.78 KB
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
let themeToggleButton = document.querySelector("#theme-toggle");
let stylesheetLink = document.querySelector("#stylesheet");
// name must equal the key
let themes = {
"light": {
"name": "light",
"href": "light.css",
},
"dark": {
"name": "dark",
"href": "dark-lite.css",
},
"hax": {
"name": "hax",
"href": "hax-lite.css"
},
"wack": {
"name": "wack",
"href": "wack-lite.css"
}
}
let themeKeys = Object.keys(themes);
let currentTheme = themes[getThemeNameFromStorage()];
window.addEventListener("DOMContentLoaded", displayInitTheme);
function displayInitTheme() {
console.log(currentTheme);
stylesheetLink.setAttribute("href", `css/${currentTheme.href}`);
let nextIndex = (themeKeys.indexOf(currentTheme.name) + 1) % themeKeys.length;
console.log(nextIndex);
themeToggleButton.innerHTML = themes[themeKeys[nextIndex]].name;
}
themeToggleButton.addEventListener("click", function() {
let nextIndex = (themeKeys.indexOf(currentTheme.name) + 1) % themeKeys.length;
currentTheme = themes[themeKeys[nextIndex]];
console.log(currentTheme);
updateThemeFromStorage(currentTheme);
this.innerHTML = themes[themeKeys[(nextIndex + 1) % themeKeys.length]].name;
stylesheetLink.setAttribute("href", `css/${currentTheme.href}`);
});
function getThemeNameFromStorage() {
if (typeof(Storage) !== "undefined") {
if (localStorage.themeName) {
return localStorage.themeName;
}
else {
localStorage.setItem("themeName", themes.light.name);
return localStorage.themeName;
}
}
else {
console.log("No web storage support");
}
}
function updateThemeFromStorage(ctheme) {
if (typeof(Storage) !== "undefined") {
localStorage.setItem("themeName", ctheme.name);
}
else {
console.log("No web storage support");
}
}