Files
Romulus21andClaude Opus 5.5 e6a1dd7e4b Permet de copier une journée vers un autre jour
Bouton copier sur l'en-tête de chaque jour : les entrées affichées sont
recopiées à la date choisie avec les mêmes heures, projet et note
(POST /ui/jours/{date}/dupliquer, tout ou rien). L'entrée en cours est
ignorée ; projet archivé ou même jour refusés.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:31:00 +01:00

114 lines
3.9 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 = Math.floor((secs % 3600) / 60);
const s = String(secs % 60).padStart(2, "0");
// Pas d'heures à zéro : 4:07, puis 1:04:07.
el.textContent = h ? `${h}:${String(m).padStart(2, "0")}:${s}` : `${m}:${s}`;
}
}
tick();
setInterval(tick, 1000);
htmx.onLoad(tick); // horloges arrivées par htmx (ex. header)
// 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();
});
// Shows/hides the "copy this day" form under a day header.
function toggleDayCopy(button) {
const row = button.closest("tbody").querySelector(".day-copy");
row.hidden = !row.hidden;
if (!row.hidden) row.querySelector('input[type="date"]').focus();
}
// 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);