Results export as plain text: procès-verbal, compact, liste, barres, markdown and csv templates with a preview, copy and download, from the share tab or a closed question; tally maths shared with the results bars

This commit is contained in:
2026-11-19 01:01:54 +01:00
parent 3ebbb67817
commit 41b4ac1a78
5 changed files with 258 additions and 18 deletions
+67
View File
@@ -1,9 +1,11 @@
import { $, h, api, pollId, takeKey, liveSocket, resultsView, STATE_LABEL, toast, fill, pct, icon } from "./common.js";
import { FORMATS, exportText } from "./export.js";
const pid = pollId();
const key = takeKey(`rv_admin_${pid}`);
const base = `/api/polls/${pid}`;
const TAB_KEY = "rv_admin_tab";
const FMT_KEY = "rv_export_fmt";
let state = null;
const TEMPLATES = [
@@ -127,6 +129,7 @@ function questionCard(q) {
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 === "closed") actions.push(h("button", { class: "small", type: "button", onclick: () => openExport(q.id) }, "Exporter"));
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])];
@@ -303,6 +306,70 @@ $("#export").addEventListener("click", async () => {
} catch (e) { toast(errMsg(e), "bad"); }
});
// text export, built in the browser from the closed tallies the socket already sends
const exDlg = $("#export-dlg");
let fmt = FORMATS[0];
try { fmt = FORMATS.find((f) => f.id === localStorage.getItem(FMT_KEY)) || fmt; } catch { /* private mode */ }
let exText = "";
for (const f of FORMATS) {
$("#ex-formats").append(h("button", {
class: "small", type: "button", role: "radio", dataset: { fmt: f.id },
onclick: () => {
fmt = f;
try { localStorage.setItem(FMT_KEY, f.id); } catch { /* private mode */ }
renderExport();
},
}, f.label));
}
function closedQuestions() {
return state.questions.filter((q) => q.state === "closed").sort((a, b) => a.id - b.id);
}
function renderExport() {
for (const b of $("#ex-formats").children) b.setAttribute("aria-checked", String(b.dataset.fmt === fmt.id));
const scope = $("#ex-scope").value;
const qs = closedQuestions().filter((q) => scope === "all" || String(q.id) === scope);
const now = new Date().toLocaleString("fr-FR", { dateStyle: "long", timeStyle: "short" });
exText = qs.length ? exportText(fmt, state.poll, state.voters, qs, now) : "";
const pre = $("#ex-preview");
pre.classList.toggle("empty", !exText);
pre.textContent = exText || "Aucune question close.";
$("#ex-copy").disabled = $("#ex-download").disabled = !exText;
}
function openExport(qid = "all") {
const qs = closedQuestions();
const sel = $("#ex-scope");
sel.replaceChildren(h("option", { value: "all" }, "Toutes les questions closes"),
...qs.map((q) => h("option", { value: String(q.id) }, q.text)));
sel.value = String(qid);
sel.hidden = qs.length < 2;
renderExport();
exDlg.showModal();
}
$("#ex-scope").addEventListener("change", renderExport);
$("#export-text").addEventListener("click", () => openExport());
$("#ex-copy").addEventListener("click", async () => {
try { await navigator.clipboard.writeText(exText); toast("Copié"); }
catch {
// prompt() would flatten the lines, select the preview so cmd+c works
getSelection().selectAllChildren($("#ex-preview"));
toast("Texte sélectionné");
}
});
$("#ex-download").addEventListener("click", () => {
// bom so excel reads the accents in the csv
const body = fmt.ext === "csv" ? "\ufeff" + exText : exText;
const blob = new Blob([body], { type: `${fmt.mime};charset=utf-8` });
const a = h("a", { href: URL.createObjectURL(blob), download: `rotatyvote-${pid}.${fmt.ext}` });
a.click();
setTimeout(() => URL.revokeObjectURL(a.href), 1000);
});
liveSocket(`/ws/${pid}/admin`, {
auth: key,
onMessage(msg) {