@kayr/umbika-web-renderer 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/README.md +46 -2
- package/THIRD_PARTY_LICENSES.txt +20 -0
- package/dist/browser/umbika.min.css +23 -0
- package/dist/browser/umbika.min.d.mts +437 -0
- package/dist/browser/umbika.min.d.ts +437 -0
- package/dist/browser/umbika.min.js +60 -0
- package/dist/browser/umbika.min.mjs +60 -0
- package/dist/casconda.d.ts +2 -0
- package/dist/casconda.d.ts.map +1 -0
- package/dist/contracts.d.ts +165 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/dom-identity.d.ts +17 -0
- package/dist/dom-identity.d.ts.map +1 -0
- package/dist/field-presentation.d.ts +26 -0
- package/dist/field-presentation.d.ts.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/layout.d.ts +25 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/mount.d.ts +14 -0
- package/dist/mount.d.ts.map +1 -0
- package/dist/render-nodes.d.ts +18 -0
- package/dist/render-nodes.d.ts.map +1 -0
- package/dist/renderers/multi-select.d.ts +18 -0
- package/dist/renderers/multi-select.d.ts.map +1 -0
- package/dist/renderers/object.d.ts +6 -0
- package/dist/renderers/object.d.ts.map +1 -0
- package/dist/renderers/output.d.ts +6 -0
- package/dist/renderers/output.d.ts.map +1 -0
- package/dist/renderers/select.d.ts +6 -0
- package/dist/renderers/select.d.ts.map +1 -0
- package/dist/renderers/table.d.ts +6 -0
- package/dist/renderers/table.d.ts.map +1 -0
- package/dist/renderers/text.d.ts +8 -0
- package/dist/renderers/text.d.ts.map +1 -0
- package/dist/row-details.d.ts +89 -0
- package/dist/row-details.d.ts.map +1 -0
- package/dist/standalone/contracts.d.ts +77 -0
- package/dist/standalone/contracts.d.ts.map +1 -0
- package/dist/standalone/index.d.ts +7 -0
- package/dist/standalone/index.d.ts.map +1 -0
- package/dist/standalone/mount.d.ts +9 -0
- package/dist/standalone/mount.d.ts.map +1 -0
- package/dist/summary.d.ts +29 -0
- package/dist/summary.d.ts.map +1 -0
- package/dist/tags.d.ts +32 -0
- package/dist/tags.d.ts.map +1 -0
- package/package.json +78 -4
- package/src/casconda.js +10 -0
- package/src/contracts.js +79 -0
- package/src/dom-identity.js +31 -0
- package/src/field-presentation.js +71 -0
- package/src/index.js +9 -0
- package/src/layout.js +176 -0
- package/src/mount.js +569 -0
- package/src/render-nodes.js +77 -0
- package/src/renderers/multi-select.js +211 -0
- package/src/renderers/object.js +43 -0
- package/src/renderers/output.js +87 -0
- package/src/renderers/select.js +104 -0
- package/src/renderers/table.js +344 -0
- package/src/renderers/text.js +82 -0
- package/src/row-details.js +305 -0
- package/src/standalone/contracts.js +22 -0
- package/src/standalone/index.js +5 -0
- package/src/standalone/mount.js +128 -0
- package/src/summary.js +135 -0
- package/src/tags.js +69 -0
- package/styles/base.css +432 -0
- package/styles/standalone.css +2 -0
- package/styles/themes/default.css +99 -0
- package/vendor/LICENSE +21 -0
- package/vendor/README.md +33 -0
- package/vendor/casconda.min.d.mts +340 -0
- package/vendor/casconda.min.d.ts +340 -0
- package/vendor/casconda.min.mjs +1 -0
package/src/mount.js
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import { createCasconda, destroy, ref } from "./casconda.js";
|
|
2
|
+
import { _fieldDomId, _structuralDomId } from "./dom-identity.js";
|
|
3
|
+
import {
|
|
4
|
+
_copyWebFormLayout,
|
|
5
|
+
_copyWebFormTheme,
|
|
6
|
+
_tableLayoutAt,
|
|
7
|
+
} from "./layout.js";
|
|
8
|
+
import { _renderNodeResources } from "./render-nodes.js";
|
|
9
|
+
import { _reconcileWorkspace, _renderRowDetails } from "./row-details.js";
|
|
10
|
+
import { form as formTag } from "./tags.js";
|
|
11
|
+
|
|
12
|
+
let _nextInstance = 1;
|
|
13
|
+
|
|
14
|
+
/** @typedef {Readonly<{node: import("@kayr/umbika-core").ActiveFieldNode, frames: readonly import("./contracts.js").WorkspaceFrame[]}>} FieldPlan */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Mount the production complete-form browser renderer.
|
|
18
|
+
*
|
|
19
|
+
* @param {Element} target
|
|
20
|
+
* @param {import("./contracts.js").MountWebFormOptions} options
|
|
21
|
+
* @returns {import("./contracts.js").MountedWebForm}
|
|
22
|
+
*/
|
|
23
|
+
export function mountWebForm(target, options) {
|
|
24
|
+
if (!(target instanceof Element)) {
|
|
25
|
+
throw new TypeError("target must be a DOM Element");
|
|
26
|
+
}
|
|
27
|
+
if (!options || typeof options.form?.getActiveFields !== "function") {
|
|
28
|
+
throw new TypeError("options.form must be a form-engine form");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const instanceId = options.instanceId ?? `web-form-${_nextInstance++}`;
|
|
32
|
+
if (instanceId.trim().length === 0) {
|
|
33
|
+
throw new TypeError("instanceId must not be empty");
|
|
34
|
+
}
|
|
35
|
+
const layout = _copyWebFormLayout(options.layout, options.form.getFields());
|
|
36
|
+
const theme = _copyWebFormTheme(options.theme);
|
|
37
|
+
let disposed = false;
|
|
38
|
+
let suppressTextRefresh = false;
|
|
39
|
+
const activeFields = ref(options.form.getActiveFields());
|
|
40
|
+
const workspace = ref(null);
|
|
41
|
+
/** @type {HTMLDialogElement | undefined} */
|
|
42
|
+
let workspaceElement;
|
|
43
|
+
/** @type {HTMLFormElement | undefined} */
|
|
44
|
+
let rootElement;
|
|
45
|
+
|
|
46
|
+
const refresh = () => {
|
|
47
|
+
if (disposed) return;
|
|
48
|
+
const focused = rootElement?.contains(document.activeElement)
|
|
49
|
+
? document.activeElement
|
|
50
|
+
: null;
|
|
51
|
+
const focusedInWorkspace = workspaceElement?.contains(focused);
|
|
52
|
+
const nodes = options.form.getActiveFields();
|
|
53
|
+
const current =
|
|
54
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
55
|
+
workspace.get()
|
|
56
|
+
);
|
|
57
|
+
const reconciled = _reconcileWorkspace(nodes, current);
|
|
58
|
+
if (reconciled !== current) workspace.set(reconciled);
|
|
59
|
+
activeFields.set(nodes);
|
|
60
|
+
_syncWorkspaceDialog();
|
|
61
|
+
if (focused instanceof HTMLElement && !focused.isConnected) {
|
|
62
|
+
if (focusedInWorkspace && workspaceElement?.open) {
|
|
63
|
+
_focusFirstWorkspaceControl();
|
|
64
|
+
} else {
|
|
65
|
+
_focusFallback(current?.frames.at(-1)?.openedBy);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** @param {boolean} refreshAfter */
|
|
71
|
+
function _flushFocusedText(refreshAfter) {
|
|
72
|
+
const active = document.activeElement;
|
|
73
|
+
if (
|
|
74
|
+
active instanceof HTMLInputElement &&
|
|
75
|
+
active.type === "text" &&
|
|
76
|
+
!active.disabled &&
|
|
77
|
+
!active.readOnly &&
|
|
78
|
+
rootElement?.contains(active)
|
|
79
|
+
) {
|
|
80
|
+
suppressTextRefresh = !refreshAfter;
|
|
81
|
+
try {
|
|
82
|
+
active.blur();
|
|
83
|
+
} finally {
|
|
84
|
+
suppressTextRefresh = false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function _syncWorkspaceDialog() {
|
|
90
|
+
const current =
|
|
91
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
92
|
+
workspace.get()
|
|
93
|
+
);
|
|
94
|
+
if (current?.frames.length) {
|
|
95
|
+
if (!workspaceElement?.open) {
|
|
96
|
+
if (typeof workspaceElement?.showModal === "function") {
|
|
97
|
+
workspaceElement.showModal();
|
|
98
|
+
} else {
|
|
99
|
+
workspaceElement?.setAttribute("open", "");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} else if (workspaceElement?.open) {
|
|
103
|
+
if (typeof workspaceElement.close === "function")
|
|
104
|
+
workspaceElement.close();
|
|
105
|
+
else workspaceElement.removeAttribute("open");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @param {string | undefined} preferredId */
|
|
110
|
+
function _focusFallback(preferredId) {
|
|
111
|
+
if (disposed || !rootElement) return;
|
|
112
|
+
const preferred = preferredId ? _elementById(preferredId) : null;
|
|
113
|
+
const fallback =
|
|
114
|
+
preferred ??
|
|
115
|
+
rootElement.querySelector(
|
|
116
|
+
'input:not([disabled]), select:not([disabled]), button:not([disabled]), [tabindex="0"]',
|
|
117
|
+
) ??
|
|
118
|
+
rootElement.querySelector("output.umbika-field__output");
|
|
119
|
+
if (fallback instanceof HTMLElement) fallback.focus();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** @param {string} id */
|
|
123
|
+
function _elementById(id) {
|
|
124
|
+
if (!rootElement) return null;
|
|
125
|
+
return rootElement.querySelector(`#${_escapeCssIdentifier(id)}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @param {readonly import("./contracts.js").WorkspaceFrame[]} frames
|
|
130
|
+
* @param {boolean} [focusFirst]
|
|
131
|
+
*/
|
|
132
|
+
function _setWorkspace(frames, focusFirst = false) {
|
|
133
|
+
if (disposed) return;
|
|
134
|
+
workspace.set(
|
|
135
|
+
frames.length
|
|
136
|
+
? Object.freeze({ frames: Object.freeze(frames.map(_copyFrame)) })
|
|
137
|
+
: null,
|
|
138
|
+
);
|
|
139
|
+
_syncWorkspaceDialog();
|
|
140
|
+
if (focusFirst) _focusFirstWorkspaceControl();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function _focusFirstWorkspaceControl() {
|
|
144
|
+
const control = workspaceElement?.querySelector(
|
|
145
|
+
".umbika-workspace__fields input:not([disabled]), .umbika-workspace__fields select:not([disabled])",
|
|
146
|
+
);
|
|
147
|
+
const target =
|
|
148
|
+
control ?? workspaceElement?.querySelector(".umbika-workspace__title");
|
|
149
|
+
if (target instanceof HTMLElement) target.focus();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {import("@kayr/umbika-core").FieldPath} rowPath
|
|
154
|
+
* @param {string} openerId
|
|
155
|
+
* @param {readonly string[]} definitionPath
|
|
156
|
+
* @param {"new" | "existing"} origin
|
|
157
|
+
*/
|
|
158
|
+
function _openRow(rowPath, openerId, definitionPath, origin) {
|
|
159
|
+
if (disposed) return;
|
|
160
|
+
const frame = _makeFrame(rowPath, definitionPath, openerId, origin);
|
|
161
|
+
const current =
|
|
162
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
163
|
+
workspace.get()
|
|
164
|
+
);
|
|
165
|
+
const existingFrames = current?.frames ?? [];
|
|
166
|
+
const top = existingFrames.at(-1);
|
|
167
|
+
const frames =
|
|
168
|
+
top && _pathStartsWith(rowPath, top.rowPath)
|
|
169
|
+
? [...existingFrames, frame]
|
|
170
|
+
: [frame];
|
|
171
|
+
_setWorkspace(frames, true);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** @param {boolean} commitFocusedDraft */
|
|
175
|
+
function _closeWorkspace(commitFocusedDraft) {
|
|
176
|
+
const current =
|
|
177
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
178
|
+
workspace.get()
|
|
179
|
+
);
|
|
180
|
+
if (!current) return;
|
|
181
|
+
const openerId = current.frames[0]?.openedBy;
|
|
182
|
+
if (commitFocusedDraft) _flushFocusedText(true);
|
|
183
|
+
_setWorkspace([]);
|
|
184
|
+
_focusFallback(openerId);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function _backWorkspace() {
|
|
188
|
+
const current =
|
|
189
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
190
|
+
workspace.get()
|
|
191
|
+
);
|
|
192
|
+
if (!current || current.frames.length < 2) return;
|
|
193
|
+
const leaving = current.frames.at(-1);
|
|
194
|
+
_flushFocusedText(true);
|
|
195
|
+
_setWorkspace(current.frames.slice(0, -1));
|
|
196
|
+
_focusFallback(leaving?.openedBy);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @param {boolean} remove */
|
|
200
|
+
function _leaveNewWorkspace(remove) {
|
|
201
|
+
const current =
|
|
202
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
203
|
+
workspace.get()
|
|
204
|
+
);
|
|
205
|
+
const frame = current?.frames.at(-1);
|
|
206
|
+
if (!current || !frame) return;
|
|
207
|
+
if (!remove) _flushFocusedText(true);
|
|
208
|
+
const remaining = current.frames.slice(0, -1);
|
|
209
|
+
_setWorkspace(remaining);
|
|
210
|
+
if (remove) {
|
|
211
|
+
options.form.removeRow(_copyPath(frame.rowPath));
|
|
212
|
+
refresh();
|
|
213
|
+
}
|
|
214
|
+
_focusFallback(frame.openedBy);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function _removeCurrentWorkspaceRow() {
|
|
218
|
+
_leaveNewWorkspace(true);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function _escapeWorkspace() {
|
|
222
|
+
const current =
|
|
223
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
224
|
+
workspace.get()
|
|
225
|
+
);
|
|
226
|
+
const frame = current?.frames.at(-1);
|
|
227
|
+
if (!current || !frame) return;
|
|
228
|
+
if (frame.origin === "new") {
|
|
229
|
+
_leaveNewWorkspace(true);
|
|
230
|
+
} else if (current.frames.length > 1) {
|
|
231
|
+
_backWorkspace();
|
|
232
|
+
} else {
|
|
233
|
+
_closeWorkspace(true);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** @type {import("./contracts.js").NodeRendererContext} */
|
|
238
|
+
const context = Object.freeze({
|
|
239
|
+
instanceId,
|
|
240
|
+
definitionPath: Object.freeze([]),
|
|
241
|
+
layout,
|
|
242
|
+
setText(path, value) {
|
|
243
|
+
if (!disposed) options.form.input(path, value);
|
|
244
|
+
},
|
|
245
|
+
touchText(path) {
|
|
246
|
+
if (disposed) return;
|
|
247
|
+
options.form.touch(path);
|
|
248
|
+
if (!suppressTextRefresh) refresh();
|
|
249
|
+
},
|
|
250
|
+
commitSelect(path, value) {
|
|
251
|
+
if (disposed) return;
|
|
252
|
+
options.form.input(path, value);
|
|
253
|
+
options.form.touch(path);
|
|
254
|
+
refresh();
|
|
255
|
+
},
|
|
256
|
+
commitMultiSelect(path, value) {
|
|
257
|
+
if (disposed) return;
|
|
258
|
+
options.form.input(path, [...value]);
|
|
259
|
+
options.form.touch(path);
|
|
260
|
+
refresh();
|
|
261
|
+
},
|
|
262
|
+
appendRow(tablePath, openDetail, openerId, definitionPath) {
|
|
263
|
+
if (disposed) return;
|
|
264
|
+
const path = _copyPath(tablePath);
|
|
265
|
+
const rowId = options.form.appendRow(path);
|
|
266
|
+
refresh();
|
|
267
|
+
if (openDetail) {
|
|
268
|
+
_openRow(_copyPath([...path, rowId]), openerId, definitionPath, "new");
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
removeRow(rowPath) {
|
|
272
|
+
if (disposed) return;
|
|
273
|
+
const path = _copyPath(rowPath);
|
|
274
|
+
const current =
|
|
275
|
+
/** @type {import("./contracts.js").WorkspaceState | null} */ (
|
|
276
|
+
workspace.get()
|
|
277
|
+
);
|
|
278
|
+
const removedFrame = current?.frames.find((frame) =>
|
|
279
|
+
_pathStartsWith(frame.rowPath, path),
|
|
280
|
+
);
|
|
281
|
+
if (current && removedFrame) {
|
|
282
|
+
const surviving = current.frames.slice(
|
|
283
|
+
0,
|
|
284
|
+
current.frames.indexOf(removedFrame),
|
|
285
|
+
);
|
|
286
|
+
_setWorkspace(surviving);
|
|
287
|
+
}
|
|
288
|
+
options.form.removeRow(path);
|
|
289
|
+
refresh();
|
|
290
|
+
_focusFallback(removedFrame?.openedBy);
|
|
291
|
+
},
|
|
292
|
+
openRow(rowPath, openerId, definitionPath) {
|
|
293
|
+
_openRow(rowPath, openerId, definitionPath, "existing");
|
|
294
|
+
},
|
|
295
|
+
renderNodes(nodes, definitionPath = []) {
|
|
296
|
+
return _renderNodeResources(nodes, context, definitionPath);
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const casconda = createCasconda({ bindings: { text: { event: "blur" } } });
|
|
301
|
+
workspaceElement = /** @type {HTMLDialogElement} */ (
|
|
302
|
+
casconda.render(
|
|
303
|
+
_renderRowDetails(workspace, activeFields, context, {
|
|
304
|
+
close: () => _closeWorkspace(true),
|
|
305
|
+
back: _backWorkspace,
|
|
306
|
+
add: () => _leaveNewWorkspace(false),
|
|
307
|
+
cancel: () => _leaveNewWorkspace(true),
|
|
308
|
+
remove: _removeCurrentWorkspaceRow,
|
|
309
|
+
escape: _escapeWorkspace,
|
|
310
|
+
}),
|
|
311
|
+
)
|
|
312
|
+
);
|
|
313
|
+
workspaceElement.addEventListener("keydown", _containWorkspaceFocus);
|
|
314
|
+
|
|
315
|
+
rootElement = /** @type {HTMLFormElement} */ (
|
|
316
|
+
casconda.render(
|
|
317
|
+
formTag(
|
|
318
|
+
{
|
|
319
|
+
class: "umbika-form",
|
|
320
|
+
"data-umbika-instance": instanceId,
|
|
321
|
+
"data-umbika-theme": theme,
|
|
322
|
+
novalidate: true,
|
|
323
|
+
onsubmit: (/** @type {SubmitEvent} */ event) => {
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
if (disposed) return;
|
|
326
|
+
_flushFocusedText(false);
|
|
327
|
+
const validation = options.form.validate();
|
|
328
|
+
refresh();
|
|
329
|
+
_revealFirstError(activeFields.get());
|
|
330
|
+
options.onSubmit?.({ validation });
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
_renderNodeResources(activeFields, context),
|
|
334
|
+
workspaceElement,
|
|
335
|
+
),
|
|
336
|
+
)
|
|
337
|
+
);
|
|
338
|
+
target.appendChild(rootElement);
|
|
339
|
+
const root = rootElement;
|
|
340
|
+
|
|
341
|
+
/** @param {import("@kayr/umbika-core").FieldPath} path */
|
|
342
|
+
function focus(path) {
|
|
343
|
+
if (disposed || !_validPath(path)) return false;
|
|
344
|
+
const plan = _findNodePlan(activeFields.get(), layout, instanceId, (node) =>
|
|
345
|
+
_pathsEqual(node.path, path),
|
|
346
|
+
);
|
|
347
|
+
if (!plan || !("readOnly" in plan.node) || plan.node.readOnly) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
_setWorkspace(plan.frames);
|
|
351
|
+
const field = _elementById(
|
|
352
|
+
_fieldDomId(instanceId, "control", plan.node.path),
|
|
353
|
+
);
|
|
354
|
+
const control =
|
|
355
|
+
plan.node.type === "multi-select"
|
|
356
|
+
? field?.querySelector('input[type="checkbox"]:not([disabled])')
|
|
357
|
+
: field;
|
|
358
|
+
if (!(control instanceof HTMLElement)) return false;
|
|
359
|
+
control.focus();
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** @param {readonly import("@kayr/umbika-core").ActiveFieldNode[]} nodes */
|
|
364
|
+
function _revealFirstError(nodes) {
|
|
365
|
+
let plan = _findNodePlan(
|
|
366
|
+
nodes,
|
|
367
|
+
layout,
|
|
368
|
+
instanceId,
|
|
369
|
+
(node) => node.errors.length > 0,
|
|
370
|
+
);
|
|
371
|
+
if (!plan) return;
|
|
372
|
+
if (plan.node.type === "object") {
|
|
373
|
+
plan =
|
|
374
|
+
_findNodePlan(
|
|
375
|
+
nodes,
|
|
376
|
+
layout,
|
|
377
|
+
instanceId,
|
|
378
|
+
(node) =>
|
|
379
|
+
"readOnly" in node && !node.readOnly && node.errors.length > 0,
|
|
380
|
+
) ?? plan;
|
|
381
|
+
}
|
|
382
|
+
_setWorkspace(plan.frames);
|
|
383
|
+
const { type, path } = plan.node;
|
|
384
|
+
const targetElement = _elementById(
|
|
385
|
+
type === "object" || type === "table"
|
|
386
|
+
? _structuralDomId(instanceId, type, path)
|
|
387
|
+
: _fieldDomId(instanceId, "control", path),
|
|
388
|
+
);
|
|
389
|
+
const focusTarget =
|
|
390
|
+
plan.node.type === "multi-select" && !plan.node.readOnly
|
|
391
|
+
? targetElement?.querySelector('input[type="checkbox"]:not([disabled])')
|
|
392
|
+
: targetElement;
|
|
393
|
+
if (focusTarget instanceof HTMLElement) focusTarget.focus();
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** @param {KeyboardEvent} event */
|
|
397
|
+
function _containWorkspaceFocus(event) {
|
|
398
|
+
if (disposed || event.key !== "Tab" || !workspaceElement?.open) return;
|
|
399
|
+
const tabbable = Array.from(
|
|
400
|
+
workspaceElement.querySelectorAll(
|
|
401
|
+
'button:not([disabled]), input:not([disabled]), select:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])',
|
|
402
|
+
),
|
|
403
|
+
).filter(
|
|
404
|
+
(element) =>
|
|
405
|
+
element instanceof HTMLElement &&
|
|
406
|
+
element.getAttribute("aria-hidden") !== "true",
|
|
407
|
+
);
|
|
408
|
+
if (tabbable.length === 0) {
|
|
409
|
+
event.preventDefault();
|
|
410
|
+
workspaceElement.focus();
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const first = tabbable[0];
|
|
414
|
+
const last = tabbable.at(-1);
|
|
415
|
+
if (
|
|
416
|
+
(!event.shiftKey && document.activeElement === last) ||
|
|
417
|
+
(event.shiftKey && document.activeElement === first)
|
|
418
|
+
) {
|
|
419
|
+
event.preventDefault();
|
|
420
|
+
const next = event.shiftKey ? last : first;
|
|
421
|
+
if (next instanceof HTMLElement) next.focus();
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
root,
|
|
427
|
+
refresh,
|
|
428
|
+
focus,
|
|
429
|
+
dispose() {
|
|
430
|
+
if (disposed) return;
|
|
431
|
+
disposed = true;
|
|
432
|
+
workspace.set(null);
|
|
433
|
+
workspaceElement?.removeEventListener("keydown", _containWorkspaceFocus);
|
|
434
|
+
destroy(root);
|
|
435
|
+
workspaceElement = undefined;
|
|
436
|
+
rootElement = undefined;
|
|
437
|
+
},
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** @param {import("./contracts.js").WorkspaceFrame} frame */
|
|
442
|
+
function _copyFrame(frame) {
|
|
443
|
+
return _makeFrame(
|
|
444
|
+
frame.rowPath,
|
|
445
|
+
frame.definitionPath,
|
|
446
|
+
frame.openedBy,
|
|
447
|
+
frame.origin,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* @param {readonly string[]} rowPath
|
|
453
|
+
* @param {readonly string[]} definitionPath
|
|
454
|
+
* @param {string} openedBy
|
|
455
|
+
* @param {"new" | "existing"} origin
|
|
456
|
+
* @returns {import("./contracts.js").WorkspaceFrame}
|
|
457
|
+
*/
|
|
458
|
+
function _makeFrame(rowPath, definitionPath, openedBy, origin) {
|
|
459
|
+
return Object.freeze({
|
|
460
|
+
rowPath: Object.freeze(
|
|
461
|
+
/** @type {import("@kayr/umbika-core").FieldPath} */ (
|
|
462
|
+
/** @type {unknown} */ ([...rowPath])
|
|
463
|
+
),
|
|
464
|
+
),
|
|
465
|
+
definitionPath: Object.freeze([...definitionPath]),
|
|
466
|
+
openedBy,
|
|
467
|
+
origin,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** @param {readonly string[]} path @returns {import("@kayr/umbika-core").FieldPath} */
|
|
472
|
+
function _copyPath(path) {
|
|
473
|
+
return /** @type {import("@kayr/umbika-core").FieldPath} */ (
|
|
474
|
+
/** @type {unknown} */ ([...path])
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* @param {readonly import("@kayr/umbika-core").ActiveFieldNode[]} nodes
|
|
480
|
+
* @param {import("./contracts.js").OwnedWebFormLayout} layout
|
|
481
|
+
* @param {string} instanceId
|
|
482
|
+
* @param {(node: import("@kayr/umbika-core").ActiveFieldNode) => boolean} matches
|
|
483
|
+
* @param {readonly string[]} [definitionParent]
|
|
484
|
+
* @param {readonly import("./contracts.js").WorkspaceFrame[]} [frames]
|
|
485
|
+
* @returns {FieldPlan | undefined}
|
|
486
|
+
*/
|
|
487
|
+
function _findNodePlan(
|
|
488
|
+
nodes,
|
|
489
|
+
layout,
|
|
490
|
+
instanceId,
|
|
491
|
+
matches,
|
|
492
|
+
definitionParent = [],
|
|
493
|
+
frames = [],
|
|
494
|
+
) {
|
|
495
|
+
for (const node of nodes) {
|
|
496
|
+
const definitionPath = [...definitionParent, node.id];
|
|
497
|
+
if (matches(node)) return { node, frames };
|
|
498
|
+
if (node.type === "object") {
|
|
499
|
+
const found = _findNodePlan(
|
|
500
|
+
node.children,
|
|
501
|
+
layout,
|
|
502
|
+
instanceId,
|
|
503
|
+
matches,
|
|
504
|
+
definitionPath,
|
|
505
|
+
frames,
|
|
506
|
+
);
|
|
507
|
+
if (found) return found;
|
|
508
|
+
} else if (node.type === "table") {
|
|
509
|
+
const compact = _tableLayoutAt(layout, definitionPath)?.mode === "table";
|
|
510
|
+
for (const row of node.rows) {
|
|
511
|
+
const rowFrames = compact
|
|
512
|
+
? [
|
|
513
|
+
...frames,
|
|
514
|
+
_makeFrame(
|
|
515
|
+
row.path,
|
|
516
|
+
definitionPath,
|
|
517
|
+
_structuralDomId(instanceId, "edit-row", row.path),
|
|
518
|
+
"existing",
|
|
519
|
+
),
|
|
520
|
+
]
|
|
521
|
+
: frames;
|
|
522
|
+
const found = _findNodePlan(
|
|
523
|
+
row.children,
|
|
524
|
+
layout,
|
|
525
|
+
instanceId,
|
|
526
|
+
matches,
|
|
527
|
+
definitionPath,
|
|
528
|
+
rowFrames,
|
|
529
|
+
);
|
|
530
|
+
if (found) return found;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** @param {unknown} path @returns {path is import("@kayr/umbika-core").FieldPath} */
|
|
538
|
+
function _validPath(path) {
|
|
539
|
+
return (
|
|
540
|
+
Array.isArray(path) &&
|
|
541
|
+
path.length > 0 &&
|
|
542
|
+
path.every((segment) => typeof segment === "string" && segment.length > 0)
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/** @param {readonly string[]} path @param {readonly string[]} prefix */
|
|
547
|
+
function _pathStartsWith(path, prefix) {
|
|
548
|
+
return (
|
|
549
|
+
path.length >= prefix.length &&
|
|
550
|
+
prefix.every((segment, index) => path[index] === segment)
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** @param {readonly string[]} left @param {readonly string[]} right */
|
|
555
|
+
function _pathsEqual(left, right) {
|
|
556
|
+
return (
|
|
557
|
+
left.length === right.length &&
|
|
558
|
+
left.every((segment, index) => segment === right[index])
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** @param {string} value */
|
|
563
|
+
function _escapeCssIdentifier(value) {
|
|
564
|
+
if (globalThis.CSS?.escape) return globalThis.CSS.escape(value);
|
|
565
|
+
return value.replace(
|
|
566
|
+
/[^A-Za-z0-9_-]/g,
|
|
567
|
+
(character) => `\\${character.codePointAt(0)?.toString(16)} `,
|
|
568
|
+
);
|
|
569
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { _resourcePathKey } from "./dom-identity.js";
|
|
2
|
+
import { _renderObjectField } from "./renderers/object.js";
|
|
3
|
+
import { _renderMultiSelectField } from "./renderers/multi-select.js";
|
|
4
|
+
import { _renderFieldOutput } from "./renderers/output.js";
|
|
5
|
+
import { _renderSelectField } from "./renderers/select.js";
|
|
6
|
+
import { _renderTableField } from "./renderers/table.js";
|
|
7
|
+
import { _renderTextField } from "./renderers/text.js";
|
|
8
|
+
import { foreach } from "./tags.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Keep each path's presentation resource while its control kind stays the
|
|
12
|
+
* same. Switching between an input and an output retires the old resource.
|
|
13
|
+
*
|
|
14
|
+
* @param {import("./contracts.js").ResourceListSource<import("@kayr/umbika-core").ActiveFieldNode>} nodes
|
|
15
|
+
* @param {import("./contracts.js").NodeRendererContext} context
|
|
16
|
+
* @param {readonly string[]} [definitionPath]
|
|
17
|
+
*/
|
|
18
|
+
export function _renderNodeResources(nodes, context, definitionPath = []) {
|
|
19
|
+
return foreach(
|
|
20
|
+
{
|
|
21
|
+
id: (/** @type {import("@kayr/umbika-core").ActiveFieldNode} */ node) =>
|
|
22
|
+
`${"readOnly" in node && node.readOnly ? "output:" : ""}${_resourcePathKey(node)}`,
|
|
23
|
+
resource: true,
|
|
24
|
+
},
|
|
25
|
+
nodes,
|
|
26
|
+
(
|
|
27
|
+
/** @type {import("@kayr/umbika-core").ActiveFieldNode} */ initial,
|
|
28
|
+
/** @type {number} */ _index,
|
|
29
|
+
/** @type {import("./contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} */ resource,
|
|
30
|
+
) => {
|
|
31
|
+
const nodeContext = Object.freeze({
|
|
32
|
+
...context,
|
|
33
|
+
definitionPath: Object.freeze([...definitionPath, initial.id]),
|
|
34
|
+
});
|
|
35
|
+
return _renderNode(resource, nodeContext);
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Explicitly dispatch one public engine node without a renderer registry or
|
|
42
|
+
* transformed view type.
|
|
43
|
+
*
|
|
44
|
+
* @param {import("./contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
|
|
45
|
+
* @param {import("./contracts.js").NodeRendererContext} context
|
|
46
|
+
*/
|
|
47
|
+
export function _renderNode(resource, context) {
|
|
48
|
+
const node = resource.get();
|
|
49
|
+
if (!node) return "";
|
|
50
|
+
if (node.type === "calculated" || ("readOnly" in node && node.readOnly)) {
|
|
51
|
+
return _renderFieldOutput(resource, context);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
switch (node.type) {
|
|
55
|
+
case "object":
|
|
56
|
+
return _renderObjectField(resource, context);
|
|
57
|
+
case "table":
|
|
58
|
+
return _renderTableField(resource, context);
|
|
59
|
+
case "text":
|
|
60
|
+
return _renderTextField(resource, context);
|
|
61
|
+
case "select":
|
|
62
|
+
return _renderSelectField(resource, context);
|
|
63
|
+
case "multi-select":
|
|
64
|
+
return _renderMultiSelectField(resource, context);
|
|
65
|
+
default:
|
|
66
|
+
return _unsupportedNode(node);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @param {unknown} node */
|
|
71
|
+
function _unsupportedNode(node) {
|
|
72
|
+
const type =
|
|
73
|
+
typeof node === "object" && node !== null && "type" in node
|
|
74
|
+
? String(node.type)
|
|
75
|
+
: "unknown";
|
|
76
|
+
throw new TypeError(`Unsupported active node type: ${type}`);
|
|
77
|
+
}
|