Files
tallytome/public/scripts/tallytome.js
2024-09-01 18:31:01 +02:00

94 lines
2.6 KiB
JavaScript

// HTMX event listener
document.addEventListener("DOMContentLoaded", (event) => {
document.body.addEventListener("htmx:beforeSwap", function(evt) {
if (evt.detail.xhr.status === 422) {
evt.detail.shouldSwap = true;
evt.detail.isError = false;
}
});
document.body.addEventListener("HPUpdated", function() {
var input = document.getElementById("damageInput");
input.value = "";
input.removeAttribute("aria-invalid");
var errorMessage = document.getElementById("damageError");
if (errorMessage) {
errorMessage.parentNode.removeChild(errorMessage);
}
var savingthrow = document.getElementById("savingthrow");
savingthrow.checked = false;
var bonushp = document.getElementById("bonushp");
var bonusInput = document.getElementById("bonusInput");
bonusInput.classList.add("hidden");
bonusInput.disabled = true;
bonusInput.value = "";
bonushp.checked = false;
});
document.body.addEventListener("ManaUpdated", function() {
var input = document.getElementById("manaInput");
input.value = "";
input.removeAttribute("aria-invalid");
var errorMessage = document.getElementById("manaError");
if (errorMessage) {
errorMessage.parentNode.removeChild(errorMessage);
}
});
document.body.addEventListener("BaseUpdated", function() {
var errorMessage = document.getElementById("baseError");
if (errorMessage) {
errorMessage.parentNode.removeChild(errorMessage);
}
});
});
// theme switching
let atr = 'data-theme';
let localTheme = localStorage.getItem(atr)
if (localTheme) {
setTheme(localTheme);
}
// if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && localTheme == null) {
// // dark mode
// setTheme('dark');
// }
function toggleColorMode() {
let el = document.documentElement;
let atr_data = el.getAttribute(atr);
if (atr_data == 'light') {
setTheme('dark');
} else {
setTheme('light');
}
}
function setTheme(theme) {
let el = document.documentElement;
el.getAttribute(atr);
el.setAttribute(atr, theme);
localStorage.setItem(atr, theme)
}
// round toggle
//function toggleRounds() {
// let box = document.getElementById("bonushp");
// let inp = document.getElementById("bonusInput");
// if (box.checked == true) {
// inp.classList.remove("hidden");
// inp.disabled = false;
// } else {
// inp.classList.add("hidden");
// inp.disabled = true;
// }
//}