60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
|
|
/* Why is the DLR shut today?
|
||
|
|
* Picks a random message on load and on demand, and handles the
|
||
|
|
* colour-scheme toggle. Messages live in messages.js (MESSAGES). */
|
||
|
|
|
||
|
|
(function () {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const FALLBACK = "Add some reasons in messages.js";
|
||
|
|
const messageEl = document.getElementById("message");
|
||
|
|
const refreshButton = document.getElementById("refresh");
|
||
|
|
const themeButtons = document.querySelectorAll("[data-set-theme]");
|
||
|
|
const THEME_KEY = "dlr-theme";
|
||
|
|
|
||
|
|
function pickMessage() {
|
||
|
|
if (!Array.isArray(MESSAGES) || MESSAGES.length === 0) {
|
||
|
|
return FALLBACK;
|
||
|
|
}
|
||
|
|
const index = Math.floor(Math.random() * MESSAGES.length);
|
||
|
|
return MESSAGES[index];
|
||
|
|
}
|
||
|
|
|
||
|
|
function showMessage() {
|
||
|
|
messageEl.textContent = pickMessage();
|
||
|
|
}
|
||
|
|
|
||
|
|
function applyTheme(theme) {
|
||
|
|
document.body.setAttribute("data-theme", theme);
|
||
|
|
themeButtons.forEach(function (button) {
|
||
|
|
const isActive = button.dataset.setTheme === theme;
|
||
|
|
button.setAttribute("aria-pressed", String(isActive));
|
||
|
|
});
|
||
|
|
try {
|
||
|
|
localStorage.setItem(THEME_KEY, theme);
|
||
|
|
} catch (err) {
|
||
|
|
/* localStorage unavailable; theme simply won't persist. */
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function initTheme() {
|
||
|
|
let saved = null;
|
||
|
|
try {
|
||
|
|
saved = localStorage.getItem(THEME_KEY);
|
||
|
|
} catch (err) {
|
||
|
|
/* ignore */
|
||
|
|
}
|
||
|
|
applyTheme(saved === "original" ? "original" : "modern");
|
||
|
|
}
|
||
|
|
|
||
|
|
themeButtons.forEach(function (button) {
|
||
|
|
button.addEventListener("click", function () {
|
||
|
|
applyTheme(button.dataset.setTheme);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
refreshButton.addEventListener("click", showMessage);
|
||
|
|
|
||
|
|
initTheme();
|
||
|
|
showMessage();
|
||
|
|
})();
|