Hand-added votes per question (paper, show of hands): counted in the turnout and shown hatched orange at the end of each result bar, kept apart from phone ballots

This commit is contained in:
2026-11-17 14:42:54 +01:00
parent ad6c3c6f57
commit c924107f1d
11 changed files with 164 additions and 32 deletions
+32 -3
View File
@@ -116,13 +116,14 @@ function questionCard(q) {
actions.push(act("Lancer le vote", "open", "small go"));
actions.push(act("Supprimer", "delete", "small danger", "Supprimer ce brouillon ?"));
} else if (q.state === "open") {
actions.push(act("Clore le vote", "close", "small primary", `Clore le vote (${q.participants} vote${q.participants > 1 ? "s" : ""}) ?`));
actions.push(act("Clore le vote", "close", "small primary", `Clore le vote (${q.participants + q.extra_ballots} vote${q.participants + q.extra_ballots > 1 ? "s" : ""}) ?`));
} else {
actions.push(q.results_public
? act("Masquer les résultats", "unpublish")
: act("Publier les résultats", "publish", "small primary", "Publier les résultats ? Le vote ne pourra plus être rouvert."));
if (!q.shown) actions.push(act("Rouvrir le vote", "reopen"));
}
if (q.state !== "draft") actions.push(h("button", { class: "small", type: "button", onclick: () => editExtra(q) }, "Votes en plus"));
if (q.state !== "draft" && !focused) actions.push(act([icon("screen", 15), "Afficher à l'écran"], "focus"));
const badges = [h("span", { class: `badge ${q.state}` }, STATE_LABEL[q.state])];
@@ -132,17 +133,45 @@ function questionCard(q) {
return h("article", { class: `q ${q.state}` },
h("div", { class: "q-head" }, h("div", {}, badges),
q.state !== "draft" ? h("span", { class: "q-meta" }, `${q.participants} / ${voters}`) : null),
q.state !== "draft" ? h("span", { class: "q-meta" }, `${q.participants} / ${voters}`,
q.extra_ballots ? h("span", { class: "extra-note" }, ` + ${q.extra_ballots}`) : null) : null),
h("div", { class: "q-text" }, q.text),
q.state === "closed"
? resultsView(q)
: h("div", { class: "chips" }, q.options.map((o) => h("span", { class: "chip" }, o)),
q.allow_blank ? h("span", { class: "chip ghost" }, "Blanc") : null),
q.state === "open" ? h("div", { class: "progress" }, fill(pct(q.participants, voters), "")) : null,
q.state === "open" ? h("div", { class: "progress" }, fill(pct(q.participants, voters + q.extra_ballots), ""),
q.extra_ballots ? fill(pct(q.extra_ballots, voters + q.extra_ballots), "p-extra") : null) : null,
h("div", { class: "actions" }, actions),
);
}
// votes added by hand, in a dialog: the question list re-renders on every incoming vote
async function editExtra(q) {
const dlg = $("#extra-dlg"), e = q.extra, inputs = [];
const field = (label, value) => {
const inp = h("input", { type: "number", min: 0, max: 100000, value: String(value), inputmode: "numeric" });
inputs.push(inp);
return h("label", { class: "extra-row" }, h("span", {}, label), inp);
};
$("#extra-q").textContent = q.text;
$("#extra-fields").replaceChildren(...[
...q.options.map((o, i) => field(o, e.counts[i] || 0)),
q.allow_blank ? field("Blanc", e.blank) : null,
q.kind === "multi" ? field("Bulletins (choix multiple)", e.ballots) : null,
].filter(Boolean));
dlg.returnValue = "";
dlg.showModal();
await new Promise((res) => dlg.addEventListener("close", res, { once: true }));
if (dlg.returnValue !== "ok") return;
const vals = inputs.map((i) => Math.max(0, Math.floor(+i.value || 0)));
const n = q.options.length;
const payload = { counts: vals.slice(0, n), blank: q.allow_blank ? vals[n] : 0 };
if (q.kind === "multi") payload.ballots = vals[vals.length - 1];
try { await call("POST", `/questions/${q.id}/extra`, payload); toast("Votes ajoutés", "ok"); }
catch (err) { toast(err.code === "bad_extra" ? "Nombre de bulletins incohérent" : errMsg(err), "bad"); }
}
// composer
const composer = $("#composer");
+24 -9
View File
@@ -117,23 +117,38 @@ export function fill(percent, cls = "bar-fill") {
export function resultsView(q) {
const r = q.results;
if (!r) return null;
const base = r.ballots - r.blank;
const max = Math.max(1, ...r.counts);
const rows = q.options.map((label, i) =>
h("div", { class: "bar-row" + (r.counts[i] === max && r.counts[i] > 0 ? " lead" : "") },
// votes added by hand ride at the end of each bar, hatched orange, so nobody mistakes them for phone ballots
const ex = r.extra || { counts: r.counts.map(() => 0), blank: 0, ballots: 0 };
const tot = r.counts.map((c, i) => c + (ex.counts[i] || 0));
const ballots = r.ballots + ex.ballots, blank = r.blank + ex.blank, base = ballots - blank;
const max = Math.max(1, ...tot);
const rows = q.options.map((label, i) => {
const stack = h("div", { class: "bar-stack" },
seg("bar-fill", r.counts[i]), ex.counts[i] ? seg("bar-extra", ex.counts[i]) : null);
stack.style.width = `${pct(tot[i], max)}%`;
return h("div", { class: "bar-row" + (tot[i] === max && tot[i] > 0 ? " lead" : "") },
h("div", { class: "bar-label" }, label),
h("div", { class: "bar-track" }, fill(pct(r.counts[i], max))),
h("div", { class: "bar-num" }, r.counts[i], h("small", {}, ` ${pct(r.counts[i], base).toLocaleString("fr-FR")} %`)),
));
h("div", { class: "bar-track" }, stack),
h("div", { class: "bar-num" }, tot[i], h("small", {}, ` ${pct(tot[i], base).toLocaleString("fr-FR")} %`)),
);
});
return h("div", { class: "results" },
rows,
h("p", { class: "results-foot" },
`${r.ballots} bulletin${r.ballots > 1 ? "s" : ""}`,
q.allow_blank ? `, dont ${r.blank} blanc${r.blank > 1 ? "s" : ""}` : "",
`${ballots} bulletin${ballots > 1 ? "s" : ""}`,
q.allow_blank ? `, dont ${blank} blanc${blank > 1 ? "s" : ""}` : "",
ex.ballots ? h("span", { class: "extra-note" }, `, dont ${ex.ballots} ajouté${ex.ballots > 1 ? "s" : ""} à la main`) : "",
),
);
}
// a flex share of a bar (CSSOM write, the CSP blocks style="")
function seg(cls, n) {
const el = h("div", { class: cls });
el.style.flexGrow = String(n);
return el;
}
export const STATE_LABEL = { draft: "Brouillon", open: "Vote ouvert", closed: "Clos" };
export function toast(msg, kind = "info") {
+18 -9
View File
@@ -1,4 +1,4 @@
import { $, h, pollId, takeKey, liveSocket, drawQR, resultsView, fill, pct } from "./common.js";
import { $, h, pollId, takeKey, liveSocket, drawQR, resultsView, pct } from "./common.js";
import { render as drawRing, DISC } from "./ringcode.js";
const pid = pollId();
@@ -80,13 +80,10 @@ function render() {
const f = st.focus;
const box = $("#focus");
const view = f ? `${f.id}|${f.state}|${f.results ? 1 : 0}|${f.text}` : "";
const view = f ? `${f.id}|${f.state}|${f.results ? JSON.stringify(f.results) : 0}|${f.text}` : "";
if (f && view === focusView && f.state === "open") {
// same question still open: only the turnout moves, in place so the bar slides
const t = box.querySelector(".turnout");
t.querySelector("b").textContent = f.participants;
t.querySelector(".t-of").textContent = ` vote${f.participants > 1 ? "s" : ""} sur ${st.voters}`;
t.querySelector(".progress > div").style.width = `${pct(f.participants, st.voters)}%`;
paintTurnout(box.querySelector(".turnout"), f);
} else if (view !== focusView) {
focusView = view;
if (f) {
@@ -95,9 +92,9 @@ function render() {
if (f.state === "open") {
box.append(
h("ul", { class: "opts" }, f.options.map((o) => h("li", {}, o))),
h("div", { class: "turnout" },
h("b", {}, f.participants), h("span", { class: "t-of" }, ` vote${f.participants > 1 ? "s" : ""} sur ${st.voters}`),
h("div", { class: "progress" }, fill(pct(f.participants, st.voters), ""))),
paintTurnout(h("div", { class: "turnout" },
h("b", {}), h("span", { class: "t-of" }),
h("div", { class: "progress" }, h("div", {}), h("div", { class: "p-extra" }))), f),
);
} else if (f.results) {
box.append(resultsView(f));
@@ -110,6 +107,18 @@ function render() {
layout();
}
// "9 votes sur 10, dont 3 ajoutés": hand-added votes count in, and show hatched at the end of the bar
function paintTurnout(t, f) {
const extra = f.extra_ballots || 0, all = f.participants + extra, of = st.voters + extra;
t.querySelector("b").textContent = all;
t.querySelector(".t-of").textContent = ` vote${all > 1 ? "s" : ""} sur ${of}` +
(extra ? `, dont ${extra} ajouté${extra > 1 ? "s" : ""}` : "");
const [phone, added] = t.querySelectorAll(".progress > div");
phone.style.width = `${pct(f.participants, of)}%`;
added.style.width = `${pct(extra, of)}%`;
return t;
}
function paintQR() {
if (!qr || qr.type !== "qr") return;
const c = $("#qr");