Files
client/app/renderer/js/dom.js
T
2026-10-24 06:34:33 +03:00

109 lines
3.6 KiB
JavaScript

import { getLanguage, onLanguageChange } from './i18n.js';
// DOM helpers. Server-provided text only ever reaches the page through textContent / attributes.
export function $(selector, root = document) {
const el = root.querySelector(selector);
if (!el) throw new Error(`missing element ${selector}`);
return el;
}
/**
* h('button', {class: 'x', onclick, 'aria-label': 'y', dataset: {cid: 1}}, 'text', child)
* Strings become text nodes; there is no innerHTML path.
*/
export function h(tag, props = {}, ...children) {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(props)) {
if (value === undefined || value === null || value === false) continue;
if (key === 'class') el.className = value;
else if (key === 'dataset') Object.assign(el.dataset, value);
else if (key === 'text') setText(el, value);
else if (key.startsWith('on') && typeof value === 'function') el.addEventListener(key.slice(2), value);
else if (key in el && typeof value !== 'string') el[key] = value;
else el.setAttribute(key, value === true ? '' : String(value));
}
append(el, children);
return el;
}
function append(el, children) {
for (const child of children) {
if (child === undefined || child === null || child === false) continue;
if (Array.isArray(child)) append(el, child);
else if (child instanceof Node) el.appendChild(child);
else el.appendChild(document.createTextNode(String(child)));
}
}
const SVG_NS = 'http://www.w3.org/2000/svg';
/** Reference to a symbol of the inline sprite in index.html. */
export function icon(name, extraClass = '') {
const svg = document.createElementNS(SVG_NS, 'svg');
svg.setAttribute('class', `icon ${extraClass}`.trim());
svg.setAttribute('aria-hidden', 'true');
svg.setAttribute('focusable', 'false');
const use = document.createElementNS(SVG_NS, 'use');
use.setAttribute('href', `#i-${name}`);
svg.appendChild(use);
return svg;
}
export function setIcon(svg, name) {
svg.firstElementChild.setAttribute('href', `#i-${name}`);
}
/**
* Keyed list reconciliation: reuses nodes by key so focus and hover survive frequent snapshots.
* create(item) → Element; update(el, item) mutates in place.
*/
export function reconcile(container, items, keyOf, create, update) {
const existing = new Map();
for (const child of Array.from(container.children)) {
if (child.dataset.key) existing.set(child.dataset.key, child);
}
let cursor = container.firstElementChild;
for (const item of items) {
const key = keyOf(item);
let el = existing.get(key);
if (el) {
existing.delete(key);
update(el, item);
} else {
el = create(item);
el.dataset.key = key;
update(el, item);
}
if (el !== cursor) container.insertBefore(el, cursor);
else cursor = cursor.nextElementSibling;
}
for (const el of existing.values()) el.remove();
}
const liveText = new WeakMap();
export function setText(el, text) {
if (typeof text === 'function') {
liveText.set(el, text);
el.dataset.liveText = '';
} else {
liveText.delete(el);
delete el.dataset.liveText;
}
// A dynamic binding supersedes an initial HTML label.
el.removeAttribute('data-i18n');
const value = (typeof text === 'function' ? text() : text) ?? '';
if (el.textContent !== value) el.textContent = value;
}
onLanguageChange(() => {
for (const el of document.querySelectorAll('[data-live-text]')) {
const render = liveText.get(el);
if (render) setText(el, render);
}
});
export function formatTime(date) {
return date.toLocaleTimeString(getLanguage(), { hour: '2-digit', minute: '2-digit' });
}