43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
|
|
import { t } from "./i18n.js";
|
||
|
|
|
||
|
|
export const $ = (id) => document.getElementById(id);
|
||
|
|
|
||
|
|
export function csv(value) {
|
||
|
|
return String(value || "").split(",").map((item) => item.trim()).filter(Boolean);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function escapeHtml(value) {
|
||
|
|
return String(value ?? "")
|
||
|
|
.replaceAll("&", "&")
|
||
|
|
.replaceAll("<", "<")
|
||
|
|
.replaceAll(">", ">")
|
||
|
|
.replaceAll('"', """)
|
||
|
|
.replaceAll("'", "'");
|
||
|
|
}
|
||
|
|
|
||
|
|
export function chip(value) {
|
||
|
|
return `<span class="chip">${escapeHtml(value)}</span>`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function table(headers, rows) {
|
||
|
|
if (!rows.length) {
|
||
|
|
return `<table><tbody><tr><td>${escapeHtml(t("table.no_data"))}</td></tr></tbody></table>`;
|
||
|
|
}
|
||
|
|
return `
|
||
|
|
<table>
|
||
|
|
<thead><tr>${headers.map((header) => `<th>${escapeHtml(header)}</th>`).join("")}</tr></thead>
|
||
|
|
<tbody>${rows.map((row) => `<tr>${row.map((cell) => `<td>${String(cell ?? "")}</td>`).join("")}</tr>`).join("")}</tbody>
|
||
|
|
</table>
|
||
|
|
`;
|
||
|
|
}
|
||
|
|
|
||
|
|
let toastTimer = null;
|
||
|
|
export function toast(message) {
|
||
|
|
const node = document.getElementById("toast");
|
||
|
|
if (!node) return;
|
||
|
|
node.textContent = message;
|
||
|
|
node.classList.add("show");
|
||
|
|
if (toastTimer) window.clearTimeout(toastTimer);
|
||
|
|
toastTimer = window.setTimeout(() => node.classList.remove("show"), 2400);
|
||
|
|
}
|