library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | /** |
| 2 | * The page's step kinds, read from its markup, and the bridge between a |
| 3 | * kind's form controls and a step's fields. |
| 4 | * |
| 5 | * A kind is a <template data-kind> child of the element. Its content is the |
| 6 | * step's editor: ordinary labelled form controls, named after the fields |
| 7 | * (constraint attributes — required, min, maxlength — apply as usual). A kind |
| 8 | * without named controls has no editor. |
| 9 | * |
| 10 | * <template data-kind="wait" data-label="Wait" data-summary="{days} days"> |
| 11 | * <label>Days <input name="days" type="number" min="1" value="3" required></label> |
| 12 | * </template> |
| 13 | * <template data-kind="connected" data-label="Accepted the invitation?" data-branches="Yes|No">…</template> |
| 14 | * |
| 15 | * Values cross as JSON-friendly types: a number or range input gives a |
| 16 | * number (null when empty), a checkbox a boolean, a radio group the checked |
| 17 | * one's value (null when none), anything else its text. |
| 18 | */ |
| 19 | import type { Field, Fields } from "./model"; |
| 20 | |
| 21 | export interface Kind { |
| 22 | name: string; |
| 23 | /** What the page calls it: the card's title and the add menu's entry. */ |
| 24 | label: string; |
| 25 | /** Outcome names of a branching kind, in order; empty for a linear one. */ |
| 26 | branches: string[]; |
| 27 | /** The card's one-line summary, with {field} holes; null joins the values. */ |
| 28 | summary: string | null; |
| 29 | template: HTMLTemplateElement; |
| 30 | /** Whether the kind has fields to edit. */ |
| 31 | editable: boolean; |
| 32 | } |
| 33 | |
| 34 | export type Control = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement; |
| 35 | |
| 36 | /** The named form controls in a container (an editor, or a template's content). */ |
| 37 | export const controlsOf = (root: ParentNode): Control[] => |
| 38 | [...root.querySelectorAll<Control>("input[name], select[name], textarea[name]")].filter(c => !(c instanceof HTMLInputElement && ["submit", "button", "reset", "image", "file"].includes(c.type))); |
| 39 | |
| 40 | /** The kinds declared by the element's <template data-kind> children, by name. */ |
| 41 | export function readKinds(host: Element): Map<string, Kind> { |
| 42 | const kinds = new Map<string, Kind>(); |
| 43 | for (const template of host.children) { |
| 44 | if (!(template instanceof HTMLTemplateElement)) continue; |
| 45 | const name = template.dataset.kind; |
| 46 | if (!name || kinds.has(name)) continue; |
| 47 | kinds.set(name, { |
| 48 | name, |
| 49 | label: template.dataset.label || name, |
| 50 | branches: (template.dataset.branches ?? "") |
| 51 | .split("|") |
| 52 | .map(b => b.trim()) |
| 53 | .filter(Boolean), |
| 54 | summary: template.dataset.summary ?? null, |
| 55 | template, |
| 56 | editable: controlsOf(template.content).length > 0, |
| 57 | }); |
| 58 | } |
| 59 | return kinds; |
| 60 | } |
| 61 | |
| 62 | /** The fields as the controls hold them. */ |
| 63 | export function readFields(root: ParentNode): Fields { |
| 64 | const fields: Fields = {}; |
| 65 | for (const control of controlsOf(root)) { |
| 66 | const name = control.name; |
| 67 | if (control instanceof HTMLInputElement && control.type === "checkbox") fields[name] = control.checked; |
| 68 | else if (control instanceof HTMLInputElement && control.type === "radio") { |
| 69 | if (control.checked) fields[name] = control.value; |
| 70 | else if (!(name in fields)) fields[name] = null; |
| 71 | } else if (control instanceof HTMLInputElement && (control.type === "number" || control.type === "range")) { |
| 72 | fields[name] = control.value === "" || Number.isNaN(Number(control.value)) ? null : Number(control.value); |
| 73 | } else fields[name] = control.value; |
| 74 | } |
| 75 | return fields; |
| 76 | } |
| 77 | |
| 78 | /** The controls set to the fields; a field the step does not have leaves its control at the kind's default. */ |
| 79 | export function writeFields(root: ParentNode, fields: Fields): void { |
| 80 | for (const control of controlsOf(root)) { |
| 81 | if (!(control.name in fields)) continue; |
| 82 | const value: Field = fields[control.name]; |
| 83 | if (control instanceof HTMLInputElement && control.type === "checkbox") control.checked = value === true; |
| 84 | else if (control instanceof HTMLInputElement && control.type === "radio") control.checked = value !== null && String(value) === control.value; |
| 85 | else control.value = value === null ? "" : String(value); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** A new step's fields: the kind's controls as the template sets them. */ |
| 90 | export const defaultsOf = (kind: Kind): Fields => readFields(kind.template.content.cloneNode(true) as DocumentFragment); |
| 91 | |
| 92 | /** The card's one-line summary of a step's fields. */ |
| 93 | export function summarize(kind: Kind | undefined, fields: Fields): string { |
| 94 | const shown = (value: Field | undefined) => (value === null || value === undefined || value === false ? "" : value === true ? "✓" : String(value)); |
| 95 | if (kind?.summary) return kind.summary.replace(/\{([^{}]+)\}/g, (_, name: string) => shown(fields[name.trim()])); |
| 96 | return Object.values(fields).map(shown).filter(Boolean).join(" · "); |
| 97 | } |