384 lines
14 KiB
HTML
384 lines
14 KiB
HTML
<!doctype html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
||
<meta name="theme-color" content="#8c2f2f" />
|
||
<title>To-do – Calcul des épices</title>
|
||
<link rel="stylesheet" href="assets/menu.css" />
|
||
<style>
|
||
:root {
|
||
--bg: #f6f1ea;
|
||
--card: #ffffff;
|
||
--ink: #2b2420;
|
||
--muted: #7a6e65;
|
||
--line: #e6ddd2;
|
||
--accent: #8c2f2f;
|
||
--accent-dark: #742626;
|
||
--accent-soft: #f7e8e5;
|
||
--ok: #2f6b4f;
|
||
--warn: #b35a00;
|
||
--radius: 16px;
|
||
--shadow: 0 1px 2px rgba(43, 36, 32, 0.06), 0 6px 18px rgba(43, 36, 32, 0.06);
|
||
}
|
||
|
||
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
|
||
[hidden] { display: none !important; }
|
||
|
||
body {
|
||
margin: 0;
|
||
background: var(--bg);
|
||
color: var(--ink);
|
||
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||
line-height: 1.35;
|
||
padding-bottom: env(safe-area-inset-bottom);
|
||
}
|
||
|
||
.wrap { max-width: 760px; margin: 0 auto; padding: 14px 14px 40px; }
|
||
|
||
/* Statut de connexion (barre du haut) */
|
||
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 0.78rem; opacity: 0.95; }
|
||
.live::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: #7be0a8; }
|
||
.live.off::before { background: #ffc266; }
|
||
|
||
/* Ajout */
|
||
.add { display: flex; gap: 10px; margin-bottom: 14px; }
|
||
.add input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
height: 56px;
|
||
padding: 0 14px;
|
||
font: inherit;
|
||
font-size: 1.1rem; /* 16px minimum : pas de zoom iOS */
|
||
border: 1.5px solid var(--line);
|
||
border-radius: 14px;
|
||
background: var(--card);
|
||
color: var(--ink);
|
||
box-shadow: var(--shadow);
|
||
}
|
||
.add input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(140, 47, 47, 0.18); }
|
||
|
||
.btn {
|
||
font: inherit;
|
||
font-weight: 700;
|
||
min-height: 52px;
|
||
padding: 0 18px;
|
||
border-radius: 14px;
|
||
border: 1.5px solid var(--line);
|
||
background: var(--card);
|
||
color: var(--ink);
|
||
cursor: pointer;
|
||
}
|
||
.btn:active { transform: scale(0.98); }
|
||
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; min-height: 56px; }
|
||
.btn.primary:active { background: var(--accent-dark); }
|
||
|
||
.info { display: flex; justify-content: space-between; align-items: center; margin: 0 2px 10px; font-size: 0.9rem; color: var(--muted); }
|
||
.info b { color: var(--accent); }
|
||
|
||
/* Liste */
|
||
.liste { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
|
||
|
||
.item {
|
||
display: flex;
|
||
align-items: center;
|
||
background: var(--card);
|
||
border-radius: 14px;
|
||
box-shadow: var(--shadow);
|
||
border: 2px solid transparent;
|
||
}
|
||
.item.flash { animation: flash 1.8s ease-out; }
|
||
@keyframes flash {
|
||
0% { background: #fde9c4; border-color: var(--warn); }
|
||
100% { background: var(--card); border-color: transparent; }
|
||
}
|
||
|
||
.check {
|
||
position: relative;
|
||
flex: 1;
|
||
min-width: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 14px;
|
||
min-height: 60px;
|
||
padding: 8px 6px 8px 14px;
|
||
cursor: pointer;
|
||
}
|
||
.check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
|
||
.box {
|
||
flex: none;
|
||
width: 30px;
|
||
height: 30px;
|
||
border-radius: 9px;
|
||
border: 2px solid #cfc3b5;
|
||
background: #fffdfa;
|
||
display: grid;
|
||
place-items: center;
|
||
transition: background 0.12s, border-color 0.12s;
|
||
}
|
||
.box::after {
|
||
content: "";
|
||
width: 7px;
|
||
height: 14px;
|
||
border: solid #fff;
|
||
border-width: 0 3px 3px 0;
|
||
transform: translateY(-1px) rotate(45deg) scale(0);
|
||
transition: transform 0.12s;
|
||
}
|
||
.check input:checked + .box { background: var(--ok); border-color: var(--ok); }
|
||
.check input:checked + .box::after { transform: translateY(-1px) rotate(45deg) scale(1); }
|
||
.check input:focus-visible + .box { box-shadow: 0 0 0 3px rgba(140, 47, 47, 0.25); }
|
||
|
||
.txt { font-size: 1.05rem; overflow-wrap: anywhere; }
|
||
.item.done .txt { color: var(--muted); text-decoration: line-through; }
|
||
|
||
.del {
|
||
flex: none;
|
||
width: 52px;
|
||
height: 52px;
|
||
margin-right: 4px;
|
||
border: none;
|
||
border-radius: 12px;
|
||
background: transparent;
|
||
color: var(--muted);
|
||
font-size: 1.1rem;
|
||
cursor: pointer;
|
||
}
|
||
.del:active { background: var(--line); }
|
||
|
||
.vide { text-align: center; color: var(--muted); padding: 36px 12px; }
|
||
|
||
.erreur {
|
||
margin: 0 0 12px;
|
||
padding: 10px 14px;
|
||
border-radius: 12px;
|
||
background: #fff1e0;
|
||
color: var(--warn);
|
||
font-weight: 600;
|
||
font-size: 0.9rem;
|
||
}
|
||
|
||
.actions { margin-top: 16px; display: grid; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header class="topbar">
|
||
<div class="topbar-row">
|
||
<h1>Liste de tâches</h1>
|
||
<span class="live off" id="statut" role="status">Connexion…</span>
|
||
</div>
|
||
<nav class="menu" aria-label="Menu principal">
|
||
<a href="index.html">Calculateur</a>
|
||
<a href="todo.html" aria-current="page">To-do</a>
|
||
</nav>
|
||
</header>
|
||
|
||
<main class="wrap">
|
||
<form class="add" id="formAjout" autocomplete="off">
|
||
<input type="text" id="champAjout" placeholder="Nouvelle tâche…" maxlength="200" enterkeyhint="done"
|
||
aria-label="Nouvelle tâche" />
|
||
<button type="submit" class="btn primary">Ajouter</button>
|
||
</form>
|
||
|
||
<p class="erreur" id="erreur" hidden></p>
|
||
|
||
<div class="info">
|
||
<span><b id="restantes">0</b> à faire · <span id="faites">0</span> faites</span>
|
||
<span>Partagée en direct</span>
|
||
</div>
|
||
|
||
<ul class="liste" id="liste"></ul>
|
||
<p class="vide" id="vide" hidden>Aucune tâche pour l'instant.</p>
|
||
|
||
<div class="actions">
|
||
<button type="button" class="btn" id="btnNettoyer" hidden>Supprimer les tâches cochées</button>
|
||
</div>
|
||
</main>
|
||
|
||
<script>
|
||
const $ = (id) => document.getElementById(id);
|
||
|
||
let todos = [];
|
||
let premierChargement = true;
|
||
let minuterieErreur;
|
||
|
||
// ---------- Affichage ----------
|
||
function rendre(modifies = new Set()) {
|
||
const liste = $("liste");
|
||
liste.textContent = "";
|
||
|
||
for (const t of todos) {
|
||
const li = document.createElement("li");
|
||
li.className = "item" + (t.done ? " done" : "") + (modifies.has(t.id) ? " flash" : "");
|
||
li.dataset.id = t.id;
|
||
|
||
const etiquette = document.createElement("label");
|
||
etiquette.className = "check";
|
||
const caseACocher = document.createElement("input");
|
||
caseACocher.type = "checkbox";
|
||
caseACocher.checked = t.done;
|
||
const boite = document.createElement("span");
|
||
boite.className = "box";
|
||
const texte = document.createElement("span");
|
||
texte.className = "txt";
|
||
texte.textContent = t.text; // textContent : jamais de HTML injecté
|
||
etiquette.append(caseACocher, boite, texte);
|
||
|
||
const suppr = document.createElement("button");
|
||
suppr.type = "button";
|
||
suppr.className = "del";
|
||
suppr.setAttribute("aria-label", "Supprimer : " + t.text);
|
||
suppr.textContent = "✕";
|
||
|
||
li.append(etiquette, suppr);
|
||
liste.append(li);
|
||
}
|
||
|
||
const faites = todos.filter((t) => t.done).length;
|
||
$("restantes").textContent = todos.length - faites;
|
||
$("faites").textContent = faites;
|
||
$("vide").hidden = todos.length > 0;
|
||
$("btnNettoyer").hidden = faites === 0;
|
||
}
|
||
|
||
// Reçoit la liste du serveur ; met en évidence ce que les autres viennent de changer
|
||
function recevoir(nouvelle) {
|
||
const avant = new Map(todos.map((t) => [t.id, t]));
|
||
const modifies = new Set();
|
||
if (!premierChargement) {
|
||
for (const t of nouvelle) {
|
||
const p = avant.get(t.id);
|
||
if (!p || p.done !== t.done || p.text !== t.text) modifies.add(t.id);
|
||
}
|
||
}
|
||
premierChargement = false;
|
||
todos = nouvelle;
|
||
rendre(modifies);
|
||
}
|
||
|
||
function etat(enLigne) {
|
||
const el = $("statut");
|
||
el.classList.toggle("off", !enLigne);
|
||
el.textContent = enLigne ? "En direct" : "Hors ligne…";
|
||
}
|
||
|
||
function afficherErreur(message) {
|
||
const el = $("erreur");
|
||
el.textContent = message;
|
||
el.hidden = false;
|
||
clearTimeout(minuterieErreur);
|
||
minuterieErreur = setTimeout(() => (el.hidden = true), 4000);
|
||
}
|
||
|
||
// ---------- Appels au serveur ----------
|
||
async function appeler(methode, url, corps) {
|
||
const reponse = await fetch(url, {
|
||
method: methode,
|
||
headers: corps ? { "Content-Type": "application/json" } : undefined,
|
||
body: corps ? JSON.stringify(corps) : undefined,
|
||
});
|
||
if (!reponse.ok) throw new Error("HTTP " + reponse.status);
|
||
return reponse.json();
|
||
}
|
||
|
||
async function resynchroniser() {
|
||
try {
|
||
recevoir(await appeler("GET", "/api/todos"));
|
||
etat(true);
|
||
} catch {
|
||
etat(false);
|
||
}
|
||
}
|
||
|
||
async function basculer(id, done) {
|
||
const tache = todos.find((t) => t.id === id);
|
||
if (!tache) return;
|
||
tache.done = done; // affichage immédiat, le serveur confirme ensuite
|
||
rendre();
|
||
try {
|
||
await appeler("PATCH", "/api/todos/" + id, { done });
|
||
} catch {
|
||
afficherErreur("Action impossible, réessaie.");
|
||
resynchroniser();
|
||
}
|
||
}
|
||
|
||
async function supprimer(id) {
|
||
todos = todos.filter((t) => t.id !== id);
|
||
rendre();
|
||
try {
|
||
await appeler("DELETE", "/api/todos/" + id);
|
||
} catch {
|
||
afficherErreur("Suppression impossible, réessaie.");
|
||
resynchroniser();
|
||
}
|
||
}
|
||
|
||
async function ajouter(texte) {
|
||
try {
|
||
const tache = await appeler("POST", "/api/todos", { text: texte });
|
||
if (!todos.some((t) => t.id === tache.id)) {
|
||
todos.push(tache);
|
||
rendre();
|
||
}
|
||
} catch {
|
||
afficherErreur("Ajout impossible, réessaie.");
|
||
$("champAjout").value = texte;
|
||
}
|
||
}
|
||
|
||
// ---------- Temps réel ----------
|
||
function connecter() {
|
||
const flux = new EventSource("/api/events");
|
||
flux.onopen = () => etat(true);
|
||
flux.onmessage = (e) => {
|
||
etat(true);
|
||
recevoir(JSON.parse(e.data));
|
||
};
|
||
flux.onerror = () => {
|
||
etat(false);
|
||
// Le navigateur se reconnecte seul ; si la connexion est fermée pour de bon, on la relance
|
||
if (flux.readyState === EventSource.CLOSED) setTimeout(connecter, 3000);
|
||
};
|
||
}
|
||
|
||
// ---------- Interactions ----------
|
||
$("formAjout").addEventListener("submit", (e) => {
|
||
e.preventDefault();
|
||
const champ = $("champAjout");
|
||
const texte = champ.value.trim();
|
||
if (!texte) return;
|
||
champ.value = ""; // le champ garde le focus : on enchaîne les ajouts
|
||
ajouter(texte);
|
||
});
|
||
|
||
$("liste").addEventListener("change", (e) => {
|
||
if (!e.target.matches('input[type="checkbox"]')) return;
|
||
basculer(e.target.closest(".item").dataset.id, e.target.checked);
|
||
});
|
||
|
||
$("liste").addEventListener("click", (e) => {
|
||
const bouton = e.target.closest(".del");
|
||
if (bouton) supprimer(bouton.closest(".item").dataset.id);
|
||
});
|
||
|
||
$("btnNettoyer").addEventListener("click", async () => {
|
||
if (!confirm("Supprimer les tâches cochées pour tout le monde ?")) return;
|
||
try {
|
||
recevoir(await appeler("POST", "/api/todos/clear-done"));
|
||
} catch {
|
||
afficherErreur("Action impossible, réessaie.");
|
||
}
|
||
});
|
||
|
||
// Au retour sur l'onglet (téléphone en veille), on se remet à jour
|
||
document.addEventListener("visibilitychange", () => {
|
||
if (document.visibilityState === "visible") resynchroniser();
|
||
});
|
||
|
||
resynchroniser();
|
||
connecter();
|
||
</script>
|
||
</body>
|
||
</html>
|