@gramlot/gramlot 0.3.30 → 0.3.32
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/charts/__init__.py +1 -1
- package/dist/components/charts/assets/collection.js +1 -1
- package/dist/components/charts/assets/index.js +1 -1
- package/dist/components/grid/__init__.py +2 -2
- package/dist/components/grid/assets/collection.js +36 -20
- package/dist/components/grid/assets/files/0a1df3a083d0c010560aa342e87959c8c1070e6fd54545741f083f22d0c8b551-fflate.txt +21 -0
- package/dist/components/grid/assets/files/8cf9b31947daf7adb4a7a00a037459e58241af050cd0a603bfcf1756e533f75e-write-excel-file.txt +21 -0
- package/dist/components/grid/assets/index.js +1 -1
- package/dist/components/grid/assets/modules/browser-LVJHKBBV.js +19 -0
- package/dist/components/grid/assets/modules/chunk-RMRBEYTI.js +1 -0
- package/dist/components/grid/commands.json +42 -1
- package/dist/components/grid/grammar.json +384 -2
- package/dist/components/vizabi/__init__.py +1 -1
- package/dist/components/vizabi/assets/collection.js +1 -1
- package/dist/components/vizabi/assets/index.js +1 -1
- package/dist/gramlot.js +376 -21
- package/dist/gramlot.min.js +46 -46
- package/dist/standard-collections.js +1 -1
- package/package.json +3 -2
- package/src/binding/grid-struct.js +4 -1
- package/src/binding/runtime.js +2 -1
- package/src/builder/gramlot-builder.js +96 -7
- package/src/builder/source.js +18 -0
- package/src/components/grid-footer.js +120 -0
- package/src/components/grid-formulas.js +403 -0
- package/src/components/grid-grammar.json +87 -2
- package/src/components/grid-selection.js +125 -0
- package/src/components/grid-structure.js +27 -4
- package/src/components/grid-styles.js +14 -1
- package/src/components/grid-tools.js +229 -0
- package/src/components/grid.js +174 -8
- package/src/components/licenses/fflate.txt +21 -0
- package/src/components/licenses/write-excel-file.txt +21 -0
- package/src/editing-session.d.ts +2 -0
- package/src/editing-session.js +35 -3
- package/src/stores/bag-collection-store.js +14 -5
- package/src/stores/bag-collection-view.js +1 -1
- package/themes/gramlot-base/theme.css +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.32",
|
|
4
4
|
"description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"prosemirror-state": "1.4.3",
|
|
55
55
|
"prosemirror-view": "1.42.6",
|
|
56
56
|
"uplot": "1.6.32",
|
|
57
|
-
"vega": "6.4.0"
|
|
57
|
+
"vega": "6.4.0",
|
|
58
|
+
"write-excel-file": "4.1.1"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
61
|
"c8": "10.1.3",
|
|
@@ -13,7 +13,10 @@ function structureData(source) {
|
|
|
13
13
|
node.builder.validateNode(node);
|
|
14
14
|
if (node.nodeTag !== 'column' || node.staticValue != null) throw new TypeError('grid column must be a scalar declaration');
|
|
15
15
|
const attrs = {tag:'cell'};
|
|
16
|
-
|
|
16
|
+
const keys = ['field', 'name', 'title', 'dtype', 'width', 'align', 'format', 'white_space', 'max_height', 'edit', 'check', 'checked', 'checkedField',
|
|
17
|
+
'formula', 'calculated', 'hidden', 'totalize', 'footer_value', 'footer_format',
|
|
18
|
+
...Object.keys(node.getAttr()).filter(key => key.startsWith('formula_'))];
|
|
19
|
+
for (const key of keys) {
|
|
17
20
|
if (node.getAttr(key) != null) attrs[key] = node.getAttr(key);
|
|
18
21
|
}
|
|
19
22
|
const label = `cell_${attrs.field}`;
|
package/src/binding/runtime.js
CHANGED
|
@@ -139,7 +139,8 @@ export class BindingRuntime {
|
|
|
139
139
|
const value = bag.node(segment)?.staticValue;
|
|
140
140
|
if (!(value instanceof Bag)) break;
|
|
141
141
|
const store = this.#collectionStores.get(value);
|
|
142
|
-
store?.receiveData(event, index + 1 < event.pathlist.length ? value.node(event.pathlist[index + 1]) : null
|
|
142
|
+
store?.receiveData(event, index + 1 < event.pathlist.length ? value.node(event.pathlist[index + 1]) : null,
|
|
143
|
+
event.pathlist[index + 2] ?? null);
|
|
143
144
|
bag = value;
|
|
144
145
|
}
|
|
145
146
|
this.router.deliver(event);
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import buttons from '../components/buttons.js';
|
|
8
8
|
import {ComponentCollection} from '../components/registry.js';
|
|
9
9
|
import {Bag} from '@genrojs/bag';
|
|
10
|
-
import {HtmlBuilder, SourceBagNode, sourceTarget} from '@genrojs/builders';
|
|
10
|
+
import {HtmlBuilder, META_ATTRS, SourceBagNode, sourceTarget} from '@genrojs/builders';
|
|
11
11
|
import {toTytx} from '@genrojs/tytx';
|
|
12
12
|
// Loading the Gramlot Source classes adds GramlotBuilderBag to the TYTX subtype
|
|
13
13
|
// dictionary of X, so every GramlotBuilder user can encode and decode it.
|
|
@@ -36,6 +36,36 @@ const DATA_PATH_ATTRIBUTES = {dataSetter: 'destination_path', dataFormula: 'resu
|
|
|
36
36
|
const DATA_INLINE_ATTRIBUTES = {dataFormula: 'formula', dataController: 'script'};
|
|
37
37
|
|
|
38
38
|
/** The P18 violation of a data-element's attributes, or null. */
|
|
39
|
+
// Builder's framework call keywords, accepted by every element.
|
|
40
|
+
const FRAMEWORK_ATTRIBUTES = new Set(['node_label', 'node_position', 'nodeId', 'target_id', 'datapath', 'iterate', 'store', '_tag', '_meta', 'ns']);
|
|
41
|
+
// Export formats of the grid:export command, in menu order, with their captions.
|
|
42
|
+
const EXPORT_FORMATS = {csv:'CSV', json:'JSON', tytx:'TYTX', xml:'XML', xlsx:'Excel'};
|
|
43
|
+
// frameGrid composes these grid attributes itself; its own parameters reach the grid too.
|
|
44
|
+
const FRAME_GRID_COMPOSED = new Set(['node_label', 'frameTarget', 'nodeId']);
|
|
45
|
+
const FRAME_GRID_PARAMETERS = new Set(['storepath', 'rowMode', 'structpath']);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An element whose `_meta.keywordPattern` is set accepts, besides its declared attributes, only the
|
|
49
|
+
* keywords matching that pattern (grid column `formula_<name>`); the error names the first other one.
|
|
50
|
+
*/
|
|
51
|
+
function keywordPatternError(spec, attrs) {
|
|
52
|
+
const pattern = spec?._meta?.keywordPattern;
|
|
53
|
+
if (!pattern) return null;
|
|
54
|
+
const declared = new Set(spec.attributes.parameters.filter(parameter => !parameter.kind.startsWith('var_')).map(parameter => parameter.name));
|
|
55
|
+
const unknown = Object.keys(attrs).find(name => !declared.has(name) && !FRAMEWORK_ATTRIBUTES.has(name) && !new RegExp(pattern).test(name));
|
|
56
|
+
return unknown === undefined ? null : `unknown attribute '${unknown}'`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** frameGrid `export`: true for every format, false for none, or a comma-separated list such as 'csv,json'. */
|
|
60
|
+
function frameGridExportFormats(exportRows) {
|
|
61
|
+
if (typeof exportRows === 'boolean') return exportRows ? Object.keys(EXPORT_FORMATS) : [];
|
|
62
|
+
if (typeof exportRows !== 'string' || !exportRows.trim()) throw new TypeError('frameGrid export requires a boolean or a list of formats');
|
|
63
|
+
const formats = exportRows.split(',').map(item => item.trim());
|
|
64
|
+
const unknown = formats.find(item => !Object.hasOwn(EXPORT_FORMATS, item));
|
|
65
|
+
if (unknown !== undefined) throw new TypeError(`frameGrid export format '${unknown}' is not one of ${Object.keys(EXPORT_FORMATS).join(', ')}`);
|
|
66
|
+
return formats;
|
|
67
|
+
}
|
|
68
|
+
|
|
39
69
|
function dataElementError(tag, attrs) {
|
|
40
70
|
if (['input', 'textarea', 'select', 'webcomponent'].includes(tag) && pointerTransform(attrs.value)) {
|
|
41
71
|
return "'value' does not accept pointer suffixes; use an ordinary value pointer";
|
|
@@ -80,7 +110,9 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
80
110
|
static { this.defineGrammar(buttons.toDocument()); }
|
|
81
111
|
|
|
82
112
|
/** Compose real Source nodes; frame/grid/menu retain their existing runtime owners. */
|
|
83
|
-
frameGrid(parent, {storepath, rowMode, structpath, searchOn = false, searchHelp = false, chart = false,
|
|
113
|
+
frameGrid(parent, {storepath, rowMode, structpath, searchOn = false, searchHelp = false, chart = false,
|
|
114
|
+
export: exportRows = false, printRows = false, advancedTools = false, addrow = false, addrow_defaults = null,
|
|
115
|
+
duprow = false, delrow = false, gridsemaphore = false, ...attrs} = {}) {
|
|
84
116
|
for (const tag of ['framePane', 'grid']) {
|
|
85
117
|
if (!this.schemaTag(tag)) throw new TypeError(`frameGrid requires the ${tag} collection`);
|
|
86
118
|
}
|
|
@@ -88,21 +120,54 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
88
120
|
for (const key of Object.keys(attrs)) if (key.startsWith('chart_')) {
|
|
89
121
|
chartOptions[key.slice(6)] = attrs[key]; delete attrs[key];
|
|
90
122
|
}
|
|
123
|
+
const [frameAttrs, routed] = this.frameGridRoute(attrs);
|
|
124
|
+
const twice = Object.keys(routed).find(name => ['storepath', 'rowMode'].includes(name) || name === 'structpath' && structpath != null);
|
|
125
|
+
if (twice !== undefined) throw new TypeError(`frameGrid: '${twice}' is given twice`);
|
|
91
126
|
if (chart !== false) {
|
|
92
127
|
if (chart !== true && (typeof chart !== 'string' || !chart)) throw new TypeError('chart requires a boolean or chart type');
|
|
93
|
-
if (!
|
|
128
|
+
if (!frameAttrs.datapath) throw new TypeError('A chart frameGrid requires its own datapath');
|
|
94
129
|
if (!this.schemaTag('chartPane')) throw new TypeError('frameGrid requires the chartPane collection');
|
|
95
130
|
if (typeof chart === 'string') chartOptions.chartType = chart;
|
|
96
131
|
} else if (Object.keys(chartOptions).length) throw new TypeError('chart_* options require chart');
|
|
132
|
+
const exportFormats = frameGridExportFormats(exportRows);
|
|
133
|
+
for (const [name, value] of Object.entries({printRows, advancedTools, addrow, duprow, delrow, gridsemaphore})) {
|
|
134
|
+
if (typeof value !== 'boolean') throw new TypeError(`frameGrid ${name} requires a boolean`);
|
|
135
|
+
}
|
|
136
|
+
if (addrow_defaults !== null && (!addrow || typeof addrow_defaults !== 'object' || Array.isArray(addrow_defaults))) {
|
|
137
|
+
throw new TypeError('addrow_defaults requires addrow and a mapping');
|
|
138
|
+
}
|
|
139
|
+
if (gridsemaphore) {
|
|
140
|
+
if (!frameAttrs.datapath) throw new TypeError('A gridsemaphore frameGrid requires its own datapath');
|
|
141
|
+
if (!this.schemaTag('editingStatus')) throw new TypeError('frameGrid requires the editingStatus collection');
|
|
142
|
+
frameAttrs.form = true;
|
|
143
|
+
}
|
|
97
144
|
const gridAttrs = {storepath, rowMode, ...(structpath == null ? {} : {structpath}),
|
|
98
|
-
node_label: 'grid', frameTarget: true, height: '100%', width: '100%'
|
|
145
|
+
node_label: 'grid', frameTarget: true, height: '100%', width: '100%',
|
|
146
|
+
// Legacy frameGrid default: the current row identity at .selectedId of the frame's own datapath.
|
|
147
|
+
...(frameAttrs.datapath ? {selectedId: '^.selectedId'} : {}), ...routed};
|
|
99
148
|
if (chart !== false) gridAttrs.nodeId = randomId();
|
|
100
149
|
this.validateNodeValues('grid', null, gridAttrs);
|
|
101
150
|
if (searchOn !== false) this.validateNodeValues('searchBox', null, {searchOn, searchHelp});
|
|
102
|
-
const frame = parent.framePane(
|
|
151
|
+
const frame = parent.framePane(frameAttrs);
|
|
103
152
|
frame.center.grid(gridAttrs);
|
|
104
153
|
const bar = frame.top.menu({presentation:'bar', node_label:'bar', aria_label:'Grid toolbar', class:'gramlot-grid-toolbar'});
|
|
105
154
|
if (searchOn !== false) bar.menuline({group:true, node_label:'search', class:'gramlot-grid-search'}).searchBox({searchOn, searchHelp});
|
|
155
|
+
if (addrow) bar.menuline({command:'grid:addrow', node_label:'addrow', ...(addrow_defaults === null ? {} : {parameters:{defaults:addrow_defaults}})});
|
|
156
|
+
if (duprow) bar.menuline({command:'grid:duprow', node_label:'duprow'});
|
|
157
|
+
if (delrow) bar.menuline({command:'grid:delrow', node_label:'delrow'});
|
|
158
|
+
const exportMenu = parent => {
|
|
159
|
+
const menu = parent.menuline({caption:'Export', icon:'⇩', labelPosition:'tooltip', node_label:'export'}).menu();
|
|
160
|
+
for (const name of exportFormats) menu.menuline({command:'grid:export', caption:EXPORT_FORMATS[name], parameters:{format:name}, node_label:name});
|
|
161
|
+
};
|
|
162
|
+
if (exportFormats.length) exportMenu(bar);
|
|
163
|
+
if (printRows) bar.menuline({command:'grid:printRows', node_label:'printRows'});
|
|
164
|
+
if (advancedTools) {
|
|
165
|
+
const tools = bar.menuline({caption:'Advanced tools', icon:'…', labelPosition:'tooltip', node_label:'advancedTools'}).menu();
|
|
166
|
+
tools.menuline({command:'grid:lineNumbers', node_label:'lineNumbers'});
|
|
167
|
+
if (exportFormats.length) exportMenu(tools);
|
|
168
|
+
if (printRows) tools.menuline({command:'grid:printRows', node_label:'printRows'});
|
|
169
|
+
}
|
|
170
|
+
if (gridsemaphore) bar.menuline({group:true, node_label:'semaphore'}).editingStatus();
|
|
106
171
|
if (chart !== false) {
|
|
107
172
|
bar.menuline({caption:'Chart', icon:'▥', labelPosition:'tooltip', node_label:'chart', action:"this.SET('._chart.open',true)"});
|
|
108
173
|
const window = frame.center.floatingPane({title:'Chart', open:'^._chart.open', width:'820px', height:'560px',
|
|
@@ -113,6 +178,29 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
113
178
|
return frame;
|
|
114
179
|
}
|
|
115
180
|
|
|
181
|
+
/** Split frameGrid keywords: unprefixed grid parameters go to the grid, `grid_`/`frame_` force any attribute. */
|
|
182
|
+
frameGridRoute(attrs) {
|
|
183
|
+
const declared = tag => new Set(this.schema[tag].attributes.parameters.filter(parameter => !parameter.kind.startsWith('var_')).map(parameter => parameter.name));
|
|
184
|
+
const source = new Set([...FRAMEWORK_ATTRIBUTES, ...META_ATTRS]);
|
|
185
|
+
const gridNames = declared('grid'), frameNames = declared('framePane');
|
|
186
|
+
const pattern = new RegExp(this.schema.grid._meta.parameterPattern);
|
|
187
|
+
// A name the frame also accepts stays on the frame unless forced with grid_.
|
|
188
|
+
const routed = name => (gridNames.has(name) || pattern.test(name)) && !frameNames.has(name) && !source.has(name)
|
|
189
|
+
&& !FRAME_GRID_COMPOSED.has(name) && !FRAME_GRID_PARAMETERS.has(name);
|
|
190
|
+
const frame = {}, grid = {};
|
|
191
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
192
|
+
let name, target;
|
|
193
|
+
if (key.startsWith('grid_')) {
|
|
194
|
+
name = key.slice('grid_'.length); target = grid;
|
|
195
|
+
if (FRAME_GRID_COMPOSED.has(name)) throw new TypeError(`frameGrid: '${key}' is composed by frameGrid`);
|
|
196
|
+
} else if (key.startsWith('frame_')) { name = key.slice('frame_'.length); target = frame; }
|
|
197
|
+
else { name = key; target = routed(key) ? grid : frame; }
|
|
198
|
+
if (Object.hasOwn(target, name)) throw new TypeError(`frameGrid: '${name}' is given twice`);
|
|
199
|
+
target[name] = value;
|
|
200
|
+
}
|
|
201
|
+
return [frame, grid];
|
|
202
|
+
}
|
|
203
|
+
|
|
116
204
|
/** Tag prefixes of the parent dialects, from the `_name` of the classes above GramlotBuilder. */
|
|
117
205
|
static get dialectPrefixes() {
|
|
118
206
|
const prefixes = [];
|
|
@@ -218,7 +306,7 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
218
306
|
if (tag === 'webcomponent') webcomponentCode(attrs);
|
|
219
307
|
const excluded = excludedAttribute(attrs);
|
|
220
308
|
if (excluded) throw new Error(`${tag}: attribute '${excluded}' is excluded from Gramlot`);
|
|
221
|
-
const error = dataElementError(tag, attrs);
|
|
309
|
+
const error = dataElementError(tag, attrs) ?? keywordPatternError(this.schema[tag], attrs);
|
|
222
310
|
if (error) throw new Error(`${tag}: ${error}`);
|
|
223
311
|
return super.validateNodeValues(tag, value, attrs);
|
|
224
312
|
}
|
|
@@ -231,7 +319,8 @@ export class GramlotBuilder extends HtmlBuilder {
|
|
|
231
319
|
if (node.nodeTag === 'webcomponent') webcomponentCode(options.attrs ?? node.getAttr());
|
|
232
320
|
const excluded = excludedAttribute(options.attrs ?? node.getAttr());
|
|
233
321
|
if (excluded) throw new Error(`${node.nodeTag} '${node.label}': attribute '${excluded}' is excluded from Gramlot`);
|
|
234
|
-
const error = dataElementError(node.nodeTag, options.attrs ?? node.getAttr())
|
|
322
|
+
const error = dataElementError(node.nodeTag, options.attrs ?? node.getAttr())
|
|
323
|
+
?? keywordPatternError(this.schema[node.nodeTag], options.attrs ?? node.getAttr());
|
|
235
324
|
if (error) throw new Error(`${node.nodeTag} '${node.label}': ${error}`);
|
|
236
325
|
return super.validateNode(node, options);
|
|
237
326
|
}
|
package/src/builder/source.js
CHANGED
|
@@ -177,6 +177,23 @@ function isExpression(value) {
|
|
|
177
177
|
* the null path rule (Q7): a read gives null, a write is an error. FIRE writes through
|
|
178
178
|
* setRelativeData too, with the Gramlot reset rules.
|
|
179
179
|
*/
|
|
180
|
+
/**
|
|
181
|
+
* The column formulas of a grid's nested struct, as `[struct.<field>.formula, text]`: the struct is
|
|
182
|
+
* consumed into Data at installation, so its formula texts are activated on the grid node.
|
|
183
|
+
*/
|
|
184
|
+
function structFormulaCode(grid) {
|
|
185
|
+
const items = [];
|
|
186
|
+
const visit = bag => {
|
|
187
|
+
for (const child of bag.getNodes()) {
|
|
188
|
+
if (child.nodeTag === 'column' && typeof child.getAttr('formula') === 'string') {
|
|
189
|
+
items.push([`struct.${child.getAttr('field')}.formula`, child.getAttr('formula')]);
|
|
190
|
+
} else if ((child._getMeta('gridStruct') || child.nodeTag === 'colgroup') && child.staticValue instanceof SourceBag) visit(child.staticValue);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
if (grid.staticValue instanceof SourceBag) visit(grid.staticValue);
|
|
194
|
+
return items;
|
|
195
|
+
}
|
|
196
|
+
|
|
180
197
|
export class GramlotBuilderBagNode extends SourceBagNode {
|
|
181
198
|
#formState = null;
|
|
182
199
|
#treeStore = null;
|
|
@@ -306,6 +323,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
306
323
|
}
|
|
307
324
|
}
|
|
308
325
|
if (isExpression(this.staticValue) || this.nodeTag === 'script' && typeof this.staticValue === 'string') items.push(['', this.staticValue]);
|
|
326
|
+
if (this._getMeta('gramlot_component')?.bagStruct) items.push(...structFormulaCode(this));
|
|
309
327
|
return items;
|
|
310
328
|
}
|
|
311
329
|
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {addNumeric, sameValue} from './grid-formulas.js';
|
|
2
|
+
import {cellText} from './grid-rows.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Grid footer row and column totals (intermediate implementation; the final placement of
|
|
6
|
+
* totals, data level or grid, is under review).
|
|
7
|
+
*
|
|
8
|
+
* A `totalize` column sums the field over every resident row of the store, never only the
|
|
9
|
+
* mounted or visible rows, and publishes the total to `.totalize.<field>` relative to the grid
|
|
10
|
+
* Source node, or to the Data path given as `totalize`. Booleans count as 0/1. Totals are
|
|
11
|
+
* published also when the footer row is not displayed, as in legacy.
|
|
12
|
+
* Legacy reference: genro_grid.js footer and totalize.
|
|
13
|
+
*/
|
|
14
|
+
export class GridFooter {
|
|
15
|
+
#source;
|
|
16
|
+
#format;
|
|
17
|
+
#anchor;
|
|
18
|
+
#frame;
|
|
19
|
+
#viewport;
|
|
20
|
+
#table;
|
|
21
|
+
#row;
|
|
22
|
+
#columns = [];
|
|
23
|
+
#caption = false;
|
|
24
|
+
#errors = new Map();
|
|
25
|
+
|
|
26
|
+
/** The footer frame is placed after `anchor` while the footer is displayed. */
|
|
27
|
+
constructor(source, format, anchor) {
|
|
28
|
+
this.#source = source;
|
|
29
|
+
this.#format = format;
|
|
30
|
+
this.#anchor = anchor;
|
|
31
|
+
const document = anchor.ownerDocument;
|
|
32
|
+
this.#frame = document.createElement('div');
|
|
33
|
+
this.#frame.className = 'footer-frame'; this.#frame.setAttribute('part', 'footer');
|
|
34
|
+
this.#viewport = document.createElement('div');
|
|
35
|
+
this.#viewport.className = 'footer-viewport';
|
|
36
|
+
this.#table = document.createElement('table');
|
|
37
|
+
this.#table.setAttribute('role', 'presentation');
|
|
38
|
+
const body = document.createElement('tfoot');
|
|
39
|
+
body.setAttribute('role', 'rowgroup');
|
|
40
|
+
this.#row = document.createElement('tr');
|
|
41
|
+
this.#row.setAttribute('role', 'row');
|
|
42
|
+
body.append(this.#row); this.#table.append(body);
|
|
43
|
+
this.#viewport.append(this.#table); this.#frame.append(this.#viewport);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get source() { return this.#source; }
|
|
47
|
+
get frame() { return this.#frame; }
|
|
48
|
+
get table() { return this.#table; }
|
|
49
|
+
get viewport() { return this.#viewport; }
|
|
50
|
+
/** The last error of each totalized column, by field name. */
|
|
51
|
+
get errors() { return new Map(this.#errors); }
|
|
52
|
+
/** The displayed cells, by field. */
|
|
53
|
+
cell(field) { return this.#row.querySelector(`td[data-field="${field}"]`); }
|
|
54
|
+
|
|
55
|
+
/** `footer` is false, true or a caption; `columns` are the displayed columns. */
|
|
56
|
+
configure(footer, columns, rowHeaders) {
|
|
57
|
+
this.#caption = footer;
|
|
58
|
+
this.#columns = columns;
|
|
59
|
+
if (footer === false) this.#frame.remove();
|
|
60
|
+
else this.#anchor.after(this.#frame);
|
|
61
|
+
const document = this.#row.ownerDocument;
|
|
62
|
+
const cells = [];
|
|
63
|
+
if (rowHeaders) {
|
|
64
|
+
const gutter = document.createElement('th');
|
|
65
|
+
gutter.className = 'row-heading'; gutter.setAttribute('role', 'rowheader');
|
|
66
|
+
cells.push(gutter);
|
|
67
|
+
}
|
|
68
|
+
for (const [index, column] of columns.entries()) {
|
|
69
|
+
const cell = document.createElement('td');
|
|
70
|
+
cell.dataset.field = column.field; cell.setAttribute('role', 'cell'); cell.setAttribute('part', 'footer-cell');
|
|
71
|
+
cell.setAttribute('aria-colindex', String(index + 1 + Number(rowHeaders)));
|
|
72
|
+
cell.style.textAlign = column.totalize ? 'end' : column.align;
|
|
73
|
+
cell.classList.toggle('numeric', Boolean(column.totalize));
|
|
74
|
+
cells.push(cell);
|
|
75
|
+
}
|
|
76
|
+
this.#row.replaceChildren(...cells);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Recompute totals over `rows`, read through `reader.value(row, field)`, publish them to Data
|
|
81
|
+
* and refresh the footer cells. A total that cannot be computed is published as null.
|
|
82
|
+
*/
|
|
83
|
+
update(reader, rows) {
|
|
84
|
+
for (const [index, column] of this.#columns.entries()) {
|
|
85
|
+
const cell = this.cell(column.field);
|
|
86
|
+
let text = column.footer_value ?? '', problem = null;
|
|
87
|
+
if (column.totalize) {
|
|
88
|
+
let total = 0;
|
|
89
|
+
try {
|
|
90
|
+
for (const row of rows) {
|
|
91
|
+
const value = reader.value(row, column.field);
|
|
92
|
+
total = addNumeric(total, typeof value === 'boolean' ? Number(value) : value);
|
|
93
|
+
}
|
|
94
|
+
this.#errors.delete(column.field);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
total = null;
|
|
97
|
+
this.#errors.set(column.field, error);
|
|
98
|
+
}
|
|
99
|
+
this.#publish(column, total);
|
|
100
|
+
if (total === null) { text = '—'; problem = this.#errors.get(column.field).message; }
|
|
101
|
+
else text = cellText(total, {...column, dtype:column.dtype === 'B' ? 'L' : column.dtype,
|
|
102
|
+
format:column.footer_format ?? column.format}, this.#format);
|
|
103
|
+
} else if (index === 0 && text === '') text = this.#caption === true ? 'Total' : this.#caption;
|
|
104
|
+
if (cell.textContent !== text) cell.textContent = text;
|
|
105
|
+
if (problem === null) { cell.removeAttribute('title'); cell.removeAttribute('aria-invalid'); }
|
|
106
|
+
else { cell.title = problem; cell.setAttribute('aria-invalid', 'true'); }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The absolute Data paths of the published totals. */
|
|
111
|
+
totalPaths() {
|
|
112
|
+
return this.#columns.filter(column => column.totalize).map(column =>
|
|
113
|
+
this.source.absDatapath(column.totalize === true ? `.totalize.${column.field}` : column.totalize));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#publish(column, total) {
|
|
117
|
+
const path = column.totalize === true ? `.totalize.${column.field}` : column.totalize;
|
|
118
|
+
if (!sameValue(this.source.getRelativeData(path), total)) this.source.setRelativeData(path, total);
|
|
119
|
+
}
|
|
120
|
+
}
|