@symbiotejs/symbiote 3.8.3 → 3.10.1
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/README.md +52 -2
- package/core/AppRouter.js +97 -58
- package/core/PubSub.js +42 -11
- package/core/Symbiote.js +90 -59
- package/core/itemizeProcessor-keyed.js +88 -188
- package/core/itemizeProcessor.js +1 -12
- package/core/itemizeSetup.js +26 -3
- package/core/ownElements.js +1 -4
- package/core/resolveTemplate.js +12 -0
- package/core/tpl-processors.js +3 -3
- package/docs/README.md +5 -4
- package/docs/html-app-vs-htmx.md +55 -0
- package/docs/lifecycle.md +2 -0
- package/docs/list-rendering.md +10 -2
- package/docs/properties.md +28 -1
- package/docs/pubsub.md +3 -0
- package/docs/routing.md +15 -4
- package/docs/security.md +41 -12
- package/docs/templates.md +29 -0
- package/html-app/BaseClass.js +203 -0
- package/html-app/README.md +369 -0
- package/html-app/action-data.js +168 -0
- package/html-app/actions.js +366 -0
- package/html-app/collection-actions.js +104 -0
- package/html-app/collection-data.js +234 -0
- package/html-app/collection-items.js +45 -0
- package/html-app/conditions.js +42 -0
- package/html-app/context-data.js +185 -0
- package/html-app/context-requests.js +374 -0
- package/html-app/context-storage.js +376 -0
- package/html-app/declarations.js +55 -0
- package/html-app/examples/assets/actions.html +56 -0
- package/html-app/examples/assets/collections.html +72 -0
- package/html-app/examples/assets/components.html +25 -0
- package/html-app/examples/assets/conditional.html +29 -0
- package/html-app/examples/assets/data.html +31 -0
- package/html-app/examples/assets/guide.html +4 -0
- package/html-app/examples/assets/imports.html +11 -0
- package/html-app/examples/assets/not-found.html +4 -0
- package/html-app/examples/assets/routes.json +58 -0
- package/html-app/examples/assets/routing.html +27 -0
- package/html-app/examples/assets/state.html +32 -0
- package/html-app/examples/assets/storage.html +28 -0
- package/html-app/examples/assets/tasks.json +25 -0
- package/html-app/examples/full-app.html +147 -0
- package/html-app/examples/todo-ref.html +37 -0
- package/html-app/fetch-source.js +147 -0
- package/html-app/fragment-content.js +29 -0
- package/html-app/fragment-source.js +169 -0
- package/html-app/index.js +34 -0
- package/html-app/initial-data.js +88 -0
- package/html-app/pub-map.js +93 -0
- package/html-app/router-data.js +50 -0
- package/html-app/router-navigation.js +70 -0
- package/html-app/storage-revisions.js +130 -0
- package/html-app/sym-collection.js +142 -0
- package/html-app/sym-component.js +20 -0
- package/html-app/sym-conditional.js +141 -0
- package/html-app/sym-context.js +176 -0
- package/html-app/sym-import.js +218 -0
- package/html-app/sym-router.js +107 -0
- package/package.json +18 -8
- package/scripts/check-html-app-package.js +117 -0
- package/scripts/check-html-app-size.js +91 -0
- package/scripts/update-exports.js +4 -0
- package/skills/symbiote/SKILL.md +197 -0
- package/skills/symbiote/agents/openai.yaml +3 -0
- package/types/core/AppRouter.d.ts +13 -4
- package/types/core/AppRouter.d.ts.map +1 -1
- package/types/core/PubSub.d.ts +1 -1
- package/types/core/PubSub.d.ts.map +1 -1
- package/types/core/Symbiote.d.ts +6 -2
- package/types/core/Symbiote.d.ts.map +1 -1
- package/types/core/itemizeProcessor-keyed.d.ts.map +1 -1
- package/types/core/itemizeProcessor.d.ts.map +1 -1
- package/types/core/itemizeSetup.d.ts +1 -0
- package/types/core/itemizeSetup.d.ts.map +1 -1
- package/types/core/ownElements.d.ts +1 -1
- package/types/core/ownElements.d.ts.map +1 -1
- package/types/core/resolveTemplate.d.ts +2 -0
- package/types/core/resolveTemplate.d.ts.map +1 -0
- package/types/html-app/BaseClass.d.ts +24 -0
- package/types/html-app/BaseClass.d.ts.map +1 -0
- package/types/html-app/action-data.d.ts +15 -0
- package/types/html-app/action-data.d.ts.map +1 -0
- package/types/html-app/actions.d.ts +104 -0
- package/types/html-app/actions.d.ts.map +1 -0
- package/types/html-app/collection-actions.d.ts +12 -0
- package/types/html-app/collection-actions.d.ts.map +1 -0
- package/types/html-app/collection-data.d.ts +13 -0
- package/types/html-app/collection-data.d.ts.map +1 -0
- package/types/html-app/collection-items.d.ts +7 -0
- package/types/html-app/collection-items.d.ts.map +1 -0
- package/types/html-app/conditions.d.ts +5 -0
- package/types/html-app/conditions.d.ts.map +1 -0
- package/types/html-app/context-data.d.ts +31 -0
- package/types/html-app/context-data.d.ts.map +1 -0
- package/types/html-app/context-requests.d.ts +97 -0
- package/types/html-app/context-requests.d.ts.map +1 -0
- package/types/html-app/context-storage.d.ts +102 -0
- package/types/html-app/context-storage.d.ts.map +1 -0
- package/types/html-app/declarations.d.ts +3 -0
- package/types/html-app/declarations.d.ts.map +1 -0
- package/types/html-app/fetch-source.d.ts +23 -0
- package/types/html-app/fetch-source.d.ts.map +1 -0
- package/types/html-app/fragment-content.d.ts +6 -0
- package/types/html-app/fragment-content.d.ts.map +1 -0
- package/types/html-app/fragment-source.d.ts +14 -0
- package/types/html-app/fragment-source.d.ts.map +1 -0
- package/types/html-app/index.d.ts +9 -0
- package/types/html-app/index.d.ts.map +1 -0
- package/types/html-app/initial-data.d.ts +8 -0
- package/types/html-app/initial-data.d.ts.map +1 -0
- package/types/html-app/pub-map.d.ts +19 -0
- package/types/html-app/pub-map.d.ts.map +1 -0
- package/types/html-app/router-data.d.ts +4 -0
- package/types/html-app/router-data.d.ts.map +1 -0
- package/types/html-app/router-navigation.d.ts +3 -0
- package/types/html-app/router-navigation.d.ts.map +1 -0
- package/types/html-app/storage-revisions.d.ts +11 -0
- package/types/html-app/storage-revisions.d.ts.map +1 -0
- package/types/html-app/sym-collection.d.ts +16 -0
- package/types/html-app/sym-collection.d.ts.map +1 -0
- package/types/html-app/sym-component.d.ts +5 -0
- package/types/html-app/sym-component.d.ts.map +1 -0
- package/types/html-app/sym-conditional.d.ts +10 -0
- package/types/html-app/sym-conditional.d.ts.map +1 -0
- package/types/html-app/sym-context.d.ts +16 -0
- package/types/html-app/sym-context.d.ts.map +1 -0
- package/types/html-app/sym-import.d.ts +19 -0
- package/types/html-app/sym-import.d.ts.map +1 -0
- package/types/html-app/sym-router.d.ts +12 -0
- package/types/html-app/sym-router.d.ts.map +1 -0
- package/docs/llms-index.md +0 -49
- package/llms-full.txt +0 -5432
- package/llms.txt +0 -49
- package/scripts/build-llms.js +0 -56
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { DICT } from '../core/dictionary.js';
|
|
2
|
+
import { ownElements } from '../core/ownElements.js';
|
|
3
|
+
import { controlValue, parseAction, resolveData, unavailable, validateValue } from './action-data.js';
|
|
4
|
+
import { prepareCollectionAction } from './collection-actions.js';
|
|
5
|
+
import { contextRequests, observeDataTarget } from './context-requests.js';
|
|
6
|
+
import { navigateAction } from './router-navigation.js';
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} ActionEvent
|
|
9
|
+
* @property {boolean} [defaultPrevented]
|
|
10
|
+
* @property {boolean} [isComposing]
|
|
11
|
+
* @property {number} [button]
|
|
12
|
+
* @property {boolean} [ctrlKey]
|
|
13
|
+
* @property {boolean} [metaKey]
|
|
14
|
+
* @property {boolean} [shiftKey]
|
|
15
|
+
* @property {boolean} [altKey]
|
|
16
|
+
* @property {Element | null} [submitter]
|
|
17
|
+
* @property {() => void} [preventDefault]
|
|
18
|
+
*
|
|
19
|
+
* @typedef {Object} Binding
|
|
20
|
+
* @property {Element} element
|
|
21
|
+
* @property {string} property
|
|
22
|
+
* @property {string} source
|
|
23
|
+
* @property {string} key
|
|
24
|
+
* @property {ReturnType<typeof parseAction>} action
|
|
25
|
+
* @property {boolean} composing
|
|
26
|
+
* @property {(() => boolean) | undefined} pending
|
|
27
|
+
* @property {ReturnType<typeof setTimeout> | undefined} timer
|
|
28
|
+
* @property {(() => void)[]} cleanup
|
|
29
|
+
* @property {{ remove(): void }[]} subscriptions
|
|
30
|
+
*/
|
|
31
|
+
let configuredForms = new WeakSet();
|
|
32
|
+
/**
|
|
33
|
+
* @param {Element | DocumentFragment} root
|
|
34
|
+
* @param {import('./BaseClass.js').BaseClass} owner
|
|
35
|
+
*/
|
|
36
|
+
export function actionProcessor(root, owner) {
|
|
37
|
+
// Preserve authored descriptors for browser hydration; SSR never invokes UI actions.
|
|
38
|
+
if (!globalThis.__SYMBIOTE_SSR) {
|
|
39
|
+
owner.actionBindings.process(root);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class ActionBindings {
|
|
43
|
+
/** @type {Binding[]} */
|
|
44
|
+
bindings = [];
|
|
45
|
+
connected = false;
|
|
46
|
+
version = 0;
|
|
47
|
+
sequence = 0;
|
|
48
|
+
/** @param {import('./BaseClass.js').BaseClass} owner */
|
|
49
|
+
constructor(owner) {
|
|
50
|
+
this.owner = owner;
|
|
51
|
+
}
|
|
52
|
+
connect() {
|
|
53
|
+
this.connected = true;
|
|
54
|
+
if (this.bindings.some((binding) => [
|
|
55
|
+
'pub-to',
|
|
56
|
+
'item-pub',
|
|
57
|
+
].includes(binding.action.operation))) {
|
|
58
|
+
this.owner.ownerDocument.addEventListener('submit', this.onSubmit, true);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
disconnect() {
|
|
62
|
+
this.connected = false;
|
|
63
|
+
this.version += 1;
|
|
64
|
+
this.owner.ownerDocument.removeEventListener('submit', this.onSubmit, true);
|
|
65
|
+
for (let binding of this.bindings) {
|
|
66
|
+
this.cancel(binding);
|
|
67
|
+
binding.composing = false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
invalidate() {
|
|
71
|
+
this.version += 1;
|
|
72
|
+
for (let binding of this.bindings) {
|
|
73
|
+
this.cancel(binding);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @param {Element | DocumentFragment} root
|
|
78
|
+
*/
|
|
79
|
+
process(root) {
|
|
80
|
+
for (let element of ownElements(root, '[bind]')) {
|
|
81
|
+
let parts = element.getAttribute('bind').split(';');
|
|
82
|
+
let events = parts.map((part) => part.split(':')[0].trim());
|
|
83
|
+
let changed = false;
|
|
84
|
+
let compiled = parts.map((part) => {
|
|
85
|
+
let colon = part.indexOf(':');
|
|
86
|
+
if (colon < 0) {
|
|
87
|
+
return part;
|
|
88
|
+
}
|
|
89
|
+
let property = part.slice(0, colon).trim();
|
|
90
|
+
let source = part.slice(colon + 1).trim();
|
|
91
|
+
if (!source.startsWith('[')) {
|
|
92
|
+
if (/^!+\[/.test(source)) {
|
|
93
|
+
throw new TypeError('Actions cannot use boolean binding casts.');
|
|
94
|
+
}
|
|
95
|
+
return part;
|
|
96
|
+
}
|
|
97
|
+
if (!/^on[a-z]+$/.test(property) || events.filter((event) => event === property).length !== 1) {
|
|
98
|
+
throw new TypeError('Each declarative action requires one event binding.');
|
|
99
|
+
}
|
|
100
|
+
let action = parseAction(source);
|
|
101
|
+
let old = this.bindings.find((binding) => binding.element === element && binding.property === property);
|
|
102
|
+
if (old) {
|
|
103
|
+
this.drop(old);
|
|
104
|
+
}
|
|
105
|
+
let key = '__htmlAppAction' + this.sequence++;
|
|
106
|
+
while (this.owner.localCtx.has(key)) {
|
|
107
|
+
key = '__htmlAppAction' + this.sequence++;
|
|
108
|
+
}
|
|
109
|
+
/** @type {Binding} */
|
|
110
|
+
let binding = {
|
|
111
|
+
element,
|
|
112
|
+
property,
|
|
113
|
+
source,
|
|
114
|
+
key,
|
|
115
|
+
action,
|
|
116
|
+
composing: false,
|
|
117
|
+
pending: undefined,
|
|
118
|
+
timer: undefined,
|
|
119
|
+
cleanup: [],
|
|
120
|
+
subscriptions: [],
|
|
121
|
+
};
|
|
122
|
+
this.bindings.push(binding);
|
|
123
|
+
this.owner.localCtx.add(key, (/** @type {ActionEvent} */ event) => this.activate(binding, event));
|
|
124
|
+
if (element instanceof HTMLFormElement && property === 'onsubmit') {
|
|
125
|
+
configuredForms.add(element);
|
|
126
|
+
}
|
|
127
|
+
if (property === 'oninput' && [
|
|
128
|
+
'pub-to',
|
|
129
|
+
'item-pub',
|
|
130
|
+
].includes(action.operation)) {
|
|
131
|
+
let start = () => {
|
|
132
|
+
this.cancel(binding);
|
|
133
|
+
binding.composing = true;
|
|
134
|
+
};
|
|
135
|
+
let end = () => {
|
|
136
|
+
binding.composing = false;
|
|
137
|
+
this.activate(binding);
|
|
138
|
+
};
|
|
139
|
+
element.addEventListener('compositionstart', start);
|
|
140
|
+
element.addEventListener('compositionend', end);
|
|
141
|
+
binding.cleanup.push(() => {
|
|
142
|
+
element.removeEventListener('compositionstart', start);
|
|
143
|
+
element.removeEventListener('compositionend', end);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
changed = true;
|
|
147
|
+
return property + ': ' + key;
|
|
148
|
+
});
|
|
149
|
+
if (changed) {
|
|
150
|
+
element.setAttribute('bind', compiled.join(';'));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** @param {Binding} binding */
|
|
155
|
+
cancel(binding) {
|
|
156
|
+
clearTimeout(binding.timer);
|
|
157
|
+
binding.timer = undefined;
|
|
158
|
+
binding.pending = undefined;
|
|
159
|
+
for (let subscription of binding.subscriptions) {
|
|
160
|
+
subscription.remove();
|
|
161
|
+
}
|
|
162
|
+
binding.subscriptions = [];
|
|
163
|
+
}
|
|
164
|
+
/** @param {Binding} binding */
|
|
165
|
+
drop(binding) {
|
|
166
|
+
this.cancel(binding);
|
|
167
|
+
for (let cleanup of binding.cleanup) {
|
|
168
|
+
cleanup();
|
|
169
|
+
}
|
|
170
|
+
if (binding.element instanceof HTMLFormElement) {
|
|
171
|
+
configuredForms.delete(binding.element);
|
|
172
|
+
}
|
|
173
|
+
this.owner.localCtx.delete(binding.key);
|
|
174
|
+
this.bindings.splice(this.bindings.indexOf(binding), 1);
|
|
175
|
+
}
|
|
176
|
+
destroy() {
|
|
177
|
+
this.disconnect();
|
|
178
|
+
for (let binding of [
|
|
179
|
+
...this.bindings,
|
|
180
|
+
]) {
|
|
181
|
+
this.drop(binding);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** @param {SubmitEvent} event */
|
|
185
|
+
onSubmit = (event) => {
|
|
186
|
+
if (!(event.target instanceof HTMLFormElement) || event.defaultPrevented) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (!this.flushForm(event.target) && configuredForms.has(event.target)) {
|
|
190
|
+
event.preventDefault();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
/** @param {HTMLFormElement} form */
|
|
194
|
+
flushForm(form) {
|
|
195
|
+
let ready = true;
|
|
196
|
+
for (let binding of this.bindings) {
|
|
197
|
+
if (Reflect.get(binding.element, 'form') !== form) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (binding.composing) {
|
|
201
|
+
ready = false;
|
|
202
|
+
}
|
|
203
|
+
let commit = binding.pending;
|
|
204
|
+
if (commit) {
|
|
205
|
+
commit();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return ready;
|
|
209
|
+
}
|
|
210
|
+
identity() {
|
|
211
|
+
/** @type {Element | null} */
|
|
212
|
+
let node = this.owner;
|
|
213
|
+
while (node) {
|
|
214
|
+
let runtime = Reflect.get(node, 'actionBindings');
|
|
215
|
+
if (Reflect.has(node, DICT.MCP_ITEM_INDEX_KEY) && runtime instanceof ActionBindings) {
|
|
216
|
+
return {
|
|
217
|
+
runtime,
|
|
218
|
+
version: runtime.version,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
node = node.parentElement;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @param {Binding} binding
|
|
226
|
+
* @param {ActionEvent} [event]
|
|
227
|
+
*/
|
|
228
|
+
activate(binding, event = {}) {
|
|
229
|
+
let element = binding.element;
|
|
230
|
+
if (!this.bindings.includes(binding) || event.defaultPrevented || unavailable(element, this.owner)) {
|
|
231
|
+
return {
|
|
232
|
+
changed: false,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
let operand = element;
|
|
236
|
+
if (element instanceof HTMLFormElement && binding.property === 'onsubmit') {
|
|
237
|
+
event.preventDefault?.();
|
|
238
|
+
if (!this.flushForm(element)) {
|
|
239
|
+
return {
|
|
240
|
+
changed: false,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
if (event.submitter) {
|
|
244
|
+
operand = event.submitter;
|
|
245
|
+
}
|
|
246
|
+
if (unavailable(operand, this.owner) || (!element.noValidate
|
|
247
|
+
&& !operand.hasAttribute('formnovalidate') && !element.checkValidity())) {
|
|
248
|
+
return {
|
|
249
|
+
changed: false,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
this.cancel(binding);
|
|
254
|
+
if (binding.composing || event.isComposing) {
|
|
255
|
+
return {
|
|
256
|
+
changed: false,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
let operation = binding.action.operation;
|
|
260
|
+
if (operation === 'navigate') {
|
|
261
|
+
return navigateAction(this.owner, binding.action.reference, element, operand, event);
|
|
262
|
+
}
|
|
263
|
+
if (operation === 'load' || operation === 'sync') {
|
|
264
|
+
let requests = contextRequests(binding.action.reference);
|
|
265
|
+
return operation === 'load'
|
|
266
|
+
? requests.load(operand.hasAttribute('action-overwrite') || element.hasAttribute('action-overwrite'))
|
|
267
|
+
: requests.sync();
|
|
268
|
+
}
|
|
269
|
+
if (operation === 'persist' || operation === 'restore') {
|
|
270
|
+
let storage = contextRequests(binding.action.reference).owner.storage;
|
|
271
|
+
return operation === 'persist'
|
|
272
|
+
? storage.persist(operand.hasAttribute('action-overwrite') || element.hasAttribute('action-overwrite'))
|
|
273
|
+
: storage.restore();
|
|
274
|
+
}
|
|
275
|
+
if (operation === 'item-pub' && Reflect.get(element, 'validity')?.valid === false) {
|
|
276
|
+
return {
|
|
277
|
+
changed: false,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
let isCollection = ![
|
|
281
|
+
'toggle',
|
|
282
|
+
'set',
|
|
283
|
+
'pub-to',
|
|
284
|
+
].includes(operation);
|
|
285
|
+
let prepared = isCollection ? prepareCollectionAction(this.owner, binding.action, element, operand) : undefined;
|
|
286
|
+
if (isCollection && !prepared) {
|
|
287
|
+
return {
|
|
288
|
+
changed: false,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
let target = prepared?.target || resolveData(this.owner, binding.action.reference);
|
|
292
|
+
let before = target.ctx.read(target.name);
|
|
293
|
+
let value;
|
|
294
|
+
if (prepared) {
|
|
295
|
+
value = prepared.value;
|
|
296
|
+
} else if (binding.action.operation === 'toggle') {
|
|
297
|
+
if (typeof before !== 'boolean') {
|
|
298
|
+
throw new TypeError('toggle requires a boolean target.');
|
|
299
|
+
}
|
|
300
|
+
value = !before;
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
let data = operand.getAttribute('action-data') ?? element.getAttribute('action-data');
|
|
304
|
+
if (data !== null && binding.action.operation === 'set') {
|
|
305
|
+
let source = resolveData(this.owner, data, true);
|
|
306
|
+
value = source.ctx.read(source.name);
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
let result = controlValue(operand, before);
|
|
310
|
+
if (result.skip) {
|
|
311
|
+
return {
|
|
312
|
+
changed: false,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
value = result.value;
|
|
316
|
+
}
|
|
317
|
+
validateValue(before, value);
|
|
318
|
+
}
|
|
319
|
+
let identity = this.identity();
|
|
320
|
+
let commit = () => {
|
|
321
|
+
this.cancel(binding);
|
|
322
|
+
if (unavailable(element, this.owner)
|
|
323
|
+
|| (identity && identity.runtime.version !== identity.version)) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
let latest;
|
|
327
|
+
try {
|
|
328
|
+
latest = prepared ? prepared.resolve() : resolveData(this.owner, binding.action.reference);
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
if (latest.ctx !== target.ctx || latest.name !== target.name || !Object.is(latest.ctx.read(latest.name), before)) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
if (Object.is(before, value)) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
observeDataTarget(latest);
|
|
340
|
+
latest.ctx.pub(latest.name, value);
|
|
341
|
+
prepared?.after?.();
|
|
342
|
+
return true;
|
|
343
|
+
};
|
|
344
|
+
if (binding.action.delay) {
|
|
345
|
+
if (!Object.is(before, value)) {
|
|
346
|
+
let intent = observeDataTarget(target)?.intent(target.name, value);
|
|
347
|
+
if (intent) {
|
|
348
|
+
binding.subscriptions.push(intent);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
binding.pending = commit;
|
|
352
|
+
let subscription = target.ctx.sub(target.name, () => this.cancel(binding), false);
|
|
353
|
+
if (subscription) {
|
|
354
|
+
binding.subscriptions.push(subscription);
|
|
355
|
+
}
|
|
356
|
+
binding.timer = setTimeout(commit, binding.action.delay);
|
|
357
|
+
return {
|
|
358
|
+
changed: false,
|
|
359
|
+
pending: true,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
changed: commit(),
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { resolveData, controlValue, validateValue } from './action-data.js';
|
|
2
|
+
import { initialDefaults } from './initial-data.js';
|
|
3
|
+
import { appendRecord, prependRecord, cloneData, collectionData, recordData, updateMatching, updateRecord } from './collection-data.js';
|
|
4
|
+
import { findCollectionItem } from './collection-items.js';
|
|
5
|
+
|
|
6
|
+
let insertionActions = {
|
|
7
|
+
append: appendRecord,
|
|
8
|
+
prepend: prependRecord,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {import('./BaseClass.js').BaseClass} owner
|
|
13
|
+
* @param {Element} element
|
|
14
|
+
* @param {Element} operand
|
|
15
|
+
* @param {string} name
|
|
16
|
+
*/
|
|
17
|
+
function readOperand(owner, element, operand, name) {
|
|
18
|
+
let reference = operand.getAttribute(name) ?? element.getAttribute(name);
|
|
19
|
+
if (!reference) {
|
|
20
|
+
throw new TypeError('Collection action requires ' + name + '.');
|
|
21
|
+
}
|
|
22
|
+
let target = resolveData(owner, reference, true);
|
|
23
|
+
return target.ctx.read(target.name);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Prepare a validated immutable update; publication remains in the action runtime.
|
|
28
|
+
* @param {import('./BaseClass.js').BaseClass} owner
|
|
29
|
+
* @param {ReturnType<import('./action-data.js').parseAction>} action
|
|
30
|
+
* @param {Element} element
|
|
31
|
+
* @param {Element} operand
|
|
32
|
+
*/
|
|
33
|
+
export function prepareCollectionAction(owner, action, element, operand) {
|
|
34
|
+
if (action.operation.startsWith('item-')) {
|
|
35
|
+
let {
|
|
36
|
+
collection,
|
|
37
|
+
id,
|
|
38
|
+
} = findCollectionItem(owner);
|
|
39
|
+
let target = collection.source();
|
|
40
|
+
let before = collectionData(target.ctx.read(target.name));
|
|
41
|
+
let record = before.find((item) => item.id === id);
|
|
42
|
+
if (!record) {
|
|
43
|
+
throw new TypeError('The collection record no longer exists.');
|
|
44
|
+
}
|
|
45
|
+
let patch = null;
|
|
46
|
+
if (action.operation !== 'item-remove') {
|
|
47
|
+
let field = action.reference;
|
|
48
|
+
if (field === 'id' || !Object.hasOwn(record, field)) {
|
|
49
|
+
throw new TypeError('Item actions require an existing mutable field.');
|
|
50
|
+
}
|
|
51
|
+
let value;
|
|
52
|
+
if (action.operation === 'item-toggle') {
|
|
53
|
+
if (typeof record[field] !== 'boolean') {
|
|
54
|
+
throw new TypeError('item-toggle requires a boolean field.');
|
|
55
|
+
}
|
|
56
|
+
value = !record[field];
|
|
57
|
+
} else {
|
|
58
|
+
let control = controlValue(operand, record[field]);
|
|
59
|
+
if (control.skip) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
value = control.value;
|
|
63
|
+
validateValue(record[field], value);
|
|
64
|
+
}
|
|
65
|
+
patch = {
|
|
66
|
+
[field]: value,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
target,
|
|
71
|
+
value: updateRecord(before, id, patch),
|
|
72
|
+
resolve: () => collection.source(),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
let target = resolveData(owner, action.reference);
|
|
76
|
+
let before = target.ctx.read(target.name);
|
|
77
|
+
let value;
|
|
78
|
+
/** @type {(() => void) | undefined} */
|
|
79
|
+
let after;
|
|
80
|
+
if (Object.hasOwn(insertionActions, action.operation)) {
|
|
81
|
+
value = insertionActions[action.operation](before, readOperand(owner, element, operand, 'action-data'));
|
|
82
|
+
let reference = operand.getAttribute('action-reset') ?? element.getAttribute('action-reset');
|
|
83
|
+
if (reference !== null) {
|
|
84
|
+
let reset = resolveData(owner, reference);
|
|
85
|
+
let defaults = initialDefaults.get(reset.ctx);
|
|
86
|
+
if (!defaults || !Object.hasOwn(defaults, reset.name)) {
|
|
87
|
+
throw new TypeError('action-reset requires a declared JSON default.');
|
|
88
|
+
}
|
|
89
|
+
let initial = cloneData(recordData(defaults[reset.name]));
|
|
90
|
+
after = () => reset.ctx.pub(reset.name, initial);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
let match = readOperand(owner, element, operand, 'action-match');
|
|
94
|
+
value = action.operation === 'patch-matching'
|
|
95
|
+
? updateMatching(before, match, readOperand(owner, element, operand, 'action-data'))
|
|
96
|
+
: updateMatching(before, match);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
target,
|
|
100
|
+
value,
|
|
101
|
+
after,
|
|
102
|
+
resolve: () => resolveData(owner, action.reference),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { isDataKey } from './initial-data.js';
|
|
2
|
+
import { validateValue } from './action-data.js';
|
|
3
|
+
|
|
4
|
+
/** @typedef {Record<string, any>} RecordData */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validate JSON values without dropping unsupported fields during cloning.
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @param {Set<object>} [ancestors]
|
|
10
|
+
*/
|
|
11
|
+
export function validateJSON(value, ancestors = new Set()) {
|
|
12
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean'
|
|
13
|
+
|| (typeof value === 'number' && Number.isFinite(value))) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (!value || typeof value !== 'object' || ancestors.has(value)
|
|
17
|
+
|| (!Array.isArray(value) && ![
|
|
18
|
+
Object.prototype,
|
|
19
|
+
null,
|
|
20
|
+
].includes(Object.getPrototypeOf(value)))) {
|
|
21
|
+
throw new TypeError('Collection data must contain JSON values only.');
|
|
22
|
+
}
|
|
23
|
+
if (Object.getOwnPropertySymbols(value).length) {
|
|
24
|
+
throw new TypeError('Collection data cannot contain symbol fields.');
|
|
25
|
+
}
|
|
26
|
+
ancestors.add(value);
|
|
27
|
+
for (let key of Object.keys(value)) {
|
|
28
|
+
if (!Array.isArray(value) && !isDataKey(key)) {
|
|
29
|
+
throw new TypeError('Collection data contains an unsafe field.');
|
|
30
|
+
}
|
|
31
|
+
validateJSON(value[key], ancestors);
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(value) && (Object.keys(value).length !== value.length
|
|
34
|
+
|| Object.keys(value).some((key, index) => key !== String(index)))) {
|
|
35
|
+
throw new TypeError('Collection arrays must not contain holes or extra properties.');
|
|
36
|
+
}
|
|
37
|
+
ancestors.delete(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @param {unknown} value
|
|
41
|
+
* @returns {RecordData}
|
|
42
|
+
*/
|
|
43
|
+
export function recordData(value) {
|
|
44
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
45
|
+
throw new TypeError('Expected a JSON record.');
|
|
46
|
+
}
|
|
47
|
+
validateJSON(value);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param {unknown} value
|
|
52
|
+
* @returns {RecordData[]}
|
|
53
|
+
*/
|
|
54
|
+
export function collectionData(value) {
|
|
55
|
+
if (!Array.isArray(value)) {
|
|
56
|
+
throw new TypeError('Expected an array of records.');
|
|
57
|
+
}
|
|
58
|
+
let keys = new Set();
|
|
59
|
+
for (let item of value) {
|
|
60
|
+
recordData(item);
|
|
61
|
+
if (typeof item.id !== 'string' || !item.id || keys.has(item.id)) {
|
|
62
|
+
throw new TypeError('Collection records require unique nonempty string IDs.');
|
|
63
|
+
}
|
|
64
|
+
keys.add(item.id);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @template T
|
|
71
|
+
* @param {T} value
|
|
72
|
+
* @returns {T}
|
|
73
|
+
*/
|
|
74
|
+
export function cloneData(value) {
|
|
75
|
+
validateJSON(value);
|
|
76
|
+
return structuredClone(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {RecordData} item
|
|
81
|
+
* @param {RecordData} match
|
|
82
|
+
*/
|
|
83
|
+
export function matches(item, match) {
|
|
84
|
+
return Object.entries(match).every(([
|
|
85
|
+
key,
|
|
86
|
+
value,
|
|
87
|
+
]) => Object.hasOwn(item, key) && Object.is(item[key], value));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @param {unknown} value */
|
|
91
|
+
export function matchData(value) {
|
|
92
|
+
let match = recordData(value);
|
|
93
|
+
if (Object.values(match).some((value) => value !== null && typeof value === 'object')) {
|
|
94
|
+
throw new TypeError('Matching accepts JSON scalar values only.');
|
|
95
|
+
}
|
|
96
|
+
return match;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {unknown} source
|
|
101
|
+
* @param {unknown} draft
|
|
102
|
+
* @param {boolean} atStart
|
|
103
|
+
*/
|
|
104
|
+
function insertRecord(source, draft, atStart) {
|
|
105
|
+
let items = collectionData(source);
|
|
106
|
+
let item = cloneData(recordData(draft));
|
|
107
|
+
if (!Object.hasOwn(item, 'id')) {
|
|
108
|
+
item.id = crypto.randomUUID();
|
|
109
|
+
}
|
|
110
|
+
let next = atStart ? [
|
|
111
|
+
item,
|
|
112
|
+
...items,
|
|
113
|
+
] : [
|
|
114
|
+
...items,
|
|
115
|
+
item,
|
|
116
|
+
];
|
|
117
|
+
collectionData(next);
|
|
118
|
+
return next;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {unknown} source
|
|
123
|
+
* @param {unknown} draft
|
|
124
|
+
*/
|
|
125
|
+
export function appendRecord(source, draft) {
|
|
126
|
+
return insertRecord(source, draft, false);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {unknown} source
|
|
131
|
+
* @param {unknown} draft
|
|
132
|
+
*/
|
|
133
|
+
export function prependRecord(source, draft) {
|
|
134
|
+
return insertRecord(source, draft, true);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {RecordData} item
|
|
139
|
+
* @param {RecordData} patch
|
|
140
|
+
*/
|
|
141
|
+
function patchedRecord(item, patch) {
|
|
142
|
+
if (Object.hasOwn(patch, 'id') && patch.id !== item.id) {
|
|
143
|
+
throw new TypeError('A collection record ID cannot be changed.');
|
|
144
|
+
}
|
|
145
|
+
for (let key of Object.keys(patch)) {
|
|
146
|
+
if (Object.hasOwn(item, key)) {
|
|
147
|
+
validateValue(item[key], patch[key]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
...item,
|
|
152
|
+
...cloneData(patch),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {unknown} source
|
|
158
|
+
* @param {string} id
|
|
159
|
+
* @param {RecordData | null} patch Null removes the record.
|
|
160
|
+
*/
|
|
161
|
+
export function updateRecord(source, id, patch) {
|
|
162
|
+
let items = collectionData(source);
|
|
163
|
+
if (!items.some((item) => item.id === id)) {
|
|
164
|
+
throw new TypeError('The collection record no longer exists.');
|
|
165
|
+
}
|
|
166
|
+
if (patch === null) {
|
|
167
|
+
return items.filter((item) => item.id !== id);
|
|
168
|
+
}
|
|
169
|
+
recordData(patch);
|
|
170
|
+
return items.map((item) => item.id === id ? patchedRecord(item, patch) : item);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @param {unknown} source
|
|
175
|
+
* @param {unknown} match
|
|
176
|
+
* @param {unknown} [patch] Omit to remove matching records.
|
|
177
|
+
*/
|
|
178
|
+
export function updateMatching(source, match, patch) {
|
|
179
|
+
let items = collectionData(source);
|
|
180
|
+
let filter = matchData(match);
|
|
181
|
+
if (patch === undefined) {
|
|
182
|
+
if (!Object.keys(filter).length) {
|
|
183
|
+
throw new TypeError('Removing records requires a nonempty match.');
|
|
184
|
+
}
|
|
185
|
+
return items.filter((item) => !matches(item, filter));
|
|
186
|
+
}
|
|
187
|
+
let changes = recordData(patch);
|
|
188
|
+
if (Object.hasOwn(changes, 'id')) {
|
|
189
|
+
throw new TypeError('Bulk patches cannot change record IDs.');
|
|
190
|
+
}
|
|
191
|
+
return items.map((item) => matches(item, filter) ? patchedRecord(item, changes) : item);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {unknown} source
|
|
196
|
+
* @param {unknown} [filter]
|
|
197
|
+
* @param {unknown} [search]
|
|
198
|
+
* @param {string[]} [fields]
|
|
199
|
+
* @param {unknown} [sort]
|
|
200
|
+
*/
|
|
201
|
+
export function projectCollection(source, filter = {}, search = '', fields = [], sort = null) {
|
|
202
|
+
let items = collectionData(source);
|
|
203
|
+
let match = matchData(filter);
|
|
204
|
+
if (typeof search !== 'string') {
|
|
205
|
+
throw new TypeError('Collection search must be a string.');
|
|
206
|
+
}
|
|
207
|
+
if (fields.some((field) => !isDataKey(field))) {
|
|
208
|
+
throw new TypeError('Search fields must be local data keys.');
|
|
209
|
+
}
|
|
210
|
+
let query = search.trim().toLocaleLowerCase();
|
|
211
|
+
if (query && !fields.length) {
|
|
212
|
+
throw new TypeError('Collection search requires explicit search-fields.');
|
|
213
|
+
}
|
|
214
|
+
let visible = items.filter((item) => matches(item, match)
|
|
215
|
+
&& (!query || fields.some((field) => String(item[field] ?? '').toLocaleLowerCase().includes(query))));
|
|
216
|
+
if (sort !== null) {
|
|
217
|
+
let order = recordData(sort);
|
|
218
|
+
if (typeof order.field !== 'string' || !isDataKey(order.field)
|
|
219
|
+
|| ![
|
|
220
|
+
'asc',
|
|
221
|
+
'desc',
|
|
222
|
+
].includes(order.direction)) {
|
|
223
|
+
throw new TypeError('Collection sort requires field and asc/desc direction.');
|
|
224
|
+
}
|
|
225
|
+
visible.sort((left, right) => {
|
|
226
|
+
let a = left[order.field];
|
|
227
|
+
let b = right[order.field];
|
|
228
|
+
let result = typeof a === 'number' && typeof b === 'number' ? a - b
|
|
229
|
+
: String(a ?? '').localeCompare(String(b ?? ''));
|
|
230
|
+
return order.direction === 'desc' ? -result : result;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return visible;
|
|
234
|
+
}
|