97 lines
3.2 KiB
JavaScript
97 lines
3.2 KiB
JavaScript
/**
|
|
* Small UI helpers built on DaisyUI: toasts, confirm dialog,
|
|
* button loading states, HTML escaping and date formatting.
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
function escapeHtml(value) {
|
|
return String(value == null ? "" : value)
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
}
|
|
|
|
function formatDate(value) {
|
|
if (!value) return "";
|
|
const date = new Date(value);
|
|
if (isNaN(date.getTime())) return String(value);
|
|
return date.toLocaleDateString(undefined, {
|
|
year: "numeric",
|
|
month: "short",
|
|
day: "numeric",
|
|
});
|
|
}
|
|
|
|
/** Show a toast notification. type: success | error | info | warning */
|
|
function toast(message, type) {
|
|
type = type || "info";
|
|
let container = document.getElementById("toast-container");
|
|
if (!container) {
|
|
container = document.createElement("div");
|
|
container.id = "toast-container";
|
|
container.className = "toast toast-end toast-bottom z-50";
|
|
container.setAttribute("aria-live", "polite");
|
|
document.body.appendChild(container);
|
|
}
|
|
const alert = document.createElement("div");
|
|
alert.className = "alert alert-" + type + " shadow-lg";
|
|
alert.setAttribute("role", type === "error" ? "alert" : "status");
|
|
const text = document.createElement("span");
|
|
text.textContent = message;
|
|
alert.appendChild(text);
|
|
container.appendChild(alert);
|
|
setTimeout(() => alert.remove(), 4500);
|
|
}
|
|
|
|
/**
|
|
* Show a confirmation dialog. Resolves true when confirmed.
|
|
* Uses the shared <dialog id="confirm-dialog"> from the layout.
|
|
*/
|
|
function confirmDialog(options) {
|
|
options = options || {};
|
|
const dialog = document.getElementById("confirm-dialog");
|
|
if (!dialog) return Promise.resolve(window.confirm(options.message || "Are you sure?"));
|
|
|
|
dialog.querySelector("[data-confirm-title]").textContent =
|
|
options.title || "Please confirm";
|
|
dialog.querySelector("[data-confirm-message]").textContent =
|
|
options.message || "Are you sure you want to continue?";
|
|
const okButton = dialog.querySelector("[data-confirm-ok]");
|
|
okButton.textContent = options.confirmText || "Delete";
|
|
|
|
return new Promise((resolve) => {
|
|
const onClose = () => {
|
|
dialog.removeEventListener("close", onClose);
|
|
resolve(dialog.returnValue === "ok");
|
|
};
|
|
dialog.addEventListener("close", onClose);
|
|
dialog.showModal();
|
|
okButton.focus();
|
|
});
|
|
}
|
|
|
|
/** Toggle a DaisyUI loading state on a button. */
|
|
function setLoading(button, isLoading) {
|
|
if (!button) return;
|
|
if (isLoading) {
|
|
button.disabled = true;
|
|
button.setAttribute("aria-busy", "true");
|
|
if (!button.querySelector(".loading")) {
|
|
const spinner = document.createElement("span");
|
|
spinner.className = "loading loading-spinner loading-xs";
|
|
button.prepend(spinner);
|
|
}
|
|
} else {
|
|
button.disabled = false;
|
|
button.removeAttribute("aria-busy");
|
|
const spinner = button.querySelector(".loading");
|
|
if (spinner) spinner.remove();
|
|
}
|
|
}
|
|
|
|
window.UI = { escapeHtml, formatDate, toast, confirm: confirmDialog, setLoading };
|
|
})();
|