81 lines
2.6 KiB
JavaScript
81 lines
2.6 KiB
JavaScript
// Small UI helpers to keep main.js lean and declarative.
|
|
|
|
export function applyTooltips(map = {}) {
|
|
Object.entries(map).forEach(([id, text]) => {
|
|
const el = document.getElementById(id);
|
|
if (el && text) el.title = text;
|
|
});
|
|
}
|
|
|
|
export function wireModalPairs(pairs = []) {
|
|
pairs.forEach(({ openBtn, modal, closeBtn }) => {
|
|
if (!modal) return;
|
|
openBtn?.addEventListener("click", () => modal.classList.remove("hidden"));
|
|
closeBtn?.addEventListener("click", () => modal.classList.add("hidden"));
|
|
modal.addEventListener("click", (e) => {
|
|
if (e.target === modal) modal.classList.add("hidden");
|
|
});
|
|
});
|
|
}
|
|
|
|
export function wireAccordions({
|
|
toggleSelector = ".accordion-toggle",
|
|
accordionSelector = ".accordion",
|
|
forceOpen = false,
|
|
} = {}) {
|
|
const accordions = Array.from(document.querySelectorAll(accordionSelector));
|
|
const toggles = Array.from(document.querySelectorAll(toggleSelector));
|
|
if (forceOpen) {
|
|
accordions.forEach((a) => a.classList.add("open"));
|
|
return;
|
|
}
|
|
toggles.forEach((btn) => {
|
|
btn.addEventListener("click", () => {
|
|
const acc = btn.closest(accordionSelector);
|
|
if (!acc) return;
|
|
if (acc.classList.contains("open")) {
|
|
acc.classList.remove("open");
|
|
} else {
|
|
accordions.forEach((a) => a.classList.remove("open"));
|
|
acc.classList.add("open");
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export function createBusyOverlay({ overlay, titleEl, textEl }) {
|
|
const showBusy = (title = "Working…", text = "This may take a few seconds.") => {
|
|
if (!overlay) return;
|
|
if (titleEl) titleEl.textContent = title;
|
|
if (textEl) {
|
|
textEl.textContent = text || "";
|
|
textEl.classList.toggle("hidden", !text);
|
|
}
|
|
overlay.classList.remove("hidden");
|
|
};
|
|
const hideBusy = () => overlay?.classList.add("hidden");
|
|
return { showBusy, hideBusy };
|
|
}
|
|
|
|
export function createConfirmModal({ modal, titleEl, bodyEl, okBtn, cancelBtn }) {
|
|
const confirmAction = (title, body) =>
|
|
new Promise((resolve) => {
|
|
if (!modal) {
|
|
resolve(window.confirm(body || title || "Are you sure?"));
|
|
return;
|
|
}
|
|
if (titleEl) titleEl.textContent = title || "Are you sure?";
|
|
if (bodyEl) bodyEl.textContent = body || "";
|
|
modal.classList.remove("hidden");
|
|
const done = (val) => {
|
|
modal.classList.add("hidden");
|
|
resolve(val);
|
|
};
|
|
const okHandler = () => done(true);
|
|
const cancelHandler = () => done(false);
|
|
okBtn?.addEventListener("click", okHandler, { once: true });
|
|
cancelBtn?.addEventListener("click", cancelHandler, { once: true });
|
|
});
|
|
return confirmAction;
|
|
}
|