separation du css et du html

This commit is contained in:
serveur
2026-10-17 23:14:07 +02:00
parent 24f42133ad
commit a54031e313
6 changed files with 620 additions and 450 deletions
+160 -157
View File
@@ -5,161 +5,7 @@
<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>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<header class="topbar">
@@ -190,7 +36,7 @@
<ul class="liste" id="liste"></ul>
<p class="vide" id="vide" hidden>Aucune tâche pour l'instant.</p>
<div class="actions">
<div class="todo-actions">
<button type="button" class="btn" id="btnNettoyer" hidden>Supprimer les tâches cochées</button>
</div>
</main>
@@ -201,10 +47,16 @@
let todos = [];
let premierChargement = true;
let minuterieErreur;
let glisse = null; // état du glissement en cours (null = aucun)
let enAttente = null; // liste reçue pendant un glissement, appliquée ensuite
// ---------- Affichage ----------
function rendre(modifies = new Set()) {
const liste = $("liste");
// On retient l'élément qui avait le focus pour le lui rendre après le redessin
const actif = document.activeElement;
const lignActive = actif && actif.closest ? actif.closest(".item") : null;
const focus = lignActive ? { id: lignActive.dataset.id, poignee: actif.classList.contains("grip") } : null;
liste.textContent = "";
for (const t of todos) {
@@ -230,7 +82,13 @@
suppr.setAttribute("aria-label", "Supprimer : " + t.text);
suppr.textContent = "✕";
li.append(etiquette, suppr);
const poignee = document.createElement("button");
poignee.type = "button";
poignee.className = "grip";
poignee.title = "Glisser pour déplacer";
poignee.setAttribute("aria-label", "Déplacer : " + t.text + " (glisser, ou flèches haut et bas)");
li.append(poignee, etiquette, suppr);
liste.append(li);
}
@@ -239,10 +97,21 @@
$("faites").textContent = faites;
$("vide").hidden = todos.length > 0;
$("btnNettoyer").hidden = faites === 0;
if (focus) {
const ligne = liste.querySelector(`.item[data-id="${CSS.escape(focus.id)}"]`);
const cible = ligne && ligne.querySelector(focus.poignee ? ".grip" : 'input[type="checkbox"]');
if (cible) cible.focus({ preventScroll: true });
}
}
// Reçoit la liste du serveur ; met en évidence ce que les autres viennent de changer
function recevoir(nouvelle) {
// Pendant un glissement, on ne redessine pas la liste (la carte déplacée disparaîtrait)
if (glisse) {
enAttente = nouvelle;
return;
}
const avant = new Map(todos.map((t) => [t.id, t]));
const modifies = new Set();
if (!premierChargement) {
@@ -342,6 +211,140 @@
};
}
// ---------- Réorganisation : glisser la poignée (doigt ou souris) ou flèches haut/bas ----------
// Le serveur ne reçoit qu'un déplacement : « cette tâche juste avant celle-là » (ou à la fin)
async function validerOrdre(li) {
// L'ordre local suit déjà l'ordre affiché à l'écran
const parId = new Map(todos.map((t) => [t.id, t]));
todos = [...$("liste").children].map((el) => parId.get(el.dataset.id)).filter(Boolean);
const suivante = li.nextElementSibling;
try {
await appeler("POST", "/api/todos/" + li.dataset.id + "/move", {
beforeId: suivante ? suivante.dataset.id : null,
});
} catch {
afficherErreur("Déplacement impossible, réessaie.");
await resynchroniser();
}
}
// Replace la carte sous le doigt et échange sa place avec ses voisines quand il le faut
function majGlisser() {
const { li } = glisse;
const liste = $("liste");
li.style.transform = `translateY(${glisse.y - glisse.departY}px)`;
for (let i = 0; i < 3; i++) {
const r = li.getBoundingClientRect();
const centre = r.top + r.height / 2;
const suivante = li.nextElementSibling;
const precedente = li.previousElementSibling;
let voisine = null;
let apres = false;
if (suivante && centre > suivante.getBoundingClientRect().top + suivante.offsetHeight / 2) {
voisine = suivante;
apres = true;
} else if (precedente && centre < precedente.getBoundingClientRect().top + precedente.offsetHeight / 2) {
voisine = precedente;
}
if (!voisine) break;
const hautAvant = li.getBoundingClientRect().top;
liste.insertBefore(li, apres ? voisine.nextSibling : voisine);
// Sa place « de base » a changé : on corrige pour que la carte reste exactement sous le doigt
glisse.departY += li.getBoundingClientRect().top - hautAvant;
li.style.transform = `translateY(${glisse.y - glisse.departY}px)`;
}
}
// Fait défiler la page quand on approche du bord de l'écran pendant un glissement
function defilementAuto() {
if (!glisse) return;
const haut = 150; // sous la barre du haut, qui reste collée
const bas = window.innerHeight - 90;
let vitesse = 0;
if (glisse.y < haut) vitesse = -Math.ceil((haut - glisse.y) / 8);
else if (glisse.y > bas) vitesse = Math.ceil((glisse.y - bas) / 8);
vitesse = Math.max(-24, Math.min(24, vitesse));
if (vitesse) {
const avant = window.scrollY;
window.scrollBy(0, vitesse);
const reel = window.scrollY - avant;
if (reel) {
glisse.departY -= reel; // la page a bougé : la carte doit suivre le doigt
majGlisser();
}
}
requestAnimationFrame(defilementAuto);
}
async function finGlisser() {
if (!glisse) return;
const { li, indexDepart } = glisse;
li.classList.remove("dragging");
li.style.transform = "";
document.body.classList.remove("glisse");
const aBouge = [...$("liste").children].indexOf(li) !== indexDepart;
glisse = null;
if (aBouge) await validerOrdre(li);
if (enAttente) {
enAttente = null;
await resynchroniser(); // des changements d'autres personnes sont arrivés pendant le glissement
}
}
$("liste").addEventListener("pointerdown", (e) => {
const poignee = e.target.closest(".grip");
if (!poignee || glisse || (e.pointerType === "mouse" && e.button !== 0)) return;
e.preventDefault();
const li = poignee.closest(".item");
poignee.setPointerCapture(e.pointerId);
glisse = {
li,
pointerId: e.pointerId,
departY: e.clientY,
y: e.clientY,
indexDepart: [...$("liste").children].indexOf(li),
};
li.classList.add("dragging");
document.body.classList.add("glisse");
requestAnimationFrame(defilementAuto);
});
$("liste").addEventListener("pointermove", (e) => {
if (!glisse || e.pointerId !== glisse.pointerId) return;
glisse.y = e.clientY;
majGlisser();
});
const relacher = (e) => {
if (glisse && e.pointerId === glisse.pointerId) finGlisser();
};
$("liste").addEventListener("pointerup", relacher);
$("liste").addEventListener("pointercancel", relacher);
$("liste").addEventListener("lostpointercapture", relacher);
// Pas de menu contextuel au toucher long sur la poignée
$("liste").addEventListener("contextmenu", (e) => {
if (e.target.closest(".grip")) e.preventDefault();
});
// Clavier : la poignée prise au focus se déplace avec les flèches haut / bas
$("liste").addEventListener("keydown", (e) => {
if (!e.target.matches(".grip") || (e.key !== "ArrowUp" && e.key !== "ArrowDown")) return;
e.preventDefault();
const li = e.target.closest(".item");
if (e.key === "ArrowUp" && li.previousElementSibling) {
li.parentNode.insertBefore(li, li.previousElementSibling);
} else if (e.key === "ArrowDown" && li.nextElementSibling) {
li.parentNode.insertBefore(li.nextElementSibling, li);
} else {
return;
}
e.target.focus();
validerOrdre(li);
});
// ---------- Interactions ----------
$("formAjout").addEventListener("submit", (e) => {
e.preventDefault();