// The element: an up/down vote over the server's two forms. The // forms work without JS (each posts its side and the server answers with a // page); the element only makes the round trip feel instant: // // //
// // // //
//
// … // //
//
// // A form whose `valeur` (the host's `field` attribute names another input) is // positive is the up side, negative the down side. On submit the element // cancels the navigation, moves the counts and the pressed state at once // (tally.ts: pressing your side takes the vote back, the other side moves // it), and posts the same form data with fetch, asking for JSON: // // - {"up": n, "down": n, "mine": 1 | -1 | 0}: the server's truth, painted over the guess; // - any other OK answer: the guess stands; // - 403, or {"captcha": true}: the guess is undone and the form is submitted // natively, so the server's page can ask for its captcha; // - any other failure (or no network): the guess is undone and an error // toast says why, the server's {"error": "…"} when it gave one, else the // host's `error` attribute. // // The toast is 's inbound `toast` window event, dispatched // raw (docs/toaster.md: equally valid): importing @pebble/toaster's helper // would bundle and register the whole toaster here. The page provides its // own . // // The buttons are the state of record: data-count, aria-pressed, aria-label // (" (n)", so the words stay the server's) and the // [data-count-label] text. While a vote is in flight the host carries // [data-busy] and aria-busy, and further presses are ignored (and still // cancelled, so a double tap never posts twice). The pressed button carries // the class `bounce` for 400ms (vote.css; none under reduced motion). Each // button gets [data-side="up" | "down"] and the host [data-mine], for CSS. import { define, on, onCleanup, subscribe } from "@pebble/pebble"; import { DEV } from "./dev"; import { fromAnswer, press, type Tally } from "./tally"; import styles from "./vote.css"; export { fromAnswer, press, type Mine, type Tally } from "./tally"; /** How long the pressed button keeps the `bounce` class; vote.css's animation lasts as long. */ export const BOUNCE_MS = 400; const FALLBACK_ERROR = "Your vote could not be saved."; interface Side { form: HTMLFormElement; button: HTMLButtonElement; /** The words before " (n)" in the button's aria-label. */ label: string; } /** What the form would send: `multipart/form-data` only when its enctype says so. */ export function bodyOf(form: HTMLFormElement): URLSearchParams | FormData { const data = new FormData(form); if (form.getAttribute("enctype")?.toLowerCase() === "multipart/form-data") return data; const params = new URLSearchParams(); for (const [name, value] of data) if (typeof value === "string") params.append(name, value); return params; } /** The button's words: its data-label, else its aria-label without the trailing " (n)", else its text. */ function labelOf(button: HTMLButtonElement): string { return ( button.dataset.label ?? button.getAttribute("aria-label")?.replace(/\s*\(\d+\)\s*$/, "") ?? button.textContent?.trim() ?? "" ); } const countOf = (button: HTMLButtonElement): number => { const n = parseInt(button.dataset.count ?? "", 10); return Number.isFinite(n) && n >= 0 ? n : 0; }; function paintSide(side: Side, count: number, pressed: boolean): void { side.button.dataset.count = String(count); side.button.setAttribute("aria-pressed", String(pressed)); if (side.label) side.button.setAttribute("aria-label", `${side.label} (${count})`); const text = side.button.querySelector("[data-count-label]"); if (text) text.textContent = String(count); } /** The server's {"error": "…"}, when the answer carries one. */ function errorOf(answer: unknown): string | null { const error = typeof answer === "object" && answer !== null ? (answer as Record).error : undefined; return typeof error === "string" && error.trim() ? error : null; } define("pebble-vote", { shadow: false, scoped: false, styles }, (_props, host) => { const field = host.getAttribute("field") || "valeur"; const sides = new Map<1 | -1, Side>(); for (const form of host.querySelectorAll("form")) { const input = form.elements.namedItem(field); const value = Number(input instanceof HTMLInputElement ? input.value : NaN); const button = form.querySelector("button"); if (!button || !Number.isFinite(value) || value === 0) continue; sides.set(value > 0 ? 1 : -1, { form, button, label: labelOf(button) }); } const up = sides.get(1); const down = sides.get(-1); if (!up || !down) { if (DEV) console.warn( `: needs two forms, each with a