Each vote can require N fresh codes read off the projector (default 3, off for rooms without a projector); voters enrolled with a paper or rescue code are exempt. Rescue codes enroll a phone without camera. Projector keeps the QR up while such a vote is open. Server pings every socket every 25s so proxies keep them open. Composer no longer closes on enter. Schema migrates existing databases.
108 lines
3.5 KiB
JavaScript
108 lines
3.5 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;
|
|
// qr stays up while enrolling, and during a vote that needs a presence scan
|
|
const qrOn = st.enrollment_open || st.presence;
|
|
const focus = st.focus;
|
|
$("#enroll").hidden = !qrOn;
|
|
$("#focus").hidden = !focus;
|
|
$("#idle").hidden = qrOn || !!focus;
|
|
$(".s-main").classList.toggle("split", qrOn && !!focus);
|
|
$("#qr-title").textContent = st.enrollment_open ? "Inscription" : "Scannez pour voter";
|
|
$("#qr-steps").hidden = !st.enrollment_open;
|
|
}
|
|
|
|
function render() {
|
|
$("#s-title").textContent = st.title;
|
|
$("#idle-title").textContent = st.title;
|
|
document.title = st.title;
|
|
$("#voters").textContent = st.voters;
|
|
$("#voters-label").textContent =
|
|
`inscription${st.voters > 1 ? "s" : ""}` + (st.expected ? ` sur ${st.expected}` : "");
|
|
$("#secs").textContent = Math.ceil((st.scans_required * st.interval_ms) / 1000);
|
|
|
|
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} vote${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 && !st.presence) 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.hidden = s === "online";
|
|
c.className = "conn offline";
|
|
c.textContent = "Reconnexion…";
|
|
if (s !== "online") { qr = null; const cv = $("#qr"); cv.getContext("2d").clearRect(0, 0, cv.width, cv.height); }
|
|
},
|
|
});
|