/** * vapor-chamber - display bindings on plain HTML, `bindDisplay`. * * `data-vc-text`, `data-vc-show` and `data-vc-each` follow a store's field * events (`$onField`); `data-vc-pending` follows the bus's command state. No * second reactivity system: one subscription per bound element, written on * the store's own per-field event. */ import { DEV } from './dev'; import { _report } from './report'; import type { BaseBus } from './command-bus'; import { _storeOf } from './applied-remotely'; import { commandState, type CommandState } from './command-state'; import { _mountAttribute, _unmountCommand } from './command-binding'; /** The class the command binding sets while a press is in flight; a pending binding sets the same. */ const LOADING_CLASS = 'vc-loading'; /** What a row's command binding needs: mount one element, unmount it. */ type RowCommands = { mount(el: Element): void; unmount(el: Element): void }; /** * `value` read down `parts`, each an own property: an inherited name * (`constructor`) reads undefined, as src/dict.ts rules for external keys. */ function read(value: unknown, parts: string[], from: number): unknown { let v = value; for (let i = from; i < parts.length; i++) { if (v === null || typeof v !== 'object' || !Object.hasOwn(v, parts[i])) return undefined; v = (v as Record)[parts[i]]; } return v; } /** Shown: truthy, and not an empty array. */ const shown = (v: unknown): boolean => (Array.isArray(v) ? v.length > 0 : !!v); function text(el: Element, v: unknown): void { el.textContent = v === null || v === undefined ? '' : String(v); } /** A row's own bindings, read from its item: `data-vc-text` and `data-vc-show`. */ function renderRow(row: DocumentFragment, item: unknown): void { for (const el of row.querySelectorAll('[data-vc-text]')) text(el, read(item, el.getAttribute('data-vc-text')!.split('.'), 0)); for (const el of row.querySelectorAll('[data-vc-show]')) (el as HTMLElement).hidden = !shown(read(item, el.getAttribute('data-vc-show')!.split('.'), 0)); } /** * bindDisplay - display bindings on plain HTML: every bound element under * `root` (and `root` itself) follows a store of `bus` or its command state. * * A path is `.[.]`, rendered at bind time and on each * write of the field (the store's `$onField`, one subscription per element): * `data-vc-text` sets the text, `data-vc-show` hides the element unless the * value is truthy and not an empty array, `data-vc-each` clones its * `