444 lines
19 KiB
HTML
444 lines
19 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>Calculateur d'épices</title>
|
||
<link rel="stylesheet" href="assets/style.css" />
|
||
<script src="assets/edition.js" defer></script>
|
||
</head>
|
||
<body>
|
||
<header class="topbar">
|
||
<div class="topbar-row">
|
||
<h1>Calcul des épices</h1>
|
||
<div class="summary" aria-live="polite">
|
||
<div><span class="lbl">Viande nette</span><b id="Resume_Net">0.000</b> kg</div>
|
||
<div><span class="lbl">Rendement</span><b id="Resume_Rendement">0 %</b></div>
|
||
</div>
|
||
</div>
|
||
<nav class="menu" aria-label="Menu principal">
|
||
<a href="index.html" aria-current="page">Calculateur</a>
|
||
<a href="todo.html">To-do</a>
|
||
</nav>
|
||
</header>
|
||
|
||
<main class="wrap wrap-large">
|
||
<section class="card">
|
||
<h2>Rendement</h2>
|
||
<div class="yield">
|
||
<div class="field">
|
||
<label for="Total_Achete">Total acheté (kg)</label>
|
||
<input type="number" id="Total_Achete" step="any" inputmode="decimal" placeholder="0" />
|
||
</div>
|
||
<div class="stat">
|
||
<div class="label">Total assaisonné (kg)</div>
|
||
<div class="value" id="Total_Assaisonne">0.000</div>
|
||
</div>
|
||
<div class="stat big">
|
||
<div class="label">Rendement</div>
|
||
<div class="value" id="Rendement">0 %</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<details class="card no-print">
|
||
<summary>Réglages (planche et proportions)</summary>
|
||
<div class="settings" id="settings"></div>
|
||
</details>
|
||
|
||
<div class="filterbar no-print">
|
||
<label class="switch">
|
||
<input type="checkbox" id="Filtre_pesees" />
|
||
<span class="track"></span>
|
||
<span>Seulement les pesées</span>
|
||
</label>
|
||
<span class="count"><b id="Compteur">0</b> / <span id="CompteurMax">0</span> pesées</span>
|
||
</div>
|
||
|
||
<section class="bassines" id="bassines"></section>
|
||
|
||
<section class="card" style="margin-top: 14px">
|
||
<h2>Totaux</h2>
|
||
<div class="totals" id="totals"></div>
|
||
</section>
|
||
|
||
<div class="actions no-print">
|
||
<button type="button" class="btn primary" id="btnPrint">Imprimer la page</button>
|
||
<button type="button" class="btn" id="btnReset">Vider les pesées</button>
|
||
</div>
|
||
|
||
<!-- Plan B affiché si l'impression ne démarre pas (iPhone) -->
|
||
<section class="card help-print no-print" id="aideImpression" hidden>
|
||
<p><b>L'impression ne s'est pas lancée.</b> Sur iPhone, touche le bouton <b>Partager</b> de Safari (le carré avec une flèche), puis <b>Imprimer</b>. Tu peux aussi envoyer le récapitulatif :</p>
|
||
<button type="button" class="btn" id="btnPartager">Partager le récapitulatif</button>
|
||
</section>
|
||
</main>
|
||
|
||
<script>
|
||
// ---------- Données ----------
|
||
const bassines = [
|
||
{ nom: "Niagara", tare: 6.5 },
|
||
{ nom: "Manu Chao", tare: 2.7 },
|
||
{ nom: "Kamini", tare: 2.7 },
|
||
{ nom: "Tina", tare: 2.7 },
|
||
{ nom: "Garou", tare: 6.0 },
|
||
{ nom: "Johnny", tare: 2.7 },
|
||
{ nom: "Vanessa", tare: 1.7 },
|
||
{ nom: "Joséphine", tare: 4.9 },
|
||
{ nom: "Céline", tare: 6.0 },
|
||
{ nom: "Dalida", tare: 2.5 },
|
||
{ nom: "Florent", tare: 2.7 },
|
||
{ nom: "Petite bassine ronde verte (JM)", tare: 1.7 },
|
||
{ nom: "Grande bassine verte", tare: 2.7 },
|
||
{ nom: "Caisse grise", tare: 2.45 },
|
||
{ nom: "Caisse verte", tare: 2.6 },
|
||
];
|
||
|
||
const reglages = [
|
||
{ id: "Poids_planche", label: "Poids de la planche (kg)", defaut: 4.4 },
|
||
{ id: "Prop_sel", label: "Sel (kg)", defaut: 0.028 },
|
||
{ id: "Prop_poivre", label: "Poivre (g)", defaut: 2 },
|
||
{ id: "Prop_muscade", label: "Muscade (g)", defaut: 1 },
|
||
{ id: "Prop_ail", label: "Ail (g)", defaut: 2 },
|
||
];
|
||
|
||
// Colonnes calculées : clé, libellé, unité des totaux
|
||
const colonnes = [
|
||
{ cle: "net", titre: "Viande nette", unite: "kg", totalId: "Total_Net" },
|
||
{ cle: "sel", titre: "Sel", unite: "kg", totalId: "Total_Sel" },
|
||
{ cle: "poivre", titre: "Poivre", unite: "g", totalId: "Total_Poivre" },
|
||
{ cle: "concasse", titre: "Poivre concassé", unite: "g", totalId: "Total_Poivre_Concasse" },
|
||
{ cle: "muscade", titre: "Muscade", unite: "g", totalId: "Total_Muscade" },
|
||
{ cle: "ail", titre: "Ail", unite: "g", totalId: "Total_Ail" },
|
||
];
|
||
|
||
// ---------- Utilitaires ----------
|
||
const $ = (id) => document.getElementById(id);
|
||
// Accepte la virgule comme séparateur décimal
|
||
const nombre = (valeur) => parseFloat(String(valeur).replace(",", ".")) || 0;
|
||
const valeurChamp = (id) => nombre($(id).value);
|
||
|
||
// ---------- Construction de l'interface ----------
|
||
function construireReglages() {
|
||
$("settings").innerHTML =
|
||
reglages
|
||
.map(
|
||
(r) => `
|
||
<div class="field">
|
||
<label for="${r.id}">${r.label}</label>
|
||
<input type="text" id="${r.id}" inputmode="decimal" value="${r.defaut}" />
|
||
</div>`,
|
||
)
|
||
.join("") + `<p class="hint">Les proportions s'appliquent par kg de viande nette.</p>`;
|
||
}
|
||
|
||
function construireTotaux() {
|
||
$("totals").innerHTML = colonnes
|
||
.map(
|
||
(c) => `
|
||
<div class="stat">
|
||
<div class="label">${c.titre} <span class="unit">(${c.unite})</span></div>
|
||
<div class="value" id="${c.totalId}">0.000</div>
|
||
</div>`,
|
||
)
|
||
.join("");
|
||
}
|
||
|
||
function construireBassines() {
|
||
const tuiles = [
|
||
["sel", "Sel (kg)"],
|
||
["poivre", "Poivre (g)"],
|
||
["concasse", "Concassé (g)"],
|
||
["muscade", "Muscade (g)"],
|
||
["ail", "Ail (g)"],
|
||
];
|
||
|
||
$("bassines").innerHTML = bassines
|
||
.map((b, i) => {
|
||
const n = i + 1;
|
||
return `
|
||
<article class="bassine" id="Bassine_${n}">
|
||
<div class="b-head">
|
||
<div>
|
||
<div class="b-name">${b.nom}</div>
|
||
<div class="b-tare">Tare ${b.tare.toFixed(3)} kg</div>
|
||
</div>
|
||
<label class="switch">
|
||
<input type="checkbox" id="isCouenne${n}" />
|
||
<span class="track"></span>
|
||
<span>Couenne</span>
|
||
</label>
|
||
</div>
|
||
<div class="b-input">
|
||
<label for="Poids_bassine_${n}">Poids brut (kg)</label>
|
||
<div class="input-wrap">
|
||
<input type="text" class="brut" id="Poids_bassine_${n}" inputmode="decimal"
|
||
enterkeyhint="next" autocomplete="off" placeholder="0.0" />
|
||
<button type="button" class="clear" aria-label="Effacer ${b.nom}">✕</button>
|
||
</div>
|
||
</div>
|
||
<div class="b-results" data-zone="resultats" hidden>
|
||
<div class="b-net">
|
||
<span class="lbl">Viande nette</span>
|
||
<span><span class="val" data-cle="net"></span><span class="unit">kg</span></span>
|
||
</div>
|
||
<div class="tiles">
|
||
${tuiles
|
||
.map(
|
||
([cle, label]) => `
|
||
<div class="tile"><span class="t-lbl">${label}</span><span class="t-val" data-cle="${cle}"></span></div>`,
|
||
)
|
||
.join("")}
|
||
</div>
|
||
</div>
|
||
<p class="b-warn" data-zone="alerte" hidden>Net négatif ou nul : vérifie la pesée.</p>
|
||
</article>`;
|
||
})
|
||
.join("");
|
||
}
|
||
|
||
// ---------- Calculs ----------
|
||
function calculer() {
|
||
const planche = valeurChamp("Poids_planche");
|
||
const propSel = valeurChamp("Prop_sel");
|
||
const propPoivre = valeurChamp("Prop_poivre");
|
||
const propMuscade = valeurChamp("Prop_muscade");
|
||
const propAil = valeurChamp("Prop_ail");
|
||
const filtre = $("Filtre_pesees").checked;
|
||
|
||
const totaux = { net: 0, sel: 0, poivre: 0, concasse: 0, muscade: 0, ail: 0 };
|
||
let nbPesees = 0;
|
||
|
||
bassines.forEach((b, i) => {
|
||
const n = i + 1;
|
||
const carte = $("Bassine_" + n);
|
||
const brut = valeurChamp("Poids_bassine_" + n);
|
||
const cellule = (cle) => carte.querySelector(`[data-cle="${cle}"]`);
|
||
const zone = (nom) => carte.querySelector(`[data-zone="${nom}"]`);
|
||
|
||
carte.classList.toggle("filled", !!brut);
|
||
zone("resultats").hidden = !brut;
|
||
|
||
// En mode « seulement les pesées », on garde visible la carte en cours de saisie
|
||
const enSaisie = carte.contains(document.activeElement);
|
||
carte.hidden = filtre && !brut && !enSaisie;
|
||
|
||
if (!brut) {
|
||
Object.keys(totaux).forEach((cle) => (cellule(cle).textContent = ""));
|
||
carte.classList.remove("warn");
|
||
zone("alerte").hidden = true;
|
||
return;
|
||
}
|
||
|
||
nbPesees++;
|
||
|
||
// Valeurs arrondies à 3 décimales, comme avant
|
||
const net = parseFloat((brut - b.tare - planche).toFixed(3));
|
||
const couenne = $("isCouenne" + n).checked;
|
||
const resultats = {
|
||
net,
|
||
sel: parseFloat((net * (couenne ? propSel / 2 : propSel)).toFixed(3)),
|
||
poivre: parseFloat((net * propPoivre * 0.6).toFixed(3)),
|
||
concasse: parseFloat((net * propPoivre * 0.4).toFixed(3)),
|
||
muscade: parseFloat((net * propMuscade).toFixed(3)),
|
||
ail: parseFloat((net * propAil).toFixed(3)),
|
||
};
|
||
|
||
Object.entries(resultats).forEach(([cle, v]) => {
|
||
cellule(cle).textContent = v.toFixed(3);
|
||
totaux[cle] += v;
|
||
});
|
||
|
||
const negatif = net <= 0;
|
||
carte.classList.toggle("warn", negatif);
|
||
zone("alerte").hidden = !negatif;
|
||
});
|
||
|
||
colonnes.forEach((c) => ($(c.totalId).textContent = totaux[c.cle].toFixed(3)));
|
||
$("Compteur").textContent = nbPesees;
|
||
|
||
calculerTotalAssaisonne(totaux);
|
||
|
||
// Récapitulatif de la barre du haut
|
||
$("Resume_Net").textContent = $("Total_Net").textContent;
|
||
$("Resume_Rendement").textContent = $("Rendement").textContent;
|
||
}
|
||
|
||
// Rendement : formule inchangée (net total / total acheté × 100)
|
||
function calculerTotalAssaisonne(totaux) {
|
||
const totalNet = parseFloat(totaux.net.toFixed(3));
|
||
const totalSel = parseFloat(totaux.sel.toFixed(3));
|
||
const totalPoivre = parseFloat(totaux.poivre.toFixed(3));
|
||
const totalPoivreConcasse = parseFloat(totaux.concasse.toFixed(3));
|
||
const totalMuscade = parseFloat(totaux.muscade.toFixed(3));
|
||
const totalAil = parseFloat(totaux.ail.toFixed(3));
|
||
|
||
const totalAssaisonne =
|
||
totalNet +
|
||
totalSel +
|
||
totalPoivre / 1000 +
|
||
totalPoivreConcasse / 1000 +
|
||
totalMuscade / 1000 +
|
||
totalAil / 1000;
|
||
$("Total_Assaisonne").textContent = totalAssaisonne.toFixed(3);
|
||
|
||
const totalAchete = valeurChamp("Total_Achete");
|
||
if (totalAchete > 0) {
|
||
const rendement = (totalNet / totalAchete) * 100;
|
||
$("Rendement").textContent = rendement.toFixed(3) + " %";
|
||
} else {
|
||
$("Rendement").textContent = "0 %";
|
||
}
|
||
}
|
||
|
||
// ---------- Sauvegarde locale (48 h) ----------
|
||
const STORAGE_KEY = "calculateurEpices";
|
||
const MAX_AGE = 48 * 60 * 60 * 1000; // 48 h
|
||
|
||
function sauvegarder() {
|
||
try {
|
||
const values = {};
|
||
document.querySelectorAll("input").forEach((input) => {
|
||
values[input.id] = input.type === "checkbox" ? input.checked : input.value;
|
||
});
|
||
localStorage.setItem(STORAGE_KEY, JSON.stringify({ timestamp: Date.now(), values }));
|
||
} catch (e) {
|
||
/* stockage indisponible : on ignore */
|
||
}
|
||
}
|
||
|
||
function restaurer() {
|
||
try {
|
||
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY));
|
||
if (saved && Date.now() - saved.timestamp < MAX_AGE) {
|
||
for (const [id, value] of Object.entries(saved.values)) {
|
||
const input = $(id);
|
||
if (!input) continue;
|
||
if (input.type === "checkbox") input.checked = value;
|
||
else input.value = value;
|
||
}
|
||
} else {
|
||
localStorage.removeItem(STORAGE_KEY);
|
||
}
|
||
} catch (e) {
|
||
/* données illisibles : on repart de zéro */
|
||
}
|
||
}
|
||
|
||
// ---------- Impression et partage ----------
|
||
// window.print() doit être appelé directement dans le toucher (sans délai ni await), sinon Safari iPhone l'ignore.
|
||
// L'événement « beforeprint » confirme que l'impression a démarré ; sinon on propose un plan B.
|
||
function lancerImpression() {
|
||
let demarree = false;
|
||
const marquer = () => (demarree = true);
|
||
window.addEventListener("beforeprint", marquer);
|
||
window.print();
|
||
setTimeout(() => {
|
||
window.removeEventListener("beforeprint", marquer);
|
||
$("aideImpression").hidden = demarree;
|
||
if (!demarree) $("aideImpression").scrollIntoView({ block: "center", behavior: "smooth" });
|
||
}, 800);
|
||
}
|
||
|
||
// Texte du récapitulatif : bassines pesées, totaux et rendement
|
||
function texteRecap() {
|
||
const ligne = (carte, cle) => carte.querySelector(`[data-cle="${cle}"]`).textContent;
|
||
const lignes = ["Calcul des épices – " + new Date().toLocaleDateString("fr-FR"), ""];
|
||
|
||
bassines.forEach((b, i) => {
|
||
const carte = $("Bassine_" + (i + 1));
|
||
if (!valeurChamp("Poids_bassine_" + (i + 1))) return;
|
||
const couenne = $("isCouenne" + (i + 1)).checked ? " (couenne)" : "";
|
||
lignes.push(
|
||
`${b.nom}${couenne} : net ${ligne(carte, "net")} kg · sel ${ligne(carte, "sel")} kg · ` +
|
||
`poivre ${ligne(carte, "poivre")} g · concassé ${ligne(carte, "concasse")} g · ` +
|
||
`muscade ${ligne(carte, "muscade")} g · ail ${ligne(carte, "ail")} g`,
|
||
);
|
||
});
|
||
|
||
lignes.push("", "TOTAUX");
|
||
colonnes.forEach((c) => lignes.push(`${c.titre} : ${$(c.totalId).textContent} ${c.unite}`));
|
||
lignes.push("", `Total acheté : ${$("Total_Achete").value || "–"} kg`);
|
||
lignes.push(`Total assaisonné : ${$("Total_Assaisonne").textContent} kg`);
|
||
lignes.push(`Rendement : ${$("Rendement").textContent}`);
|
||
return lignes.join("\n");
|
||
}
|
||
|
||
function partagerRecap() {
|
||
// Appelé directement dans le toucher, comme exigé par le navigateur
|
||
navigator.share({ title: "Calcul des épices", text: texteRecap() }).catch(() => {
|
||
/* partage annulé : rien à faire */
|
||
});
|
||
}
|
||
|
||
// ---------- Démarrage ----------
|
||
function demarrer() {
|
||
construireReglages();
|
||
construireTotaux();
|
||
construireBassines();
|
||
$("CompteurMax").textContent = bassines.length;
|
||
restaurer();
|
||
calculer();
|
||
|
||
const maj = () => {
|
||
calculer();
|
||
sauvegarder();
|
||
};
|
||
document.body.addEventListener("input", maj);
|
||
document.body.addEventListener("change", maj);
|
||
|
||
// Sélectionne le contenu au focus : on remplace une valeur d'un seul geste
|
||
document.body.addEventListener("focusin", (e) => {
|
||
if (e.target.matches('input[type="text"], input[type="number"]')) {
|
||
setTimeout(() => e.target.select(), 0);
|
||
}
|
||
});
|
||
|
||
// « Entrée / Suivant » passe à la bassine suivante
|
||
document.body.addEventListener("keydown", (e) => {
|
||
if (e.key !== "Enter" || !e.target.matches(".brut")) return;
|
||
e.preventDefault();
|
||
const champs = [...document.querySelectorAll(".brut")].filter((c) => !c.closest("[hidden]"));
|
||
const suivant = champs[champs.indexOf(e.target) + 1];
|
||
if (suivant) {
|
||
suivant.focus();
|
||
suivant.scrollIntoView({ block: "center", behavior: "smooth" });
|
||
} else {
|
||
e.target.blur();
|
||
}
|
||
});
|
||
|
||
// Bouton ✕ : efface la pesée de la bassine
|
||
document.body.addEventListener("click", (e) => {
|
||
const bouton = e.target.closest(".clear");
|
||
if (!bouton) return;
|
||
const champ = bouton.previousElementSibling;
|
||
champ.value = "";
|
||
champ.dispatchEvent(new Event("input", { bubbles: true }));
|
||
champ.focus();
|
||
});
|
||
|
||
$("btnPrint").addEventListener("click", lancerImpression);
|
||
|
||
// Le bouton de partage n'existe que si le navigateur sait partager (iPhone, Android)
|
||
if (navigator.share) $("btnPartager").addEventListener("click", partagerRecap);
|
||
else $("btnPartager").hidden = true;
|
||
|
||
$("btnReset").addEventListener("click", () => {
|
||
if (!confirm("Vider toutes les pesées et le total acheté ? (les réglages sont conservés)")) return;
|
||
bassines.forEach((_, i) => {
|
||
$("Poids_bassine_" + (i + 1)).value = "";
|
||
$("isCouenne" + (i + 1)).checked = false;
|
||
});
|
||
$("Total_Achete").value = "";
|
||
maj();
|
||
window.scrollTo({ top: 0, behavior: "smooth" });
|
||
});
|
||
}
|
||
|
||
demarrer();
|
||
</script>
|
||
</body>
|
||
</html>
|