// Live clock for the running timer. function tick() { for (const el of document.querySelectorAll(".clock[data-started]")) { const secs = Math.max(0, Math.floor((Date.now() - Date.parse(el.dataset.started)) / 1000)); const h = Math.floor(secs / 3600); const m = String(Math.floor((secs % 3600) / 60)).padStart(2, "0"); const s = String(secs % 60).padStart(2, "0"); el.textContent = `${h}:${m}:${s}`; } } tick(); setInterval(tick, 1000); // Show server errors from htmx requests in a toast. let toastTimer; function toast(message) { const el = document.getElementById("toast"); el.textContent = message; el.hidden = false; clearTimeout(toastTimer); toastTimer = setTimeout(() => (el.hidden = true), 5000); } document.body.addEventListener("htmx:responseError", (event) => { const html = new DOMParser().parseFromString(event.detail.xhr.responseText, "text/html"); toast(html.getElementById("error-message")?.textContent || "Une erreur est survenue"); }); document.body.addEventListener("htmx:sendError", () => toast("Serveur injoignable")); // Confirmation for plain (non-htmx) destructive forms. document.addEventListener("submit", (event) => { const message = event.target.dataset.confirm; if (message && !confirm(message)) event.preventDefault(); }); // Date/time pickers on entry fields, including rows swapped in by htmx. // The original input keeps the `YYYY-MM-DDTHH:MM` value sent to the server. htmx.onLoad((root) => { for (const input of root.querySelectorAll('input[type="datetime-local"]')) { flatpickr(input, { locale: "fr", enableTime: true, time_24hr: true, dateFormat: "Y-m-d\\TH:i", altInput: true, altFormat: "d/m/Y H:i", allowInput: true, onReady: (_dates, _str, fp) => fp.altInput.setAttribute("aria-label", input.getAttribute("aria-label")), }); } }); // The calendars live in : drop them with the inputs htmx removes. document.body.addEventListener("htmx:beforeCleanupElement", (event) => { event.target._flatpickr?.destroy(); }); // Removes a row without a request (e.g. cancelling a duplicate). function removeRow(button) { const row = button.closest("tr"); for (const input of row.querySelectorAll("input")) input._flatpickr?.destroy(); row.remove(); }