@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
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** PoC row-header gestures, with preview separated from Source-owned heights. */
|
|
2
|
+
export class GridRowInteraction {
|
|
3
|
+
#body;
|
|
4
|
+
#control;
|
|
5
|
+
#layout;
|
|
6
|
+
#nodeForRow;
|
|
7
|
+
#gesture = null;
|
|
8
|
+
#drag = null;
|
|
9
|
+
#store;
|
|
10
|
+
#dragEnabled;
|
|
11
|
+
#layoutChanged;
|
|
12
|
+
constructor(body, control, layout, nodeForRow, store, dragEnabled, layoutChanged) {
|
|
13
|
+
this.#body = body;
|
|
14
|
+
this.#control = control;
|
|
15
|
+
this.#layout = layout;
|
|
16
|
+
this.#nodeForRow = nodeForRow;
|
|
17
|
+
this.#store = store;
|
|
18
|
+
this.#dragEnabled = dragEnabled;
|
|
19
|
+
this.#layoutChanged = layoutChanged;
|
|
20
|
+
}
|
|
21
|
+
get resizing() { return this.#gesture !== null; }
|
|
22
|
+
connect() {
|
|
23
|
+
this.#body.addEventListener('pointerdown', this.#start);
|
|
24
|
+
this.#body.addEventListener('pointermove', this.#move);
|
|
25
|
+
this.#body.addEventListener('pointerup', this.#finish);
|
|
26
|
+
this.#body.addEventListener('pointercancel', this.#cancel);
|
|
27
|
+
this.#body.addEventListener('lostpointercapture', this.#cancel);
|
|
28
|
+
this.#body.addEventListener('keydown', this.#key);
|
|
29
|
+
this.#body.addEventListener('dblclick', this.#reset);
|
|
30
|
+
this.#body.addEventListener('dragstart', this.#dragStart);
|
|
31
|
+
this.#body.addEventListener('dragover', this.#dragOver);
|
|
32
|
+
this.#body.addEventListener('dragleave', this.#dragLeave);
|
|
33
|
+
this.#body.addEventListener('drop', this.#drop);
|
|
34
|
+
this.#body.addEventListener('dragend', this.#dragEnd);
|
|
35
|
+
}
|
|
36
|
+
disconnect() {
|
|
37
|
+
this.cancel();
|
|
38
|
+
this.cancelDrag();
|
|
39
|
+
this.#body.removeEventListener('pointerdown', this.#start);
|
|
40
|
+
this.#body.removeEventListener('pointermove', this.#move);
|
|
41
|
+
this.#body.removeEventListener('pointerup', this.#finish);
|
|
42
|
+
this.#body.removeEventListener('pointercancel', this.#cancel);
|
|
43
|
+
this.#body.removeEventListener('lostpointercapture', this.#cancel);
|
|
44
|
+
this.#body.removeEventListener('keydown', this.#key);
|
|
45
|
+
this.#body.removeEventListener('dblclick', this.#reset);
|
|
46
|
+
this.#body.removeEventListener('dragstart', this.#dragStart);
|
|
47
|
+
this.#body.removeEventListener('dragover', this.#dragOver);
|
|
48
|
+
this.#body.removeEventListener('dragleave', this.#dragLeave);
|
|
49
|
+
this.#body.removeEventListener('drop', this.#drop);
|
|
50
|
+
this.#body.removeEventListener('dragend', this.#dragEnd);
|
|
51
|
+
}
|
|
52
|
+
#dragStart = event => {
|
|
53
|
+
const row = event.target.closest('tr');
|
|
54
|
+
const node = this.#nodeForRow(row);
|
|
55
|
+
if (!this.#dragEnabled() || !node || this.#gesture || event.target.closest('.row-height-handle')) {
|
|
56
|
+
event.preventDefault(); return;
|
|
57
|
+
}
|
|
58
|
+
this.#drag = {node, bag:this.#store().bag};
|
|
59
|
+
event.dataTransfer.effectAllowed = 'move';
|
|
60
|
+
event.dataTransfer.setData('text/plain', node.label);
|
|
61
|
+
row.classList.add('row-dragging');
|
|
62
|
+
};
|
|
63
|
+
#dropTarget(event) {
|
|
64
|
+
if (!this.#drag || !this.#dragEnabled()) return null;
|
|
65
|
+
const {node, bag} = this.#drag;
|
|
66
|
+
const row = event.target.closest('tr'), target = this.#nodeForRow(row);
|
|
67
|
+
if (!target || target === node || this.#store().bag !== bag || node.parentBag !== bag || target.parentBag !== bag) return null;
|
|
68
|
+
const box = row.getBoundingClientRect();
|
|
69
|
+
return {row, target, after:event.clientY > box.top + box.height/2};
|
|
70
|
+
}
|
|
71
|
+
#clearMarker() {
|
|
72
|
+
for (const row of this.#body.querySelectorAll('[data-row-drop]')) row.removeAttribute('data-row-drop');
|
|
73
|
+
}
|
|
74
|
+
#dragOver = event => {
|
|
75
|
+
this.#clearMarker();
|
|
76
|
+
const target = this.#dropTarget(event);
|
|
77
|
+
if (!target) return;
|
|
78
|
+
event.preventDefault(); event.dataTransfer.dropEffect = 'move';
|
|
79
|
+
target.row.dataset.rowDrop = target.after ? 'after' : 'before';
|
|
80
|
+
};
|
|
81
|
+
#dragLeave = event => {
|
|
82
|
+
if (!this.#body.contains(event.relatedTarget)) this.#clearMarker();
|
|
83
|
+
};
|
|
84
|
+
#drop = event => {
|
|
85
|
+
const target = this.#dropTarget(event);
|
|
86
|
+
if (!target) return;
|
|
87
|
+
event.preventDefault(); event.stopPropagation();
|
|
88
|
+
const {node} = this.#drag;
|
|
89
|
+
this.cancelDrag();
|
|
90
|
+
this.#store().moveRow(node, target.target, target.after);
|
|
91
|
+
};
|
|
92
|
+
#dragEnd = () => { this.cancelDrag(); };
|
|
93
|
+
cancelDrag() {
|
|
94
|
+
this.#drag = null;
|
|
95
|
+
this.#clearMarker();
|
|
96
|
+
for (const row of this.#body.querySelectorAll('.row-dragging')) row.classList.remove('row-dragging');
|
|
97
|
+
}
|
|
98
|
+
#target(event) {
|
|
99
|
+
const handle = event.target.closest('.row-height-handle');
|
|
100
|
+
if (!handle) return null;
|
|
101
|
+
const row = handle.closest('tr');
|
|
102
|
+
const node = this.#nodeForRow(row);
|
|
103
|
+
return node ? {handle, row, node} : null;
|
|
104
|
+
}
|
|
105
|
+
project(row, node) {
|
|
106
|
+
const height = this.#layout().height(node);
|
|
107
|
+
row.style.height = height === null ? '' : `${height}px`;
|
|
108
|
+
}
|
|
109
|
+
#value(row) {
|
|
110
|
+
const handle = row.querySelector('.row-height-handle');
|
|
111
|
+
if (handle) handle.setAttribute('aria-valuenow', String(Math.round(row.getBoundingClientRect().height)));
|
|
112
|
+
this.#layoutChanged();
|
|
113
|
+
}
|
|
114
|
+
#start = event => {
|
|
115
|
+
const target = this.#target(event);
|
|
116
|
+
if (!target || event.button !== 0 || this.#gesture) return;
|
|
117
|
+
const {row} = target;
|
|
118
|
+
const height = row.getBoundingClientRect().height;
|
|
119
|
+
const original = row.style.height;
|
|
120
|
+
// A table row cannot be shorter than its contents; measure that same native layout.
|
|
121
|
+
row.style.height = '';
|
|
122
|
+
const minimum = row.getBoundingClientRect().height;
|
|
123
|
+
row.style.height = original;
|
|
124
|
+
this.#gesture = {...target, pointerId:event.pointerId, y:event.clientY, height, minimum, preview:height};
|
|
125
|
+
target.handle.focus({preventScroll:true});
|
|
126
|
+
target.handle.setPointerCapture(event.pointerId);
|
|
127
|
+
this.#control.classList.add('resizing-row');
|
|
128
|
+
event.preventDefault();
|
|
129
|
+
};
|
|
130
|
+
#move = event => {
|
|
131
|
+
const gesture = this.#gesture;
|
|
132
|
+
if (!gesture || gesture.pointerId !== event.pointerId) return;
|
|
133
|
+
gesture.preview = Math.max(gesture.minimum, Math.round(gesture.height + event.clientY - gesture.y));
|
|
134
|
+
gesture.row.style.height = `${gesture.preview}px`;
|
|
135
|
+
this.#value(gesture.row);
|
|
136
|
+
};
|
|
137
|
+
#finish = event => {
|
|
138
|
+
const gesture = this.#gesture;
|
|
139
|
+
if (!gesture || gesture.pointerId !== event.pointerId) return;
|
|
140
|
+
this.#move(event);
|
|
141
|
+
this.cancel();
|
|
142
|
+
if (this.#nodeForRow(gesture.row) !== gesture.node || gesture.preview === gesture.height) return;
|
|
143
|
+
this.#layout().setHeight(gesture.node, gesture.preview);
|
|
144
|
+
this.project(gesture.row, gesture.node);
|
|
145
|
+
this.#value(gesture.row);
|
|
146
|
+
};
|
|
147
|
+
#key = event => {
|
|
148
|
+
if (event.key === 'Escape' && (this.#gesture || this.#drag)) {
|
|
149
|
+
this.cancel(); this.cancelDrag(); event.preventDefault(); return;
|
|
150
|
+
}
|
|
151
|
+
const target = this.#target(event);
|
|
152
|
+
if (!target || !['ArrowUp','ArrowDown','Home'].includes(event.key)) return;
|
|
153
|
+
this.cancel();
|
|
154
|
+
let height = null;
|
|
155
|
+
if (event.key !== 'Home') {
|
|
156
|
+
const current = target.row.getBoundingClientRect().height;
|
|
157
|
+
target.row.style.height = '';
|
|
158
|
+
const minimum = target.row.getBoundingClientRect().height;
|
|
159
|
+
height = Math.max(minimum, current + (event.key === 'ArrowDown' ? 10 : -10));
|
|
160
|
+
}
|
|
161
|
+
this.#layout().setHeight(target.node, height);
|
|
162
|
+
this.project(target.row, target.node);
|
|
163
|
+
this.#value(target.row);
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
};
|
|
166
|
+
#reset = event => {
|
|
167
|
+
const target = this.#target(event);
|
|
168
|
+
if (!target) return;
|
|
169
|
+
this.cancel();
|
|
170
|
+
this.#layout().setHeight(target.node, null);
|
|
171
|
+
this.project(target.row, target.node);
|
|
172
|
+
this.#value(target.row);
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
};
|
|
175
|
+
#cancel = () => { this.cancel(); };
|
|
176
|
+
cancel() {
|
|
177
|
+
const gesture = this.#gesture;
|
|
178
|
+
if (!gesture) return;
|
|
179
|
+
this.#gesture = null;
|
|
180
|
+
this.project(gesture.row, gesture.node);
|
|
181
|
+
this.#control.classList.remove('resizing-row');
|
|
182
|
+
if (gesture.handle.hasPointerCapture(gesture.pointerId)) gesture.handle.releasePointerCapture(gesture.pointerId);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** Per-grid row geometry. BagNode identity survives sorting and filtering. */
|
|
2
|
+
export class GridRowLayout {
|
|
3
|
+
#store;
|
|
4
|
+
#window = null;
|
|
5
|
+
setWindow(info) {
|
|
6
|
+
if (info !== null && (!Number.isSafeInteger(info.start) || info.start < 0
|
|
7
|
+
|| !Number.isSafeInteger(info.total) || info.total < info.start)) {
|
|
8
|
+
throw new TypeError('Invalid grid window');
|
|
9
|
+
}
|
|
10
|
+
this.#window = info;
|
|
11
|
+
this.#dirty = true;
|
|
12
|
+
}
|
|
13
|
+
#headerWidth = null;
|
|
14
|
+
get headerWidth() { return this.#headerWidth; }
|
|
15
|
+
setHeaderWidth(width) {
|
|
16
|
+
if (!Number.isFinite(width) || width <= 0) throw new TypeError('Grid row header width must be positive pixels');
|
|
17
|
+
this.#headerWidth = Math.round(width);
|
|
18
|
+
}
|
|
19
|
+
#heights = new Map();
|
|
20
|
+
#measured = new Map();
|
|
21
|
+
#nodes = [];
|
|
22
|
+
#offsets = [0];
|
|
23
|
+
#dirty = true;
|
|
24
|
+
#estimate = 32;
|
|
25
|
+
bindStore(store) {
|
|
26
|
+
if (store !== this.#store) {
|
|
27
|
+
this.#heights.clear(); this.resetMeasurements();
|
|
28
|
+
this.#store = store;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
retain(nodes) {
|
|
32
|
+
const current = new Set(nodes);
|
|
33
|
+
for (const map of [this.#heights, this.#measured]) {
|
|
34
|
+
for (const node of map.keys()) if (!current.has(node)) map.delete(node);
|
|
35
|
+
}
|
|
36
|
+
this.#dirty = true;
|
|
37
|
+
}
|
|
38
|
+
height(node) { return this.#heights.get(node) ?? null; }
|
|
39
|
+
setHeight(node, height) {
|
|
40
|
+
if (node.parentBag !== this.#store) return;
|
|
41
|
+
if (height === null) this.#heights.delete(node);
|
|
42
|
+
else {
|
|
43
|
+
if (!Number.isFinite(height) || height <= 0) throw new TypeError('Grid row height must be positive pixels');
|
|
44
|
+
this.#heights.set(node, Math.round(height));
|
|
45
|
+
}
|
|
46
|
+
this.forget(node);
|
|
47
|
+
}
|
|
48
|
+
setRows(nodes) { this.#nodes = nodes; this.#dirty = true; }
|
|
49
|
+
resetMeasurements() { this.#measured.clear(); this.#dirty = true; }
|
|
50
|
+
forget(node) { this.#measured.delete(node); this.#dirty = true; }
|
|
51
|
+
measure(node, height) {
|
|
52
|
+
// Hidden/unlaid-out rows have no usable measurement yet.
|
|
53
|
+
if (height <= 0 || this.#measured.get(node) === height) return false;
|
|
54
|
+
this.#measured.set(node, height);
|
|
55
|
+
this.#dirty = true;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
#index() {
|
|
59
|
+
if (!this.#dirty) return;
|
|
60
|
+
// The smallest observed natural row gives a theme-derived estimate for
|
|
61
|
+
// unseen rows. Explicit heights and measured wrapped rows take priority.
|
|
62
|
+
let natural = Infinity;
|
|
63
|
+
for (const [node, height] of this.#measured) if (!this.#heights.has(node)) natural = Math.min(natural, height);
|
|
64
|
+
if (natural !== Infinity) this.#estimate = natural;
|
|
65
|
+
this.#offsets = [0];
|
|
66
|
+
for (const node of this.#nodes) this.#offsets.push(this.#offsets.at(-1) +
|
|
67
|
+
(this.#measured.get(node) ?? Math.max(this.#heights.get(node) ?? 0, this.#estimate)));
|
|
68
|
+
this.#dirty = false;
|
|
69
|
+
}
|
|
70
|
+
offset(index) {
|
|
71
|
+
this.#index();
|
|
72
|
+
if (!this.#window) return this.#offsets[index];
|
|
73
|
+
const local = index - this.#window.start;
|
|
74
|
+
if (local <= 0) return index * this.#estimate;
|
|
75
|
+
if (local <= this.#nodes.length) return this.#window.start * this.#estimate + this.#offsets[local];
|
|
76
|
+
return index * this.#estimate + this.#offsets.at(-1) - this.#nodes.length * this.#estimate;
|
|
77
|
+
}
|
|
78
|
+
get count() { return this.#window?.total ?? this.#nodes.length; }
|
|
79
|
+
get totalHeight() { return this.offset(this.count); }
|
|
80
|
+
indexAt(position) {
|
|
81
|
+
this.#index();
|
|
82
|
+
let low = 0, high = this.count;
|
|
83
|
+
while (low < high) {
|
|
84
|
+
const middle = (low + high) >>> 1;
|
|
85
|
+
if (this.offset(middle + 1) <= position) low = middle + 1;
|
|
86
|
+
else high = middle;
|
|
87
|
+
}
|
|
88
|
+
return Math.min(low, Math.max(0, this.count - 1));
|
|
89
|
+
}
|
|
90
|
+
window(top, height) {
|
|
91
|
+
this.#index();
|
|
92
|
+
const margin = Math.max(height, this.#estimate * 8);
|
|
93
|
+
return {start: this.indexAt(Math.max(0, top - margin)),
|
|
94
|
+
end: Math.min(this.count, this.indexAt(top + height + margin) + 1)};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {isDecimal} from '@genrojs/tytx';
|
|
2
|
+
|
|
3
|
+
/** Formatting projection only; collection membership and ordering belong to the store. */
|
|
4
|
+
export class GridRows {
|
|
5
|
+
constructor(store, format) {
|
|
6
|
+
this.store = store;
|
|
7
|
+
this.format = format;
|
|
8
|
+
}
|
|
9
|
+
read(columns, nodes = this.store.getRows()) {
|
|
10
|
+
return nodes.map(node => ({node, cells:columns.map(column =>
|
|
11
|
+
cellText(this.store.value(node, column.field), column, this.format))}));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cellText(value, column, format) {
|
|
16
|
+
if (value == null) return '';
|
|
17
|
+
const {dtype, format: pattern} = column;
|
|
18
|
+
if (dtype === 'T' && typeof value !== 'string' || dtype === 'B' && typeof value !== 'boolean') throw new TypeError(`grid ${column.field} does not match ${dtype}`);
|
|
19
|
+
if (typeof value === 'string' || typeof value === 'boolean') {
|
|
20
|
+
if (dtype && !['T', 'B'].includes(dtype) || pattern != null) throw new TypeError(`grid ${column.field} requires a typed numeric or temporal value`);
|
|
21
|
+
return String(value);
|
|
22
|
+
}
|
|
23
|
+
if (value instanceof Date || pattern != null) return format(value, {dtype, pattern: pattern ?? 'short'});
|
|
24
|
+
if (dtype && !['L', 'R', 'N'].includes(dtype)) throw new TypeError(`grid ${column.field} does not match ${dtype}`);
|
|
25
|
+
if (dtype === 'L' && !Number.isInteger(value)) throw new TypeError('grid L requires an integer');
|
|
26
|
+
if (typeof value === 'number' && Number.isFinite(value) || isDecimal(value) && value.isFinite()) return String(value);
|
|
27
|
+
throw new TypeError(`grid ${column.field} requires a scalar value`);
|
|
28
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {defineComponent} from './registry.js';
|
|
2
|
+
import {Container} from './container.js';
|
|
3
|
+
import {gridSearch, searchColumns} from './grid-search.js';
|
|
4
|
+
import {SVG_NS} from '../renderer/attributes.js';
|
|
5
|
+
import {positionPopup} from './popup.js';
|
|
6
|
+
import {gridSearchStyles} from './grid-search-styles.js';
|
|
7
|
+
|
|
8
|
+
/** FrameGrid's search slot. The collection view owns the accepted filter. */
|
|
9
|
+
export const SearchBox = defineComponent({
|
|
10
|
+
name:'searchBox', tag:'gramlot-search-box', base:Container, sourceContext:true,
|
|
11
|
+
parameters:{searchOn:{dtype:['B','T'], default:true, binding:'read'},
|
|
12
|
+
searchTime:{dtype:'R', nullable:true, default:null, binding:'write'}},
|
|
13
|
+
implementation:Base => class extends Base {
|
|
14
|
+
#context; #grid; #release; #releaseView; #view; #timer = null;
|
|
15
|
+
#field; #input; #error; #helpText; #filterSignature; #bag;
|
|
16
|
+
constructor() {
|
|
17
|
+
super();
|
|
18
|
+
const doc = this.ownerDocument;
|
|
19
|
+
const style = doc.createElement('style');
|
|
20
|
+
style.textContent = gridSearchStyles;
|
|
21
|
+
const icon = path => {
|
|
22
|
+
const svg = doc.createElementNS(SVG_NS,'svg'); svg.setAttribute('viewBox','0 0 24 24'); svg.setAttribute('aria-hidden','true');
|
|
23
|
+
const shape = doc.createElementNS(SVG_NS,'path'); shape.setAttribute('d',path); svg.append(shape); return svg;
|
|
24
|
+
};
|
|
25
|
+
this.#field = doc.createElement('select'); this.#field.setAttribute('aria-label','Search column');
|
|
26
|
+
this.#input = doc.createElement('input'); this.#input.type = 'text'; this.#input.setAttribute('role','searchbox'); this.#input.setAttribute('aria-label','Filter rows');
|
|
27
|
+
this.#input.setAttribute('aria-describedby','search-error');
|
|
28
|
+
this.#input.placeholder = ' ';
|
|
29
|
+
const clear = doc.createElement('button'); clear.type = 'button'; clear.title = 'Clear filter'; clear.setAttribute('aria-label','Clear filter'); clear.append(icon('M6 6L18 18M18 6L6 18'));
|
|
30
|
+
this.#error = doc.createElement('span'); this.#error.id = 'search-error'; this.#error.className = 'error'; this.#error.setAttribute('role','alert');
|
|
31
|
+
const choice = doc.createElement('span'); choice.className = 'column-choice';
|
|
32
|
+
choice.append(this.#field,icon('M7 10L12 15L17 10'));
|
|
33
|
+
const entry = doc.createElement('span'); entry.className = 'search-entry';
|
|
34
|
+
const lens = icon('M21 21L16 16M18 10A8 8 0 1 1 2 10A8 8 0 0 1 18 10'); lens.classList.add('search-lens');
|
|
35
|
+
clear.className = 'clear';
|
|
36
|
+
const info = doc.createElement('button'); info.type = 'button'; info.className = 'info';
|
|
37
|
+
info.setAttribute('aria-label','Search instructions'); info.title = 'Search instructions';
|
|
38
|
+
info.setAttribute('aria-haspopup','dialog'); info.setAttribute('aria-expanded','false'); info.setAttribute('aria-controls','search-help');
|
|
39
|
+
info.append(icon('M21 12A9 9 0 1 1 3 12A9 9 0 0 1 21 12M12 11V17M12 7V7.1'));
|
|
40
|
+
const help = doc.createElement('div'); help.id = 'search-help'; help.className = 'search-help';
|
|
41
|
+
help.setAttribute('popover','auto'); help.setAttribute('role','dialog'); help.setAttribute('aria-label','Search instructions');
|
|
42
|
+
const heading = doc.createElement('strong'); heading.textContent = 'Search instructions';
|
|
43
|
+
this.#helpText = doc.createElement('p');
|
|
44
|
+
const common = doc.createElement('p'); common.textContent = 'Clear with × or Escape. Changing column clears the query. An incomplete expression keeps the last valid result.';
|
|
45
|
+
help.append(heading,this.#helpText,common); this.shadowRoot.append(help);
|
|
46
|
+
info.addEventListener('click', () => {
|
|
47
|
+
if (help.matches(':popover-open')) help.hidePopover();
|
|
48
|
+
else {help.showPopover(); positionPopup(help,info,{align:'end'});}
|
|
49
|
+
});
|
|
50
|
+
help.addEventListener('toggle', () => info.setAttribute('aria-expanded',String(help.matches(':popover-open'))));
|
|
51
|
+
entry.append(lens,this.#input,clear,info);
|
|
52
|
+
this.control.replaceChildren(choice,entry,this.#error);
|
|
53
|
+
this.shadowRoot.append(style);
|
|
54
|
+
this.#input.addEventListener('input', () => {
|
|
55
|
+
this.#cancel(); this.#timer = doc.defaultView.setTimeout(() => { this.#timer = null; this.#apply(); },100);
|
|
56
|
+
});
|
|
57
|
+
this.#input.addEventListener('keydown', event => {
|
|
58
|
+
if (event.isComposing) return;
|
|
59
|
+
if (event.key === 'Enter') { event.preventDefault(); this.#cancel(); this.#apply(); }
|
|
60
|
+
if (event.key === 'Escape') { event.preventDefault(); this.#clear(); }
|
|
61
|
+
});
|
|
62
|
+
this.#field.addEventListener('change', () => this.#clear());
|
|
63
|
+
clear.addEventListener('click', () => {this.#clear(); this.#input.focus();});
|
|
64
|
+
}
|
|
65
|
+
setSourceContext(context) {
|
|
66
|
+
this.#context = context;
|
|
67
|
+
const target = context.frameRecipient();
|
|
68
|
+
if (target?.nodeTag !== 'grid') throw new TypeError('searchBox requires a FrameGrid target');
|
|
69
|
+
this.#grid = gridSearch(target);
|
|
70
|
+
}
|
|
71
|
+
projectAttributes(attrs, adapt) {
|
|
72
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
73
|
+
if (this.isConnected) this.#configure();
|
|
74
|
+
return rest;
|
|
75
|
+
}
|
|
76
|
+
connectedCallback() {
|
|
77
|
+
this.#release = this.#grid.subscribe(() => this.#configure());
|
|
78
|
+
this.#configure();
|
|
79
|
+
}
|
|
80
|
+
disconnectedCallback() {
|
|
81
|
+
this.#cancel(); this.#release?.(); this.#releaseView?.(); this.#release = this.#releaseView = null;
|
|
82
|
+
}
|
|
83
|
+
#cancel() { if (this.#timer !== null) this.ownerDocument.defaultView.clearTimeout(this.#timer); this.#timer = null; }
|
|
84
|
+
#configure() {
|
|
85
|
+
try { this.#update(); }
|
|
86
|
+
catch (error) { this.#input.disabled = true; this.#error.textContent = error.message; }
|
|
87
|
+
}
|
|
88
|
+
#update() {
|
|
89
|
+
if (!this.#grid.view) return;
|
|
90
|
+
if (this.#view !== this.#grid.view || this.#bag !== this.#grid.view.bag || !this.#releaseView) {
|
|
91
|
+
this.#cancel(); this.#releaseView?.(); this.#view = this.#grid.view; this.#bag = this.#view.bag;
|
|
92
|
+
this.#filterSignature = undefined;
|
|
93
|
+
this.#releaseView = this.#view.subscribe(() => this.#sync());
|
|
94
|
+
}
|
|
95
|
+
const previous = this.#field.value;
|
|
96
|
+
const options = searchColumns(this.parameters.searchOn,this.#grid.columns);
|
|
97
|
+
this.#field.replaceChildren(...options.map(({field,name}) => {
|
|
98
|
+
const option = this.ownerDocument.createElement('option'); option.value = field; option.textContent = name; return option;
|
|
99
|
+
}));
|
|
100
|
+
if (options.some(option => option.field === previous)) this.#field.value = previous;
|
|
101
|
+
this.#input.disabled = !options.length;
|
|
102
|
+
this.#sync(); this.#hint();
|
|
103
|
+
}
|
|
104
|
+
#sync() {
|
|
105
|
+
const filter = this.#view.filter;
|
|
106
|
+
// Uncommitted drafts (including errors) survive unrelated row notifications.
|
|
107
|
+
const signature = JSON.stringify(filter);
|
|
108
|
+
if (this.#filterSignature === signature) return;
|
|
109
|
+
this.#filterSignature = signature;
|
|
110
|
+
this.#cancel(); this.#input.value = filter?.value ?? '';
|
|
111
|
+
if (filter) this.#field.value = filter.columns.join('+');
|
|
112
|
+
this.#error.textContent = ''; this.#input.removeAttribute('aria-invalid'); this.#hint();
|
|
113
|
+
}
|
|
114
|
+
#dtype() {
|
|
115
|
+
const column = this.#grid.columns.find(column => column.field === this.#field.value);
|
|
116
|
+
return ['L','R','N','D'].includes(column?.dtype) ? column.dtype : 'T';
|
|
117
|
+
}
|
|
118
|
+
#hint() {
|
|
119
|
+
const dtype = this.#dtype();
|
|
120
|
+
this.#helpText.textContent = dtype === 'T'
|
|
121
|
+
? 'Type a word to find it anywhere. Text is case-insensitive. You can also use expressions: lamp|desk matches either word; ^lamp matches the start.'
|
|
122
|
+
: dtype === 'D'
|
|
123
|
+
? 'Compare dates: = 2026-10-15, >= 2026-10-15 or < 2026-10-15. ISO dates and your browser’s local date notation are accepted.'
|
|
124
|
+
: 'Compare numbers: = 12, > 100, <= 50 or ! 0 (not equal). Decimal punctuation follows your browser locale.';
|
|
125
|
+
}
|
|
126
|
+
#clear() { this.#cancel(); this.#input.value = ''; this.#apply(); this.#hint(); }
|
|
127
|
+
#apply() {
|
|
128
|
+
if (!this.#view) return;
|
|
129
|
+
try {
|
|
130
|
+
const value = this.#input.value;
|
|
131
|
+
const started = performance.now();
|
|
132
|
+
this.#view.applyFilter(value.trim() ? {value, columns:this.#field.value.split('+'), dtype:this.#dtype(),
|
|
133
|
+
locale:this.#context.locale,
|
|
134
|
+
timeZone:this.#context.timeZone} : null);
|
|
135
|
+
this.#context.writeParameter('searchTime', performance.now() - started);
|
|
136
|
+
this.#error.textContent = ''; this.#input.removeAttribute('aria-invalid');
|
|
137
|
+
} catch (error) {
|
|
138
|
+
this.#error.textContent = error.message; this.#input.setAttribute('aria-invalid','true');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Search capsule: uses shared field, surface and focus tokens in every theme. */
|
|
2
|
+
export const gridSearchStyles = `
|
|
3
|
+
:host{display:inline-block;inline-size:clamp(14rem,40vw,22rem);max-inline-size:100%;min-inline-size:0}
|
|
4
|
+
.control-box{display:flex;align-items:stretch;position:relative;min-block-size:var(--gramlot-field-height,2.75rem);
|
|
5
|
+
border:1px solid var(--gramlot-border,GrayText);border-radius:999px;
|
|
6
|
+
background:var(--gramlot-field-background,Canvas);color:var(--gramlot-ink,CanvasText);
|
|
7
|
+
transition:border-color .15s}
|
|
8
|
+
.control-box:hover{border-color:var(--gramlot-border-control,GrayText)}
|
|
9
|
+
.control-box:focus-within{border-color:var(--gramlot-border-control,GrayText)}
|
|
10
|
+
select,input,button{font:inherit;color:inherit;min-inline-size:0;border:0;background:transparent}
|
|
11
|
+
.column-choice{display:flex;align-items:center;background:var(--gramlot-surface-subtle,ButtonFace);border-radius:999px 0 0 999px;position:relative;flex:0 1 auto;max-inline-size:42%;border-inline-end:1px solid var(--gramlot-border,GrayText)}
|
|
12
|
+
select{display:block;appearance:none;inline-size:100%;padding:.35rem 1.8rem .35rem .85rem;
|
|
13
|
+
border-radius:999px;font-size:.9em;color:var(--gramlot-text-secondary,GrayText);cursor:pointer;text-overflow:ellipsis}
|
|
14
|
+
select option{background:var(--gramlot-field-background,Canvas);color:var(--gramlot-ink,CanvasText)}
|
|
15
|
+
.column-choice>svg{position:absolute;inset-inline-end:.55rem;inset-block-start:50%;transform:translateY(-50%);pointer-events:none;color:var(--gramlot-text-secondary,GrayText);width:.8rem;height:.8rem}
|
|
16
|
+
.search-entry{position:relative;display:flex;align-items:center;flex:1;min-inline-size:0}
|
|
17
|
+
.search-lens{position:absolute;inset-inline-start:.65rem;pointer-events:none;color:var(--gramlot-text-secondary,GrayText);opacity:.6}
|
|
18
|
+
input{inline-size:100%;min-block-size:var(--gramlot-field-height,2.75rem);padding:.4rem 3.6rem .4rem 2.1rem;border-radius:999px;outline:none}
|
|
19
|
+
input::placeholder{color:var(--gramlot-text-secondary,GrayText);opacity:.75}
|
|
20
|
+
input:disabled{opacity:.5}
|
|
21
|
+
.clear,.info{position:absolute;display:grid;place-items:center;padding:.3rem;
|
|
22
|
+
inline-size:1.6rem;block-size:1.6rem;border-radius:50%;color:var(--gramlot-text-secondary,GrayText);cursor:pointer}
|
|
23
|
+
.clear{inset-inline-end:1.95rem}
|
|
24
|
+
.info{inset-inline-end:.35rem;opacity:.7}
|
|
25
|
+
.clear:hover,.info:hover{color:var(--gramlot-ink,CanvasText);background:var(--gramlot-surface-subtle,ButtonFace)}
|
|
26
|
+
input:placeholder-shown~.clear{display:none}
|
|
27
|
+
select:focus-visible,button:focus-visible{outline:1px solid var(--gramlot-text-secondary,GrayText);outline-offset:-3px}
|
|
28
|
+
svg{width:1rem;height:1rem;fill:none;stroke:currentColor;stroke-width:1.7;stroke-linecap:round;stroke-linejoin:round;flex:none}
|
|
29
|
+
.control-box:has([aria-invalid=true]){border-color:var(--gramlot-error,Mark)}
|
|
30
|
+
.error{position:absolute;inset-block-start:calc(100% + .35rem);inset-inline:0;z-index:1;
|
|
31
|
+
border:1px solid var(--gramlot-border,GrayText);border-radius:var(--gramlot-radius,.5rem);
|
|
32
|
+
background:var(--gramlot-field-background,Canvas);color:var(--gramlot-error,Mark);
|
|
33
|
+
font-size:.85em;padding:.5rem .75rem;box-shadow:var(--gramlot-shadow,0 2px 5px #0002)}
|
|
34
|
+
.error:empty{display:none}
|
|
35
|
+
.search-help{position:fixed;inset:auto;margin:0;inline-size:19rem;max-inline-size:calc(100vw - 1rem);
|
|
36
|
+
max-block-size:var(--gramlot-popup-available-height,80vh);overflow:auto;padding:1rem;
|
|
37
|
+
border:1px solid var(--gramlot-border,GrayText);border-radius:var(--gramlot-radius-lg,.75rem);
|
|
38
|
+
background:var(--gramlot-surface,Canvas);color:var(--gramlot-ink,CanvasText);
|
|
39
|
+
box-shadow:var(--gramlot-shadow,0 3px 12px #0002);font:inherit;font-size:.9em;line-height:1.5}
|
|
40
|
+
.search-help p{margin:.6rem 0 0}.search-help p:last-child{color:var(--gramlot-text-secondary,GrayText)}
|
|
41
|
+
@media(prefers-reduced-motion:reduce){.control-box{transition:none}}
|
|
42
|
+
`;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Source-owned connection: current grid resources, never copied row data. */
|
|
2
|
+
export class GridSearch {
|
|
3
|
+
#listeners = new Set();
|
|
4
|
+
view = null;
|
|
5
|
+
#bag;
|
|
6
|
+
columns = [];
|
|
7
|
+
update(view, columns) {
|
|
8
|
+
if (this.view === view && this.#bag === view.bag && this.columns === columns) return;
|
|
9
|
+
this.view = view; this.#bag = view.bag; this.columns = columns;
|
|
10
|
+
for (const listener of this.#listeners) listener();
|
|
11
|
+
}
|
|
12
|
+
subscribe(listener) { this.#listeners.add(listener); return () => this.#listeners.delete(listener); }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function gridSearch(node) {
|
|
16
|
+
if (!Object.hasOwn(node, 'gridSearch')) {
|
|
17
|
+
Object.defineProperty(node, 'gridSearch', {value:new GridSearch(), configurable:true});
|
|
18
|
+
}
|
|
19
|
+
return node.gridSearch;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function searchColumns(searchOn, columns) {
|
|
23
|
+
if (searchOn === true || typeof searchOn === 'string' && searchOn.startsWith('*')) {
|
|
24
|
+
const allowed = searchOn === true ? columns : columns.filter(column => searchOn.slice(1).includes(column.dtype ?? 'T'));
|
|
25
|
+
return allowed.length ? [{field:allowed.map(column => column.field).join('+'), name:'All columns'},
|
|
26
|
+
...allowed.map(column => ({field:column.field, name:column.name}))] : [];
|
|
27
|
+
}
|
|
28
|
+
if (typeof searchOn !== 'string' || !searchOn.trim()) throw new TypeError('searchOn requires true or a column list');
|
|
29
|
+
return searchOn.split(',').map(item => {
|
|
30
|
+
const parts = item.trim().split(':');
|
|
31
|
+
const field = parts.at(-1), name = parts.length === 2 ? parts[0] : columns.find(column => column.field === field)?.name;
|
|
32
|
+
if (parts.length > 2 || !field || field.split('+').some(key => !columns.some(column => column.field === key))) {
|
|
33
|
+
throw new TypeError(`Unknown searchOn column: ${item}`);
|
|
34
|
+
}
|
|
35
|
+
return {field, name:name ?? field};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {Bag} from '@gramlot/gramlot';
|
|
2
|
+
|
|
3
|
+
const TYPES = new Set(['T', 'B', 'L', 'R', 'N', 'D', 'H', 'DHZ']);
|
|
4
|
+
const COLUMN_KEYS = new Set(['tag', 'field', 'name', 'dtype', 'width', 'align', 'format', 'columnset', 'white_space', 'edit']);
|
|
5
|
+
const GROUP_KEYS = new Set(['tag', 'code', 'name', 'background', 'color']);
|
|
6
|
+
|
|
7
|
+
/** Legacy Data contract: ordered cells and independent columnset definitions. */
|
|
8
|
+
export class GridStructure {
|
|
9
|
+
#nodes = new Map();
|
|
10
|
+
constructor(bag) {
|
|
11
|
+
if (!(bag instanceof Bag)) throw new TypeError('grid structpath must resolve to a Bag');
|
|
12
|
+
const view = branch(bag, 'view_0'), info = branch(bag, 'info');
|
|
13
|
+
const cells = branch(view, 'rows_0'), definitions = branch(info, 'columnsets');
|
|
14
|
+
const fields = new Set(), groups = new Map();
|
|
15
|
+
for (const node of definitions.getNodes()) {
|
|
16
|
+
const attrs = node.getAttr();
|
|
17
|
+
requireKeys(attrs, GROUP_KEYS);
|
|
18
|
+
requireIdentity(attrs.code, 'colgroup code');
|
|
19
|
+
if (attrs.tag !== 'columnset' || node.resolver || node.staticValue != null || node.label !== attrs.code) {
|
|
20
|
+
throw new TypeError('grid columnset requires a scalar definition labelled by code');
|
|
21
|
+
}
|
|
22
|
+
if (groups.has(attrs.code)) throw new TypeError(`Duplicate grid colgroup: ${attrs.code}`);
|
|
23
|
+
if (attrs.name != null && typeof attrs.name !== 'string') throw new TypeError('grid group name must be text');
|
|
24
|
+
for (const key of ['background','color']) if (attrs[key] != null && typeof attrs[key] !== 'string') throw new TypeError(`grid ${key} must be text`);
|
|
25
|
+
groups.set(attrs.code, {...attrs, name:attrs.name ?? attrs.code});
|
|
26
|
+
}
|
|
27
|
+
this.columns = [];
|
|
28
|
+
this.groups = [];
|
|
29
|
+
for (const node of cells.getNodes()) {
|
|
30
|
+
const attrs = node.getAttr();
|
|
31
|
+
requireKeys(attrs, COLUMN_KEYS);
|
|
32
|
+
if (attrs.tag !== 'cell' || node.staticValue != null || node.resolver) throw new TypeError('grid cell must be a scalar declaration');
|
|
33
|
+
requireIdentity(attrs.field, 'field');
|
|
34
|
+
if (fields.has(attrs.field)) throw new TypeError(`Duplicate grid field: ${attrs.field}`);
|
|
35
|
+
fields.add(attrs.field); this.#nodes.set(attrs.field, node);
|
|
36
|
+
if (attrs.columnset != null && !groups.has(attrs.columnset)) throw new TypeError(`Unknown grid columnset: ${attrs.columnset}`);
|
|
37
|
+
if (attrs.dtype != null && !TYPES.has(attrs.dtype)) throw new TypeError(`Unsupported grid dtype: ${attrs.dtype}`);
|
|
38
|
+
if (attrs.align != null && !['start', 'center', 'end'].includes(attrs.align)) throw new TypeError('grid align requires start, center or end');
|
|
39
|
+
if (attrs.width != null && (typeof attrs.width !== 'string' || !/^(?:\d+(?:\.\d+)?)(?:px|em|rem|ch|%)$/.test(attrs.width))) {
|
|
40
|
+
throw new TypeError('grid width requires a nonnegative CSS length (px, em, rem, ch or %)');
|
|
41
|
+
}
|
|
42
|
+
if (attrs.white_space != null && !['normal','nowrap','pre-wrap'].includes(attrs.white_space)) throw new TypeError('grid white_space requires normal, nowrap or pre-wrap');
|
|
43
|
+
for (const key of ['name', 'format']) if (attrs[key] != null && typeof attrs[key] !== 'string') throw new TypeError(`grid ${key} must be text`);
|
|
44
|
+
if (attrs.edit != null && typeof attrs.edit !== 'boolean' && (typeof attrs.edit !== 'object' || Array.isArray(attrs.edit))) throw new TypeError('grid edit requires a boolean or editor options');
|
|
45
|
+
const column = Object.freeze({...attrs, name: attrs.name ?? attrs.field,
|
|
46
|
+
align: attrs.align ?? (['L', 'R', 'N'].includes(attrs.dtype) ? 'end' : 'start')});
|
|
47
|
+
this.columns.push(column);
|
|
48
|
+
const code = attrs.columnset ?? null;
|
|
49
|
+
let run = this.groups.at(-1);
|
|
50
|
+
if (!run || run.code !== code) {
|
|
51
|
+
run = {code, ...(code === null ? {name:null} : groups.get(code)), columns:[]};
|
|
52
|
+
this.groups.push(run);
|
|
53
|
+
}
|
|
54
|
+
run.columns.push(column);
|
|
55
|
+
}
|
|
56
|
+
if (!this.columns.length) throw new TypeError('grid struct requires at least one column');
|
|
57
|
+
for (const group of this.groups) { Object.freeze(group.columns); Object.freeze(group); }
|
|
58
|
+
Object.freeze(this.columns); Object.freeze(this.groups); Object.freeze(this);
|
|
59
|
+
}
|
|
60
|
+
columnNode(field) { return this.#nodes.get(field); }
|
|
61
|
+
moveColumn(field, targetField, after = false) {
|
|
62
|
+
const node = this.#nodes.get(field), target = this.#nodes.get(targetField);
|
|
63
|
+
if (!node || !target || node === target) return;
|
|
64
|
+
const bag = node.parentBag, nodes = bag.getNodes();
|
|
65
|
+
const from = nodes.indexOf(node), targetIndex = nodes.indexOf(target);
|
|
66
|
+
const position = targetIndex + Number(after) - Number(from < targetIndex);
|
|
67
|
+
if (position !== from) bag.move(from, position);
|
|
68
|
+
}
|
|
69
|
+
setColumnWidth(field, width) {
|
|
70
|
+
if (!Number.isFinite(width) || width <= 0) throw new TypeError('grid resized width must be positive pixels');
|
|
71
|
+
this.#nodes.get(field).setAttr({width: `${Math.round(width)}px`});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function branch(bag, label) {
|
|
76
|
+
const node = bag.getNode(label);
|
|
77
|
+
if (!node || node.resolver || !(node.staticValue instanceof Bag)) throw new TypeError(`grid struct requires Bag ${label}`);
|
|
78
|
+
return node.staticValue;
|
|
79
|
+
}
|
|
80
|
+
function requireKeys(attrs, allowed) {
|
|
81
|
+
for (const key of Object.keys(attrs)) if (!allowed.has(key)) throw new TypeError(`Unsupported grid struct attribute: ${key}`);
|
|
82
|
+
}
|
|
83
|
+
function requireIdentity(value, label) {
|
|
84
|
+
if (typeof value !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(value)) throw new TypeError(`grid ${label} requires a simple field identifier`);
|
|
85
|
+
}
|