tailcat-webui 0.0.0-stage → 0.1.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 +23 -0
- package/README.en.md +104 -0
- package/README.md +93 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +378 -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/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 +669 -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/boot.js +29 -0
- package/web/components.js +504 -0
- package/web/dict.js +655 -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.svg +11 -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 +416 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2741 -0
- package/web/ticket.js +212 -0
package/web/api.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The only place that knows the local HTTP contract.
|
|
3
|
+
*
|
|
4
|
+
* `GET /api/state` returns the snapshot; `POST /api/action` carries one action
|
|
5
|
+
* plus its payload and answers with the next snapshot. Every failure becomes an
|
|
6
|
+
* `ApiError` whose `message` is what the interface shows to the user.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The header the server requires on every write. */
|
|
10
|
+
const ACTION_HEADER = 'x-tailcat-webui';
|
|
11
|
+
const REQUEST_TIMEOUT_MS = 15_000;
|
|
12
|
+
const ACTION_TIMEOUT_MS = 5 * 60_000;
|
|
13
|
+
|
|
14
|
+
/** A request that failed; `code` mirrors the server error code when present. */
|
|
15
|
+
export class ApiError extends Error {
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} message - operator-facing message.
|
|
18
|
+
* @param {string} code - server error code, or `network`/`http`.
|
|
19
|
+
* @param {number} [status] - HTTP status.
|
|
20
|
+
*/
|
|
21
|
+
constructor(message, code = 'http', status = 0) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'ApiError';
|
|
24
|
+
this.code = code;
|
|
25
|
+
this.status = status;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {Response} response - fetch response.
|
|
31
|
+
* @returns {Promise<object | null>} parsed body, or null.
|
|
32
|
+
*/
|
|
33
|
+
async function readJson(response) {
|
|
34
|
+
try {
|
|
35
|
+
return await response.json();
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {object | null} data - parsed body.
|
|
43
|
+
* @param {number} status - HTTP status.
|
|
44
|
+
* @returns {ApiError} normalized failure.
|
|
45
|
+
*/
|
|
46
|
+
function failure(data, status) {
|
|
47
|
+
const error = data && typeof data === 'object' && data.error && typeof data.error === 'object' ? data.error : null;
|
|
48
|
+
const message = error && typeof error.message === 'string' && error.message !== '' ? error.message : `HTTP ${status}`;
|
|
49
|
+
const code = error && typeof error.code === 'string' && error.code !== '' ? error.code : 'http';
|
|
50
|
+
return new ApiError(message, code, status);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} path - request path.
|
|
55
|
+
* @param {RequestInit} options - fetch options.
|
|
56
|
+
* @param {number} timeoutMs - abort bound.
|
|
57
|
+
* @returns {Promise<object>} response body.
|
|
58
|
+
*/
|
|
59
|
+
async function requestJson(path, options, timeoutMs) {
|
|
60
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
61
|
+
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
62
|
+
let response;
|
|
63
|
+
try {
|
|
64
|
+
response = await fetch(path, controller ? { ...options, signal: controller.signal } : options);
|
|
65
|
+
} catch {
|
|
66
|
+
throw new ApiError('Could not reach the local service.', 'network');
|
|
67
|
+
} finally {
|
|
68
|
+
if (timer !== null) clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
const data = await readJson(response);
|
|
71
|
+
if (!response.ok || !data || data.ok !== true) throw failure(data, response.status);
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {{ baseUrl?: string }} [options] - optional absolute base for tests.
|
|
77
|
+
* @returns {{ state: () => Promise<object>, action: (action: string, payload?: object) => Promise<object> }} client.
|
|
78
|
+
*/
|
|
79
|
+
export function createApi(options = {}) {
|
|
80
|
+
const base = typeof options.baseUrl === 'string' ? options.baseUrl.replace(/\/$/, '') : '';
|
|
81
|
+
return {
|
|
82
|
+
/**
|
|
83
|
+
* @returns {Promise<object>} the current snapshot (plus `server`).
|
|
84
|
+
*/
|
|
85
|
+
state() {
|
|
86
|
+
return requestJson(`${base}/api/state`, {
|
|
87
|
+
method: 'GET',
|
|
88
|
+
headers: { accept: 'application/json' },
|
|
89
|
+
cache: 'no-store',
|
|
90
|
+
credentials: 'same-origin',
|
|
91
|
+
}, REQUEST_TIMEOUT_MS);
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {string} action - one of start/stop/restart/rotate/settings/devices/derpMap/install.
|
|
96
|
+
* @param {object} [payload] - action payload; merged next to `action`.
|
|
97
|
+
* @returns {Promise<object>} the snapshot after the action.
|
|
98
|
+
*/
|
|
99
|
+
action(action, payload = {}) {
|
|
100
|
+
if (typeof action !== 'string' || action === '') throw new ApiError('Missing action name.', 'invalid-action');
|
|
101
|
+
return requestJson(`${base}/api/action`, {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: {
|
|
104
|
+
accept: 'application/json',
|
|
105
|
+
'content-type': 'application/json',
|
|
106
|
+
[ACTION_HEADER]: '1',
|
|
107
|
+
},
|
|
108
|
+
cache: 'no-store',
|
|
109
|
+
credentials: 'same-origin',
|
|
110
|
+
body: JSON.stringify({ action, ...payload }),
|
|
111
|
+
}, ACTION_TIMEOUT_MS);
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/web/boot.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Runs before the stylesheet so the first paint already has the right theme and
|
|
3
|
+
* language. Kept as a classic script on purpose: module scripts run after the
|
|
4
|
+
* document is parsed, which would flash the wrong colours in dark mode.
|
|
5
|
+
*/
|
|
6
|
+
(function () {
|
|
7
|
+
var root = document.documentElement;
|
|
8
|
+
var theme = 'system';
|
|
9
|
+
var lang = null;
|
|
10
|
+
try {
|
|
11
|
+
var storedTheme = window.localStorage.getItem('tailcat-webui.theme');
|
|
12
|
+
if (storedTheme === 'light' || storedTheme === 'dark' || storedTheme === 'system') theme = storedTheme;
|
|
13
|
+
var storedLang = window.localStorage.getItem('tailcat-webui.lang');
|
|
14
|
+
if (storedLang === 'zh' || storedLang === 'en') lang = storedLang;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
/* Storage is optional; the system preference still applies. */
|
|
17
|
+
}
|
|
18
|
+
var dark = theme === 'dark';
|
|
19
|
+
if (theme === 'system' && typeof window.matchMedia === 'function') {
|
|
20
|
+
dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
21
|
+
}
|
|
22
|
+
root.setAttribute('data-theme', dark ? 'dark' : 'light');
|
|
23
|
+
root.setAttribute('data-theme-mode', theme);
|
|
24
|
+
if (lang === null) {
|
|
25
|
+
var browser = (window.navigator && window.navigator.language) || '';
|
|
26
|
+
lang = /^zh/i.test(browser) ? 'zh' : 'en';
|
|
27
|
+
}
|
|
28
|
+
root.setAttribute('lang', lang);
|
|
29
|
+
}());
|
|
@@ -0,0 +1,504 @@
|
|
|
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
|
+
* The round amber stamp with the cat face. Decorative: the ticket text around
|
|
107
|
+
* it already says everything it stands for.
|
|
108
|
+
* @param {number} [size] - pixel size.
|
|
109
|
+
* @returns {HTMLElement} seal.
|
|
110
|
+
*/
|
|
111
|
+
export function catSeal(size = 56) {
|
|
112
|
+
const markup = `<svg viewBox="0 0 64 64" width="${size}" height="${size}" aria-hidden="true" focusable="false">
|
|
113
|
+
<circle cx="32" cy="32" r="30.25" fill="none" stroke="currentColor" stroke-width="1.5"/>
|
|
114
|
+
<g transform="translate(15.5 16) scale(0.92)">
|
|
115
|
+
<path fill="currentColor" fill-rule="evenodd" d="${CAT_FACE_PATH} ${CAT_EYES_PATH}"/>
|
|
116
|
+
</g>
|
|
117
|
+
</svg>`;
|
|
118
|
+
return h('span', { class: 'cat-seal', 'aria-hidden': 'true' }, fromMarkup(markup));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Field hint ids only need to be unique inside this document. */
|
|
122
|
+
let fieldSequence = 0;
|
|
123
|
+
|
|
124
|
+
/** The switch knob; the track is the button itself. */
|
|
125
|
+
const SWITCH_KNOB = '<span class="switch-knob"></span>';
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {{ checked: boolean, label: string, onChange: (next: boolean) => void, disabled?: boolean, testid?: string }} options - wiring.
|
|
129
|
+
* @returns {HTMLButtonElement} a real switch with `role="switch"`.
|
|
130
|
+
*/
|
|
131
|
+
export function switchControl({ checked, label, onChange, disabled = false, testid = '' }) {
|
|
132
|
+
const knob = fromMarkup(SWITCH_KNOB);
|
|
133
|
+
const el = h('button', {
|
|
134
|
+
type: 'button',
|
|
135
|
+
class: `switch${checked ? ' is-on' : ''}`,
|
|
136
|
+
role: 'switch',
|
|
137
|
+
'aria-checked': checked ? 'true' : 'false',
|
|
138
|
+
'aria-label': label,
|
|
139
|
+
disabled,
|
|
140
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
141
|
+
}, knob);
|
|
142
|
+
el.addEventListener('click', () => {
|
|
143
|
+
if (el.disabled) return;
|
|
144
|
+
onChange(el.getAttribute('aria-checked') !== 'true');
|
|
145
|
+
});
|
|
146
|
+
return el;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {HTMLButtonElement} el - switch element.
|
|
151
|
+
* @param {boolean} checked - next state.
|
|
152
|
+
*/
|
|
153
|
+
export function setSwitch(el, checked) {
|
|
154
|
+
el.setAttribute('aria-checked', checked ? 'true' : 'false');
|
|
155
|
+
el.classList.toggle('is-on', checked);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {{ label: string, variant?: 'primary' | 'secondary' | 'ghost' | 'danger', onClick?: Function, disabled?: boolean, title?: string, testid?: string, busy?: boolean, children?: unknown }} options - button.
|
|
160
|
+
* @returns {HTMLButtonElement} button.
|
|
161
|
+
*/
|
|
162
|
+
export function button(options) {
|
|
163
|
+
const { label, variant = 'secondary', onClick, disabled = false, title = '', testid = '', children = null } = options;
|
|
164
|
+
const el = h('button', {
|
|
165
|
+
type: 'button',
|
|
166
|
+
class: `btn btn-${variant}`,
|
|
167
|
+
disabled,
|
|
168
|
+
...(title ? { title } : {}),
|
|
169
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
170
|
+
}, children === null ? label : children);
|
|
171
|
+
if (typeof onClick === 'function') el.addEventListener('click', onClick);
|
|
172
|
+
return el;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {{ label: string, onClick: Function, title?: string, testid?: string, disabled?: boolean, danger?: boolean }} options - icon button.
|
|
177
|
+
* @returns {HTMLButtonElement} square ghost button carrying an icon.
|
|
178
|
+
*/
|
|
179
|
+
export function iconButton({ label, onClick, title = '', testid = '', disabled = false, danger = false }) {
|
|
180
|
+
const el = h('button', {
|
|
181
|
+
type: 'button',
|
|
182
|
+
class: `icon-btn${danger ? ' icon-btn-danger' : ''}`,
|
|
183
|
+
'aria-label': label,
|
|
184
|
+
title: title || label,
|
|
185
|
+
disabled,
|
|
186
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
187
|
+
}, icon('trash'));
|
|
188
|
+
el.addEventListener('click', onClick);
|
|
189
|
+
return el;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @param {string} text - button text.
|
|
194
|
+
* @param {string} testid - hook.
|
|
195
|
+
* @returns {HTMLButtonElement} copy button that reports its own result.
|
|
196
|
+
*/
|
|
197
|
+
export function copyButton({ text, t, onResult, variant = 'ghost', label = '', testid = '' }) {
|
|
198
|
+
const idle = label || t('copy');
|
|
199
|
+
const el = button({ label: idle, variant, testid });
|
|
200
|
+
let timer = null;
|
|
201
|
+
el.addEventListener('click', async () => {
|
|
202
|
+
const ok = await copyText(text);
|
|
203
|
+
if (timer !== null) clearTimeout(timer);
|
|
204
|
+
el.textContent = ok ? t('copied') : t('copyFailed');
|
|
205
|
+
el.classList.toggle('is-copied', ok);
|
|
206
|
+
el.classList.toggle('is-copy-failed', !ok);
|
|
207
|
+
timer = setTimeout(() => {
|
|
208
|
+
timer = null;
|
|
209
|
+
el.textContent = idle;
|
|
210
|
+
el.classList.remove('is-copied', 'is-copy-failed');
|
|
211
|
+
}, 1_800);
|
|
212
|
+
if (typeof onResult === 'function') onResult(ok);
|
|
213
|
+
});
|
|
214
|
+
return el;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Copy through the async clipboard API, with a selection fallback for contexts
|
|
219
|
+
* that only allow copying inside a user gesture.
|
|
220
|
+
* @param {string} text - payload.
|
|
221
|
+
* @returns {Promise<boolean>} whether the copy succeeded.
|
|
222
|
+
*/
|
|
223
|
+
export async function copyText(text) {
|
|
224
|
+
try {
|
|
225
|
+
await navigator.clipboard.writeText(text);
|
|
226
|
+
return true;
|
|
227
|
+
} catch {
|
|
228
|
+
try {
|
|
229
|
+
const area = document.createElement('textarea');
|
|
230
|
+
area.value = text;
|
|
231
|
+
area.setAttribute('readonly', '');
|
|
232
|
+
area.style.position = 'fixed';
|
|
233
|
+
area.style.top = '-1000px';
|
|
234
|
+
area.style.opacity = '0';
|
|
235
|
+
document.body.appendChild(area);
|
|
236
|
+
area.select();
|
|
237
|
+
const ok = typeof document.execCommand === 'function' ? document.execCommand('copy') : false;
|
|
238
|
+
area.remove();
|
|
239
|
+
return ok;
|
|
240
|
+
} catch {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A field row: label, hint, optional override dot, and the control.
|
|
248
|
+
* @param {{ label: string, hint?: string, override?: string, descriptionId?: string, inline?: boolean, control: Node }} options - field.
|
|
249
|
+
* @returns {HTMLElement} row.
|
|
250
|
+
*/
|
|
251
|
+
export function field({ label, hint = '', override = '', inline = false, control }) {
|
|
252
|
+
fieldSequence += 1;
|
|
253
|
+
const hintId = hint ? `field-hint-${fieldSequence}` : '';
|
|
254
|
+
const labelLine = h('span', { class: 'field-name' }, label);
|
|
255
|
+
let dot = null;
|
|
256
|
+
if (override) {
|
|
257
|
+
dot = h('span', {
|
|
258
|
+
class: 'override-dot',
|
|
259
|
+
role: 'img',
|
|
260
|
+
'aria-label': override,
|
|
261
|
+
title: override,
|
|
262
|
+
hidden: true,
|
|
263
|
+
});
|
|
264
|
+
labelLine.prepend(dot);
|
|
265
|
+
}
|
|
266
|
+
const target = control && control.selectEl ? control.selectEl : control;
|
|
267
|
+
if (hintId && target && typeof target.setAttribute === 'function' && !target.hasAttribute('aria-describedby')) {
|
|
268
|
+
target.setAttribute('aria-describedby', hintId);
|
|
269
|
+
}
|
|
270
|
+
const row = h('div', { class: `field${inline ? ' field-inline' : ''}` },
|
|
271
|
+
h('div', { class: 'field-label' },
|
|
272
|
+
h('div', { class: 'field-label-line' }, labelLine),
|
|
273
|
+
hint ? h('p', { class: 'field-hint', id: hintId }, hint) : null),
|
|
274
|
+
h('div', { class: 'field-control' }, control));
|
|
275
|
+
row.overrideDot = dot;
|
|
276
|
+
return row;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Show or hide the "overridden here" dot of one field row.
|
|
281
|
+
* @param {HTMLElement} row - element returned by field().
|
|
282
|
+
* @param {boolean} on - whether the field is overridden.
|
|
283
|
+
*/
|
|
284
|
+
export function setOverride(row, on) {
|
|
285
|
+
if (row && row.overrideDot) {
|
|
286
|
+
if (row.overrideDot.hidden === on) row.overrideDot.hidden = !on;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @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.
|
|
292
|
+
* @returns {HTMLInputElement} input.
|
|
293
|
+
*/
|
|
294
|
+
export function textInput(options) {
|
|
295
|
+
const el = h('input', {
|
|
296
|
+
class: `input${options.mono ? ' input-mono' : ''}`,
|
|
297
|
+
type: options.type || 'text',
|
|
298
|
+
value: options.value ?? '',
|
|
299
|
+
placeholder: options.placeholder || '',
|
|
300
|
+
'aria-label': options.ariaLabel,
|
|
301
|
+
disabled: options.disabled === true,
|
|
302
|
+
...(options.testid ? { 'data-testid': options.testid } : {}),
|
|
303
|
+
...(options.min !== undefined ? { min: String(options.min) } : {}),
|
|
304
|
+
...(options.max !== undefined ? { max: String(options.max) } : {}),
|
|
305
|
+
...(options.step !== undefined ? { step: String(options.step) } : {}),
|
|
306
|
+
...(options.inputMode ? { inputmode: options.inputMode } : {}),
|
|
307
|
+
});
|
|
308
|
+
el.addEventListener('input', () => options.onInput(el.value));
|
|
309
|
+
return el;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* @param {{ value: string, options: Array<[string, string]>, ariaLabel: string, onChange: Function, disabled?: boolean, testid?: string }} settings - select wiring.
|
|
314
|
+
* @returns {HTMLElement} select wrapper (native select plus a drawn chevron).
|
|
315
|
+
*/
|
|
316
|
+
export function selectControl({ value, options, ariaLabel, onChange, disabled = false, testid = '' }) {
|
|
317
|
+
const select = h('select', {
|
|
318
|
+
class: 'select-input',
|
|
319
|
+
'aria-label': ariaLabel,
|
|
320
|
+
disabled,
|
|
321
|
+
...(testid ? { 'data-testid': testid } : {}),
|
|
322
|
+
}, options.map(([optionValue, optionLabel]) => h('option', { value: optionValue }, optionLabel)));
|
|
323
|
+
select.value = value;
|
|
324
|
+
select.addEventListener('change', () => onChange(select.value));
|
|
325
|
+
const wrapper = h('span', { class: `select${disabled ? ' is-disabled' : ''}` }, select, h('span', { class: 'select-chevron', 'aria-hidden': 'true' }));
|
|
326
|
+
wrapper.selectEl = select;
|
|
327
|
+
return wrapper;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* @param {HTMLSelectElement} select - the native element inside a selectControl wrapper.
|
|
332
|
+
* @param {string} value - next value.
|
|
333
|
+
*/
|
|
334
|
+
export function setSelect(select, value) {
|
|
335
|
+
if (select.value !== String(value)) select.value = String(value);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @param {{ value: string, ariaLabel: string, onInput: Function, placeholder?: string, disabled?: boolean, rows?: number, testid?: string }} options - textarea wiring.
|
|
340
|
+
* @returns {HTMLTextAreaElement} textarea.
|
|
341
|
+
*/
|
|
342
|
+
export function textareaControl(options) {
|
|
343
|
+
const el = h('textarea', {
|
|
344
|
+
class: 'textarea',
|
|
345
|
+
'aria-label': options.ariaLabel,
|
|
346
|
+
placeholder: options.placeholder || '',
|
|
347
|
+
rows: String(options.rows || 6),
|
|
348
|
+
disabled: options.disabled === true,
|
|
349
|
+
...(options.testid ? { 'data-testid': options.testid } : {}),
|
|
350
|
+
});
|
|
351
|
+
el.value = options.value ?? '';
|
|
352
|
+
el.addEventListener('input', () => options.onInput(el.value));
|
|
353
|
+
return el;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @param {{ text: string, t: Function, emptyText?: string, testid?: string, mono?: boolean, ariaLabel?: string }} options - code line.
|
|
358
|
+
* @returns {HTMLElement} scrollable code line with its own copy button.
|
|
359
|
+
*/
|
|
360
|
+
export function codeBlock({ text, t, emptyText = '', testid = '', ariaLabel = '', mono = true }) {
|
|
361
|
+
if (!text) return h('p', { class: 'code-empty' }, emptyText);
|
|
362
|
+
return h('div', { class: 'code-block', ...(ariaLabel ? { 'aria-label': ariaLabel } : {}) },
|
|
363
|
+
h('code', { class: `code-text${mono ? ' is-mono' : ''}`, ...(testid ? { 'data-testid': testid } : {}) }, text),
|
|
364
|
+
copyButton({ text, t }));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* A page-level notice strip.
|
|
369
|
+
* @param {{ kind: 'ok' | 'error' | 'info', text: string, t: Function, onClose: Function }} options - notice.
|
|
370
|
+
* @returns {HTMLElement} notice with status role.
|
|
371
|
+
*/
|
|
372
|
+
export function noticeBar({ kind, text, t, onClose }) {
|
|
373
|
+
const iconName = kind === 'ok' ? 'check' : kind === 'error' ? 'caution' : 'postquantum';
|
|
374
|
+
return h('div', { class: `notice notice-${kind}`, role: 'status', 'aria-live': 'polite' },
|
|
375
|
+
h('span', { class: 'notice-icon', 'aria-hidden': 'true' }, icon(iconName)),
|
|
376
|
+
h('p', { class: 'notice-text' }, text),
|
|
377
|
+
h('button', {
|
|
378
|
+
type: 'button',
|
|
379
|
+
class: 'notice-close',
|
|
380
|
+
'aria-label': t('dismiss'),
|
|
381
|
+
title: t('dismiss'),
|
|
382
|
+
onclick: onClose,
|
|
383
|
+
}, '×'));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* @param {{ tone?: 'danger' | 'warn' | 'info', title?: string, children?: unknown }} options - inline alert.
|
|
388
|
+
* @returns {HTMLElement} alert box.
|
|
389
|
+
*/
|
|
390
|
+
export function alertBox({ tone = 'info', title = '', children }) {
|
|
391
|
+
return h('div', { class: `alert alert-${tone}` },
|
|
392
|
+
title ? h('p', { class: 'alert-title' }, title) : null,
|
|
393
|
+
h('div', { class: 'alert-body' }, children));
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* @param {string} text - pill text.
|
|
398
|
+
* @param {'pq' | 'accent' | 'quiet'} [tone] - colour role.
|
|
399
|
+
* @returns {HTMLElement} small capsule.
|
|
400
|
+
*/
|
|
401
|
+
export function pill(text, tone = 'quiet') {
|
|
402
|
+
return h('span', { class: `pill pill-${tone}` }, text);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* @returns {HTMLElement} three loading skeleton lines.
|
|
407
|
+
*/
|
|
408
|
+
export function skeleton() {
|
|
409
|
+
return h('div', { class: 'skeleton', 'aria-hidden': 'true' },
|
|
410
|
+
h('div', { class: 'skeleton-line skeleton-title' }),
|
|
411
|
+
h('div', { class: 'skeleton-line skeleton-body' }),
|
|
412
|
+
h('div', { class: 'skeleton-line skeleton-body-short' }));
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* @param {{ matrix: object, t: Function }} options - pairing QR matrix.
|
|
417
|
+
* @returns {HTMLElement | null} white QR board, or null when the matrix is unusable.
|
|
418
|
+
*/
|
|
419
|
+
export function qrBoard({ matrix, t }) {
|
|
420
|
+
const geometry = qrGeometry(matrix);
|
|
421
|
+
if (!geometry) return null;
|
|
422
|
+
const svgNs = 'http://www.w3.org/2000/svg';
|
|
423
|
+
const svg = document.createElementNS(svgNs, 'svg');
|
|
424
|
+
svg.setAttribute('viewBox', geometry.viewBox);
|
|
425
|
+
svg.setAttribute('role', 'img');
|
|
426
|
+
svg.setAttribute('aria-label', t('qrAlt'));
|
|
427
|
+
svg.setAttribute('shape-rendering', 'crispEdges');
|
|
428
|
+
const board = document.createElementNS(svgNs, 'rect');
|
|
429
|
+
board.setAttribute('width', '100%');
|
|
430
|
+
board.setAttribute('height', '100%');
|
|
431
|
+
board.setAttribute('fill', '#ffffff');
|
|
432
|
+
const path = document.createElementNS(svgNs, 'path');
|
|
433
|
+
path.setAttribute('d', geometry.path);
|
|
434
|
+
path.setAttribute('fill', '#000000');
|
|
435
|
+
svg.append(board, path);
|
|
436
|
+
return h('div', { class: 'qr-board' }, svg);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* @param {HTMLElement} el - element.
|
|
441
|
+
* @param {string} text - next text.
|
|
442
|
+
*/
|
|
443
|
+
export function setText(el, text) {
|
|
444
|
+
if (el.textContent !== text) el.textContent = text;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* @param {HTMLElement} el - element.
|
|
449
|
+
* @param {boolean} hidden - next visibility.
|
|
450
|
+
*/
|
|
451
|
+
export function setHidden(el, hidden) {
|
|
452
|
+
if (el.hidden !== hidden) el.hidden = hidden;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @param {HTMLElement} el - button or control.
|
|
457
|
+
* @param {boolean} disabled - next disabled state.
|
|
458
|
+
*/
|
|
459
|
+
export function setDisabled(el, disabled) {
|
|
460
|
+
if (el.disabled !== disabled) el.disabled = disabled;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* @param {HTMLElement} el - element.
|
|
465
|
+
* @param {string} name - class name.
|
|
466
|
+
* @param {boolean} on - whether to add.
|
|
467
|
+
*/
|
|
468
|
+
export function setClass(el, name, on) {
|
|
469
|
+
if (el.classList.contains(name) !== on) el.classList.toggle(name, on);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Update an input's value, but never while the user is typing into it.
|
|
474
|
+
* @param {HTMLInputElement | HTMLTextAreaElement} input - control.
|
|
475
|
+
* @param {string} value - next value.
|
|
476
|
+
*/
|
|
477
|
+
export function setValue(input, value) {
|
|
478
|
+
if (input === document.activeElement) return;
|
|
479
|
+
if (input.value !== String(value)) input.value = String(value);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Rebuild an element's children only when the signature changed.
|
|
484
|
+
* @param {HTMLElement} el - container.
|
|
485
|
+
* @param {unknown} signature - comparable identity; callers pass JSON.
|
|
486
|
+
* @param {() => Node[]} build - child factory.
|
|
487
|
+
*/
|
|
488
|
+
export function syncSignature(el, signature, build) {
|
|
489
|
+
const key = typeof signature === 'string' ? signature : JSON.stringify(signature);
|
|
490
|
+
if (el.__syncKey === key) return;
|
|
491
|
+
el.__syncKey = key;
|
|
492
|
+
el.replaceChildren(...build());
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Update the check state of a switch and its disabled flag.
|
|
497
|
+
* @param {HTMLButtonElement} el - switch.
|
|
498
|
+
* @param {boolean} checked - next checked.
|
|
499
|
+
* @param {boolean} disabled - next disabled.
|
|
500
|
+
*/
|
|
501
|
+
export function setSwitchState(el, checked, disabled) {
|
|
502
|
+
setSwitch(el, checked);
|
|
503
|
+
setDisabled(el, disabled);
|
|
504
|
+
}
|