@gramlot/gramlot 0.3.20 → 0.3.26
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/dist/components/chat/__init__.py +13 -0
- package/dist/components/chat/assets/collection.js +9 -0
- package/dist/components/chat/assets/index.js +3 -0
- package/dist/components/chat/commands.json +7 -0
- package/dist/components/chat/grammar.json +48 -0
- package/dist/components/editors/__init__.py +2 -2
- package/dist/components/editors/assets/collection.js +26 -120912
- package/dist/components/editors/assets/index.js +2 -1
- package/dist/components/editors/assets/modules/chunk-74O4WKIV.js +1 -0
- package/dist/components/editors/assets/modules/chunk-AU4HFPIY.js +1 -0
- package/dist/components/editors/assets/modules/chunk-YSGSC7KM.js +1 -0
- package/dist/components/editors/assets/modules/code-mirror-engine-RWQTR5G5.js +92 -0
- package/dist/components/editors/assets/modules/jodit-engine-MTSA4GZ3.js +1853 -0
- package/dist/components/editors/assets/modules/markdown-document-VFC23FAY.js +1 -0
- package/dist/components/editors/assets/modules/prose-mirror-engine-GMZPJPNN.js +24 -0
- package/dist/components/grid/__init__.py +14 -0
- package/dist/components/grid/assets/collection.js +262 -0
- package/dist/components/grid/assets/index.js +3 -0
- package/dist/components/grid/commands.json +7 -0
- package/dist/components/grid/grammar.json +770 -0
- package/dist/components/grouplets/__init__.py +13 -0
- package/dist/components/grouplets/assets/collection.js +185 -0
- package/dist/components/grouplets/assets/index.js +3 -0
- package/dist/components/grouplets/assets/modules/chunk-BLMUM44K.js +1 -0
- package/dist/components/grouplets/assets/modules/markdown-document-YWNZTOKK.js +1 -0
- package/dist/components/grouplets/commands.json +7 -0
- package/dist/components/grouplets/grammar.json +355 -0
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/collection.js +65 -24511
- package/dist/components/inputs/assets/index.js +2 -1
- package/dist/components/inputs/grammar.json +278 -6
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +68 -26732
- package/dist/components/layout/assets/index.js +2 -1
- package/dist/components/layout/grammar.json +39 -0
- package/dist/components/selection_trial/__init__.py +13 -0
- package/dist/components/selection_trial/assets/collection.js +4 -0
- package/dist/components/selection_trial/assets/index.js +3 -0
- package/dist/components/selection_trial/commands.json +7 -0
- package/dist/components/selection_trial/grammar.json +587 -0
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +27 -24612
- package/dist/components/tree/assets/index.js +2 -1
- package/dist/components/tree/grammar.json +341 -1
- package/dist/components/typst/__init__.py +2 -2
- package/dist/components/typst/assets/collection.js +12 -4
- package/dist/components/typst/assets/index.js +2 -1
- package/dist/components/typst/assets/modules/chunk-BLMUM44K.js +1 -0
- package/dist/components/typst/assets/modules/typst-client-JHC6UVYG.js +4 -0
- package/dist/components/uploads/__init__.py +13 -0
- package/dist/components/uploads/assets/collection.js +67 -0
- package/dist/components/uploads/assets/index.js +3 -0
- package/dist/components/uploads/commands.json +7 -0
- package/dist/components/uploads/grammar.json +789 -0
- package/dist/gramlot-markdown.js +16 -0
- package/dist/gramlot.js +6861 -1395
- package/dist/gramlot.min.js +1595 -46
- package/dist/runtime-notices.json +28 -0
- package/package.json +10 -3
- package/src/binding/grid-struct.js +63 -0
- package/src/binding/installation.js +5 -2
- package/src/binding/providers.js +4 -1
- package/src/binding/remote-resolver.js +1 -8
- package/src/binding/runtime.js +90 -6
- package/src/binding/validation-context.js +40 -0
- package/src/browser-camera-provider.d.ts +8 -0
- package/src/browser-camera-provider.js +104 -0
- package/src/builder/gramlot-builder.d.ts +5 -0
- package/src/builder/gramlot-builder.js +16 -0
- package/src/builder/source.js +34 -0
- package/src/chat.d.ts +12 -0
- package/src/chat.js +198 -0
- package/src/collection-artifact.js +1 -1
- package/src/components/attachment-manager.js +160 -0
- package/src/components/bases.js +19 -6
- package/src/components/border-container.js +86 -8
- package/src/components/buttons.js +11 -1
- package/src/components/chat.js +28 -0
- package/src/components/choice-editor.js +106 -38
- package/src/components/database-choices.js +133 -9
- package/src/components/editing-status-styles.js +26 -0
- package/src/components/editing-status.js +115 -0
- package/src/components/field-state.js +42 -54
- package/src/components/grid-cell-editor.js +181 -0
- package/src/components/grid-grammar.json +258 -0
- package/src/components/grid-header-interaction.js +196 -0
- package/src/components/grid-row-interaction.js +184 -0
- package/src/components/grid-row-layout.js +96 -0
- package/src/components/grid-rows.js +28 -0
- package/src/components/grid-search-box.js +142 -0
- package/src/components/grid-search-styles.js +42 -0
- package/src/components/grid-search.js +37 -0
- package/src/components/grid-structure.js +85 -0
- package/src/components/grid-styles.js +201 -0
- package/src/components/grid.js +666 -0
- package/src/components/grouplet-card.js +445 -0
- package/src/components/grouplets.js +17 -0
- package/src/components/html-preview.js +17 -4
- package/src/components/image-crop-dialog.js +272 -0
- package/src/components/image-crop.js +79 -0
- package/src/components/image-uploader.js +252 -0
- package/src/components/inputs.js +2 -1
- package/src/components/jodit-engine.js +1 -1
- package/src/components/markdown-document.js +2 -2
- package/src/components/menus.js +1 -0
- package/src/components/registry.js +4 -3
- package/src/components/screen-capture.js +43 -0
- package/src/components/selected-container.js +6 -2
- package/src/components/tray-pane.js +32 -4
- package/src/components/tree.js +416 -17
- package/src/components/typed-editor.js +23 -11
- package/src/components/upload-engine.js +156 -0
- package/src/components/uploader.js +208 -0
- package/src/components/uploads.js +7 -0
- package/src/editing-session.d.ts +25 -0
- package/src/editing-session.js +396 -0
- package/src/experiments/selection-transfer.js +78 -0
- package/src/experiments/selection-trial.js +64 -0
- package/src/experiments/window-selection.js +124 -0
- package/src/format.js +22 -7
- package/src/gramlot.d.ts +2 -0
- package/src/gramlot.js +2 -1
- package/src/handlers/device-handler.d.ts +2 -1
- package/src/handlers/rpc-handler.d.ts +3 -3
- package/src/index-page-styles.js +2 -1
- package/src/index.d.ts +16 -1
- package/src/index.js +16 -1
- package/src/inout.d.ts +2 -0
- package/src/inout.js +6 -0
- package/src/markdown-loader.d.ts +1 -0
- package/src/markdown-loader.js +4 -0
- package/src/markdown.d.ts +1 -0
- package/src/markdown.js +2 -0
- package/src/media-frame.js +9 -0
- package/src/renderer/attributes.js +7 -1
- package/src/renderer/gramlot-renderer.d.ts +2 -0
- package/src/renderer/gramlot-renderer.js +39 -3
- package/src/server/assets.d.ts +1 -1
- package/src/server/assets.js +1 -1
- package/src/server/gramlot-server.d.ts +10 -1
- package/src/server/gramlot-server.js +96 -8
- package/src/server/index.d.ts +2 -0
- package/src/server/index.js +2 -0
- package/src/server/page-base.js +17 -4
- package/src/server/page.d.ts +13 -1
- package/src/server/page.js +6 -0
- package/src/server/source-import.d.ts +2 -0
- package/src/server/source-import.js +31 -0
- package/src/server/upload.d.ts +20 -0
- package/src/server/upload.js +142 -0
- package/src/server/visual-editor.d.ts +11 -0
- package/src/server/visual-editor.js +149 -0
- package/src/source-picker.d.ts +9 -0
- package/src/source-picker.js +57 -0
- package/src/stores/bag-collection-store.js +51 -0
- package/src/stores/bag-collection-view.js +127 -0
- package/src/stores/collection-filter.js +77 -0
- package/src/tree-store.d.ts +24 -0
- package/src/tree-store.js +193 -0
- package/src/validation-run.js +50 -0
- package/src/view/button.js +8 -5
- package/src/view/controls.js +31 -32
- package/src/view/grouplet-card.js +54 -0
- package/src/view/html.js +29 -4
- package/src/view/labelbox.js +7 -3
- package/src/visual-editor-icons.js +10 -0
- package/src/visual-editor.d.ts +56 -0
- package/src/visual-editor.js +844 -0
- package/themes/gramlot-base/theme.css +127 -0
- package/themes/gramlot-compact/theme.css +8 -0
- package/themes/joanna/theme.css +8 -0
- package/themes/mimi/theme.css +8 -0
|
@@ -1,35 +1,62 @@
|
|
|
1
|
+
import {createDOMPurify} from '@gramlot/gramlot';
|
|
2
|
+
|
|
1
3
|
/** Database suggestions share the choice editor and the ordinary data RPC. */
|
|
2
4
|
export const databaseParameters = {
|
|
3
5
|
dbtable: {dtype:'T', binding:'read'},
|
|
6
|
+
caption_field: {dtype:'T', binding:'read'},
|
|
7
|
+
condition: {dtype:'T', binding:'read'},
|
|
8
|
+
weakCondition: {dtype:['B','T'], default:false, binding:'read'},
|
|
4
9
|
method: {dtype:'T', default:'dbSelect', binding:'read'},
|
|
5
10
|
columns: {dtype:'T', binding:'read'},
|
|
6
11
|
hiddenColumns: {dtype:'T', binding:'read'},
|
|
12
|
+
auxColumns: {dtype:'T', binding:'read'},
|
|
13
|
+
template: {dtype:'T', binding:'read'},
|
|
14
|
+
templateNull: {dtype:'T', default:'', binding:'read'},
|
|
7
15
|
order_by: {dtype:'T', binding:'read'},
|
|
8
16
|
limit: {dtype:'L', default:10, binding:'read'},
|
|
9
17
|
searchDelay: {dtype:'L', default:300, binding:'read'},
|
|
10
18
|
ignoreCase: {dtype:'B', default:true, binding:'read'},
|
|
11
19
|
hasDownArrow: {dtype:'B', default:true, binding:'read'},
|
|
12
|
-
emptyLabel: {dtype:'T', default:'', binding:'read'},
|
|
20
|
+
emptyLabel: {dtype:'T', qualification:'intl_str', default:'Clear selection', binding:'read'},
|
|
13
21
|
selectedCaption: {dtype:'T'},
|
|
14
22
|
selectedRecord: {dtype:'T'},
|
|
15
23
|
selectedCb: {dtype:'T'}
|
|
16
24
|
};
|
|
17
|
-
const requestParameters = ['dbtable','method','columns','hiddenColumns','order_by','limit','ignoreCase'];
|
|
25
|
+
const requestParameters = ['dbtable','method','caption_field','columns','hiddenColumns','auxColumns','order_by','limit','ignoreCase','condition','weakCondition'];
|
|
18
26
|
|
|
19
27
|
export function databaseChoices(Base) {
|
|
20
28
|
return class extends Base {
|
|
21
29
|
#configuration = {};
|
|
30
|
+
#conditionChanged = false;
|
|
22
31
|
#outputs = {};
|
|
23
32
|
#revision = 0;
|
|
24
33
|
#acceptedRow;
|
|
34
|
+
#template;
|
|
35
|
+
#sanitizer;
|
|
36
|
+
constructor() {
|
|
37
|
+
super();
|
|
38
|
+
this.#sanitizer = createDOMPurify(this.ownerDocument.defaultView);
|
|
39
|
+
}
|
|
25
40
|
get choiceSource() { return this.#revision; }
|
|
26
41
|
get searchDelay() { return this.parameters.searchDelay; }
|
|
42
|
+
get revalidateDraftOnSourceChange() { return this.constrained; }
|
|
43
|
+
get autoHighlight() { return this.constrained; }
|
|
44
|
+
get matchTypedChoice() { return this.constrained; }
|
|
45
|
+
get emptyChoice() {
|
|
46
|
+
return this.constrained && !this.parameters.validate_notnull
|
|
47
|
+
? {id:null, caption:this.parameters.emptyLabel} : null;
|
|
48
|
+
}
|
|
27
49
|
// Database writes wait for a complete choice/free-text edit, even with live=True.
|
|
28
50
|
get commitOnChange() { return true; }
|
|
29
51
|
textValue(text) { return text === '' ? null : text; }
|
|
30
52
|
projectAttributes(attrs, adapt) {
|
|
31
53
|
const configuration = Object.fromEntries(Object.entries(attrs).filter(([key]) =>
|
|
32
|
-
requestParameters.includes(key) ||
|
|
54
|
+
requestParameters.includes(key) || ['autoSelect','template','templateNull','emptyLabel','validate_notnull'].includes(key)
|
|
55
|
+
|| key.startsWith('condition_') || key.startsWith('selected_')));
|
|
56
|
+
const conditionKeys = new Set([...Object.keys(configuration), ...Object.keys(this.#configuration)]
|
|
57
|
+
.filter(key => key === 'condition' || key === 'weakCondition' || key.startsWith('condition_')));
|
|
58
|
+
if (Object.keys(this.#configuration).length && [...conditionKeys].some(key =>
|
|
59
|
+
!Object.is(configuration[key], this.#configuration[key]))) this.#conditionChanged = true;
|
|
33
60
|
const keys = new Set([...Object.keys(configuration), ...Object.keys(this.#configuration)]);
|
|
34
61
|
if ([...keys].some(key => !Object.is(configuration[key], this.#configuration[key]))) {
|
|
35
62
|
this.cancelValidation(); this.#acceptedRow = undefined;
|
|
@@ -37,18 +64,50 @@ export function databaseChoices(Base) {
|
|
|
37
64
|
}
|
|
38
65
|
this.#outputs = Object.fromEntries(Object.entries(attrs).filter(([key]) => key.startsWith('selected_')));
|
|
39
66
|
const rest = super.projectAttributes(attrs, adapt);
|
|
40
|
-
|
|
67
|
+
this.#template = null;
|
|
68
|
+
if (this.parameters.template != null) {
|
|
69
|
+
this.#template = this.#sanitizer.sanitize(this.parameters.template, {
|
|
70
|
+
USE_PROFILES: {html:true}, RETURN_DOM_FRAGMENT:true,
|
|
71
|
+
FORBID_TAGS: ['style','form','input','button','select','textarea','a','audio','video','details','summary'],
|
|
72
|
+
FORBID_ATTR: ['id','name','role','tabindex','contenteditable','autofocus']
|
|
73
|
+
});
|
|
74
|
+
for (const element of this.#template.querySelectorAll('*')) {
|
|
75
|
+
if ([...element.attributes].some(attr => /\{[A-Za-z0-9_]+\}/.test(attr.value))) {
|
|
76
|
+
this.#template = null;
|
|
77
|
+
throw new TypeError('Selector template placeholders belong in text, not HTML attributes.');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
this.choices.style.minWidth = this.#template
|
|
82
|
+
? 'min(var(--gramlot-choice-popup-min-width,28rem),var(--gramlot-popup-available-width,100vw))' : '';
|
|
83
|
+
if (this.parameters.caption_field != null && !this.parameters.caption_field.trim()) throw new TypeError('caption_field must be a nonempty field name.');
|
|
84
|
+
if (this.parameters.condition != null && !this.parameters.condition.trim()) throw new TypeError('condition must be a nonempty expression.');
|
|
85
|
+
if (typeof this.parameters.weakCondition !== 'boolean' &&
|
|
86
|
+
(typeof this.parameters.weakCondition !== 'string' || !this.parameters.weakCondition.trim())) {
|
|
87
|
+
throw new TypeError('weakCondition must be boolean or a nonempty expression.');
|
|
88
|
+
}
|
|
89
|
+
const {limit, searchDelay, hasDownArrow} = this.parameters;
|
|
41
90
|
if (!Number.isInteger(limit) || limit < 1) throw new TypeError('limit must be a positive integer.');
|
|
42
91
|
if (!Number.isInteger(searchDelay) || searchDelay < 0) throw new TypeError('searchDelay must be a non-negative integer.');
|
|
43
92
|
this.toggle.hidden = !hasDownArrow;
|
|
44
93
|
this.control.style.paddingRight = hasDownArrow ? '' : 'var(--gramlot-field-padding, .25em)';
|
|
45
|
-
if (!this.parameters.placeholder) this.control.placeholder = emptyLabel;
|
|
46
94
|
for (const key of Object.keys(rest)) if (key.startsWith('condition_') || key.startsWith('selected_')) delete rest[key];
|
|
47
95
|
return rest;
|
|
48
96
|
}
|
|
49
97
|
async loadChoices(keywords, signal) {
|
|
50
98
|
const parameters = Object.fromEntries(requestParameters.filter(key => key !== 'method' && this.parameters[key] != null)
|
|
51
99
|
.map(key => [key, this.parameters[key]]));
|
|
100
|
+
if (parameters.weakCondition === false) delete parameters.weakCondition;
|
|
101
|
+
const required = new Set(Object.entries(this.#outputs).filter(([, path]) => path).map(([name]) => name.slice(9)));
|
|
102
|
+
if (this.#template) {
|
|
103
|
+
const walker = this.ownerDocument.createTreeWalker(this.#template, 4);
|
|
104
|
+
while (walker.nextNode()) {
|
|
105
|
+
for (const match of walker.currentNode.data.matchAll(/\{([A-Za-z0-9_]+)\}/g)) required.add(match[1]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (required.size) parameters.hiddenColumns = [...new Set([
|
|
109
|
+
...(parameters.hiddenColumns ?? '').split(',').map(name => name.trim()).filter(Boolean), ...required
|
|
110
|
+
])].join(',');
|
|
52
111
|
for (const [key, value] of Object.entries(this.#configuration)) {
|
|
53
112
|
if (key.startsWith('condition_')) {
|
|
54
113
|
const name = key.slice(10);
|
|
@@ -60,17 +119,71 @@ export function databaseChoices(Base) {
|
|
|
60
119
|
if (!result || !Array.isArray(result.rows) || typeof result.identifier !== 'string' || typeof result.caption !== 'string') {
|
|
61
120
|
throw new TypeError('Database selectors require {rows, identifier, caption}.');
|
|
62
121
|
}
|
|
122
|
+
if (parameters.auxColumns != null) {
|
|
123
|
+
if (!Array.isArray(result.columns) || !Array.isArray(result.headers)
|
|
124
|
+
|| result.columns.length !== result.headers.length || !result.columns.length
|
|
125
|
+
|| result.columns.some(name => typeof name !== 'string' || !name)
|
|
126
|
+
|| result.headers.some(label => typeof label !== 'string')
|
|
127
|
+
|| new Set(result.columns).size !== result.columns.length) {
|
|
128
|
+
throw new TypeError('auxColumns requires columns and headers display metadata.');
|
|
129
|
+
}
|
|
130
|
+
for (const name of result.columns) required.add(name);
|
|
131
|
+
}
|
|
132
|
+
if (result.errors != null && (typeof result.errors !== 'string' || !Object.hasOwn(keywords, '_id'))) {
|
|
133
|
+
throw new TypeError('Selector errors must be literal text attached to identity lookup.');
|
|
134
|
+
}
|
|
63
135
|
const ids = new Set();
|
|
64
136
|
return result.rows.map(record => {
|
|
65
137
|
const id = record[result.identifier], caption = record[result.caption];
|
|
66
138
|
if (!((typeof id === 'string' && id.length) || Number.isSafeInteger(id)) || typeof caption !== 'string' || ids.has(id)) {
|
|
67
139
|
throw new TypeError('Invalid or duplicate database choice identity/caption.');
|
|
68
140
|
}
|
|
69
|
-
|
|
141
|
+
for (const name of required) if (!Object.hasOwn(record, name)) {
|
|
142
|
+
throw new TypeError(`Missing requested selector column: ${name}`);
|
|
143
|
+
}
|
|
144
|
+
ids.add(id); return {id, caption, record, error:result.errors, captionField:result.caption,
|
|
145
|
+
columns:parameters.auxColumns != null ? result.columns : null,
|
|
146
|
+
headers:parameters.auxColumns != null ? result.headers : null};
|
|
70
147
|
});
|
|
71
148
|
}
|
|
72
149
|
selectionAccepted(row) { this.#acceptedRow = row; }
|
|
73
150
|
renderChoice(row, item) {
|
|
151
|
+
if (row.id === null) {
|
|
152
|
+
super.renderChoice(row, item);
|
|
153
|
+
item.style.color = 'var(--gramlot-text-secondary,GrayText)';
|
|
154
|
+
item.style.borderBottom = '1px solid var(--gramlot-border,#d5dce5)';
|
|
155
|
+
item.style.marginBottom = 'var(--gramlot-space-1,.25rem)';
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (this.#template) {
|
|
159
|
+
const content = this.#template.cloneNode(true);
|
|
160
|
+
const walker = this.ownerDocument.createTreeWalker(content, 4);
|
|
161
|
+
while (walker.nextNode()) {
|
|
162
|
+
walker.currentNode.data = walker.currentNode.data.replace(/\{([A-Za-z0-9_]+)\}/g,
|
|
163
|
+
(_, name) => row.record[name] == null ? this.parameters.templateNull : String(row.record[name]));
|
|
164
|
+
}
|
|
165
|
+
item.style.display = 'block';
|
|
166
|
+
item.style.overflowWrap = 'anywhere';
|
|
167
|
+
item.replaceChildren(content);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (row.columns) {
|
|
171
|
+
item.style.display = 'grid';
|
|
172
|
+
item.style.gridTemplateColumns = `repeat(${row.columns.length}, minmax(0, 1fr))`;
|
|
173
|
+
item.style.gap = 'var(--gramlot-space-3,.75rem)';
|
|
174
|
+
item.style.alignItems = 'start';
|
|
175
|
+
item.replaceChildren(...row.columns.map((name, index) => {
|
|
176
|
+
const cell = this.ownerDocument.createElement('span');
|
|
177
|
+
cell.style.overflowWrap = 'anywhere';
|
|
178
|
+
const label = this.ownerDocument.createElement('small');
|
|
179
|
+
label.textContent = row.headers[index];
|
|
180
|
+
label.style.display = 'block'; label.style.opacity = '.7';
|
|
181
|
+
const value = this.ownerDocument.createElement('span');
|
|
182
|
+
value.textContent = row.record[name] ?? '';
|
|
183
|
+
cell.append(label, value); return cell;
|
|
184
|
+
}));
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
74
187
|
super.renderChoice(row, item);
|
|
75
188
|
for (const name of (this.parameters.columns ?? '').split(',').map(name => name.trim()).filter(name => name && name !== row.captionField)) {
|
|
76
189
|
const detail = this.ownerDocument.createElement('span');
|
|
@@ -100,9 +213,20 @@ export function databaseChoices(Base) {
|
|
|
100
213
|
super.finishValidation(outcome);
|
|
101
214
|
}
|
|
102
215
|
async resolveValue() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
216
|
+
const changed = this.#conditionChanged;
|
|
217
|
+
this.#conditionChanged = false;
|
|
218
|
+
if (changed && this.active && !this.locked && this.constrained && this.parameters.validate_notnull) {
|
|
219
|
+
const rows = await this.request({_querystring:'', limit:2}, {selection:true});
|
|
220
|
+
if (!rows) return;
|
|
221
|
+
if (rows.length === 1) { this.choose(rows[0]); return; }
|
|
222
|
+
}
|
|
223
|
+
const value = this.readControlValue();
|
|
224
|
+
const pending = super.resolveValue();
|
|
225
|
+
// Identity resolution must not start autoSelect after a later clear/edit.
|
|
226
|
+
if (value != null && value !== '') return pending;
|
|
227
|
+
await pending;
|
|
228
|
+
if (this.active && !this.locked && this.parameters.autoSelect && this.readControlValue() == null && this.control.value === '') {
|
|
229
|
+
const rows = await this.request({_querystring:'', limit:2}, {selection:true});
|
|
106
230
|
if (rows?.length === 1 && this.readControlValue() == null && !this.locked) this.choose(rows[0]);
|
|
107
231
|
}
|
|
108
232
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Theme-owned colors; compact reports are shared by forms and grids. */
|
|
2
|
+
export const editingStatusStyles = `
|
|
3
|
+
:host{display:inline-block;font:inherit;color:var(--gramlot-ink,CanvasText)}
|
|
4
|
+
.control-box{display:flex;align-items:center;gap:.35rem}
|
|
5
|
+
button{font:inherit;color:inherit;cursor:pointer;border:0;background:transparent;border-radius:var(--gramlot-radius-sm,4px)}
|
|
6
|
+
.control-box>button{display:inline-flex;align-items:center;gap:.4rem;padding:.3rem .5rem;white-space:nowrap}
|
|
7
|
+
button:hover{background:var(--gramlot-surface-subtle,ButtonFace)}
|
|
8
|
+
button:focus-visible{outline:1px solid var(--gramlot-focus-ring,Highlight);outline-offset:-1px}
|
|
9
|
+
svg{width:1rem;height:1rem;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round;stroke-linecap:round}
|
|
10
|
+
.count{font-variant-numeric:tabular-nums;color:var(--gramlot-text-secondary,GrayText)}
|
|
11
|
+
.invalid{color:var(--gramlot-error,#b3261e)}
|
|
12
|
+
.report{position:fixed;inset:auto;margin:0;padding:.65rem;min-width:15rem;width:28rem;
|
|
13
|
+
max-width:var(--gramlot-popup-available-width,calc(100vw - 16px));max-height:var(--gramlot-popup-available-height,60vh);overflow:auto;
|
|
14
|
+
border:1px solid var(--gramlot-border,ButtonBorder);border-radius:var(--gramlot-radius-md,8px);
|
|
15
|
+
background:var(--gramlot-surface,Canvas);color:var(--gramlot-ink,CanvasText);box-shadow:0 6px 24px #0002;font:inherit}
|
|
16
|
+
header{display:flex;align-items:center;justify-content:space-between;gap:1rem;padding:0 .35rem .45rem}
|
|
17
|
+
header button{font-size:1.2em;line-height:1;padding:.25rem .4rem}
|
|
18
|
+
ul{list-style:none;margin:0;padding:0}
|
|
19
|
+
li+li{border-top:1px solid var(--gramlot-border,ButtonBorder)}
|
|
20
|
+
li button{display:block;width:100%;text-align:start;padding:.55rem .35rem}
|
|
21
|
+
.field{display:block;font-weight:600;overflow-wrap:anywhere}
|
|
22
|
+
.detail{display:block;white-space:pre-wrap;overflow-wrap:anywhere;margin-top:.2rem}
|
|
23
|
+
.previous,.notice{color:var(--gramlot-text-secondary,GrayText)}
|
|
24
|
+
.arrow{padding:0 .5rem}
|
|
25
|
+
.notice{font-size:.9em;margin:.5rem .35rem .15rem}
|
|
26
|
+
`;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {Bag} from '@gramlot/gramlot';
|
|
2
|
+
import {defineComponent} from './registry.js';
|
|
3
|
+
import {Container} from './container.js';
|
|
4
|
+
import {SVG_NS} from '../renderer/attributes.js';
|
|
5
|
+
import {positionPopup} from './popup.js';
|
|
6
|
+
import {editingStatusStyles} from './editing-status-styles.js';
|
|
7
|
+
|
|
8
|
+
/** Presentation of the nearest shared session. Registers and validation stay in EditingSession. */
|
|
9
|
+
export const EditingStatus = defineComponent({
|
|
10
|
+
name:'editingStatus', tag:'gramlot-editing-status', base:Container, sourceContext:true,
|
|
11
|
+
parameters:{},
|
|
12
|
+
implementation:Base => class extends Base {
|
|
13
|
+
#session; #release; #buttons = new Map(); #popup; #title; #list; #notice; #kind;
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
const doc = this.ownerDocument;
|
|
17
|
+
const style = doc.createElement('style'); style.textContent = editingStatusStyles;
|
|
18
|
+
this.shadowRoot.append(style);
|
|
19
|
+
this.control.replaceChildren();
|
|
20
|
+
this.control.setAttribute('role','group'); this.control.setAttribute('aria-label','Editing reports');
|
|
21
|
+
for (const [kind,caption,path] of [
|
|
22
|
+
['errors','Errors','M12 3L22 21H2ZM12 9V14M12 17V17.1'],
|
|
23
|
+
['changes','Changes','M4 5H14M4 10H10M4 15H8M13 20L20 13L17 10L10 17L9 21Z']
|
|
24
|
+
]) {
|
|
25
|
+
const button = doc.createElement('button'); button.type = 'button'; button.dataset.kind = kind;
|
|
26
|
+
button.setAttribute('aria-haspopup','dialog'); button.setAttribute('aria-expanded','false'); button.setAttribute('aria-controls','editing-report');
|
|
27
|
+
const svg = doc.createElementNS(SVG_NS,'svg'); svg.setAttribute('viewBox','0 0 24 24'); svg.setAttribute('aria-hidden','true');
|
|
28
|
+
const shape = doc.createElementNS(SVG_NS,'path'); shape.setAttribute('d',path); svg.append(shape);
|
|
29
|
+
const label = doc.createElement('span'); label.textContent = caption;
|
|
30
|
+
const count = doc.createElement('span'); count.className = 'count'; count.textContent = '0';
|
|
31
|
+
button.append(svg,label,count); this.control.append(button);
|
|
32
|
+
this.#buttons.set(kind,{button,count});
|
|
33
|
+
button.addEventListener('click',() => {
|
|
34
|
+
const open = this.#popup.matches(':popover-open');
|
|
35
|
+
if (open && this.#kind === kind) { this.#popup.hidePopover(); return; }
|
|
36
|
+
this.#kind = kind; this.#renderList();
|
|
37
|
+
if (!open) this.#popup.showPopover();
|
|
38
|
+
this.#expanded(); positionPopup(this.#popup,button,{align:'end'});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.#popup = doc.createElement('section'); this.#popup.id = 'editing-report';
|
|
42
|
+
this.#popup.className = 'report'; this.#popup.setAttribute('popover','auto');
|
|
43
|
+
this.#popup.setAttribute('role','dialog'); this.#popup.setAttribute('aria-labelledby','report-title');
|
|
44
|
+
const heading = doc.createElement('header');
|
|
45
|
+
this.#title = doc.createElement('strong'); this.#title.id = 'report-title';
|
|
46
|
+
const close = doc.createElement('button'); close.type = 'button'; close.textContent = '×'; close.setAttribute('aria-label','Close report');
|
|
47
|
+
close.addEventListener('click',() => this.#popup.hidePopover()); heading.append(this.#title,close);
|
|
48
|
+
this.#list = doc.createElement('ul');
|
|
49
|
+
this.#notice = doc.createElement('p'); this.#notice.className = 'notice'; this.#notice.setAttribute('role','status');
|
|
50
|
+
this.#popup.append(heading,this.#list,this.#notice); this.shadowRoot.append(this.#popup);
|
|
51
|
+
this.#popup.addEventListener('toggle',() => this.#expanded());
|
|
52
|
+
}
|
|
53
|
+
setSourceContext(context) {
|
|
54
|
+
this.#release?.(); this.#release = null;
|
|
55
|
+
this.#session = context.sourceNode.formState;
|
|
56
|
+
if (!this.#session) throw new Error('editingStatus requires a shared editing session.');
|
|
57
|
+
this.#connect();
|
|
58
|
+
}
|
|
59
|
+
connectedCallback() { this.#connect(); }
|
|
60
|
+
disconnectedCallback() {
|
|
61
|
+
this.#release?.(); this.#release = null;
|
|
62
|
+
if (this.#popup.matches(':popover-open')) this.#popup.hidePopover();
|
|
63
|
+
}
|
|
64
|
+
#connect() {
|
|
65
|
+
if (!this.#session || this.#release || !this.isConnected) return;
|
|
66
|
+
this.#release = this.#session.subscribe(() => this.#refresh()); this.#refresh();
|
|
67
|
+
}
|
|
68
|
+
#expanded() {
|
|
69
|
+
const open = this.#popup.matches(':popover-open');
|
|
70
|
+
for (const [kind,{button}] of this.#buttons) button.setAttribute('aria-expanded',String(open && kind === this.#kind));
|
|
71
|
+
}
|
|
72
|
+
#refresh() {
|
|
73
|
+
this.#buttons.get('errors').count.textContent = String(this.#session.errorEntries.length);
|
|
74
|
+
this.#buttons.get('changes').count.textContent = String(this.#session.changeEntries.length);
|
|
75
|
+
this.#buttons.get('errors').button.classList.toggle('invalid',this.#session.errorEntries.length > 0);
|
|
76
|
+
if (this.#popup.matches(':popover-open')) this.#renderList();
|
|
77
|
+
}
|
|
78
|
+
#value(value,exists) {
|
|
79
|
+
if (!exists) return 'Not present';
|
|
80
|
+
if (value == null) return '∅';
|
|
81
|
+
if (value instanceof Bag) return `${value.length} fields`;
|
|
82
|
+
if (value instanceof Date) return value.toLocaleString();
|
|
83
|
+
return String(value);
|
|
84
|
+
}
|
|
85
|
+
#renderList() {
|
|
86
|
+
const errors = this.#kind === 'errors';
|
|
87
|
+
const entries = errors ? this.#session.errorEntries : this.#session.changeEntries;
|
|
88
|
+
this.#title.textContent = errors ? 'Validation errors' : 'Unsaved changes';
|
|
89
|
+
this.#notice.textContent = entries.length ? 'Select an entry to return to its field.' : (errors ? 'No validation errors.' : 'No changes since the accepted snapshot.');
|
|
90
|
+
// Preserve list focus when asynchronous validation updates another entry.
|
|
91
|
+
const focusedPath = this.shadowRoot.activeElement?.dataset.path;
|
|
92
|
+
this.#list.replaceChildren();
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
const doc = this.ownerDocument, item = doc.createElement('li'), button = doc.createElement('button');
|
|
95
|
+
button.type = 'button'; button.dataset.path = entry.path;
|
|
96
|
+
const field = doc.createElement('span'); field.className = 'field';
|
|
97
|
+
field.textContent = entry.path.slice(this.#session.path ? this.#session.path.length+1 : 0);
|
|
98
|
+
const detail = doc.createElement('span'); detail.className = 'detail';
|
|
99
|
+
if (errors) detail.textContent = entry.message;
|
|
100
|
+
else {
|
|
101
|
+
const before = doc.createElement('span'); before.className = 'previous'; before.textContent = this.#value(entry.previous,entry.existed);
|
|
102
|
+
const arrow = doc.createElement('span'); arrow.className = 'arrow'; arrow.textContent = '→';
|
|
103
|
+
const after = doc.createElement('span'); after.className = 'current'; after.textContent = this.#value(entry.current,entry.exists);
|
|
104
|
+
detail.append(before,arrow,after);
|
|
105
|
+
}
|
|
106
|
+
button.append(field,detail); item.append(button); this.#list.append(item);
|
|
107
|
+
button.addEventListener('click',() => {
|
|
108
|
+
if (this.#session.focusField(entry.path)) this.#popup.hidePopover();
|
|
109
|
+
else this.#notice.textContent = 'This field is not visible. Check the grid filter or whether the row still exists.';
|
|
110
|
+
});
|
|
111
|
+
if (entry.path === focusedPath) button.focus({preventScroll:true});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import {validateInput, validationParameters} from './validation.js';
|
|
2
|
+
import {ValidationRun} from '../validation-run.js';
|
|
2
3
|
import {positionPopup} from './popup.js';
|
|
3
4
|
|
|
4
5
|
const MESSAGE_DURATION = 1800;
|
|
5
6
|
const MESSAGE_FADE_DURATION = 200;
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
+
/** Validation presentation; sessions own execution independently of mounted controls. */
|
|
8
9
|
export function FieldState(Base) {
|
|
9
10
|
return class extends Base {
|
|
11
|
+
get supportsFormValidation() { return true; }
|
|
12
|
+
get validationExecutor() { return validateInput; }
|
|
13
|
+
connectedCallback() { super.connectedCallback(); if (this.pageContext?.form) this.pageContext.formValidate(); }
|
|
10
14
|
#rejected = false;
|
|
11
15
|
#projected;
|
|
12
16
|
#messageTimer;
|
|
13
17
|
#messageEvents;
|
|
14
|
-
#validation;
|
|
15
|
-
#validationEpoch = 0;
|
|
18
|
+
#validation = new ValidationRun();
|
|
16
19
|
#validationCurrent;
|
|
17
20
|
#rules = {};
|
|
18
21
|
validationResult = null;
|
|
@@ -67,7 +70,7 @@ export function FieldState(Base) {
|
|
|
67
70
|
view.addEventListener('scroll', () => this.#hideMessage(), options);
|
|
68
71
|
view.addEventListener('resize', () => this.#hideMessage(), options);
|
|
69
72
|
}
|
|
70
|
-
releaseControl() { this.
|
|
73
|
+
releaseControl() { this.#validation.cancel(); this.#hideMessage(); super.releaseControl(); }
|
|
71
74
|
disconnectedCallback() { this.releaseControl(); }
|
|
72
75
|
setFieldState({invalid = false, pending = false} = {}) {
|
|
73
76
|
this.control.setAttribute('aria-invalid', String(invalid));
|
|
@@ -80,68 +83,51 @@ export function FieldState(Base) {
|
|
|
80
83
|
this.message.textContent = message;
|
|
81
84
|
if (message) this.#showMessage(); else this.#hideMessage();
|
|
82
85
|
}
|
|
86
|
+
restoreFormValue(value) { this.error(''); this.#rejected = false; super.restoreFormValue(value); }
|
|
83
87
|
cancelValidation() {
|
|
84
|
-
this
|
|
85
|
-
this.#validation
|
|
88
|
+
if (this.pageContext?.form) this.pageContext.formCancelValidation();
|
|
89
|
+
this.#validation.cancel(); this.#validationCurrent = null;
|
|
86
90
|
this.control.setAttribute('aria-busy', 'false');
|
|
87
91
|
}
|
|
92
|
+
presentValidation(result) {
|
|
93
|
+
this.validationResult = result;
|
|
94
|
+
this.control.setAttribute('aria-busy','false');
|
|
95
|
+
this.error(result.error || '');
|
|
96
|
+
this.message.dataset.severity = result.error ? 'error' : 'warning';
|
|
97
|
+
this.#rejected = Boolean(result.error);
|
|
98
|
+
if (!result.error && result.warnings.length) {
|
|
99
|
+
this.message.textContent = result.warnings.join('\n'); this.#showMessage();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
88
102
|
validateCandidate(value, accept) {
|
|
89
|
-
this.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const valid = () => this.#validationEpoch === epoch && !controller.signal.aborted && current();
|
|
95
|
-
const failure = error => ({value, modified:false, error:error.message, warnings:[], issues:[], datachanges:[]});
|
|
96
|
-
const finish = result => {
|
|
97
|
-
if (!valid()) {
|
|
98
|
-
if (this.#validation === controller) this.cancelValidation();
|
|
99
|
-
return {ok:false};
|
|
100
|
-
}
|
|
101
|
-
this.#validation = null; this.#validationCurrent = null;
|
|
102
|
-
this.control.setAttribute('aria-busy', 'false');
|
|
103
|
-
try { if (!result.error) accept(result.value); }
|
|
104
|
-
catch (error) { result = failure(error); }
|
|
105
|
-
this.validationResult = result;
|
|
106
|
-
this.error(result.error || ''); this.message.dataset.severity = result.error ? 'error' : 'warning'; this.#rejected = Boolean(result.error);
|
|
107
|
-
if (!result.error && result.warnings.length) {
|
|
108
|
-
this.message.textContent = result.warnings.join('\n'); this.#showMessage();
|
|
109
|
-
}
|
|
110
|
-
const outcome = {ok:!result.error, value:result.value, result, rules, isCurrent:valid};
|
|
111
|
-
if (!outcome.ok) this.finishValidation(outcome);
|
|
112
|
-
return outcome;
|
|
113
|
-
};
|
|
114
|
-
let result;
|
|
115
|
-
try {
|
|
116
|
-
if (rules.validate_timeout != null && (!Number.isInteger(rules.validate_timeout) || rules.validate_timeout <= 0)) throw new TypeError('validate_timeout must be a positive integer in milliseconds.');
|
|
117
|
-
result = validateInput(value, rules, {...this.pageContext, signal:controller.signal, selection:value => this.validateSelection(value)});
|
|
103
|
+
if (this.pageContext.form) {
|
|
104
|
+
// Capture selection evidence before accepting clears the choice's picked row.
|
|
105
|
+
const selection = this.validationSelection();
|
|
106
|
+
accept(value);
|
|
107
|
+
return this.pageContext.formValidateCandidate(value,selection);
|
|
118
108
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
settle(failure(new Error('Validation timed out.'))); controller.abort();
|
|
132
|
-
}, rules.validate_timeout);
|
|
133
|
-
result.then(settle, error => settle(failure(error)));
|
|
134
|
-
});
|
|
109
|
+
this.cancelValidation();
|
|
110
|
+
this.#validationCurrent = this.pageContext.validationCurrent();
|
|
111
|
+
return this.#validation.start(value,this.#rules,
|
|
112
|
+
{...this.pageContext,selection:value => this.validateSelection(value)},validateInput,outcome => {
|
|
113
|
+
this.#validationCurrent = null;
|
|
114
|
+
if (outcome.ok) {
|
|
115
|
+
try { accept(outcome.value); }
|
|
116
|
+
catch (error) { outcome.ok = false; outcome.result.error = error.message; }
|
|
117
|
+
}
|
|
118
|
+
this.presentValidation(outcome.result);
|
|
119
|
+
if (!outcome.ok) this.finishValidation(outcome);
|
|
120
|
+
},() => this.control.setAttribute('aria-busy','true'));
|
|
135
121
|
}
|
|
136
122
|
finishValidation(outcome) {
|
|
137
123
|
if (!outcome.result) return;
|
|
138
124
|
this.pageContext.finishValidation(outcome);
|
|
139
125
|
}
|
|
140
126
|
shouldProjectControlValue(value) {
|
|
141
|
-
if (this.#validationCurrent && !this.#validationCurrent()) this.cancelValidation();
|
|
127
|
+
if (!this.pageContext?.form && this.#validationCurrent && !this.#validationCurrent()) this.cancelValidation();
|
|
142
128
|
const same = this.sameControlValue(value, this.#projected);
|
|
143
129
|
if (same && this.#rejected) return false;
|
|
144
|
-
if (!same) this.cancelValidation();
|
|
130
|
+
if (!same && !this.pageContext?.form) this.cancelValidation();
|
|
145
131
|
this.#projected = value;
|
|
146
132
|
if (!same && this.#rejected) this.error('');
|
|
147
133
|
return true;
|
|
@@ -150,8 +136,10 @@ export function FieldState(Base) {
|
|
|
150
136
|
const rest = super.projectAttributes(attrs, adapt);
|
|
151
137
|
const rules = Object.fromEntries(Object.entries(attrs).filter(([name]) => name.startsWith('validate_')));
|
|
152
138
|
const keys = new Set([...Object.keys(rules), ...Object.keys(this.#rules)]);
|
|
153
|
-
if ([...keys].some(key => !Object.is(rules[key], this.#rules[key])) || attrs.disabled || attrs.readonly) this.cancelValidation();
|
|
139
|
+
if (!this.pageContext?.form && ([...keys].some(key => !Object.is(rules[key], this.#rules[key])) || attrs.disabled || attrs.readonly)) this.cancelValidation();
|
|
140
|
+
const rulesChanged = [...keys].some(key => !Object.is(rules[key], this.#rules[key]));
|
|
154
141
|
this.#rules = rules;
|
|
142
|
+
if (rulesChanged && this.pageContext?.form) this.pageContext.formValidate();
|
|
155
143
|
for (const key of Object.keys(rules)) delete rest[key];
|
|
156
144
|
const required = Boolean(this.parameters.validate_notnull);
|
|
157
145
|
this.control.setAttribute('aria-required', String(required));
|