2023-06-02 21:15:10 +02:00
|
|
|
var toggleCount = 0;
|
|
|
|
var lastToggleTime = Date.now();
|
|
|
|
|
2023-08-19 17:59:34 +02:00
|
|
|
var elements = {
|
|
|
|
lightModeStyles: null,
|
|
|
|
darkModeStyles: null,
|
|
|
|
rainbowModeStyles: null,
|
|
|
|
darkModeIcon: null
|
|
|
|
};
|
|
|
|
|
|
|
|
function getElements() {
|
|
|
|
elements.lightModeStyles = document.getElementById("light-mode-styles");
|
|
|
|
elements.darkModeStyles = document.getElementById("dark-mode-styles");
|
|
|
|
elements.rainbowModeStyles = document.getElementById("rainbow-mode-styles");
|
|
|
|
elements.darkModeIcon = document.getElementById("dark-mode-icon");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setMode(mode) {
|
2023-08-28 01:49:14 +02:00
|
|
|
var event = new CustomEvent('modeChanged', { detail: mode });
|
|
|
|
document.dispatchEvent(event);
|
2023-08-19 17:59:34 +02:00
|
|
|
elements.lightModeStyles.disabled = mode !== "off";
|
|
|
|
elements.darkModeStyles.disabled = mode !== "on";
|
|
|
|
elements.rainbowModeStyles.disabled = mode !== "rainbow";
|
2023-08-27 20:58:20 +02:00
|
|
|
var jumbotron = document.getElementById('jumbotron');
|
2023-08-19 17:59:34 +02:00
|
|
|
if (mode === "on") {
|
|
|
|
elements.darkModeIcon.src = "moon.svg";
|
|
|
|
// Add the table-dark class to tables for dark mode
|
|
|
|
var tables = document.querySelectorAll('.table');
|
|
|
|
tables.forEach(table => {
|
|
|
|
table.classList.add('table-dark');
|
|
|
|
});
|
2023-08-27 20:58:20 +02:00
|
|
|
if(jumbotron) {
|
|
|
|
jumbotron.classList.add('bg-dark');
|
|
|
|
jumbotron.classList.remove('bg-light');
|
|
|
|
}
|
2023-08-19 17:59:34 +02:00
|
|
|
} else if (mode === "off") {
|
|
|
|
elements.darkModeIcon.src = "sun.svg";
|
|
|
|
// Remove the table-dark class for light mode
|
|
|
|
var tables = document.querySelectorAll('.table-dark');
|
|
|
|
tables.forEach(table => {
|
|
|
|
table.classList.remove('table-dark');
|
|
|
|
});
|
2023-08-27 20:58:20 +02:00
|
|
|
if(jumbotron){
|
|
|
|
console.log(mode)
|
|
|
|
jumbotron.classList.remove('bg-dark');
|
|
|
|
jumbotron.classList.add('bg-light');
|
|
|
|
}
|
2023-08-19 17:59:34 +02:00
|
|
|
} else if (mode === "rainbow") {
|
|
|
|
elements.darkModeIcon.src = "rainbow.svg";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 21:15:10 +02:00
|
|
|
function toggleDarkMode() {
|
|
|
|
var currentTime = Date.now();
|
|
|
|
if (currentTime - lastToggleTime < 1000) {
|
|
|
|
toggleCount++;
|
|
|
|
} else {
|
|
|
|
toggleCount = 1;
|
|
|
|
}
|
|
|
|
lastToggleTime = currentTime;
|
|
|
|
|
|
|
|
if (toggleCount >= 18) {
|
|
|
|
localStorage.setItem("dark-mode", "rainbow");
|
2023-08-19 17:59:34 +02:00
|
|
|
setMode("rainbow");
|
2023-06-02 21:15:10 +02:00
|
|
|
} else if (localStorage.getItem("dark-mode") == "on") {
|
|
|
|
localStorage.setItem("dark-mode", "off");
|
2023-08-19 17:59:34 +02:00
|
|
|
setMode("off");
|
2023-06-02 21:15:10 +02:00
|
|
|
} else {
|
|
|
|
localStorage.setItem("dark-mode", "on");
|
2023-08-19 17:59:34 +02:00
|
|
|
setMode("on");
|
2023-06-02 21:15:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
2023-08-19 17:59:34 +02:00
|
|
|
getElements();
|
2023-06-02 21:15:10 +02:00
|
|
|
|
2023-08-19 17:59:34 +02:00
|
|
|
var currentMode = localStorage.getItem("dark-mode");
|
|
|
|
if (currentMode === "on" || currentMode === "off" || currentMode === "rainbow") {
|
|
|
|
setMode(currentMode);
|
|
|
|
} else if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
|
|
setMode("on");
|
2023-06-02 21:15:10 +02:00
|
|
|
} else {
|
2023-08-19 17:59:34 +02:00
|
|
|
setMode("off");
|
2023-06-02 21:15:10 +02:00
|
|
|
}
|
2023-08-27 20:58:20 +02:00
|
|
|
|
2023-06-02 21:15:10 +02:00
|
|
|
document.getElementById("dark-mode-toggle").addEventListener("click", function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
toggleDarkMode();
|
|
|
|
});
|
2023-08-19 17:59:34 +02:00
|
|
|
});
|