Stirling-PDF/src/main/resources/static/js/darkmode.js

151 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-10-30 08:29:55 +01:00
var toggleCount = 0
var lastToggleTime = Date.now()
2023-06-02 21:15:10 +02:00
var elements = {
2023-10-30 08:29:55 +01:00
lightModeStyles: null,
darkModeStyles: null,
rainbowModeStyles: null,
darkModeIcon: null,
2023-11-02 16:15:02 +01:00
searchBar: null,
formControls: null,
navbar: null,
navIcons: null,
navDropdownMenus: null,
2023-10-30 08:29:55 +01:00
}
function getElements() {
2023-10-30 08:29:55 +01:00
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")
elements.searchBar = document.getElementById("searchBar")
elements.formControls = document.querySelectorAll(".form-control")
2023-11-02 16:15:02 +01:00
elements.navbar = document.querySelectorAll("nav.navbar")
elements.navIcons = document.querySelectorAll("nav .icon, .navbar-icon")
elements.navDropdownMenus = document.querySelectorAll("nav .dropdown-menu")
}
function setMode(mode) {
2023-12-10 16:29:12 +01:00
var event = new CustomEvent("modeChanged", { detail: mode });
document.dispatchEvent(event);
if (elements && elements.lightModeStyles) {
elements.lightModeStyles.disabled = mode !== "off";
}
if (elements && elements.darkModeStyles) {
elements.darkModeStyles.disabled = mode !== "on";
}
if (elements && elements.rainbowModeStyles) {
elements.rainbowModeStyles.disabled = mode !== "rainbow";
}
var jumbotron = document.getElementById("jumbotron");
2023-10-30 08:29:55 +01:00
if (mode === "on") {
2023-12-10 16:29:12 +01:00
if (elements && elements.darkModeIcon) {
elements.darkModeIcon.src = "moon.svg";
}
if (elements && elements.searchBar) {
elements.searchBar.classList.add("dark-mode-search");
}
if (elements && elements.formControls) {
elements.formControls.forEach(input => input.classList.add("bg-dark", "text-white"));
}
if (elements && elements.navbar) {
elements.navbar.forEach(navElement => {
navElement.classList.remove("navbar-light", "bg-light");
navElement.classList.add("navbar-dark", "bg-dark");
});
}
if (elements && elements.navDropdownMenus) {
elements.navDropdownMenus.forEach(menu => menu.classList.add("dropdown-menu-dark"));
}
if (elements && elements.navIcons) {
elements.navIcons.forEach(icon => (icon.style.filter = "invert(1)"));
}
var tables = document.querySelectorAll(".table");
2023-10-30 08:29:55 +01:00
tables.forEach(table => {
2023-12-10 16:29:12 +01:00
table.classList.add("table-dark");
});
2023-10-30 08:29:55 +01:00
if (jumbotron) {
2023-12-10 16:29:12 +01:00
jumbotron.classList.add("bg-dark");
jumbotron.classList.remove("bg-light");
2023-10-30 08:29:55 +01:00
}
} else if (mode === "off") {
2023-12-10 16:29:12 +01:00
if (elements && elements.darkModeIcon) {
elements.darkModeIcon.src = "sun.svg";
}
if (elements && elements.searchBar) {
elements.searchBar.classList.remove("dark-mode-search");
}
if (elements && elements.formControls) {
elements.formControls.forEach(input => input.classList.remove("bg-dark", "text-white"));
}
if (elements && elements.navbar) {
elements.navbar.forEach(navElement => {
navElement.classList.remove("navbar-dark", "bg-dark");
navElement.classList.add("navbar-light", "bg-light");
});
}
if (elements && elements.navDropdownMenus) {
elements.navDropdownMenus.forEach(menu => menu.classList.remove("dropdown-menu-dark"));
}
if (elements && elements.navIcons) {
elements.navIcons.forEach(icon => (icon.style.filter = "none"));
}
var tables = document.querySelectorAll(".table-dark");
2023-10-30 08:29:55 +01:00
tables.forEach(table => {
2023-12-10 16:29:12 +01:00
table.classList.remove("table-dark");
});
2023-10-30 08:29:55 +01:00
if (jumbotron) {
2023-12-10 16:29:12 +01:00
jumbotron.classList.remove("bg-dark");
jumbotron.classList.add("bg-light");
2023-10-30 08:29:55 +01:00
}
} else if (mode === "rainbow") {
2023-12-10 16:29:12 +01:00
if (elements && elements.darkModeIcon) {
elements.darkModeIcon.src = "rainbow.svg";
}
2023-10-30 08:29:55 +01:00
}
}
2023-06-02 21:15:10 +02:00
function toggleDarkMode() {
2023-10-30 08:29:55 +01:00
var currentTime = Date.now()
if (currentTime - lastToggleTime < 1000) {
toggleCount++
} else {
toggleCount = 1
}
lastToggleTime = currentTime
2023-06-02 21:15:10 +02:00
2023-10-30 08:29:55 +01:00
if (toggleCount >= 18) {
localStorage.setItem("dark-mode", "rainbow")
setMode("rainbow")
} else if (localStorage.getItem("dark-mode") == "on") {
localStorage.setItem("dark-mode", "off")
setMode("off")
} else {
localStorage.setItem("dark-mode", "on")
setMode("on")
}
2023-06-02 21:15:10 +02:00
}
2023-10-30 08:29:55 +01:00
document.addEventListener("DOMContentLoaded", function () {
getElements()
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")
} else {
setMode("off")
}
2023-06-02 21:15:10 +02:00
2023-12-16 11:35:45 +01:00
var darkModeToggle = document.getElementById("dark-mode-toggle");
if (darkModeToggle !== null) {
darkModeToggle.addEventListener("click", function (event) {
event.preventDefault();
toggleDarkMode();
});
}
2023-10-30 08:29:55 +01:00
})