@gramlot/gramlot 0.2.16 → 0.2.18
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/assets/branding/app-icon.png +0 -0
- package/dist/gramlot.js +1413 -235
- package/dist/gramlot.min.js +74 -17
- package/package.json +9 -5
- package/src/binding/ask-dialog.js +108 -0
- package/src/binding/providers.js +41 -6
- package/src/binding/remote.js +19 -20
- package/src/binding/runtime.js +38 -2
- package/src/builder/gramlot-builder.js +21 -3
- package/src/builder/source.js +18 -5
- package/src/builder/webcomponent.json +51 -0
- package/src/frame-messages.js +177 -0
- package/src/gramlot.d.ts +14 -0
- package/src/gramlot.js +9 -1
- package/src/handlers/rpc-handler.js +2 -0
- package/src/handlers/source-handler.js +1 -0
- package/src/handlers/subscriptions-handler.js +103 -0
- package/src/index-page-runtime.d.ts +11 -0
- package/src/index-page-runtime.js +287 -0
- package/src/index-page-styles.js +2 -0
- package/src/index-theme.js +5 -0
- package/src/index.d.ts +4 -1
- package/src/index.js +3 -0
- package/src/inout.js +3 -1
- package/src/references.d.ts +5 -0
- package/src/references.js +23 -1
- package/src/renderer/attributes.js +14 -3
- package/src/renderer/gramlot-html-renderer.js +9 -2
- package/src/renderer/gramlot-renderer.js +5 -1
- package/src/renderer/gramlot-svg-renderer.js +2 -1
- package/src/server/conformance.js +22 -2
- package/src/server/gramlot-server.d.ts +1 -0
- package/src/server/gramlot-server.js +12 -0
- package/src/server/index-page.d.ts +14 -0
- package/src/server/index-page.js +102 -0
- package/src/server/page-base.js +88 -0
- package/src/server/page.d.ts +8 -2
- package/src/server/page.js +3 -76
- package/src/view/button.js +17 -3
- package/src/view/html.js +4 -3
- package/src/webcomponent.js +13 -0
- package/themes/gramlot-base/README.md +8 -0
- package/themes/gramlot-compact/README.md +49 -0
- package/themes/gramlot-compact/theme.css +57 -0
- package/themes/gramlot-index/theme.css +51 -0
- package/tools/gramlot-tools-inspector.js +1733 -0
- package/tools/inspector-agent.js +252 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"prepack": "node scripts/build-runtime.mjs"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@genrojs/bag": ">=0.
|
|
20
|
-
"@genrojs/builders": ">=0.
|
|
19
|
+
"@genrojs/bag": ">=0.11.0",
|
|
20
|
+
"@genrojs/builders": ">=0.5.2",
|
|
21
21
|
"@genrojs/tytx": ">=0.16.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
@@ -32,7 +32,10 @@
|
|
|
32
32
|
"./page": "./src/server/page.js",
|
|
33
33
|
"./gramlot-server": "./src/server/gramlot-server.js",
|
|
34
34
|
"./themes/*": "./themes/*",
|
|
35
|
-
"./assets/branding/*": "./assets/branding/*"
|
|
35
|
+
"./assets/branding/*": "./assets/branding/*",
|
|
36
|
+
"./tools/inspector": "./tools/gramlot-tools-inspector.js",
|
|
37
|
+
"./index-page-runtime": "./src/index-page-runtime.js",
|
|
38
|
+
"./tools/inspector-agent": "./tools/inspector-agent.js"
|
|
36
39
|
},
|
|
37
40
|
"files": [
|
|
38
41
|
"LICENSE",
|
|
@@ -40,7 +43,8 @@
|
|
|
40
43
|
"src",
|
|
41
44
|
"dist",
|
|
42
45
|
"themes",
|
|
43
|
-
"assets"
|
|
46
|
+
"assets",
|
|
47
|
+
"tools"
|
|
44
48
|
],
|
|
45
49
|
"engines": {
|
|
46
50
|
"node": ">=22"
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
2
|
+
|
|
3
|
+
const pages = new WeakMap();
|
|
4
|
+
const SPEC_KEYS = new Set(['title', 'msg', 'fields', '_if', 'askIf']);
|
|
5
|
+
const FIELD_KEYS = new Set(['name', 'lbl', 'tag', 'type', 'default_value', 'required', 'min', 'max', 'step', 'placeholder']);
|
|
6
|
+
|
|
7
|
+
/** A bounded native dialog authored through Source/Data and named Logic. */
|
|
8
|
+
export class AskDialog {
|
|
9
|
+
static required(binding, specification, parameters, attribute) {
|
|
10
|
+
const evaluate = (name, code) => typeof code === 'boolean' ? code
|
|
11
|
+
: binding.runtime.inlineCompiler.compile(binding.node, name, code,
|
|
12
|
+
Object.keys(parameters), `return (${code});`)(parameters);
|
|
13
|
+
if (typeof specification === 'string') {
|
|
14
|
+
const condition = binding.node.getAttr('_ask_if');
|
|
15
|
+
return !condition || Boolean(evaluate('_ask_if', condition));
|
|
16
|
+
}
|
|
17
|
+
// Legacy askIf alone does not suppress the prompt; askOn/skipOn are not ported.
|
|
18
|
+
if (specification?.askIf) evaluate(`${attribute}.askIf`, specification.askIf);
|
|
19
|
+
return !specification?._if || Boolean(evaluate(`${attribute}._if`, specification._if));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static show(binding, specification, parameters, title = 'Parameters') {
|
|
23
|
+
const spec = typeof specification === 'string' ? {msg: specification, title} : specification;
|
|
24
|
+
if (!spec || typeof spec !== 'object' || Array.isArray(spec)) throw new TypeError('ask requires text or a parameter description');
|
|
25
|
+
for (const key of Object.keys(spec)) {
|
|
26
|
+
if (!SPEC_KEYS.has(key)) throw new Error(`Unsupported ask option: ${key}`);
|
|
27
|
+
}
|
|
28
|
+
const fields = spec.fields ?? [];
|
|
29
|
+
if (!Array.isArray(fields)) throw new TypeError('ask fields must be an array');
|
|
30
|
+
const names = new Set();
|
|
31
|
+
for (const field of fields) {
|
|
32
|
+
if (!field || !/^[A-Za-z_]\w*$/.test(field.name) || names.has(field.name)) throw new Error('ask fields require distinct simple names');
|
|
33
|
+
names.add(field.name);
|
|
34
|
+
for (const key of Object.keys(field)) {
|
|
35
|
+
if (!FIELD_KEYS.has(key)) throw new Error(`Unsupported ask field option: ${key}`);
|
|
36
|
+
}
|
|
37
|
+
if (field.tag && !['input', 'textarea'].includes(field.tag)) throw new Error(`Unsupported ask widget: ${field.tag}`);
|
|
38
|
+
if (field.type && !['text', 'number', 'date', 'checkbox', 'email', 'password', 'url', 'tel'].includes(field.type)) {
|
|
39
|
+
throw new Error(`Unsupported ask input type: ${field.type}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const app = binding.runtime.gramlot;
|
|
43
|
+
let page = pages.get(app);
|
|
44
|
+
if (!page) {
|
|
45
|
+
const group = `ask_${Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
46
|
+
const requests = new Map();
|
|
47
|
+
class Logic {
|
|
48
|
+
confirm(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(true); }
|
|
49
|
+
cancel(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(false); }
|
|
50
|
+
}
|
|
51
|
+
app.src.logicRegistry.register(Logic, {group, resource: 'gramlot:ask'});
|
|
52
|
+
page = {group, requests};
|
|
53
|
+
pages.set(app, page);
|
|
54
|
+
}
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const id = `ask_${Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
57
|
+
let finished = false;
|
|
58
|
+
let release = () => {};
|
|
59
|
+
const finish = (confirmed, failure = null) => {
|
|
60
|
+
if (finished) return;
|
|
61
|
+
finished = true;
|
|
62
|
+
const result = confirmed ? Object.fromEntries(fields.map(field =>
|
|
63
|
+
[field.name, app.data.getItem(`${id}.${field.name}`)])) : null;
|
|
64
|
+
page.requests.delete(id);
|
|
65
|
+
release();
|
|
66
|
+
try {
|
|
67
|
+
app.src.source.popNode(id);
|
|
68
|
+
if (app.data.getNode(id)) app.data.popNode(id);
|
|
69
|
+
if (failure) reject(failure);
|
|
70
|
+
else resolve(result);
|
|
71
|
+
} catch (error) { reject(error); }
|
|
72
|
+
};
|
|
73
|
+
page.requests.set(id, {finish});
|
|
74
|
+
release = binding.track(() => finish(false));
|
|
75
|
+
try {
|
|
76
|
+
const builder = new GramlotBuilder();
|
|
77
|
+
const dialog = builder.root.dialog({node_id: id, datapath: id, request: id,
|
|
78
|
+
connect_oncancel: `${page.group}.cancel`, connect_onclose: `${page.group}.cancel`,
|
|
79
|
+
class: 'gramlot-ask', 'aria-labelledby': `${id}_title`,
|
|
80
|
+
style: 'border:1px solid #aab4c2;border-radius:8px;padding:16px;max-width:32rem;width:calc(100% - 48px);font:inherit;'});
|
|
81
|
+
dialog.h2(spec.title ?? title, {id: `${id}_title`, style: 'font-size:1.1em;margin:0 0 12px;'});
|
|
82
|
+
if (spec.msg) dialog.p(spec.msg);
|
|
83
|
+
const form = dialog.form({request: id, connect_onsubmit: `${page.group}.confirm`});
|
|
84
|
+
for (const field of fields) {
|
|
85
|
+
form.dataSetter({destination_path: `.${field.name}`,
|
|
86
|
+
value: Object.hasOwn(parameters, field.name) ? parameters[field.name]
|
|
87
|
+
: field.default_value ?? (field.type === 'checkbox' ? false : '')});
|
|
88
|
+
const row = form.div({style: 'margin:8px 0;'});
|
|
89
|
+
row.html_label(field.lbl ?? field.name, {for: `${id}_${field.name}`, style: 'display:block;margin-bottom:4px;'});
|
|
90
|
+
const attrs = {id: `${id}_${field.name}`, value: `^.${field.name}`, live: true};
|
|
91
|
+
for (const key of ['required', 'min', 'max', 'step', 'placeholder']) {
|
|
92
|
+
if (field[key] !== undefined) attrs[key] = field[key];
|
|
93
|
+
}
|
|
94
|
+
if (field.tag === 'textarea') row.textarea(attrs);
|
|
95
|
+
else row.input({...attrs, type: field.type ?? 'text'});
|
|
96
|
+
}
|
|
97
|
+
const buttons = form.div({style: 'display:flex;justify-content:flex-end;gap:8px;margin-top:16px;'});
|
|
98
|
+
buttons.button('Cancel', {type: 'button', request: id, connect_onclick: `${page.group}.cancel`});
|
|
99
|
+
buttons.button('Confirm', {type: 'submit', autofocus: fields.length === 0});
|
|
100
|
+
app.src.source.setItem(id, app.src.prepareSource(builder.source));
|
|
101
|
+
const node = app.src.binding.nodeIds.get(id);
|
|
102
|
+
app.dom.getDomNode(node).showModal();
|
|
103
|
+
} catch (error) {
|
|
104
|
+
finish(false, error);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/binding/providers.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {AskDialog} from './ask-dialog.js';
|
|
1
2
|
import {Bag} from '@genrojs/bag';
|
|
2
3
|
import {RpcResolver} from './rpc-resolver.js';
|
|
3
4
|
|
|
@@ -17,6 +18,7 @@ import {RpcResolver} from './rpc-resolver.js';
|
|
|
17
18
|
const CONTROL_ATTRIBUTES = new Set([
|
|
18
19
|
'destination_path', 'result_path', 'func', 'formula', 'script', 'if', 'else',
|
|
19
20
|
'_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
21
|
+
'_ask', '_ask_if', '_ask_title', '_ask_onCancel',
|
|
20
22
|
'method', 'timeout', '_onCalling', '_onResult', '_onError', 'cacheTime',
|
|
21
23
|
]);
|
|
22
24
|
|
|
@@ -70,6 +72,22 @@ export class Provider {
|
|
|
70
72
|
this.#registrations.push(registration);
|
|
71
73
|
if (name === '_timing') this.#timingRegistration = registration;
|
|
72
74
|
}
|
|
75
|
+
const subscriptions = this.binding.runtime.gramlot._subscriptions;
|
|
76
|
+
for (const name of Object.keys(node.getAttr())) {
|
|
77
|
+
const prefix = name.startsWith('selfsubscribe_') ? 'selfsubscribe_' : name.startsWith('subscribe_') ? 'subscribe_' : null;
|
|
78
|
+
if (!prefix) continue;
|
|
79
|
+
const topic = prefix === 'selfsubscribe_'
|
|
80
|
+
? subscriptions.topicFor(node, name.slice(prefix.length)) : name.slice(prefix.length);
|
|
81
|
+
// Legacy provider declarations subscribe by presence, including a false value.
|
|
82
|
+
const handle = subscriptions.subscribe(topic, payload => {
|
|
83
|
+
const trigger = {kind: 'subscription', topic, payload};
|
|
84
|
+
if (this.#resolved('_userChanges') && topic !== 'node') return;
|
|
85
|
+
const delay = this.#resolved('_delay');
|
|
86
|
+
if (delay > 0) this.#debounce(trigger, delay);
|
|
87
|
+
else this.invoke(trigger);
|
|
88
|
+
});
|
|
89
|
+
this.#registrations.push({close: () => subscriptions.unsubscribe(handle)});
|
|
90
|
+
}
|
|
73
91
|
this.#startTiming();
|
|
74
92
|
if (this.kind === 'remote') this.invoke({kind: 'install', change: null});
|
|
75
93
|
}
|
|
@@ -110,6 +128,22 @@ export class Provider {
|
|
|
110
128
|
const rpc = this.kind === 'rpc';
|
|
111
129
|
const remote = this.kind === 'remote';
|
|
112
130
|
const kwargs = this.readArguments(trigger);
|
|
131
|
+
if (trigger.askResult) {
|
|
132
|
+
Object.assign(kwargs, trigger.askResult);
|
|
133
|
+
kwargs._askResult = trigger.askResult;
|
|
134
|
+
}
|
|
135
|
+
const ask = node.getAttr('_ask');
|
|
136
|
+
if (ask && !trigger.askDone && AskDialog.required(this.binding, ask, kwargs, '_ask')) {
|
|
137
|
+
return AskDialog.show(this.binding, ask, kwargs, node.getAttr('_ask_title') ?? 'Warning').then(result => {
|
|
138
|
+
if (this.binding.closed) return;
|
|
139
|
+
if (result === null) {
|
|
140
|
+
const cancel = node.getAttr('_ask_onCancel');
|
|
141
|
+
if (cancel) this.#inline('_ask_onCancel', cancel, cancel, kwargs, true);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
return this.invoke({...trigger, askDone: true, askResult: result});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
113
147
|
let [attr, body] = [formula ? 'formula' : 'script', node.getAttr(formula ? 'formula' : 'script')];
|
|
114
148
|
// Truthiness of `_if`, `_else` and the body, as legacy (`gnrdomsource.js:371-385, 473-482`).
|
|
115
149
|
const condition = node.getAttr('_if');
|
|
@@ -148,12 +182,13 @@ export class Provider {
|
|
|
148
182
|
readArguments(trigger) {
|
|
149
183
|
const node = this.node;
|
|
150
184
|
const [, attrs] = node.builder.runtimeValues(node);
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
185
|
+
const authored = Object.fromEntries(Object.entries(attrs).filter(([name]) =>
|
|
186
|
+
!CONTROL_ATTRIBUTES.has(name) && !name.startsWith('subscribe_') && !name.startsWith('selfsubscribe_')));
|
|
187
|
+
const kwargs = {...(trigger.kind === 'subscription' ? trigger.payload : {}),
|
|
188
|
+
...this.binding.runtime.renderer.references.resolveArguments(authored)};
|
|
189
|
+
if (trigger.kind === 'subscription') kwargs._subscription_kwargs = trigger.payload;
|
|
155
190
|
const change = trigger.change ?? null;
|
|
156
|
-
const reason = change ? change.level : trigger.kind;
|
|
191
|
+
const reason = change ? change.level : trigger.kind === 'subscription' ? trigger.topic : trigger.kind;
|
|
157
192
|
kwargs._node = change ? change.node : null;
|
|
158
193
|
kwargs._triggerpars = {kw: change, trigger_reason: reason};
|
|
159
194
|
kwargs._reason = reason;
|
|
@@ -330,7 +365,7 @@ export class RemoteProvider extends Provider {
|
|
|
330
365
|
}
|
|
331
366
|
|
|
332
367
|
// The trigger fields of the kwargs, in the legacy order of the inline parameters.
|
|
333
|
-
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason'];
|
|
368
|
+
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason', '_subscription_kwargs', '_askResult'];
|
|
334
369
|
// The fields a click adds (P9): locals of the inline bodies, never author arguments.
|
|
335
370
|
const CLICK_FIELDS = ['_evt', 'button_counter', 'button_shift', 'button_ctrl', 'button_alt', 'button_meta'];
|
|
336
371
|
|
package/src/binding/remote.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {toTytx} from '@genrojs/tytx';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* The `remote` attribute of a Source node (GC-095 §105): the content of the element is the fragment of a
|
|
3
5
|
* `@source` method, requested through `gramlot.src.remoteSource` when the node starts and again when a
|
|
@@ -5,13 +7,14 @@
|
|
|
5
7
|
* request is cancelled with it by `remoteSource` itself (P24), and the answer of a superseded request is
|
|
6
8
|
* dropped there (latest wins).
|
|
7
9
|
*
|
|
8
|
-
* The control attributes: `_cachedRemote` (no call when
|
|
10
|
+
* The control attributes: `_cachedRemote` (no call when name and parameters equal the last mount's), `_if`
|
|
9
11
|
* (inline, false → no call, and `_else` runs when present), `_onRemote` (inline, after the fragment is
|
|
10
12
|
* mounted). The inline code has the parameters as locals and `this` = the node.
|
|
11
13
|
*/
|
|
12
14
|
export class RemoteBinding {
|
|
13
15
|
#binding;
|
|
14
|
-
// The parameters of the last call,
|
|
16
|
+
// The name and TYTX parameters of the last mounted call, captured before sending it.
|
|
17
|
+
#lastName = null;
|
|
15
18
|
#lastParams = null;
|
|
16
19
|
|
|
17
20
|
constructor(binding) {
|
|
@@ -25,8 +28,9 @@ export class RemoteBinding {
|
|
|
25
28
|
/**
|
|
26
29
|
* Request the fragment for the current parameters. Nothing happens before the node's `start` step,
|
|
27
30
|
* on a closed node, without a name, or when `_if` or `_cachedRemote` stop the call; a false `_if`
|
|
28
|
-
* also cancels the request in flight, so its fragment is not mounted after `_else`. A
|
|
29
|
-
*
|
|
31
|
+
* also cancels the request in flight, so its fragment is not mounted after `_else`. A cache hit
|
|
32
|
+
* cancels a pending request too, keeping the mounted fragment. A failure of the request rejects
|
|
33
|
+
* with an error naming the node, its cause attached.
|
|
30
34
|
*/
|
|
31
35
|
async update() {
|
|
32
36
|
if (this.closed || !this.binding.hasStamp('start')) return;
|
|
@@ -45,15 +49,24 @@ export class RemoteBinding {
|
|
|
45
49
|
if (otherwise) this.#inline('_else', otherwise, otherwise, params);
|
|
46
50
|
return;
|
|
47
51
|
}
|
|
48
|
-
if (attrs._cachedRemote && this.#lastParams && sameValue(params, this.#lastParams)) return;
|
|
49
52
|
let mounted;
|
|
53
|
+
let text;
|
|
50
54
|
try {
|
|
55
|
+
text = toTytx(params);
|
|
56
|
+
if (attrs._cachedRemote && name === this.#lastName && text === this.#lastParams) {
|
|
57
|
+
// Keeping the mounted fragment also supersedes any different request still in flight.
|
|
58
|
+
this.binding.runtime.gramlot.rpc.remoteRequests.get(node)?.controller.abort();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
51
61
|
mounted = await this.binding.runtime.gramlot.src.remoteSource(node, name, params);
|
|
52
62
|
} catch (error) {
|
|
53
63
|
throw new Error(`${node.nodeTag} '${node.label}': remote '${name}' failed: ${error.message}`, {cause: error});
|
|
54
64
|
}
|
|
55
65
|
// Only a mounted fragment is the last call `_cachedRemote` compares: a failed request is retried.
|
|
56
|
-
if (mounted)
|
|
66
|
+
if (mounted) {
|
|
67
|
+
this.#lastName = name;
|
|
68
|
+
this.#lastParams = text;
|
|
69
|
+
}
|
|
57
70
|
const onRemote = node.getAttr('_onRemote');
|
|
58
71
|
if (mounted && onRemote) this.#inline('_onRemote', onRemote, onRemote, params);
|
|
59
72
|
}
|
|
@@ -62,17 +75,3 @@ export class RemoteBinding {
|
|
|
62
75
|
return this.binding.runtime.inlineCompiler.compile(this.node, attr, code, Object.keys(params), source)(params);
|
|
63
76
|
}
|
|
64
77
|
}
|
|
65
|
-
|
|
66
|
-
/** Deep equality of the parameter values: plain objects and arrays by content, dates by time. */
|
|
67
|
-
function sameValue(a, b) {
|
|
68
|
-
if (Object.is(a, b)) return true;
|
|
69
|
-
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
70
|
-
if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((item, i) => sameValue(item, b[i]));
|
|
71
|
-
if (!isPlain(a) || !isPlain(b)) return false;
|
|
72
|
-
const keys = Object.keys(a);
|
|
73
|
-
return keys.length === Object.keys(b).length && keys.every(key => Object.hasOwn(b, key) && sameValue(a[key], b[key]));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function isPlain(value) {
|
|
77
|
-
return value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype;
|
|
78
|
-
}
|
package/src/binding/runtime.js
CHANGED
|
@@ -30,7 +30,8 @@ export class BindingRuntime {
|
|
|
30
30
|
#router;
|
|
31
31
|
#installer;
|
|
32
32
|
#inlineCompiler;
|
|
33
|
-
#subscription = `gramlot-data-${crypto.
|
|
33
|
+
#subscription = `gramlot-data-${Array.from(crypto.getRandomValues(new Uint8Array(16)),
|
|
34
|
+
byte => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
34
35
|
#disposed = false;
|
|
35
36
|
|
|
36
37
|
constructor(gramlot) {
|
|
@@ -387,6 +388,11 @@ export class NodeBinding {
|
|
|
387
388
|
* A node whose `remote` is a string gets its RemoteBinding, created once.
|
|
388
389
|
*/
|
|
389
390
|
registerPointers() {
|
|
391
|
+
// Data elements have semantic lifetime but no rendered item to register their reference.
|
|
392
|
+
if (this.node._getMeta('data_element')) {
|
|
393
|
+
this.runtime.renderer.references.remove(this.node);
|
|
394
|
+
this.runtime.renderer.references.register(this.node, null);
|
|
395
|
+
}
|
|
390
396
|
const node = this.node;
|
|
391
397
|
const dataElement = node._getMeta('data_element');
|
|
392
398
|
if (dataElement && !PROVIDERS[node.nodeTag]) return;
|
|
@@ -402,11 +408,40 @@ export class NodeBinding {
|
|
|
402
408
|
if (name === 'datapath') this.#datapathRegistration = registration;
|
|
403
409
|
if (this.remote && (name === 'remote' || name.startsWith('remote_'))) this.#remoteRegistrations.add(registration);
|
|
404
410
|
}
|
|
405
|
-
if (!dataElement)
|
|
411
|
+
if (!dataElement) {
|
|
412
|
+
this.#registerTopics();
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
406
415
|
this.#provider ??= new PROVIDERS[node.nodeTag](this);
|
|
407
416
|
this.#provider.register();
|
|
408
417
|
}
|
|
409
418
|
|
|
419
|
+
/** Rendered node topic handlers share semantic lifetime, not DOM lifetime. */
|
|
420
|
+
#registerTopics() {
|
|
421
|
+
const node = this.node;
|
|
422
|
+
const subscriptions = this.runtime.gramlot._subscriptions;
|
|
423
|
+
for (const [name, handler] of Object.entries(node.getAttr())) {
|
|
424
|
+
const prefix = name.startsWith('selfsubscribe_') ? 'selfsubscribe_' : name.startsWith('subscribe_') ? 'subscribe_' : null;
|
|
425
|
+
if (!prefix) continue;
|
|
426
|
+
if (typeof handler !== 'string' || !handler) throw new TypeError(`${name} requires a handler`);
|
|
427
|
+
const topic = prefix === 'selfsubscribe_'
|
|
428
|
+
? subscriptions.topicFor(node, name.slice(prefix.length)) : name.slice(prefix.length);
|
|
429
|
+
const handle = subscriptions.subscribe(topic, payload => {
|
|
430
|
+
if (this.closed) return;
|
|
431
|
+
if (/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/.test(handler)) {
|
|
432
|
+
const {group, method} = this.runtime.logicRegistry.resolve(handler, node);
|
|
433
|
+
return method.call(group, node, payload);
|
|
434
|
+
}
|
|
435
|
+
const [, attrs] = node.builder.runtimeValues(node);
|
|
436
|
+
const authored = Object.fromEntries(Object.entries(attrs).filter(([key]) =>
|
|
437
|
+
!key.startsWith('subscribe_') && !key.startsWith('selfsubscribe_')));
|
|
438
|
+
const values = {...this.runtime.renderer.references.resolveArguments(authored), p_0: payload};
|
|
439
|
+
return this.runtime.inlineCompiler.compile(node, name, handler, Object.keys(values))(values);
|
|
440
|
+
});
|
|
441
|
+
this.#registrations.push({close: () => subscriptions.unsubscribe(handle)});
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
410
445
|
/** Close the registrations and make them again from the current pointers, the provider's included (D1). */
|
|
411
446
|
rebind() {
|
|
412
447
|
this.#closeRegistrations();
|
|
@@ -440,6 +475,7 @@ export class NodeBinding {
|
|
|
440
475
|
close() {
|
|
441
476
|
if (this.closed) return;
|
|
442
477
|
this.#closed = true;
|
|
478
|
+
if (this.node._getMeta('data_element')) this.runtime.renderer.references.remove(this.node);
|
|
443
479
|
this.#closeRegistrations();
|
|
444
480
|
const errors = [];
|
|
445
481
|
for (const disposer of this.#disposers.splice(0).reverse()) {
|
|
@@ -13,13 +13,15 @@ import {GramlotBuilderBag} from './source.js';
|
|
|
13
13
|
import {GramlotHtmlRenderer} from '../renderer/gramlot-html-renderer.js';
|
|
14
14
|
// Byte copy of src/gramlot/collections/binding.json: the JS package ships js/ only.
|
|
15
15
|
import BINDING from './binding.json' with {type: 'json'};
|
|
16
|
+
import WEBCOMPONENT from './webcomponent.json' with {type: 'json'};
|
|
17
|
+
import {webcomponentCode} from '../webcomponent.js';
|
|
16
18
|
|
|
17
19
|
// P19: `data` is ambiguous between the legacy data-element and the HTML5 element.
|
|
18
20
|
const DATA_FORBIDDEN = 'data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element';
|
|
19
21
|
// Declarations outside Gramlot 0.2.0 that fail explicitly (source plan, section 2).
|
|
20
22
|
const EXCLUDED_ELEMENTS = new Set();
|
|
21
|
-
const EXCLUDED_ATTRIBUTES = new Set(['serverpath', 'dbenv', 'shared_id'
|
|
22
|
-
const EXCLUDED_ATTRIBUTE_PREFIXES = ['
|
|
23
|
+
const EXCLUDED_ATTRIBUTES = new Set(['serverpath', 'dbenv', 'shared_id']);
|
|
24
|
+
const EXCLUDED_ATTRIBUTE_PREFIXES = ['formsubscribe_'];
|
|
23
25
|
|
|
24
26
|
// P18 and B8: the Data path of each data-element, which does not accept `?attr`,
|
|
25
27
|
// and its inline attribute, which excludes `func` on the same node, as `_if` does (Q11.1).
|
|
@@ -29,6 +31,11 @@ const DATA_INLINE_ATTRIBUTES = {dataFormula: 'formula', dataController: 'script'
|
|
|
29
31
|
|
|
30
32
|
/** The P18 violation of a data-element's attributes, or null. */
|
|
31
33
|
function dataElementError(tag, attrs) {
|
|
34
|
+
if (Object.hasOwn(attrs, 'ask') && tag !== 'button') return "'ask' is supported on buttons";
|
|
35
|
+
if (Object.hasOwn(attrs, '_ask') && !['dataController', 'dataFormula', 'dataRpc'].includes(tag)) return "'_ask' requires a controller, formula or RPC";
|
|
36
|
+
if (['dataSetter', 'dataRemote'].includes(tag) && Object.keys(attrs).some(name => name.startsWith('subscribe_') || name.startsWith('selfsubscribe_'))) {
|
|
37
|
+
return 'topic subscriptions are supported on controllers, formulas, RPCs and rendered elements';
|
|
38
|
+
}
|
|
32
39
|
const pathName = DATA_PATH_ATTRIBUTES[tag];
|
|
33
40
|
if (pathName && String(attrs[pathName] ?? '').includes('?')) {
|
|
34
41
|
return `'${pathName}' does not accept '?attr': '${attrs[pathName]}'`;
|
|
@@ -55,6 +62,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
55
62
|
static _sourceClass = GramlotBuilderBag;
|
|
56
63
|
// Composed on the inherited HtmlBuilder grammar; its data-elements replace Builder's.
|
|
57
64
|
static { this.defineGrammar(BINDING); }
|
|
65
|
+
static { this.defineGrammar(WEBCOMPONENT); }
|
|
58
66
|
|
|
59
67
|
/** Tag prefixes of the parent dialects, from the `_name` of the classes above GramlotBuilder. */
|
|
60
68
|
static get dialectPrefixes() {
|
|
@@ -128,6 +136,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
128
136
|
|
|
129
137
|
/** Builder's value check, plus the excluded attributes (authoring names the tag). */
|
|
130
138
|
validateNodeValues(tag, value, attrs = {}) {
|
|
139
|
+
if (tag === 'webcomponent') webcomponentCode(attrs);
|
|
131
140
|
const excluded = excludedAttribute(attrs);
|
|
132
141
|
if (excluded) throw new Error(`${tag}: attribute '${excluded}' is excluded from Gramlot 0.2.0`);
|
|
133
142
|
const error = dataElementError(tag, attrs);
|
|
@@ -137,6 +146,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
137
146
|
|
|
138
147
|
/** Builder's node check, plus the excluded attributes (a Source node names tag and label). */
|
|
139
148
|
validateNode(node, options = {}) {
|
|
149
|
+
if (node.nodeTag === 'webcomponent') webcomponentCode(options.attrs ?? node.getAttr());
|
|
140
150
|
const excluded = excludedAttribute(options.attrs ?? node.getAttr());
|
|
141
151
|
if (excluded) throw new Error(`${node.nodeTag} '${node.label}': attribute '${excluded}' is excluded from Gramlot 0.2.0`);
|
|
142
152
|
const error = dataElementError(node.nodeTag, options.attrs ?? node.getAttr());
|
|
@@ -144,8 +154,15 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
144
154
|
return super.validateNode(node, options);
|
|
145
155
|
}
|
|
146
156
|
|
|
157
|
+
/** Custom elements are outside the native HTML child catalogue, as components are. */
|
|
158
|
+
_countsForGrammar(info) {
|
|
159
|
+
return info !== this.schema.webcomponent && super._countsForGrammar(info);
|
|
160
|
+
}
|
|
161
|
+
|
|
147
162
|
/** Builder's child insertion; a plain object `value` of `dataSetter` becomes `new Bag(value)` (P25). */
|
|
148
163
|
setChild(bag, tag, value, attrs, parentOverride = null) {
|
|
164
|
+
attrs = Object.fromEntries(Object.entries(attrs ?? {}).map(([key, value]) =>
|
|
165
|
+
[key, sourceTarget(value) instanceof SourceBagNode ? this.reference(value) : value]));
|
|
149
166
|
const plain = tag === 'dataSetter' && attrs?.value !== null && typeof attrs?.value === 'object'
|
|
150
167
|
&& Object.getPrototypeOf(attrs.value) === Object.prototype;
|
|
151
168
|
const actual = plain ? {...attrs, value: new Bag(attrs.value)} : attrs;
|
|
@@ -183,7 +200,8 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
183
200
|
}
|
|
184
201
|
let id = node.getAttr('__ref');
|
|
185
202
|
if (id === null || id === undefined) {
|
|
186
|
-
id =
|
|
203
|
+
id = Array.from(crypto.getRandomValues(new Uint8Array(16)),
|
|
204
|
+
byte => byte.toString(16).padStart(2, '0')).join('');
|
|
187
205
|
node.setAttr({__ref: id});
|
|
188
206
|
}
|
|
189
207
|
return {$gramlotRef: id, kind};
|
package/src/builder/source.js
CHANGED
|
@@ -3,11 +3,11 @@ import {getSubtypeDict, setSubtypeDict} from '@genrojs/tytx';
|
|
|
3
3
|
|
|
4
4
|
// The inline code attributes of each tag, besides `connect_on<event>` and the `==` expressions of any tag.
|
|
5
5
|
const CODE_ATTRIBUTES = {
|
|
6
|
-
dataFormula: ['formula', '_if', '_else'],
|
|
7
|
-
dataController: ['script', '_if', '_else'],
|
|
8
|
-
dataRpc: ['_if', '_else', '_onCalling', '_onResult', '_onError'],
|
|
6
|
+
dataFormula: ['formula', '_if', '_else', '_ask_if', '_ask_onCancel'],
|
|
7
|
+
dataController: ['script', '_if', '_else', '_ask_if', '_ask_onCancel'],
|
|
8
|
+
dataRpc: ['_ask_if', '_ask_onCancel', '_if', '_else', '_onCalling', '_onResult', '_onError'],
|
|
9
9
|
dataRemote: ['_if', '_else'],
|
|
10
|
-
button: ['action'],
|
|
10
|
+
button: ['action', '_ask_if'],
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
// The control attributes of `remote`, written on the node as they are; every other parameter becomes `remote_<k>`.
|
|
@@ -46,7 +46,15 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
46
46
|
...(typeof this.getAttr('remote') === 'string' ? REMOTE_CODE_ATTRIBUTES : [])];
|
|
47
47
|
const items = [];
|
|
48
48
|
for (const [name, value] of Object.entries(this.getAttr() ?? {})) {
|
|
49
|
-
if (typeof value === 'string' && (own.includes(name) || name.startsWith('connect_on'))) items.push([name, value]);
|
|
49
|
+
if (typeof value === 'string' && (own.includes(name) || name.startsWith('connect_on') || name.startsWith('subscribe_') || name.startsWith('selfsubscribe_'))) items.push([name, value]);
|
|
50
|
+
}
|
|
51
|
+
for (const attribute of ['ask', '_ask']) {
|
|
52
|
+
const spec = this.getAttr(attribute);
|
|
53
|
+
if (spec && typeof spec === 'object') {
|
|
54
|
+
for (const key of ['_if', 'askIf']) {
|
|
55
|
+
if (typeof spec[key] === 'string') items.push([`${attribute}.${key}`, spec[key]]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
50
58
|
}
|
|
51
59
|
for (const [name, value] of sourceAttributeItems(this.getAttr())) {
|
|
52
60
|
if (isExpression(value)) items.push([name, value]);
|
|
@@ -101,6 +109,11 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
101
109
|
return wrapSource(this);
|
|
102
110
|
}
|
|
103
111
|
|
|
112
|
+
/** Publish to this node's selfsubscribe topics, as in the legacy Source API. */
|
|
113
|
+
publish(topic, payload = {}) {
|
|
114
|
+
return this.builder.binding.gramlot._subscriptions.publishNode(this, topic, payload);
|
|
115
|
+
}
|
|
116
|
+
|
|
104
117
|
/** Builder's pointerType, except that a string starting with `==` is not a pointer. */
|
|
105
118
|
pointerType(v) {
|
|
106
119
|
if (typeof v === 'string' && v.startsWith('==')) return null;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"document_format": {
|
|
3
|
+
"name": "builder_grammar",
|
|
4
|
+
"version": "1.1"
|
|
5
|
+
},
|
|
6
|
+
"grammar": {
|
|
7
|
+
"name": "webcomponent",
|
|
8
|
+
"version": null,
|
|
9
|
+
"title": "Gramlot webcomponent",
|
|
10
|
+
"description": "An explicitly loaded autonomous custom element."
|
|
11
|
+
},
|
|
12
|
+
"abstracts": {},
|
|
13
|
+
"elements": {
|
|
14
|
+
"webcomponent": {
|
|
15
|
+
"doc": "Create the custom-element tag named by literal code.",
|
|
16
|
+
"sub_tags": "*",
|
|
17
|
+
"parent_tags": null,
|
|
18
|
+
"inherits_from": null,
|
|
19
|
+
"ns": null,
|
|
20
|
+
"attributes": {
|
|
21
|
+
"parameters": [
|
|
22
|
+
{
|
|
23
|
+
"name": "code",
|
|
24
|
+
"kind": "keyword_only",
|
|
25
|
+
"role": "attribute",
|
|
26
|
+
"annotation": {
|
|
27
|
+
"kind": "type",
|
|
28
|
+
"module": "builtins",
|
|
29
|
+
"name": "str"
|
|
30
|
+
},
|
|
31
|
+
"has_default": false
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "attr",
|
|
35
|
+
"kind": "var_keyword",
|
|
36
|
+
"role": "attribute",
|
|
37
|
+
"annotation": {
|
|
38
|
+
"kind": "any"
|
|
39
|
+
},
|
|
40
|
+
"has_default": false
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"accepts_var_keyword": true,
|
|
44
|
+
"accepts_var_positional": false
|
|
45
|
+
},
|
|
46
|
+
"node_label": null,
|
|
47
|
+
"collection_key": null,
|
|
48
|
+
"_meta": null
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|