tailcat-webui 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +29 -0
- package/README.en.md +112 -0
- package/README.md +101 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +398 -0
- package/docs/assets/character.jpg +0 -0
- package/docs/assets/portrait.jpg +0 -0
- package/docs/screenshots/01-overview-light.png +0 -0
- package/docs/screenshots/02-overview-dark.png +0 -0
- package/docs/screenshots/03-ticket-light.png +0 -0
- package/docs/screenshots/04-ticket-qr-light.png +0 -0
- package/docs/screenshots/05-settings-light.png +0 -0
- package/docs/screenshots/06-diagnostics-light.png +0 -0
- package/docs/screenshots/07-mobile-overview.png +0 -0
- package/docs/screenshots/08-install-missing.png +0 -0
- package/docs/screenshots/09-keeper.png +0 -0
- package/package.json +56 -4
- package/src/cli.js +238 -0
- package/src/config.js +167 -0
- package/src/devices.js +163 -0
- package/src/errors.js +24 -0
- package/src/index.js +19 -0
- package/src/installer.js +907 -0
- package/src/manager.js +647 -0
- package/src/parse.js +207 -0
- package/src/paths.js +97 -0
- package/src/preferences.js +190 -0
- package/src/qr.js +37 -0
- package/src/relay.js +76 -0
- package/src/runtime/node.js +281 -0
- package/src/serve-targets.js +127 -0
- package/src/server.js +674 -0
- package/src/settings.js +262 -0
- package/src/supervisor.js +833 -0
- package/vendor/qrcode.mjs +2289 -0
- package/web/api.js +114 -0
- package/web/assets/portrait.jpg +0 -0
- package/web/boot.js +29 -0
- package/web/components.js +558 -0
- package/web/dict.js +663 -0
- package/web/drawer-access.js +318 -0
- package/web/drawer-address.js +271 -0
- package/web/drawer-automation.js +223 -0
- package/web/drawer-relay.js +128 -0
- package/web/drawer-tunnel.js +309 -0
- package/web/drawer.js +302 -0
- package/web/format.js +252 -0
- package/web/icon.png +0 -0
- package/web/index.html +29 -0
- package/web/main.js +471 -0
- package/web/room-diagnostics.js +426 -0
- package/web/room-overview.js +438 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2873 -0
- package/web/ticket.js +212 -0
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM primitives shared by the three rooms.
|
|
3
|
+
*
|
|
4
|
+
* The page has no framework: `h` builds one element, the exporters here build
|
|
5
|
+
* the controls that appear more than once, and the `sync*` helpers let polling
|
|
6
|
+
* update text and attributes without touching a focused input.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { qrGeometry } from './format.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The cat face from the package icon, in its original 48-unit coordinate
|
|
13
|
+
* space. The eyes are separate subpaths so `fill-rule="evenodd"` cuts them out.
|
|
14
|
+
*/
|
|
15
|
+
export const CAT_FACE_PATH =
|
|
16
|
+
'M32.348 13.999s3.445-8.812 1.651-11.998c-.604-1.073-8 1.998-10.723 5.442 0 0-2.586-.86-5.276-.86s-5.276.86-5.276.86C10.001 3.999 2.605.928 2.001 2.001.207 5.187 3.652 13.999 3.652 13.999c-.897 1.722-1.233 4.345-1.555 7.16-.354 3.086.35 5.546.658 6.089.35.617 2.123 2.605 4.484 4.306 3.587 2.583 8.967 3.445 10.761 3.445s7.174-.861 10.761-3.445c2.361-1.701 4.134-3.689 4.484-4.306.308-.543 1.012-3.003.659-6.089-.324-2.814-.659-5.438-1.556-7.16z';
|
|
17
|
+
|
|
18
|
+
export const CAT_EYES_PATH =
|
|
19
|
+
'M14.001 20.001c0 1.105-.896 1.999-2 1.999s-2-.894-2-1.999c0-1.104.896-1.999 2-1.999s2 .896 2 1.999zm11.998 0c0 1.105-.896 1.999-2 1.999-1.105 0-2-.894-2-1.999 0-1.104.895-1.999 2-1.999s2 .896 2 1.999';
|
|
20
|
+
|
|
21
|
+
const ICONS = {
|
|
22
|
+
chevron:
|
|
23
|
+
'<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M4 6l4 4 4-4"/></svg>',
|
|
24
|
+
sun: '<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><circle cx="8" cy="8" r="3.1"/><path d="M8 1.4v1.4M8 13.2v1.4M1.4 8h1.4M13.2 8h1.4M3.4 3.4l1 1M11.6 11.6l1 1M12.6 3.4l-1 1M4.4 11.6l-1 1"/></svg>',
|
|
25
|
+
moon: '<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><path d="M13.4 9.6A5.6 5.6 0 0 1 6.4 2.6a5.7 5.7 0 1 0 7 7Z"/></svg>',
|
|
26
|
+
system:
|
|
27
|
+
'<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><circle cx="8" cy="8" r="5.4"/><path d="M8 2.6v10.8" /><path d="M8 2.6a5.4 5.4 0 0 1 0 10.8Z" fill="currentColor" stroke="none"/></svg>',
|
|
28
|
+
trash:
|
|
29
|
+
'<svg viewBox="0 0 16 16" width="15" height="15" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><path d="M3 4.6h10M6.2 4.6V3.2h3.6v1.4M4.2 4.6l.5 8.2h6.6l.5-8.2M6.7 7v3.6M9.3 7v3.6"/></svg>',
|
|
30
|
+
caution:
|
|
31
|
+
'<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><path d="M8 2.2 14.4 13.4H1.6Z"/><path d="M8 6.2v3.2"/><path d="M8 11.4v.4"/></svg>',
|
|
32
|
+
check:
|
|
33
|
+
'<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><path d="M2.9 8.4 6.4 12 13.1 4.4"/></svg>',
|
|
34
|
+
postquantum:
|
|
35
|
+
'<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"><path d="M12 2.6 20.1 7.3v9.4L12 21.4 3.9 16.7V7.3Z"/><circle cx="12" cy="12" r="2" fill="currentColor" stroke="none"/></svg>',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const TEMPLATE = document.createElement('template');
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build one HTML element.
|
|
42
|
+
* @param {string} tag - tag name.
|
|
43
|
+
* @param {Record<string, unknown>} [props] - class/text/attrs/listeners/style/hidden.
|
|
44
|
+
* @param {...unknown} children - child nodes, strings, arrays, or nothing.
|
|
45
|
+
* @returns {HTMLElement} element.
|
|
46
|
+
*/
|
|
47
|
+
export function h(tag, props = {}, ...children) {
|
|
48
|
+
const el = document.createElement(tag);
|
|
49
|
+
for (const [key, value] of Object.entries(props || {})) {
|
|
50
|
+
if (value === null || value === undefined || value === false) continue;
|
|
51
|
+
if (key === 'class') el.className = String(value);
|
|
52
|
+
else if (key === 'text') el.textContent = String(value);
|
|
53
|
+
else if (key === 'style' && typeof value === 'object') {
|
|
54
|
+
for (const [styleName, styleValue] of Object.entries(value)) {
|
|
55
|
+
if (styleValue !== null && styleValue !== undefined) el.style[styleName] = String(styleValue);
|
|
56
|
+
}
|
|
57
|
+
} else if (key === 'hidden') {
|
|
58
|
+
el.hidden = Boolean(value);
|
|
59
|
+
} else if (key.startsWith('on') && typeof value === 'function') {
|
|
60
|
+
el.addEventListener(key.slice(2).toLowerCase(), value);
|
|
61
|
+
} else if (value === true) {
|
|
62
|
+
el.setAttribute(key, '');
|
|
63
|
+
} else {
|
|
64
|
+
el.setAttribute(key, String(value));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
append(el, children);
|
|
68
|
+
return el;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Append children, flattening arrays and skipping empty values.
|
|
73
|
+
* @param {Element} el - parent.
|
|
74
|
+
* @param {unknown[]} children - child nodes or text.
|
|
75
|
+
*/
|
|
76
|
+
export function append(el, children) {
|
|
77
|
+
for (const child of children) {
|
|
78
|
+
if (child === null || child === undefined || child === false || child === '') continue;
|
|
79
|
+
if (Array.isArray(child)) append(el, child);
|
|
80
|
+
else if (child instanceof Node) el.appendChild(child);
|
|
81
|
+
else el.appendChild(document.createTextNode(String(child)));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Parse a trusted static SVG string into an element.
|
|
87
|
+
* @param {string} markup - fixed markup owned by this file.
|
|
88
|
+
* @returns {SVGElement} root element.
|
|
89
|
+
*/
|
|
90
|
+
export function fromMarkup(markup) {
|
|
91
|
+
TEMPLATE.innerHTML = markup.trim();
|
|
92
|
+
return TEMPLATE.content.firstElementChild;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {string} name - one of the ICONS keys.
|
|
97
|
+
* @returns {Element} decorative icon.
|
|
98
|
+
*/
|
|
99
|
+
export function icon(name) {
|
|
100
|
+
const markup = ICONS[name];
|
|
101
|
+
return markup ? fromMarkup(markup) : h('span', { 'aria-hidden': 'true' });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A small bow, echoing the ribbon in the artwork. Decoration only.
|
|
107
|
+
* @param {number} [size] - rendered width in pixels.
|
|
108
|
+
* @returns {Element} bow.
|
|
109
|
+
*/
|
|
110
|
+
export function ribbonBow(size = 26) {
|
|
111
|
+
return fromMarkup(`<svg class="ribbon-bow" width="${size}" height="${Math.round(size * 0.72)}" viewBox="0 0 32 23" aria-hidden="true" focusable="false">
|
|
112
|
+
<g fill="var(--tw-ribbon)" stroke="var(--tw-ribbon-deep)" stroke-width="1.1" stroke-linejoin="round">
|
|
113
|
+
<path d="M15 11C10.2 3.6 2.2 4.4 3.3 11c-1.1 6.6 6.9 7.4 11.7 0Z"/>
|
|
114
|
+
<path d="M17 11c4.8-7.4 12.8-6.6 11.7 0 1.1 6.6-6.9 7.4-11.7 0Z"/>
|
|
115
|
+
<path d="M15.1 13.2 12.7 20l3.3-1.7 1.2 2.8 1.2-2.8 3.3 1.7-2.4-6.8Z"/>
|
|
116
|
+
<circle cx="16" cy="11" r="2.2" fill="var(--tw-ribbon-soft)" stroke-width="1"/>
|
|
117
|
+
</g>
|
|
118
|
+
</svg>`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The pixel-cat plush from the artwork, drawn as a sticker. `currentColor`
|
|
123
|
+
* carries the head so the cat inverts with the theme; the ears and nose keep
|
|
124
|
+
* the rose accent.
|
|
125
|
+
* @param {number} [size] - rendered size in pixels.
|
|
126
|
+
* @returns {Element} pixel cat.
|
|
127
|
+
*/
|
|
128
|
+
export function pixelCat(size = 28) {
|
|
129
|
+
return fromMarkup(`<svg class="pixel-cat" width="${size}" height="${size}" viewBox="0 0 16 16" shape-rendering="crispEdges" aria-hidden="true" focusable="false">
|
|
130
|
+
<g fill="currentColor">
|
|
131
|
+
<rect x="2" y="1" width="3" height="4"/>
|
|
132
|
+
<rect x="11" y="1" width="3" height="4"/>
|
|
133
|
+
<rect x="4" y="3" width="8" height="2"/>
|
|
134
|
+
<rect x="3" y="4" width="10" height="7"/>
|
|
135
|
+
</g>
|
|
136
|
+
<g fill="var(--tw-ribbon)">
|
|
137
|
+
<rect x="2" y="2" width="1" height="2"/>
|
|
138
|
+
<rect x="13" y="2" width="1" height="2"/>
|
|
139
|
+
<rect x="7" y="8" width="2" height="1"/>
|
|
140
|
+
</g>
|
|
141
|
+
<g fill="var(--tw-surface)">
|
|
142
|
+
<rect x="5" y="6" width="2" height="2"/>
|
|
143
|
+
<rect x="9" y="6" width="2" height="2"/>
|
|
144
|
+
<rect x="4" y="11" width="8" height="3"/>
|
|
145
|
+
</g>
|
|
146
|
+
<g fill="currentColor">
|
|
147
|
+
<rect x="5" y="6" width="1" height="1"/>
|
|
148
|
+
<rect x="9" y="6" width="1" height="1"/>
|
|
149
|
+
<rect x="6" y="12" width="1" height="1"/>
|
|
150
|
+
<rect x="8" y="12" width="1" height="1"/>
|
|
151
|
+
<rect x="7" y="13" width="1" height="1"/>
|
|
152
|
+
</g>
|
|
153
|
+
</svg>`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The ticket's round stamp: the artwork portrait inside the amber ring, with a
|
|
158
|
+
* ribbon pinned to its corner. Decorative; the ticket text carries the facts.
|
|
159
|
+
* @param {number} [size] - pixel size.
|
|
160
|
+
* @returns {HTMLElement} seal.
|
|
161
|
+
*/
|
|
162
|
+
export function catSeal(size = 56) {
|
|
163
|
+
return h('span', { class: 'cat-seal', 'aria-hidden': 'true', style: { width: `${size}px`, height: `${size}px` } },
|
|
164
|
+
h('img', {
|
|
165
|
+
class: 'cat-seal-img',
|
|
166
|
+
src: '/assets/portrait.jpg',
|
|
167
|
+
alt: '',
|
|
168
|
+
width: size,
|
|
169
|
+
height: size,
|
|
170
|
+
decoding: 'async',
|
|
171
|
+
}),
|
|
172
|
+
ribbonBow(Math.max(18, Math.round(size * 0.42))));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Field hint ids only need to be unique inside this document. */
|
|
176
|
+
let fieldSequence = 0;
|
|
177
|
+
|
|
178
|
+
/** The switch knob; the track is the button itself. */
|
|
179
|
+
const SWITCH_KNOB = '<span class="switch-knob"></span>';
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* @param {{ checked: boolean, label: string, onChange: (next: boolean) => void, disabled?: boolean, testid?: string }} options - wiring.
|
|
183
|
+
* @returns {HTMLButtonElement} a real switch with `role="switch"`.
|
|
184
|
+
*/
|
|
185
|
+
export function switchControl({ checked, label, onChange, disabled = false, testid = '' }) {
|
|
186
|
+
const knob = fromMarkup(SWITCH_KNOB);
|
|
187
|
+
const el = h('button', {
|
|
188
|
+
type: 'button',
|
|
189
|
+
class: `switch${checked ? ' is-on' : ''}`,
|
|
190
|
+
role: 'switch',
|
|
191
|
+
'aria-checked': checked ? 'true' : 'false',
|
|
192
|
+
'aria-label': label,
|
|
193
|
+
disabled,
|
|
194
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
195
|
+
}, knob);
|
|
196
|
+
el.addEventListener('click', () => {
|
|
197
|
+
if (el.disabled) return;
|
|
198
|
+
onChange(el.getAttribute('aria-checked') !== 'true');
|
|
199
|
+
});
|
|
200
|
+
return el;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @param {HTMLButtonElement} el - switch element.
|
|
205
|
+
* @param {boolean} checked - next state.
|
|
206
|
+
*/
|
|
207
|
+
export function setSwitch(el, checked) {
|
|
208
|
+
el.setAttribute('aria-checked', checked ? 'true' : 'false');
|
|
209
|
+
el.classList.toggle('is-on', checked);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @param {{ label: string, variant?: 'primary' | 'secondary' | 'ghost' | 'danger', onClick?: Function, disabled?: boolean, title?: string, testid?: string, busy?: boolean, children?: unknown }} options - button.
|
|
214
|
+
* @returns {HTMLButtonElement} button.
|
|
215
|
+
*/
|
|
216
|
+
export function button(options) {
|
|
217
|
+
const { label, variant = 'secondary', onClick, disabled = false, title = '', testid = '', children = null } = options;
|
|
218
|
+
const el = h('button', {
|
|
219
|
+
type: 'button',
|
|
220
|
+
class: `btn btn-${variant}`,
|
|
221
|
+
disabled,
|
|
222
|
+
...(title ? { title } : {}),
|
|
223
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
224
|
+
}, children === null ? label : children);
|
|
225
|
+
if (typeof onClick === 'function') el.addEventListener('click', onClick);
|
|
226
|
+
return el;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @param {{ label: string, onClick: Function, title?: string, testid?: string, disabled?: boolean, danger?: boolean }} options - icon button.
|
|
231
|
+
* @returns {HTMLButtonElement} square ghost button carrying an icon.
|
|
232
|
+
*/
|
|
233
|
+
export function iconButton({ label, onClick, title = '', testid = '', disabled = false, danger = false }) {
|
|
234
|
+
const el = h('button', {
|
|
235
|
+
type: 'button',
|
|
236
|
+
class: `icon-btn${danger ? ' icon-btn-danger' : ''}`,
|
|
237
|
+
'aria-label': label,
|
|
238
|
+
title: title || label,
|
|
239
|
+
disabled,
|
|
240
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
241
|
+
}, icon('trash'));
|
|
242
|
+
el.addEventListener('click', onClick);
|
|
243
|
+
return el;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @param {string} text - button text.
|
|
248
|
+
* @param {string} testid - hook.
|
|
249
|
+
* @returns {HTMLButtonElement} copy button that reports its own result.
|
|
250
|
+
*/
|
|
251
|
+
export function copyButton({ text, t, onResult, variant = 'ghost', label = '', testid = '' }) {
|
|
252
|
+
const idle = label || t('copy');
|
|
253
|
+
const el = button({ label: idle, variant, testid });
|
|
254
|
+
let timer = null;
|
|
255
|
+
el.addEventListener('click', async () => {
|
|
256
|
+
const ok = await copyText(text);
|
|
257
|
+
if (timer !== null) clearTimeout(timer);
|
|
258
|
+
el.textContent = ok ? t('copied') : t('copyFailed');
|
|
259
|
+
el.classList.toggle('is-copied', ok);
|
|
260
|
+
el.classList.toggle('is-copy-failed', !ok);
|
|
261
|
+
timer = setTimeout(() => {
|
|
262
|
+
timer = null;
|
|
263
|
+
el.textContent = idle;
|
|
264
|
+
el.classList.remove('is-copied', 'is-copy-failed');
|
|
265
|
+
}, 1_800);
|
|
266
|
+
if (typeof onResult === 'function') onResult(ok);
|
|
267
|
+
});
|
|
268
|
+
return el;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Copy through the async clipboard API, with a selection fallback for contexts
|
|
273
|
+
* that only allow copying inside a user gesture.
|
|
274
|
+
* @param {string} text - payload.
|
|
275
|
+
* @returns {Promise<boolean>} whether the copy succeeded.
|
|
276
|
+
*/
|
|
277
|
+
export async function copyText(text) {
|
|
278
|
+
try {
|
|
279
|
+
await navigator.clipboard.writeText(text);
|
|
280
|
+
return true;
|
|
281
|
+
} catch {
|
|
282
|
+
try {
|
|
283
|
+
const area = document.createElement('textarea');
|
|
284
|
+
area.value = text;
|
|
285
|
+
area.setAttribute('readonly', '');
|
|
286
|
+
area.style.position = 'fixed';
|
|
287
|
+
area.style.top = '-1000px';
|
|
288
|
+
area.style.opacity = '0';
|
|
289
|
+
document.body.appendChild(area);
|
|
290
|
+
area.select();
|
|
291
|
+
const ok = typeof document.execCommand === 'function' ? document.execCommand('copy') : false;
|
|
292
|
+
area.remove();
|
|
293
|
+
return ok;
|
|
294
|
+
} catch {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* A field row: label, hint, optional override dot, and the control.
|
|
302
|
+
* @param {{ label: string, hint?: string, override?: string, descriptionId?: string, inline?: boolean, control: Node }} options - field.
|
|
303
|
+
* @returns {HTMLElement} row.
|
|
304
|
+
*/
|
|
305
|
+
export function field({ label, hint = '', override = '', inline = false, control }) {
|
|
306
|
+
fieldSequence += 1;
|
|
307
|
+
const hintId = hint ? `field-hint-${fieldSequence}` : '';
|
|
308
|
+
const labelLine = h('span', { class: 'field-name' }, label);
|
|
309
|
+
let dot = null;
|
|
310
|
+
if (override) {
|
|
311
|
+
dot = h('span', {
|
|
312
|
+
class: 'override-dot',
|
|
313
|
+
role: 'img',
|
|
314
|
+
'aria-label': override,
|
|
315
|
+
title: override,
|
|
316
|
+
hidden: true,
|
|
317
|
+
});
|
|
318
|
+
labelLine.prepend(dot);
|
|
319
|
+
}
|
|
320
|
+
const target = control && control.selectEl ? control.selectEl : control;
|
|
321
|
+
if (hintId && target && typeof target.setAttribute === 'function' && !target.hasAttribute('aria-describedby')) {
|
|
322
|
+
target.setAttribute('aria-describedby', hintId);
|
|
323
|
+
}
|
|
324
|
+
const row = h('div', { class: `field${inline ? ' field-inline' : ''}` },
|
|
325
|
+
h('div', { class: 'field-label' },
|
|
326
|
+
h('div', { class: 'field-label-line' }, labelLine),
|
|
327
|
+
hint ? h('p', { class: 'field-hint', id: hintId }, hint) : null),
|
|
328
|
+
h('div', { class: 'field-control' }, control));
|
|
329
|
+
row.overrideDot = dot;
|
|
330
|
+
return row;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Show or hide the "overridden here" dot of one field row.
|
|
335
|
+
* @param {HTMLElement} row - element returned by field().
|
|
336
|
+
* @param {boolean} on - whether the field is overridden.
|
|
337
|
+
*/
|
|
338
|
+
export function setOverride(row, on) {
|
|
339
|
+
if (row && row.overrideDot) {
|
|
340
|
+
if (row.overrideDot.hidden === on) row.overrideDot.hidden = !on;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @param {{ value: string, ariaLabel: string, onInput: Function, placeholder?: string, disabled?: boolean, mono?: boolean, testid?: string, type?: string, min?: number, max?: number, step?: number, inputMode?: string }} options - input wiring.
|
|
346
|
+
* @returns {HTMLInputElement} input.
|
|
347
|
+
*/
|
|
348
|
+
export function textInput(options) {
|
|
349
|
+
const el = h('input', {
|
|
350
|
+
class: `input${options.mono ? ' input-mono' : ''}`,
|
|
351
|
+
type: options.type || 'text',
|
|
352
|
+
value: options.value ?? '',
|
|
353
|
+
placeholder: options.placeholder || '',
|
|
354
|
+
'aria-label': options.ariaLabel,
|
|
355
|
+
disabled: options.disabled === true,
|
|
356
|
+
...(options.testid ? { 'data-testid': options.testid } : {}),
|
|
357
|
+
...(options.min !== undefined ? { min: String(options.min) } : {}),
|
|
358
|
+
...(options.max !== undefined ? { max: String(options.max) } : {}),
|
|
359
|
+
...(options.step !== undefined ? { step: String(options.step) } : {}),
|
|
360
|
+
...(options.inputMode ? { inputmode: options.inputMode } : {}),
|
|
361
|
+
});
|
|
362
|
+
el.addEventListener('input', () => options.onInput(el.value));
|
|
363
|
+
return el;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @param {{ value: string, options: Array<[string, string]>, ariaLabel: string, onChange: Function, disabled?: boolean, testid?: string }} settings - select wiring.
|
|
368
|
+
* @returns {HTMLElement} select wrapper (native select plus a drawn chevron).
|
|
369
|
+
*/
|
|
370
|
+
export function selectControl({ value, options, ariaLabel, onChange, disabled = false, testid = '' }) {
|
|
371
|
+
const select = h('select', {
|
|
372
|
+
class: 'select-input',
|
|
373
|
+
'aria-label': ariaLabel,
|
|
374
|
+
disabled,
|
|
375
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
376
|
+
}, options.map(([optionValue, optionLabel]) => h('option', { value: optionValue }, optionLabel)));
|
|
377
|
+
select.value = value;
|
|
378
|
+
select.addEventListener('change', () => onChange(select.value));
|
|
379
|
+
const wrapper = h('span', { class: `select${disabled ? ' is-disabled' : ''}` }, select, h('span', { class: 'select-chevron', 'aria-hidden': 'true' }));
|
|
380
|
+
wrapper.selectEl = select;
|
|
381
|
+
return wrapper;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* @param {HTMLSelectElement} select - the native element inside a selectControl wrapper.
|
|
386
|
+
* @param {string} value - next value.
|
|
387
|
+
*/
|
|
388
|
+
export function setSelect(select, value) {
|
|
389
|
+
if (select.value !== String(value)) select.value = String(value);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* @param {{ value: string, ariaLabel: string, onInput: Function, placeholder?: string, disabled?: boolean, rows?: number, testid?: string }} options - textarea wiring.
|
|
394
|
+
* @returns {HTMLTextAreaElement} textarea.
|
|
395
|
+
*/
|
|
396
|
+
export function textareaControl(options) {
|
|
397
|
+
const el = h('textarea', {
|
|
398
|
+
class: 'textarea',
|
|
399
|
+
'aria-label': options.ariaLabel,
|
|
400
|
+
placeholder: options.placeholder || '',
|
|
401
|
+
rows: String(options.rows || 6),
|
|
402
|
+
disabled: options.disabled === true,
|
|
403
|
+
...(options.testid ? { 'data-testid': options.testid } : {}),
|
|
404
|
+
});
|
|
405
|
+
el.value = options.value ?? '';
|
|
406
|
+
el.addEventListener('input', () => options.onInput(el.value));
|
|
407
|
+
return el;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @param {{ text: string, t: Function, emptyText?: string, testid?: string, mono?: boolean, ariaLabel?: string }} options - code line.
|
|
412
|
+
* @returns {HTMLElement} scrollable code line with its own copy button.
|
|
413
|
+
*/
|
|
414
|
+
export function codeBlock({ text, t, emptyText = '', testid = '', ariaLabel = '', mono = true }) {
|
|
415
|
+
if (!text) return h('p', { class: 'code-empty' }, emptyText);
|
|
416
|
+
return h('div', { class: 'code-block', ...(ariaLabel ? { 'aria-label': ariaLabel } : {}) },
|
|
417
|
+
h('code', { class: `code-text${mono ? ' is-mono' : ''}`, ...(testid ? { 'data-testid': testid } : {}) }, text),
|
|
418
|
+
copyButton({ text, t }));
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* A page-level notice strip.
|
|
423
|
+
* @param {{ kind: 'ok' | 'error' | 'info', text: string, t: Function, onClose: Function }} options - notice.
|
|
424
|
+
* @returns {HTMLElement} notice with status role.
|
|
425
|
+
*/
|
|
426
|
+
export function noticeBar({ kind, text, t, onClose }) {
|
|
427
|
+
const iconName = kind === 'ok' ? 'check' : kind === 'error' ? 'caution' : 'postquantum';
|
|
428
|
+
return h('div', { class: `notice notice-${kind}`, role: 'status', 'aria-live': 'polite' },
|
|
429
|
+
h('span', { class: 'notice-icon', 'aria-hidden': 'true' }, icon(iconName)),
|
|
430
|
+
h('p', { class: 'notice-text' }, text),
|
|
431
|
+
h('button', {
|
|
432
|
+
type: 'button',
|
|
433
|
+
class: 'notice-close',
|
|
434
|
+
'aria-label': t('dismiss'),
|
|
435
|
+
title: t('dismiss'),
|
|
436
|
+
onclick: onClose,
|
|
437
|
+
}, '×'));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @param {{ tone?: 'danger' | 'warn' | 'info', title?: string, children?: unknown }} options - inline alert.
|
|
442
|
+
* @returns {HTMLElement} alert box.
|
|
443
|
+
*/
|
|
444
|
+
export function alertBox({ tone = 'info', title = '', children }) {
|
|
445
|
+
return h('div', { class: `alert alert-${tone}` },
|
|
446
|
+
title ? h('p', { class: 'alert-title' }, title) : null,
|
|
447
|
+
h('div', { class: 'alert-body' }, children));
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* @param {string} text - pill text.
|
|
452
|
+
* @param {'pq' | 'accent' | 'quiet'} [tone] - colour role.
|
|
453
|
+
* @returns {HTMLElement} small capsule.
|
|
454
|
+
*/
|
|
455
|
+
export function pill(text, tone = 'quiet') {
|
|
456
|
+
return h('span', { class: `pill pill-${tone}` }, text);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @returns {HTMLElement} three loading skeleton lines.
|
|
461
|
+
*/
|
|
462
|
+
export function skeleton() {
|
|
463
|
+
return h('div', { class: 'skeleton', 'aria-hidden': 'true' },
|
|
464
|
+
h('div', { class: 'skeleton-line skeleton-title' }),
|
|
465
|
+
h('div', { class: 'skeleton-line skeleton-body' }),
|
|
466
|
+
h('div', { class: 'skeleton-line skeleton-body-short' }));
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* @param {{ matrix: object, t: Function }} options - pairing QR matrix.
|
|
471
|
+
* @returns {HTMLElement | null} white QR board, or null when the matrix is unusable.
|
|
472
|
+
*/
|
|
473
|
+
export function qrBoard({ matrix, t }) {
|
|
474
|
+
const geometry = qrGeometry(matrix);
|
|
475
|
+
if (!geometry) return null;
|
|
476
|
+
const svgNs = 'http://www.w3.org/2000/svg';
|
|
477
|
+
const svg = document.createElementNS(svgNs, 'svg');
|
|
478
|
+
svg.setAttribute('viewBox', geometry.viewBox);
|
|
479
|
+
svg.setAttribute('role', 'img');
|
|
480
|
+
svg.setAttribute('aria-label', t('qrAlt'));
|
|
481
|
+
svg.setAttribute('shape-rendering', 'crispEdges');
|
|
482
|
+
const board = document.createElementNS(svgNs, 'rect');
|
|
483
|
+
board.setAttribute('width', '100%');
|
|
484
|
+
board.setAttribute('height', '100%');
|
|
485
|
+
board.setAttribute('fill', '#ffffff');
|
|
486
|
+
const path = document.createElementNS(svgNs, 'path');
|
|
487
|
+
path.setAttribute('d', geometry.path);
|
|
488
|
+
path.setAttribute('fill', '#000000');
|
|
489
|
+
svg.append(board, path);
|
|
490
|
+
return h('div', { class: 'qr-board' }, svg);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* @param {HTMLElement} el - element.
|
|
495
|
+
* @param {string} text - next text.
|
|
496
|
+
*/
|
|
497
|
+
export function setText(el, text) {
|
|
498
|
+
if (el.textContent !== text) el.textContent = text;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* @param {HTMLElement} el - element.
|
|
503
|
+
* @param {boolean} hidden - next visibility.
|
|
504
|
+
*/
|
|
505
|
+
export function setHidden(el, hidden) {
|
|
506
|
+
if (el.hidden !== hidden) el.hidden = hidden;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* @param {HTMLElement} el - button or control.
|
|
511
|
+
* @param {boolean} disabled - next disabled state.
|
|
512
|
+
*/
|
|
513
|
+
export function setDisabled(el, disabled) {
|
|
514
|
+
if (el.disabled !== disabled) el.disabled = disabled;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* @param {HTMLElement} el - element.
|
|
519
|
+
* @param {string} name - class name.
|
|
520
|
+
* @param {boolean} on - whether to add.
|
|
521
|
+
*/
|
|
522
|
+
export function setClass(el, name, on) {
|
|
523
|
+
if (el.classList.contains(name) !== on) el.classList.toggle(name, on);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Update an input's value, but never while the user is typing into it.
|
|
528
|
+
* @param {HTMLInputElement | HTMLTextAreaElement} input - control.
|
|
529
|
+
* @param {string} value - next value.
|
|
530
|
+
*/
|
|
531
|
+
export function setValue(input, value) {
|
|
532
|
+
if (input === document.activeElement) return;
|
|
533
|
+
if (input.value !== String(value)) input.value = String(value);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Rebuild an element's children only when the signature changed.
|
|
538
|
+
* @param {HTMLElement} el - container.
|
|
539
|
+
* @param {unknown} signature - comparable identity; callers pass JSON.
|
|
540
|
+
* @param {() => Node[]} build - child factory.
|
|
541
|
+
*/
|
|
542
|
+
export function syncSignature(el, signature, build) {
|
|
543
|
+
const key = typeof signature === 'string' ? signature : JSON.stringify(signature);
|
|
544
|
+
if (el.__syncKey === key) return;
|
|
545
|
+
el.__syncKey = key;
|
|
546
|
+
el.replaceChildren(...build());
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Update the check state of a switch and its disabled flag.
|
|
551
|
+
* @param {HTMLButtonElement} el - switch.
|
|
552
|
+
* @param {boolean} checked - next checked.
|
|
553
|
+
* @param {boolean} disabled - next disabled.
|
|
554
|
+
*/
|
|
555
|
+
export function setSwitchState(el, checked, disabled) {
|
|
556
|
+
setSwitch(el, checked);
|
|
557
|
+
setDisabled(el, disabled);
|
|
558
|
+
}
|