Starlette backend with sqlite, rotating HMAC QR tokens pushed to a key-protected projector page, multi-scan enrollment, optional one-time paper codes, anonymous ballots split from participation, live question push over websockets, e2e tests over HTTP and TLS.
107 lines
3.4 KiB
JavaScript
107 lines
3.4 KiB
JavaScript
import { $, h, pollId, takeKey, liveSocket, drawQR, resultsView, fill, pct } from "./common.js";
|
|
|
|
const pid = pollId();
|
|
const key = takeKey(`rv_screen_${pid}`);
|
|
let st = null;
|
|
let qr = null;
|
|
let raf = 0;
|
|
|
|
function layout() {
|
|
if (!st) return;
|
|
const enroll = st.enrollment_open;
|
|
const focus = st.focus;
|
|
$("#enroll").hidden = !enroll;
|
|
$("#focus").hidden = !focus;
|
|
$("#idle").hidden = enroll || !!focus;
|
|
$(".s-main").classList.toggle("split", enroll && !!focus);
|
|
}
|
|
|
|
function render() {
|
|
$("#s-title").textContent = st.title;
|
|
$("#idle-title").textContent = st.title;
|
|
document.title = st.title;
|
|
$("#voters").textContent = st.voters;
|
|
$("#voters-label").textContent =
|
|
`inscrit${st.voters > 1 ? "s" : ""}` + (st.expected ? ` / ${st.expected}` : "");
|
|
$("#secs").textContent = Math.ceil((st.scans_required * st.interval_ms) / 1000);
|
|
const b = $("#enroll-badge");
|
|
b.className = `badge ${st.enrollment_open ? "open" : "closed"}`;
|
|
b.textContent = st.enrollment_open ? "Inscriptions ouvertes" : "Inscriptions closes";
|
|
|
|
const f = st.focus;
|
|
const box = $("#focus");
|
|
box.replaceChildren();
|
|
if (f) {
|
|
box.append(h("div", { class: "q-text" }, f.text));
|
|
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), ` vote${f.participants > 1 ? "s" : ""} sur ${st.voters}`,
|
|
h("div", { class: "progress" }, fill(pct(f.participants, st.voters), ""))),
|
|
);
|
|
} else if (f.results) {
|
|
box.append(resultsView(f));
|
|
} else {
|
|
box.append(h("p", { class: "status" }, `Vote clos, ${f.participants} votant${f.participants > 1 ? "s" : ""}`));
|
|
}
|
|
}
|
|
layout();
|
|
}
|
|
|
|
function paintQR() {
|
|
if (!qr) return;
|
|
const c = $("#qr");
|
|
const px = Math.min(c.clientWidth || 600, c.clientHeight || 600) * (window.devicePixelRatio || 1);
|
|
drawQR(c, qr.modules, px);
|
|
}
|
|
|
|
function animateTick() {
|
|
cancelAnimationFrame(raf);
|
|
const bar = $("#tick-bar");
|
|
const step = () => {
|
|
if (!qr) return;
|
|
const left = qr.valid_until - (Date.now() - skew);
|
|
bar.style.width = `${Math.max(0, Math.min(100, (left / qr.interval_ms) * 100))}%`;
|
|
raf = requestAnimationFrame(step);
|
|
};
|
|
step();
|
|
}
|
|
|
|
let skew = 0;
|
|
window.addEventListener("resize", paintQR);
|
|
|
|
$("#fs").addEventListener("click", () => document.documentElement.requestFullscreen?.());
|
|
|
|
liveSocket(`/ws/${pid}/screen`, {
|
|
auth: key,
|
|
onMessage(msg) {
|
|
if (msg.type === "state") {
|
|
st = msg;
|
|
if (!st.enrollment_open) qr = null;
|
|
render();
|
|
} else if (msg.type === "qr") {
|
|
// server clock drives validity, estimate offset so the countdown bar is right
|
|
skew = Date.now() - msg.slot * msg.interval_ms;
|
|
qr = msg;
|
|
paintQR();
|
|
animateTick();
|
|
const host = new URL(msg.url).hostname;
|
|
const local = ["localhost", "127.0.0.1", "::1", "[::1]"].includes(host);
|
|
const w = $("#url-warn");
|
|
w.hidden = !local;
|
|
if (local) w.textContent = `QR vers ${host} : injoignable depuis un téléphone. Définissez PUBLIC_URL.`;
|
|
}
|
|
},
|
|
onStatus(s) {
|
|
const c = $("#conn");
|
|
if (s.startsWith("denied")) {
|
|
$("#denied").hidden = false;
|
|
return;
|
|
}
|
|
c.className = `conn ${s}`;
|
|
c.textContent = s === "online" ? "en direct" : "reconnexion…";
|
|
if (s !== "online") { qr = null; const cv = $("#qr"); cv.getContext("2d").clearRect(0, 0, cv.width, cv.height); }
|
|
},
|
|
});
|