Favicon par défaut (static/favicon.svg) ; tant que /api/timer renvoie une entrée en cours, il alterne chaque seconde entre un point rouge plein et pâle, sur toutes les pages. État relu au chargement, après chaque requête htmx, au retour sur l'onglet et toutes les 20 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.5 KiB
JavaScript
105 lines
3.5 KiB
JavaScript
// 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 <body>: 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();
|
|
}
|
|
|
|
// Favicon : point rouge clignotant tant qu'un enregistrement est en cours,
|
|
// sur toutes les pages (état lu sur /api/timer).
|
|
const favicon = document.getElementById("favicon");
|
|
const IDLE_ICON = favicon.href;
|
|
const recIcon = (opacity) =>
|
|
"data:image/svg+xml," +
|
|
encodeURIComponent(
|
|
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><circle cx="16" cy="16" r="13" fill="#e5322d" fill-opacity="${opacity}"/></svg>`,
|
|
);
|
|
const REC_ON = recIcon(1);
|
|
const REC_OFF = recIcon(0.2);
|
|
let recording = false;
|
|
let blinkOn = false;
|
|
|
|
function setRecording(value) {
|
|
if (value === recording) return;
|
|
recording = value;
|
|
blinkOn = true;
|
|
favicon.href = recording ? REC_ON : IDLE_ICON;
|
|
}
|
|
setInterval(() => {
|
|
if (!recording) return;
|
|
blinkOn = !blinkOn;
|
|
favicon.href = blinkOn ? REC_ON : REC_OFF;
|
|
}, 1000);
|
|
|
|
async function refreshRecording() {
|
|
try {
|
|
const res = await fetch("/api/timer", { headers: { accept: "application/json" } });
|
|
if (res.ok) setRecording((await res.json()) !== null);
|
|
} catch {
|
|
// Serveur injoignable : on garde l'état connu.
|
|
}
|
|
}
|
|
refreshRecording();
|
|
setInterval(refreshRecording, 20000);
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (!document.hidden) refreshRecording();
|
|
});
|
|
document.body.addEventListener("htmx:afterRequest", refreshRecording);
|