@gramlot/gramlot 0.2.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/LICENSE +176 -0
- package/NOTICE +8 -0
- package/dist/gramlot.js +25616 -0
- package/dist/runtime-notices.json +34 -0
- package/package.json +47 -0
- package/src/adapters/file-host.js +93 -0
- package/src/adapters/host.js +128 -0
- package/src/adapters/index.js +6 -0
- package/src/adapters/page.js +32 -0
- package/src/adapters/resources.js +73 -0
- package/src/binding/inline.js +171 -0
- package/src/binding/installation.js +207 -0
- package/src/binding/logic.js +234 -0
- package/src/binding/providers.js +230 -0
- package/src/binding/router.js +190 -0
- package/src/binding/runtime.js +432 -0
- package/src/bootstrap.js +72 -0
- package/src/builder/binding.json +216 -0
- package/src/builder/gramlot-builder.js +181 -0
- package/src/builder/source.js +227 -0
- package/src/gramlot.js +151 -0
- package/src/index.js +9 -0
- package/src/references.js +43 -0
- package/src/renderer/attributes.js +120 -0
- package/src/renderer/gramlot-html-renderer.js +125 -0
- package/src/renderer/gramlot-renderer.js +618 -0
- package/src/renderer/gramlot-svg-renderer.js +53 -0
- package/src/transport.js +42 -0
- package/src/view/button.js +137 -0
- package/src/view/controls.js +469 -0
- package/src/view/events.js +64 -0
- package/src/view/html.js +154 -0
package/src/transport.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Main and remote Source transport; application pages do not issue fetch calls. */
|
|
2
|
+
export class MainTransport {
|
|
3
|
+
constructor(url, fetcher = globalThis.fetch?.bind(globalThis), sourceUrl = '/gramlot/source',
|
|
4
|
+
closeUrl = '/gramlot/close', navigator = globalThis.navigator) {
|
|
5
|
+
this.url = url;
|
|
6
|
+
this.fetcher = fetcher;
|
|
7
|
+
this.sourceUrl = sourceUrl;
|
|
8
|
+
this.closeUrl = closeUrl;
|
|
9
|
+
this.navigator = navigator;
|
|
10
|
+
}
|
|
11
|
+
async main(pageId, signal) {
|
|
12
|
+
return this.request('main', this.url, {pageId}, signal);
|
|
13
|
+
}
|
|
14
|
+
async source(pageId, method, params, signal) {
|
|
15
|
+
return this.request('source', this.sourceUrl, {pageId, method, params}, signal);
|
|
16
|
+
}
|
|
17
|
+
async request(operation, url, payload, signal) {
|
|
18
|
+
const response = await this.fetcher(url, {
|
|
19
|
+
method: 'POST', credentials: 'same-origin', signal,
|
|
20
|
+
headers: {'Content-Type': 'application/json'},
|
|
21
|
+
body: JSON.stringify(payload),
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok) throw new Error(`${operation} failed: HTTP ${response.status}`);
|
|
24
|
+
return response.text();
|
|
25
|
+
}
|
|
26
|
+
close(pageId, {beacon = false} = {}) {
|
|
27
|
+
const body = JSON.stringify({pageId});
|
|
28
|
+
if (beacon) {
|
|
29
|
+
try {
|
|
30
|
+
this.navigator.sendBeacon(this.closeUrl, new Blob([body], {type: 'application/json'}));
|
|
31
|
+
} catch { /* TTL handles delivery failure. */ }
|
|
32
|
+
} else {
|
|
33
|
+
// Server cleanup is best effort; local disposal must complete offline.
|
|
34
|
+
try {
|
|
35
|
+
Promise.resolve(this.fetcher(this.closeUrl, {
|
|
36
|
+
method: 'POST', credentials: 'same-origin', keepalive: true,
|
|
37
|
+
headers: {'Content-Type': 'application/json'}, body,
|
|
38
|
+
})).catch(() => {});
|
|
39
|
+
} catch { /* TTL handles delivery failure. */ }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The click of a `<button>` (source plan §2 Button, §4.11, S12). One mechanism per button (P10): the
|
|
3
|
+
* `dataController` nested in it, `action`, or the `fire`/`fire_*` family. The nested controller is a
|
|
4
|
+
* normal ControllerProvider (B7); the click is one more trigger. Controller, `action` and counter use
|
|
5
|
+
* the button's own node, also when the click hits a descendant (R11).
|
|
6
|
+
*
|
|
7
|
+
* R3, provisional: only a button with a mechanism gets `type="button"` when the author wrote no
|
|
8
|
+
* `type`, and its click stops propagating; no `preventDefault`. A button without a mechanism stays
|
|
9
|
+
* native. A closed NodeBinding does nothing (R12).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {XHTML_NS} from '../renderer/attributes.js';
|
|
13
|
+
|
|
14
|
+
export class ButtonBinding {
|
|
15
|
+
#binding;
|
|
16
|
+
#record;
|
|
17
|
+
|
|
18
|
+
/** The ButtonBinding of a built element, or null when the element is not a `<button>`. */
|
|
19
|
+
static for(binding, record) {
|
|
20
|
+
const element = record.element;
|
|
21
|
+
if (element?.namespaceURI !== XHTML_NS || element.localName !== 'button') return null;
|
|
22
|
+
return new ButtonBinding(binding, record);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
constructor(binding, record) {
|
|
26
|
+
this.#binding = binding;
|
|
27
|
+
this.#record = record;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get binding() { return this.#binding; }
|
|
31
|
+
get node() { return this.binding.node; }
|
|
32
|
+
get element() { return this.#record.element; }
|
|
33
|
+
|
|
34
|
+
/** The `dataController` children of the button, in Source order. */
|
|
35
|
+
get controllers() {
|
|
36
|
+
const value = this.node.value;
|
|
37
|
+
if (!value?.getNodes) return [];
|
|
38
|
+
return value.getNodes().filter(child => child.nodeTag === 'dataController');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController`
|
|
43
|
+
* children, or two mechanisms together, are an error naming the button (P10).
|
|
44
|
+
*/
|
|
45
|
+
get mechanism() {
|
|
46
|
+
const node = this.node;
|
|
47
|
+
const controllers = this.controllers;
|
|
48
|
+
if (controllers.length > 1) {
|
|
49
|
+
throw new Error(`${node.nodeTag} '${node.label}': several dataController children; a button has one click mechanism`);
|
|
50
|
+
}
|
|
51
|
+
const attrs = node.getAttr() ?? {};
|
|
52
|
+
const found = [];
|
|
53
|
+
if (controllers.length) found.push('controller');
|
|
54
|
+
if (attrs.action != null) found.push('action');
|
|
55
|
+
if (fireAttributes(attrs).length) found.push('fire');
|
|
56
|
+
if (found.length > 1) {
|
|
57
|
+
throw new Error(`${node.nodeTag} '${node.label}': ${found.join(' and ')} together; a button has one click mechanism`);
|
|
58
|
+
}
|
|
59
|
+
return found[0] ?? null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Check the mechanism, apply R3's `type`, then listen to `click`. */
|
|
63
|
+
attach() {
|
|
64
|
+
this.refresh();
|
|
65
|
+
this.element.addEventListener('click', this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The Source of the button changed (its attributes, a child inserted): the mechanism is checked
|
|
70
|
+
* again, so an ambiguity raises now, and a button that gained a mechanism gets `type="button"`.
|
|
71
|
+
*/
|
|
72
|
+
refresh() {
|
|
73
|
+
const mechanism = this.mechanism;
|
|
74
|
+
if (mechanism && this.node.getAttr('type') == null && this.element.getAttribute('type') !== 'button') {
|
|
75
|
+
this.element.setAttribute('type', 'button');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
detach() {
|
|
80
|
+
this.element.removeEventListener('click', this);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
handleEvent(event) {
|
|
84
|
+
this.onClick(event);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* One click: nothing on a closed node (R12) or without a mechanism; `stopPropagation` (R3); a
|
|
89
|
+
* disabled button counts nothing and runs nothing (legacy early return); then the counter and
|
|
90
|
+
* the mechanism. An error propagates from the listener.
|
|
91
|
+
*/
|
|
92
|
+
onClick(event) {
|
|
93
|
+
if (this.binding.closed) return;
|
|
94
|
+
const mechanism = this.mechanism;
|
|
95
|
+
if (!mechanism) return;
|
|
96
|
+
event.stopPropagation();
|
|
97
|
+
if (this.element.disabled) return;
|
|
98
|
+
const counter = this.binding.countClick();
|
|
99
|
+
const node = this.node;
|
|
100
|
+
const modifiers = eventToString(event);
|
|
101
|
+
if (mechanism === 'controller') {
|
|
102
|
+
const provider = this.binding.runtime.bindingFor(this.controllers[0])?.providers[0];
|
|
103
|
+
provider?.invoke({kind: 'click', event, counter});
|
|
104
|
+
} else if (mechanism === 'action') {
|
|
105
|
+
const [, attrs] = node.builder.runtimeValues(node);
|
|
106
|
+
const values = {...attrs, event, _counter: counter, modifiers};
|
|
107
|
+
this.binding.runtime.inlineCompiler.compile(node, 'action', node.getAttr('action'), Object.keys(values))(values);
|
|
108
|
+
} else {
|
|
109
|
+
const attributes = {modifier: modifiers, _counter: counter};
|
|
110
|
+
// `fire` wins over the `fire_*` of the same button, as the legacy chain.
|
|
111
|
+
const fire = node.getAttr('fire');
|
|
112
|
+
if (fire != null) {
|
|
113
|
+
node.FIRE(fire, modifiers || true, attributes);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// A recipient that removes the button stops the remaining `fire_*` (R12).
|
|
117
|
+
for (const [name, path] of fireAttributes(node.getAttr())) {
|
|
118
|
+
if (this.binding.closed) return;
|
|
119
|
+
node.FIRE(path, name.slice('fire_'.length), attributes);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The `fire` and `fire_*` attributes of `attrs` with a value, in attribute order. */
|
|
126
|
+
function fireAttributes(attrs) {
|
|
127
|
+
return Object.entries(attrs ?? {}).filter(([name, value]) => value != null
|
|
128
|
+
&& (name === 'fire' || name.startsWith('fire_')));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The modifier keys of `event`, as legacy `eventToString` (`gnrlang.js`): 'Shift', 'CtrlAlt', …, or ''. */
|
|
132
|
+
function eventToString(event) {
|
|
133
|
+
return ['shift', 'ctrl', 'alt', 'meta']
|
|
134
|
+
.filter(key => event[`${key}Key`])
|
|
135
|
+
.map(key => key[0].toUpperCase() + key.slice(1))
|
|
136
|
+
.join('');
|
|
137
|
+
}
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native editing (source plan §4.11, S10, S11): one ControlAdapter per built form control consumes the
|
|
3
|
+
* `value` attribute. Data → DOM goes through `project`, DOM → Data through `commit`; HtmlElement
|
|
4
|
+
* writes neither the `value` property nor the `value` attribute of an element that has an adapter.
|
|
5
|
+
* Checkbox and radio consume `value` as a boolean; RadioGroups gives the radios of a group their name.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {XHTML_NS} from '../renderer/attributes.js';
|
|
9
|
+
|
|
10
|
+
// The elements whose `type`, `multiple` and `group` have no setter: a change rebuilds (C04.1, D7).
|
|
11
|
+
const CONTROL_TAGS = new Set(['input', 'select', 'textarea']);
|
|
12
|
+
const REBUILD_ATTRIBUTES = ['type', 'multiple', 'group'];
|
|
13
|
+
|
|
14
|
+
/** The resolved value of the attribute `name` of `node`: a pointer reads the Data. */
|
|
15
|
+
function resolved(node, name) {
|
|
16
|
+
const value = node.getAttr(name);
|
|
17
|
+
return node.pointerType(value) ? node.getRelativeData(value) : value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class ControlAdapter {
|
|
21
|
+
// Whether `live` moves the write from `change` to `input` (gate decision 3).
|
|
22
|
+
static liveWrites = false;
|
|
23
|
+
// Whether a literal `value` is also written as the default a native form Reset restores.
|
|
24
|
+
static writesDefault = true;
|
|
25
|
+
#binding;
|
|
26
|
+
#record;
|
|
27
|
+
#committing = false;
|
|
28
|
+
#composing = false;
|
|
29
|
+
#pending = false;
|
|
30
|
+
|
|
31
|
+
/** The ControlAdapter of a built element, or null when the element is not a form control. */
|
|
32
|
+
static for(binding, record) {
|
|
33
|
+
const Control = ControlAdapter.classFor(record.element, record.attrs);
|
|
34
|
+
return Control ? new Control(binding, record) : null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The ControlAdapter subclass of `element` with the applied `attrs`, or null. `hidden` and `file`
|
|
39
|
+
* have no adapter, the button types belong to S12; a type the browser does not know is its text
|
|
40
|
+
* state.
|
|
41
|
+
*/
|
|
42
|
+
static classFor(element, attrs) {
|
|
43
|
+
if (element.namespaceURI !== XHTML_NS) return null;
|
|
44
|
+
if (element.localName === 'textarea') return TextControl;
|
|
45
|
+
if (element.localName === 'select') return SelectControl;
|
|
46
|
+
if (element.localName !== 'input') return null;
|
|
47
|
+
const type = attrs.type == null ? 'text' : String(attrs.type).toLowerCase();
|
|
48
|
+
return Object.hasOwn(INPUT_CONTROLS, type) ? INPUT_CONTROLS[type] : TextControl;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The resolved `type`, `multiple` and `group` of a form control element, as one comparable
|
|
53
|
+
* string, or null for any other element: a different shape rebuilds the element (C04.1, D7).
|
|
54
|
+
* `attrs` are the attributes of the renderer's resolved projection (`^`, `=` and `==` resolved).
|
|
55
|
+
*/
|
|
56
|
+
static shape(element, attrs) {
|
|
57
|
+
if (element?.namespaceURI !== XHTML_NS || !CONTROL_TAGS.has(element.localName)) return null;
|
|
58
|
+
return JSON.stringify(REBUILD_ATTRIBUTES.map(name => attrs[name] ?? null));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
constructor(binding, record) {
|
|
62
|
+
this.#binding = binding;
|
|
63
|
+
this.#record = record;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get binding() { return this.#binding; }
|
|
67
|
+
get element() { return this.#record.element; }
|
|
68
|
+
get node() { return this.binding.node; }
|
|
69
|
+
/** The attributes of the last resolved projection of the node (`^`, `=` and `==` resolved). */
|
|
70
|
+
get resolved() { return this.#record.resolved; }
|
|
71
|
+
/**
|
|
72
|
+
* The `live` of the last resolved projection, JS truthiness: `^` and `==` follow the Data, `=` is read
|
|
73
|
+
* at every projection of the node (GC-210 §090); a change needs no rebuild.
|
|
74
|
+
*/
|
|
75
|
+
get live() { return Boolean(this.resolved.live); }
|
|
76
|
+
/** The DOM event that writes the Data: `input` with `live` on the controls that accept it, else `change`. */
|
|
77
|
+
get writeEvent() { return this.constructor.liveWrites && this.live ? 'input' : 'change'; }
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Data → DOM. During an IME composition the projection waits for its end. P1: while this adapter
|
|
81
|
+
* is writing the Data, a value equal to what the DOM already holds (typed comparison) is not
|
|
82
|
+
* written back; any other value is, so a correction made by a controller reaches the control.
|
|
83
|
+
* A literal value (neither `^` nor `=`) is first written as the default, so a native form Reset
|
|
84
|
+
* restores it; a pointer value is written as the property only (P1).
|
|
85
|
+
*/
|
|
86
|
+
project(value, change) {
|
|
87
|
+
if (this.#composing) {
|
|
88
|
+
this.#pending = true;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (this.#committing && this.same(this.read(), value)) return;
|
|
92
|
+
if (this.constructor.writesDefault && !this.node.pointerType(this.node.getAttr('value'))) {
|
|
93
|
+
this.writeDefault(value);
|
|
94
|
+
}
|
|
95
|
+
this.write(value);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** DOM → typed value: the text of the control. */
|
|
99
|
+
read() {
|
|
100
|
+
return this.element.value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Show `value`: null is empty. An equal value is not written again, so the caret stays. */
|
|
104
|
+
write(value) {
|
|
105
|
+
const target = value == null ? '' : String(value);
|
|
106
|
+
if (this.element.value !== target) this.element.value = target;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Write `value` as the default: the `value` attribute of an input, the text of a textarea (its
|
|
111
|
+
* text node, which the renderer keeps). null is empty; an equal default is not written again.
|
|
112
|
+
*/
|
|
113
|
+
writeDefault(value) {
|
|
114
|
+
const target = value == null ? '' : String(value);
|
|
115
|
+
if (this.element.localName === 'textarea') {
|
|
116
|
+
if (this.#record.text.data !== target) this.#record.text.data = target;
|
|
117
|
+
} else if (this.element.defaultValue !== target) {
|
|
118
|
+
this.element.defaultValue = target;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Whether the value read from the DOM and the value to project are the same typed value. */
|
|
123
|
+
same(current, value) {
|
|
124
|
+
return Object.is(current, value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Write `read()` at the `value` pointer (`^`, `?attr` included). A literal or `=` value accepts
|
|
129
|
+
* input and writes nothing (gate decision 5); a null path raises from the listener (Q7). A closed
|
|
130
|
+
* NodeBinding writes nothing (R12).
|
|
131
|
+
*/
|
|
132
|
+
commit(event) {
|
|
133
|
+
if (this.binding.closed) return;
|
|
134
|
+
const pointer = this.node.getAttr('value');
|
|
135
|
+
if (this.node.pointerType(pointer) !== '^') return;
|
|
136
|
+
this.#committing = true;
|
|
137
|
+
try {
|
|
138
|
+
this.node.setRelativeData(pointer.slice(1), this.read());
|
|
139
|
+
} finally {
|
|
140
|
+
this.#committing = false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
attach() {
|
|
145
|
+
for (const type of ['input', 'change', 'compositionstart', 'compositionend']) {
|
|
146
|
+
this.element.addEventListener(type, this);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
detach() {
|
|
151
|
+
for (const type of ['input', 'change', 'compositionstart', 'compositionend']) {
|
|
152
|
+
this.element.removeEventListener(type, this);
|
|
153
|
+
}
|
|
154
|
+
this.#pending = false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Called after every projection of the node; a control whose membership depends on its context follows it. */
|
|
158
|
+
refresh() {}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* DOM listener: no write during a composition; at its end the write per `live`, then the waiting
|
|
162
|
+
* projection. The control of a closed NodeBinding (a node removed under freeze) does nothing (R12).
|
|
163
|
+
*/
|
|
164
|
+
handleEvent(event) {
|
|
165
|
+
if (this.binding.closed) return;
|
|
166
|
+
if (event.type === 'compositionstart') {
|
|
167
|
+
this.#composing = true;
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (event.type === 'compositionend') {
|
|
171
|
+
this.#composing = false;
|
|
172
|
+
if (this.writeEvent === 'input') this.commit(event);
|
|
173
|
+
if (this.#pending) {
|
|
174
|
+
this.#pending = false;
|
|
175
|
+
this.binding.runtime.renderer.project(this.node);
|
|
176
|
+
}
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (this.#composing || event.isComposing) return;
|
|
180
|
+
if (event.type === this.writeEvent) this.commit(event);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** text, password, email, url, tel, search, textarea: a string; the empty string stays. */
|
|
185
|
+
export class TextControl extends ControlAdapter {
|
|
186
|
+
static liveWrites = true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** number: a finite number or null; empty → null; an invalid draft stays in the field (gate decision 2). */
|
|
190
|
+
export class NumberControl extends ControlAdapter {
|
|
191
|
+
static liveWrites = true;
|
|
192
|
+
|
|
193
|
+
read() {
|
|
194
|
+
return this.element.value === '' ? null : Number(this.element.value);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
commit(event) {
|
|
198
|
+
if (this.element.validity.badInput) return;
|
|
199
|
+
super.commit(event);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** range: a number; the browser may limit the shown value, the projection does not write the Data. */
|
|
204
|
+
export class RangeControl extends ControlAdapter {
|
|
205
|
+
static liveWrites = true;
|
|
206
|
+
|
|
207
|
+
read() {
|
|
208
|
+
return Number(this.element.value);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* select: a string or null; multiple: an array of strings, also []. A value without option selects
|
|
214
|
+
* nothing. The default of a select is the `selected` attribute of its option nodes, not written here.
|
|
215
|
+
*/
|
|
216
|
+
export class SelectControl extends ControlAdapter {
|
|
217
|
+
static writesDefault = false;
|
|
218
|
+
|
|
219
|
+
read() {
|
|
220
|
+
if (this.element.multiple) return [...this.element.selectedOptions].map(option => option.value);
|
|
221
|
+
return this.element.selectedIndex === -1 ? null : this.element.value;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
write(value) {
|
|
225
|
+
const element = this.element;
|
|
226
|
+
if (element.multiple) {
|
|
227
|
+
if (value != null && !Array.isArray(value)) {
|
|
228
|
+
throw new TypeError(`${this.node.nodeTag} '${this.node.label}': a multiple select requires an array`);
|
|
229
|
+
}
|
|
230
|
+
const selected = new Set((value ?? []).map(String));
|
|
231
|
+
for (const option of element.options) {
|
|
232
|
+
const on = selected.has(option.value);
|
|
233
|
+
if (option.selected !== on) option.selected = on;
|
|
234
|
+
}
|
|
235
|
+
} else if (value == null) {
|
|
236
|
+
if (element.selectedIndex !== -1) element.selectedIndex = -1;
|
|
237
|
+
} else if (element.selectedIndex === -1 || element.value !== String(value)) {
|
|
238
|
+
element.value = String(value);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
same(current, value) {
|
|
243
|
+
if (!Array.isArray(current)) return super.same(current, value);
|
|
244
|
+
return Array.isArray(value) && value.length === current.length
|
|
245
|
+
&& value.every((item, index) => Object.is(item, current[index]));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** date, time, month, week, datetime-local: the lexical string or null; no Date, no time zone. */
|
|
250
|
+
export class TemporalControl extends ControlAdapter {
|
|
251
|
+
read() {
|
|
252
|
+
return this.element.value === '' ? null : this.element.value;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** color: the serialized string; the browser's normalization does not write the Data. */
|
|
257
|
+
export class ColorControl extends ControlAdapter {}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* checkbox: `value` is the boolean of `checked` (D4). A non-boolean Data value is shown with JS
|
|
261
|
+
* truthiness, null is off, only `true`/`false` are written, on `change`. A literal or `=` value sets
|
|
262
|
+
* the initial state and writes nothing; an authored `checked` with a `^` value raises (two sources).
|
|
263
|
+
* The default is the authored `checked` attribute, written by html.js, not by this adapter.
|
|
264
|
+
*/
|
|
265
|
+
export class CheckboxControl extends ControlAdapter {
|
|
266
|
+
static writesDefault = false;
|
|
267
|
+
|
|
268
|
+
read() {
|
|
269
|
+
return this.element.checked;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
write(value) {
|
|
273
|
+
const on = Boolean(value);
|
|
274
|
+
if (this.element.checked !== on) this.element.checked = on;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
same(current, value) {
|
|
278
|
+
return current === Boolean(value);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
attach() {
|
|
282
|
+
const node = this.node;
|
|
283
|
+
if (node.getAttr('checked') != null && node.pointerType(node.getAttr('value')) === '^') {
|
|
284
|
+
throw new TypeError(`${node.nodeTag} '${node.label}': checked and a ^ value are two sources`);
|
|
285
|
+
}
|
|
286
|
+
super.attach();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* radio: one boolean per button (D4). With `group` the button joins RadioGroups, which generates its
|
|
292
|
+
* DOM `name` (P11); without `group` it is a standalone boolean. Choosing a button writes `true` on
|
|
293
|
+
* it, then `false` on the other bound buttons of the group that are on, in join order, synchronously (legacy
|
|
294
|
+
* dijit RadioButton order); a `true` from the Data does the same through the same method (C04.2).
|
|
295
|
+
*/
|
|
296
|
+
export class RadioControl extends CheckboxControl {
|
|
297
|
+
#name = null;
|
|
298
|
+
#projected = false;
|
|
299
|
+
|
|
300
|
+
/** The RadioGroups of the page's live renderer. */
|
|
301
|
+
get groups() { return this.binding.runtime.renderer.radioGroups; }
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Join the group and write the generated `name`. Raises before any effect when the node also
|
|
305
|
+
* authors `name` (two names) or when it is on and another button of the group is on (P11); a
|
|
306
|
+
* button whose node was removed under freeze is not counted (its NodeBinding is closed).
|
|
307
|
+
*/
|
|
308
|
+
attach() {
|
|
309
|
+
const node = this.node;
|
|
310
|
+
const group = this.resolved.group ?? null;
|
|
311
|
+
const form = formNode(node);
|
|
312
|
+
if (group != null) {
|
|
313
|
+
if (node.getAttr('name') != null) {
|
|
314
|
+
throw new TypeError(`${node.nodeTag} '${node.label}': name and group are two names`);
|
|
315
|
+
}
|
|
316
|
+
this.#requireSingleTrue(group, form);
|
|
317
|
+
}
|
|
318
|
+
super.attach();
|
|
319
|
+
if (group == null) return;
|
|
320
|
+
this.#name = this.groups.join(this, group, form);
|
|
321
|
+
this.element.setAttribute('name', this.#name);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
detach() {
|
|
325
|
+
if (this.#name !== null) {
|
|
326
|
+
this.groups.leave(this);
|
|
327
|
+
this.element.removeAttribute('name');
|
|
328
|
+
this.#name = null;
|
|
329
|
+
}
|
|
330
|
+
super.detach();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Follow the semantic form owner: when a `form` or `formId` above the node was added, removed or
|
|
335
|
+
* moved, the button leaves its group and joins the group of the same name in its new scope, with a
|
|
336
|
+
* new generated `name`; the NodeBinding and the element stay (ASTRA-04). A button that is on and
|
|
337
|
+
* meets another button on in the new scope raises before any effect, as at the join (P11).
|
|
338
|
+
*/
|
|
339
|
+
refresh() {
|
|
340
|
+
if (this.#name === null) return;
|
|
341
|
+
const group = this.resolved.group;
|
|
342
|
+
const form = formNode(this.node);
|
|
343
|
+
if (this.groups.nameOf(group, form) === this.#name) return;
|
|
344
|
+
this.#requireSingleTrue(group, form);
|
|
345
|
+
this.groups.leave(this);
|
|
346
|
+
this.#name = this.groups.join(this, group, form);
|
|
347
|
+
this.element.setAttribute('name', this.#name);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* The button is on and another button of `group` within `form` is on: an error. A button whose node
|
|
352
|
+
* was removed under freeze is not counted (its NodeBinding is closed).
|
|
353
|
+
*/
|
|
354
|
+
#requireSingleTrue(group, form) {
|
|
355
|
+
const node = this.node;
|
|
356
|
+
if (resolved(node, 'value') && this.groups.peersOf(group, form).some(peer => !peer.binding.closed && peer.read())) {
|
|
357
|
+
throw new Error(`${node.nodeTag} '${node.label}': several true values in radio group '${group}'`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Data → DOM; a button turned on by the Data, after its first projection, turns its peers off (C04.2). */
|
|
362
|
+
project(value, change) {
|
|
363
|
+
const first = !this.#projected;
|
|
364
|
+
this.#projected = true;
|
|
365
|
+
const was = this.read();
|
|
366
|
+
super.project(value, change);
|
|
367
|
+
if (!first && !was && this.read()) this.turnOffPeers();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* DOM → Data: `true` on this button, then `false` on its bound peers. The peers follow the user's
|
|
372
|
+
* choice even when a recipient of the first write turned this button off again; a recipient that
|
|
373
|
+
* removed this button stops the choice there (R12).
|
|
374
|
+
*/
|
|
375
|
+
commit(event) {
|
|
376
|
+
const chosen = this.read();
|
|
377
|
+
super.commit(event);
|
|
378
|
+
if (chosen && !this.binding.closed) this.turnOffPeers();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Turn the other buttons of the group off. A peer that is on in the Data and bound with `^` gets
|
|
383
|
+
* `false` through its own guarded commit; a peer already off is not written (legacy dijit
|
|
384
|
+
* RadioButton turns off only the checked peers). A peer whose node was removed under freeze is
|
|
385
|
+
* left alone, and a write that removes this button stops the loop (R12).
|
|
386
|
+
*/
|
|
387
|
+
turnOffPeers() {
|
|
388
|
+
if (this.#name === null) return;
|
|
389
|
+
for (const peer of this.groups.peers(this)) {
|
|
390
|
+
if (this.binding.closed) return;
|
|
391
|
+
if (peer.binding.closed) continue;
|
|
392
|
+
if (peer.element.checked) peer.element.checked = false;
|
|
393
|
+
if (resolved(peer.node, 'value')) peer.commit(null);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The radio groups of one live renderer (P11). A group is the `group` value within its form: the
|
|
400
|
+
* nearest Source ancestor with `formId` or `form=true`, or none. The generated `name` joins the
|
|
401
|
+
* renderer's instance id, the form and the group, so two Gramlot pages never share a group.
|
|
402
|
+
*/
|
|
403
|
+
export class RadioGroups {
|
|
404
|
+
#renderer;
|
|
405
|
+
#groups = new Map();
|
|
406
|
+
#names = new Map();
|
|
407
|
+
// One index per form node, in order of first use; a removed form node is not retained.
|
|
408
|
+
#forms = new WeakMap();
|
|
409
|
+
#nextForm = 0;
|
|
410
|
+
|
|
411
|
+
constructor(renderer) {
|
|
412
|
+
this.#renderer = renderer;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Add `control` to `group` within `form` (a Source node or null) and return the generated name. */
|
|
416
|
+
join(control, group, form) {
|
|
417
|
+
const name = this.nameOf(group, form);
|
|
418
|
+
if (!this.#groups.has(name)) this.#groups.set(name, new Set());
|
|
419
|
+
this.#groups.get(name).add(control);
|
|
420
|
+
this.#names.set(control, name);
|
|
421
|
+
return name;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
leave(control) {
|
|
425
|
+
const name = this.#names.get(control);
|
|
426
|
+
this.#names.delete(control);
|
|
427
|
+
const members = this.#groups.get(name);
|
|
428
|
+
members?.delete(control);
|
|
429
|
+
if (members?.size === 0) this.#groups.delete(name);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** The other buttons of the group of `control`, in join order. */
|
|
433
|
+
peers(control) {
|
|
434
|
+
const name = this.#names.get(control);
|
|
435
|
+
return [...this.#groups.get(name) ?? []].filter(member => member !== control);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** The buttons already in `group` within `form`, in join order. */
|
|
439
|
+
peersOf(group, form) {
|
|
440
|
+
return [...this.#groups.get(this.nameOf(group, form)) ?? []];
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** The generated DOM name of `group` within `form`. */
|
|
444
|
+
nameOf(group, form) {
|
|
445
|
+
if (form !== null && !this.#forms.has(form)) this.#forms.set(form, this.#nextForm++);
|
|
446
|
+
const scope = form === null ? 'page' : `form${this.#forms.get(form)}`;
|
|
447
|
+
return `gramlot-${this.#renderer.instanceId}-${scope}-${group}`;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** The nearest Source node, from `node` upwards, with `formId` or `form=true` (as `#FORM`), or null. */
|
|
452
|
+
function formNode(node) {
|
|
453
|
+
for (let current = node; current; current = current.parentNode) {
|
|
454
|
+
const attrs = current.getAttr() ?? {};
|
|
455
|
+
if (attrs.formId != null || attrs.form === true) return current;
|
|
456
|
+
}
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// The input types with an adapter, and those without one (S10 gate decision 1).
|
|
461
|
+
const INPUT_CONTROLS = {
|
|
462
|
+
text: TextControl, password: TextControl, email: TextControl, url: TextControl,
|
|
463
|
+
tel: TextControl, search: TextControl,
|
|
464
|
+
number: NumberControl, range: RangeControl, color: ColorControl,
|
|
465
|
+
date: TemporalControl, time: TemporalControl, month: TemporalControl, week: TemporalControl,
|
|
466
|
+
'datetime-local': TemporalControl, checkbox: CheckboxControl, radio: RadioControl,
|
|
467
|
+
hidden: null, file: null,
|
|
468
|
+
button: null, submit: null, reset: null, image: null,
|
|
469
|
+
};
|