@gramlot/gramlot 0.3.27 → 0.3.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/icons/chart.svg +1 -0
- package/dist/components/audio/__init__.py +13 -0
- package/dist/components/audio/assets/collection.js +3 -0
- package/dist/components/audio/assets/index.js +3 -0
- package/dist/components/audio/commands.json +7 -0
- package/dist/components/audio/grammar.json +335 -0
- package/dist/components/charts/__init__.py +13 -0
- package/dist/components/charts/assets/collection.js +47 -0
- package/dist/components/charts/assets/files/55ea01207028f76d844678511f29fc800f8a1e67a8a0fe80470128677847ad32-echarts-gl.txt +29 -0
- package/dist/components/charts/assets/files/63727832aaf62004a2b249c933e327f3c90caf49e41a72f4bf436edf632cbda8-vega.txt +27 -0
- package/dist/components/charts/assets/files/8c45d9eaf50c2f72dd9c7ab9ef440788ba58a3538484b5c51c1f17d5128752e7-plotly.txt +21 -0
- package/dist/components/charts/assets/files/8f989229699b4fe2f1a0432d0e9edc338a8a911e250e2d1b01ecd770a5f5b1bd-uplot.txt +21 -0
- package/dist/components/charts/assets/files/c8fa058cba98f7f288b5d8fa4116f441d6dacb25b96da859e518c973fff41d0c-claygl.txt +23 -0
- package/dist/components/charts/assets/index.js +3 -0
- package/dist/components/charts/assets/modules/chunk-2CCTLZE7.js +76 -0
- package/dist/components/charts/assets/modules/chunk-RMRBEYTI.js +1 -0
- package/dist/components/charts/assets/modules/echarts-gl-CXVTZA6G.js +4409 -0
- package/dist/components/charts/assets/modules/plotly.min-4LB7HE7P.js +3596 -0
- package/dist/components/charts/assets/modules/uPlot.esm-5JJNHHRF.js +3 -0
- package/dist/components/charts/assets/modules/uPlot.min-N3E3FA74.js +1 -0
- package/dist/components/charts/assets/modules/vega.module-2QCOVQ3A.js +13 -0
- package/dist/components/charts/commands.json +7 -0
- package/dist/components/charts/grammar.json +1113 -0
- package/dist/components/chat/__init__.py +1 -1
- package/dist/components/chat/assets/collection.js +3 -3
- package/dist/components/chat/assets/index.js +1 -1
- package/dist/components/drawing/__init__.py +15 -0
- package/dist/components/drawing/assets/collection.js +28 -0
- package/dist/components/drawing/assets/index.js +3 -0
- package/dist/components/drawing/commands.json +7 -0
- package/dist/components/drawing/grammar.json +110 -0
- package/dist/components/editors/__init__.py +2 -2
- package/dist/components/editors/assets/collection.js +18 -18
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/editors/assets/modules/{prose-mirror-engine-GMZPJPNN.js → prose-mirror-engine-DQEAEIMV.js} +2 -2
- package/dist/components/editors/grammar.json +150 -0
- package/dist/components/grid/__init__.py +2 -2
- package/dist/components/grid/assets/collection.js +71 -34
- 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 +650 -10
- package/dist/components/grouplets/__init__.py +1 -1
- package/dist/components/grouplets/assets/collection.js +3 -3
- package/dist/components/grouplets/assets/index.js +1 -1
- package/dist/components/inputs/__init__.py +3 -2
- package/dist/components/inputs/assets/collection.js +38 -23
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/inputs/grammar.json +1533 -321
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +27 -23
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/layout/grammar.json +85 -0
- package/dist/components/selection_trial/__init__.py +1 -1
- package/dist/components/selection_trial/assets/collection.js +3 -3
- package/dist/components/selection_trial/assets/index.js +1 -1
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +11 -11
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/components/tree/grammar.json +126 -0
- package/dist/components/typst/__init__.py +1 -1
- package/dist/components/typst/assets/collection.js +2 -2
- package/dist/components/typst/assets/index.js +1 -1
- package/dist/components/uploads/__init__.py +1 -1
- package/dist/components/uploads/assets/collection.js +8 -8
- package/dist/components/uploads/assets/index.js +1 -1
- package/dist/components/vizabi/__init__.py +13 -0
- package/dist/components/vizabi/assets/collection.js +22 -0
- package/dist/components/vizabi/assets/index.js +3 -0
- package/dist/components/vizabi/commands.json +7 -0
- package/dist/components/vizabi/grammar.json +128 -0
- package/dist/gramlot.js +944 -193
- package/dist/gramlot.min.js +85 -53
- package/dist/standard-collections.js +2 -0
- package/package.json +13 -4
- package/src/binding/grid-struct.js +5 -2
- package/src/binding/runtime.js +12 -1
- package/src/bootstrap.js +1 -1
- package/src/builder/gramlot-builder.d.ts +15 -2
- package/src/builder/gramlot-builder.js +116 -8
- package/src/builder/source.js +42 -1
- package/src/components/audio-analysis-session.js +65 -0
- package/src/components/audio.js +44 -0
- package/src/components/bases.js +6 -4
- package/src/components/border-container.js +6 -3
- package/src/components/buttons.js +47 -3
- package/src/components/chart-bar3d-options.js +30 -0
- package/src/components/chart-controls.js +83 -0
- package/src/components/chart-data-view.js +52 -0
- package/src/components/chart-data.js +42 -0
- package/src/components/chart-domains.js +46 -0
- package/src/components/chart-engines/echarts-options.js +50 -0
- package/src/components/chart-engines/echarts.js +82 -0
- package/src/components/chart-engines/index.js +22 -0
- package/src/components/chart-engines/plotly-map.js +34 -0
- package/src/components/chart-engines/plotly-spatial.js +35 -0
- package/src/components/chart-engines/plotly.js +138 -0
- package/src/components/chart-engines/uplot.js +57 -0
- package/src/components/chart-engines/vega.js +101 -0
- package/src/components/chart-fields.js +110 -0
- package/src/components/chart-geographic-options.js +47 -0
- package/src/components/chart-geojson.js +60 -0
- package/src/components/chart-geometry.js +11 -0
- package/src/components/chart-map-anchors.js +24 -0
- package/src/components/chart-map-bar3d-options.js +41 -0
- package/src/components/chart-map-data.js +25 -0
- package/src/components/chart-map-labels.js +8 -0
- package/src/components/chart-map-options.js +20 -0
- package/src/components/chart-pane.js +91 -0
- package/src/components/chart-renderer.js +58 -0
- package/src/components/chart-sample-view.js +32 -0
- package/src/components/chart-spatial-options.js +40 -0
- package/src/components/chart-time.js +84 -0
- package/src/components/chart-timeline.js +70 -0
- package/src/components/chart-types/area.js +2 -0
- package/src/components/chart-types/bar.js +2 -0
- package/src/components/chart-types/bar3D.js +2 -0
- package/src/components/chart-types/donut.js +2 -0
- package/src/components/chart-types/horizontalBar.js +2 -0
- package/src/components/chart-types/index.js +26 -0
- package/src/components/chart-types/line.js +2 -0
- package/src/components/chart-types/line3D.js +2 -0
- package/src/components/chart-types/lines3D.js +2 -0
- package/src/components/chart-types/map.js +2 -0
- package/src/components/chart-types/map3D.js +2 -0
- package/src/components/chart-types/mapBar3D.js +2 -0
- package/src/components/chart-types/pie.js +2 -0
- package/src/components/chart-types/polygons3D.js +2 -0
- package/src/components/chart-types/scatter.js +2 -0
- package/src/components/chart-types/scatter3D.js +2 -0
- package/src/components/chart-types/stackedArea.js +2 -0
- package/src/components/chart-types/stackedBar.js +2 -0
- package/src/components/chart-types/surface.js +2 -0
- package/src/components/charts.js +152 -0
- package/src/components/check-box-text-editor.js +88 -0
- package/src/components/check-box-text.js +193 -0
- package/src/components/check-box-tree.js +62 -0
- package/src/components/choice-editor.js +7 -5
- package/src/components/choice-field-styles.js +14 -0
- package/src/components/choices.js +5 -0
- package/src/components/drawing.js +199 -0
- package/src/components/grid-check-sets.js +98 -0
- package/src/components/grid-column-filters.js +198 -0
- package/src/components/grid-footer.js +120 -0
- package/src/components/grid-formulas.js +403 -0
- package/src/components/grid-grammar.json +131 -2
- package/src/components/grid-header-interaction.js +3 -1
- package/src/components/grid-rows.js +2 -2
- package/src/components/grid-search-box.js +38 -22
- package/src/components/grid-search-styles.js +13 -14
- package/src/components/grid-search.js +8 -1
- package/src/components/grid-selection.js +118 -0
- package/src/components/grid-structure.js +36 -5
- package/src/components/grid-styles.js +37 -1
- package/src/components/grid-tools.js +229 -0
- package/src/components/grid.js +223 -15
- package/src/components/image-uploader.js +2 -2
- package/src/components/inputs.js +19 -10
- package/src/components/labelbox.js +5 -1
- package/src/components/licenses/claygl.txt +23 -0
- package/src/components/licenses/echarts-gl.txt +29 -0
- package/src/components/licenses/fflate.txt +21 -0
- package/src/components/licenses/plotly.txt +21 -0
- package/src/components/licenses/uplot.txt +21 -0
- package/src/components/licenses/vega.txt +27 -0
- package/src/components/licenses/write-excel-file.txt +21 -0
- package/src/components/menus.js +19 -11
- package/src/components/toolbar-overflow.js +12 -8
- package/src/components/tree.js +30 -8
- package/src/components/typed-editor.js +14 -3
- package/src/components/vizabi-session.js +66 -0
- package/src/components/vizabi.js +66 -0
- package/src/components/window-surface.js +20 -9
- package/src/components/windows.js +1 -1
- package/src/editing-session.d.ts +2 -0
- package/src/editing-session.js +35 -3
- package/src/gramlot.d.ts +1 -0
- package/src/gramlot.js +2 -0
- package/src/handlers/device-handler.js +32 -32
- package/src/handlers/source-handler.d.ts +2 -2
- package/src/ide-documents.js +19 -3
- package/src/ide-runtime.d.ts +1 -0
- package/src/ide-runtime.js +45 -31
- package/src/index.d.ts +1 -0
- package/src/renderer/drag-drop.js +1 -0
- package/src/renderer/gramlot-renderer.d.ts +2 -0
- package/src/renderer/gramlot-renderer.js +5 -4
- package/src/server/assets.d.ts +3 -0
- package/src/server/assets.js +9 -0
- package/src/server/gramlot-server.d.ts +1 -1
- package/src/server/gramlot-server.js +2 -1
- package/src/server/index.d.ts +1 -1
- package/src/server/index.js +1 -1
- package/src/server/visual-editor.js +17 -17
- package/src/stores/bag-collection-store.js +14 -5
- package/src/stores/bag-collection-view.js +80 -18
- package/src/stores/collection-filter.js +2 -1
- package/src/stores/column-filter.js +49 -0
- package/src/stores/id-set.js +10 -0
- package/src/view/controls.js +8 -0
- package/src/view/html.js +10 -1
- package/src/view/label-menu.js +61 -0
- package/src/view/labelbox.js +15 -2
- package/themes/gramlot-base/theme.css +6 -0
- package/tools/gramlot-tools-inspector.js +169 -505
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import {getDecimalLibrary, isDecimal} from '@genrojs/tytx';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Grid row formulas and footer arithmetic (intermediate implementation; the final placement
|
|
5
|
+
* of formulas and totals, data level or grid, is under review).
|
|
6
|
+
*
|
|
7
|
+
* A data formula computes its field for each resident row and writes the result into the row
|
|
8
|
+
* Bag, so every view on the same store sees it. The row index `#` (1-based), the running sum
|
|
9
|
+
* `+=field` and every formula depending on them are view values: each grid computes them on the
|
|
10
|
+
* visible rows of its own view, in view order, and never writes them.
|
|
11
|
+
* Legacy reference: gnr.GridChangeManager.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const SPECIAL_INDEX = '#';
|
|
15
|
+
const IDENTIFIER = /[A-Za-z_$][\w$]*/g;
|
|
16
|
+
|
|
17
|
+
/** The formula definitions of a grid structure, their dependency order, triggers and view fields. */
|
|
18
|
+
export function formulaDefinitions(columns) {
|
|
19
|
+
const entries = new Map();
|
|
20
|
+
for (const column of columns) {
|
|
21
|
+
if (column.formula == null) continue;
|
|
22
|
+
const formula = column.formula.trim();
|
|
23
|
+
const special = formula === SPECIAL_INDEX ? {kind:'index', field:null}
|
|
24
|
+
: formula.startsWith('+=') ? {kind:'running', field:formula.slice(2).trim()}
|
|
25
|
+
: formula.startsWith('%=') ? {kind:'percent', field:formula.slice(2).trim()}
|
|
26
|
+
: null;
|
|
27
|
+
if (special && special.kind !== 'index' && !/^[A-Za-z_][A-Za-z0-9_]*$/.test(special.field)) {
|
|
28
|
+
throw new TypeError(`grid formula '${formula}' requires a field`);
|
|
29
|
+
}
|
|
30
|
+
const parameters = new Map();
|
|
31
|
+
for (const [name, raw] of Object.entries(column)) {
|
|
32
|
+
if (name.startsWith('formula_')) parameters.set(name.slice('formula_'.length), raw);
|
|
33
|
+
}
|
|
34
|
+
let arithmetic = null;
|
|
35
|
+
if (!special) {
|
|
36
|
+
try { arithmetic = parseArithmetic(formula); } catch { /* outside the arithmetic grammar: inline code */ }
|
|
37
|
+
}
|
|
38
|
+
const references = special ? new Set(special.field ? [special.field] : []) : new Set(formula.match(IDENTIFIER) ?? []);
|
|
39
|
+
entries.set(column.field, {field:column.field, formula:column.formula, special, arithmetic,
|
|
40
|
+
parameters, references, calculated:column.calculated === true, column});
|
|
41
|
+
}
|
|
42
|
+
const triggered = new Map();
|
|
43
|
+
for (const entry of entries.values()) {
|
|
44
|
+
for (const dependency of entry.references) {
|
|
45
|
+
if (!triggered.has(dependency)) triggered.set(dependency, new Set());
|
|
46
|
+
triggered.get(dependency).add(entry.field);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const visiting = new Set(), visited = new Set(), order = [];
|
|
50
|
+
const visit = field => {
|
|
51
|
+
if (visited.has(field)) return;
|
|
52
|
+
if (visiting.has(field)) throw new TypeError(`grid formula dependency cycle at '${field}'`);
|
|
53
|
+
visiting.add(field);
|
|
54
|
+
for (const dependency of entries.get(field).references) if (entries.has(dependency)) visit(dependency);
|
|
55
|
+
visiting.delete(field); visited.add(field); order.push(field);
|
|
56
|
+
};
|
|
57
|
+
for (const field of entries.keys()) visit(field);
|
|
58
|
+
// Dependencies come first in `order`: a formula is a view value when it is `#`/`+=` or depends on one.
|
|
59
|
+
const viewFields = new Set();
|
|
60
|
+
for (const field of order) {
|
|
61
|
+
const entry = entries.get(field);
|
|
62
|
+
if (['index', 'running'].includes(entry.special?.kind) || [...entry.references].some(name => viewFields.has(name))) viewFields.add(field);
|
|
63
|
+
}
|
|
64
|
+
const definitions = new Map([...entries].map(([field, entry]) => [field, Object.freeze({...entry, view:viewFields.has(field)})]));
|
|
65
|
+
return Object.freeze({definitions, triggered, order:Object.freeze(order), viewFields});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Bounded arithmetic grammar: numbers, names, + - * / % ** and parentheses. */
|
|
69
|
+
export function parseArithmetic(code) {
|
|
70
|
+
const tokens = tokenize(code);
|
|
71
|
+
let position = 0;
|
|
72
|
+
const peek = () => tokens[position];
|
|
73
|
+
const take = value => {
|
|
74
|
+
if (peek().value !== value) throw new SyntaxError(`expected '${value}' in arithmetic formula`);
|
|
75
|
+
return tokens[position++];
|
|
76
|
+
};
|
|
77
|
+
const precedence = {'+':1, '-':1, '*':2, '/':2, '%':2, '**':3};
|
|
78
|
+
const primary = () => {
|
|
79
|
+
const token = peek();
|
|
80
|
+
if (token.type === 'number') { position++; return {kind:'number', value:token.value}; }
|
|
81
|
+
if (token.type === 'name') { position++; return {kind:'name', value:token.value}; }
|
|
82
|
+
if (token.value === '(') { position++; const node = expression(0); take(')'); return node; }
|
|
83
|
+
if (token.value === '+' || token.value === '-') { position++; return {kind:'unary', operator:token.value, child:primary()}; }
|
|
84
|
+
throw new SyntaxError(`unexpected '${token.value || 'end'}' in arithmetic formula`);
|
|
85
|
+
};
|
|
86
|
+
const expression = minimum => {
|
|
87
|
+
let left = primary();
|
|
88
|
+
while (precedence[peek().value] >= minimum) {
|
|
89
|
+
const operator = tokens[position++].value;
|
|
90
|
+
const right = expression(precedence[operator] + (operator === '**' ? 0 : 1));
|
|
91
|
+
left = {kind:'binary', operator, left, right};
|
|
92
|
+
}
|
|
93
|
+
return left;
|
|
94
|
+
};
|
|
95
|
+
const tree = expression(0);
|
|
96
|
+
if (peek().type !== 'eof') throw new SyntaxError(`unexpected '${peek().value}' in arithmetic formula`);
|
|
97
|
+
return tree;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function tokenize(code) {
|
|
101
|
+
const result = [];
|
|
102
|
+
let offset = 0;
|
|
103
|
+
while (offset < code.length) {
|
|
104
|
+
const rest = code.slice(offset);
|
|
105
|
+
const whitespace = /^\s+/.exec(rest);
|
|
106
|
+
if (whitespace) { offset += whitespace[0].length; continue; }
|
|
107
|
+
const number = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(rest);
|
|
108
|
+
if (number) { result.push({type:'number', value:number[0]}); offset += number[0].length; continue; }
|
|
109
|
+
const name = /^[A-Za-z_$][\w$]*/.exec(rest);
|
|
110
|
+
if (name) { result.push({type:'name', value:name[0]}); offset += name[0].length; continue; }
|
|
111
|
+
const operator = /^(?:\*\*|[()+\-*/%])/.exec(rest);
|
|
112
|
+
if (operator) { result.push({type:'operator', value:operator[0]}); offset += operator[0].length; continue; }
|
|
113
|
+
throw new SyntaxError(`unsupported arithmetic formula token at ${offset}`);
|
|
114
|
+
}
|
|
115
|
+
result.push({type:'eof', value:''});
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function requireDecimalBackend() {
|
|
120
|
+
if (getDecimalLibrary() === 'number') throw new Error('A Decimal backend is required for exact grid formulas');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function decimalValue(value, Decimal) {
|
|
124
|
+
if (isDecimal(value)) return value;
|
|
125
|
+
if (value == null || value === '') return null;
|
|
126
|
+
if (typeof value !== 'number' && typeof value !== 'string') throw new TypeError('Decimal arithmetic requires numeric values');
|
|
127
|
+
return new Decimal(String(value));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** JavaScript operators on plain values. */
|
|
131
|
+
const OPERATORS = {'+':(a, b) => a + b, '-':(a, b) => a - b, '*':(a, b) => a * b, '/':(a, b) => a / b,
|
|
132
|
+
'%':(a, b) => a % b, '**':(a, b) => a ** b};
|
|
133
|
+
|
|
134
|
+
function evaluateNumber(node, scope) {
|
|
135
|
+
if (node.kind === 'number') return Number(node.value);
|
|
136
|
+
if (node.kind === 'name') {
|
|
137
|
+
if (!Object.hasOwn(scope, node.value)) throw new ReferenceError(`${node.value} is not defined`);
|
|
138
|
+
return scope[node.value];
|
|
139
|
+
}
|
|
140
|
+
if (node.kind === 'unary') return node.operator === '-' ? -evaluateNumber(node.child, scope) : +evaluateNumber(node.child, scope);
|
|
141
|
+
return OPERATORS[node.operator](evaluateNumber(node.left, scope), evaluateNumber(node.right, scope));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function evaluateDecimal(node, scope, Decimal) {
|
|
145
|
+
if (node.kind === 'number') return new Decimal(node.value);
|
|
146
|
+
if (node.kind === 'name') {
|
|
147
|
+
if (!Object.hasOwn(scope, node.value)) throw new ReferenceError(`${node.value} is not defined`);
|
|
148
|
+
return decimalValue(scope[node.value], Decimal);
|
|
149
|
+
}
|
|
150
|
+
if (node.kind === 'unary') {
|
|
151
|
+
const value = evaluateDecimal(node.child, scope, Decimal);
|
|
152
|
+
if (value == null) return null;
|
|
153
|
+
return node.operator === '-' ? value.neg() : value;
|
|
154
|
+
}
|
|
155
|
+
const left = evaluateDecimal(node.left, scope, Decimal);
|
|
156
|
+
const right = evaluateDecimal(node.right, scope, Decimal);
|
|
157
|
+
if (left == null || right == null) return null;
|
|
158
|
+
if (node.operator === '**') return left.pow(Number(right.toString()));
|
|
159
|
+
const method = ({'+':'plus', '-':'minus', '*':'times', '/':'div', '%':'mod'})[node.operator];
|
|
160
|
+
return left[method](right);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Equality of two computed values: identity, or Decimal value equality. */
|
|
164
|
+
export function sameValue(left, right) {
|
|
165
|
+
if (Object.is(left, right)) return true;
|
|
166
|
+
return isDecimal(left) && isDecimal(right) && left.eq(right);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** `left + right` for aggregates; an empty right operand leaves `left` unchanged. */
|
|
170
|
+
export function addNumeric(left, right) {
|
|
171
|
+
if (right == null || right === '') return left;
|
|
172
|
+
if (isDecimal(left) || isDecimal(right)) {
|
|
173
|
+
requireDecimalBackend();
|
|
174
|
+
const Decimal = isDecimal(left) ? left.constructor : right.constructor;
|
|
175
|
+
return decimalValue(left, Decimal).plus(decimalValue(right, Decimal));
|
|
176
|
+
}
|
|
177
|
+
if (typeof right !== 'number' || !Number.isFinite(right)) throw new TypeError('Grid aggregate formulas require numeric values');
|
|
178
|
+
return left + right;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function dividePercent(value, total) {
|
|
182
|
+
const zero = isDecimal(total) ? total.eq(0) : Object.is(total, 0);
|
|
183
|
+
if (value == null || value === '' || total == null || zero) return null;
|
|
184
|
+
if (isDecimal(value) || isDecimal(total)) {
|
|
185
|
+
const Decimal = isDecimal(value) ? value.constructor : total.constructor;
|
|
186
|
+
return decimalValue(value, Decimal).div(decimalValue(total, Decimal)).times(new Decimal(100));
|
|
187
|
+
}
|
|
188
|
+
if (typeof value !== 'number' || typeof total !== 'number') throw new TypeError('Percentage formulas require numeric values');
|
|
189
|
+
return value / total * 100;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Formula lifecycle of one grid: store and view subscriptions, `^` parameter registrations,
|
|
194
|
+
* recalculation of data formulas in dependency order with writes into the row Bags, and the
|
|
195
|
+
* view values of this grid.
|
|
196
|
+
*
|
|
197
|
+
* Data results are written only when they differ from the stored value. Writes made by this
|
|
198
|
+
* grid do not trigger it again; another grid on the same store recomputes equal values and
|
|
199
|
+
* writes nothing. Editing drafts never reach the row Bag, so only committed values recalculate.
|
|
200
|
+
*/
|
|
201
|
+
export class GridFormulas {
|
|
202
|
+
#source;
|
|
203
|
+
#formulas = formulaDefinitions([]);
|
|
204
|
+
#fields = new Set();
|
|
205
|
+
#store = null;
|
|
206
|
+
#view = null;
|
|
207
|
+
#release = [];
|
|
208
|
+
#registrations = [];
|
|
209
|
+
#running = false;
|
|
210
|
+
#errors = new Map();
|
|
211
|
+
#viewValues = null;
|
|
212
|
+
|
|
213
|
+
constructor(source) {
|
|
214
|
+
this.#source = source;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get source() { return this.#source; }
|
|
218
|
+
get runtime() { return this.source.rootBuilder.binding; }
|
|
219
|
+
/** The last error of each formula field, by field name. */
|
|
220
|
+
get errors() { return new Map(this.#errors); }
|
|
221
|
+
get formulas() { return this.#formulas; }
|
|
222
|
+
/** The data formula fields: written into the row Bag. */
|
|
223
|
+
get dataFields() { return new Set(this.formulas.order.filter(field => !this.viewFields.has(field))); }
|
|
224
|
+
/** The view-value fields: computed per grid, never written. */
|
|
225
|
+
get viewFields() { return this.formulas.viewFields; }
|
|
226
|
+
|
|
227
|
+
/** Adopt `structure` over `view`/`store`; a new structure or store runs the calculated data formulas. */
|
|
228
|
+
configure(structure, view, store) {
|
|
229
|
+
if (structure.formulas === this.#formulas && store === this.#store && view === this.#view && this.#release.length) return;
|
|
230
|
+
this.disconnect();
|
|
231
|
+
this.#formulas = structure.formulas;
|
|
232
|
+
this.#fields = new Set(structure.fields);
|
|
233
|
+
this.#store = store; this.#view = view;
|
|
234
|
+
this.#errors.clear();
|
|
235
|
+
if (!this.formulas.definitions.size) return;
|
|
236
|
+
this.#release = [store.subscribe(change => this.#storeChanged(change)),
|
|
237
|
+
view.subscribe(() => { this.#viewValues = null; })];
|
|
238
|
+
for (const definition of this.formulas.definitions.values()) {
|
|
239
|
+
for (const raw of definition.parameters.values()) {
|
|
240
|
+
if (typeof raw !== 'string' || !raw.startsWith('^')) continue;
|
|
241
|
+
const [path, attr = null] = this.source.absDatapath(raw).split('?');
|
|
242
|
+
this.#registrations.push(this.runtime.router.register({path, attr, recipient:{receive:() => {
|
|
243
|
+
this.#viewValues = null;
|
|
244
|
+
this.recalculate(this.#closure([definition.field]));
|
|
245
|
+
}}}));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
this.recalculate(this.#closure(this.#selected(definition => definition.calculated)));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
disconnect() {
|
|
252
|
+
for (const release of this.#release) release();
|
|
253
|
+
for (const registration of this.#registrations) registration.close();
|
|
254
|
+
this.#release = []; this.#registrations = [];
|
|
255
|
+
this.#store = null; this.#view = null; this.#viewValues = null;
|
|
256
|
+
this.#formulas = formulaDefinitions([]);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** The value shown by this grid: a view value, or the stored field. */
|
|
260
|
+
value(node, field) {
|
|
261
|
+
if (!this.viewFields.has(field)) return this.#store.value(node, field);
|
|
262
|
+
// A row filtered out of the view has no view value.
|
|
263
|
+
const values = this.#computeViewValues().get(node);
|
|
264
|
+
return values === undefined ? null : values[field];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
#selected(predicate) {
|
|
268
|
+
return [...this.formulas.definitions.values()].filter(predicate).map(definition => definition.field);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#closure(fields) {
|
|
272
|
+
const result = new Set(fields);
|
|
273
|
+
const pending = [...fields];
|
|
274
|
+
while (pending.length) {
|
|
275
|
+
for (const dependent of this.formulas.triggered.get(pending.shift()) ?? []) {
|
|
276
|
+
if (!result.has(dependent)) { result.add(dependent); pending.push(dependent); }
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
#storeChanged({full, rows, event, field}) {
|
|
283
|
+
this.#viewValues = null;
|
|
284
|
+
if (this.#running) return;
|
|
285
|
+
if (full) {
|
|
286
|
+
this.recalculate(this.#closure(this.#selected(definition => definition.calculated || definition.special)));
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const [row] = rows;
|
|
290
|
+
let fields;
|
|
291
|
+
if (field !== null) fields = [field];
|
|
292
|
+
else if (event.node === row) {
|
|
293
|
+
fields = event.evt === 'upd_attrs' ? Object.keys(event.attrs_diff) : [...this.formulas.triggered.keys()];
|
|
294
|
+
} else fields = (Array.isArray(event.node) ? event.node : [event.node]).map(node => node.label);
|
|
295
|
+
const affected = new Set();
|
|
296
|
+
for (const name of fields) for (const formula of this.formulas.triggered.get(name) ?? []) affected.add(formula);
|
|
297
|
+
const closure = this.#closure(affected);
|
|
298
|
+
const global = [...closure].some(name => this.formulas.definitions.get(name).special?.kind === 'percent');
|
|
299
|
+
this.recalculate(closure, global ? null : [row]);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
#parameters(definition) {
|
|
303
|
+
const result = {};
|
|
304
|
+
for (const [name, raw] of definition.parameters) {
|
|
305
|
+
result[name] = typeof raw === 'string' && (raw.startsWith('^') || raw.startsWith('='))
|
|
306
|
+
? this.source.getRelativeData(raw) : raw;
|
|
307
|
+
}
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** One formula on one row; `read(row, field)` gives the field values it may reference. */
|
|
312
|
+
#evaluate(definition, row, index, aggregate, read) {
|
|
313
|
+
if (definition.special?.kind === 'index') return index;
|
|
314
|
+
if (definition.special?.kind === 'running') return addNumeric(aggregate, read(row, definition.special.field));
|
|
315
|
+
if (definition.special?.kind === 'percent') return dividePercent(read(row, definition.special.field), aggregate);
|
|
316
|
+
const parameters = this.#parameters(definition);
|
|
317
|
+
const scope = {};
|
|
318
|
+
for (const name of definition.references) {
|
|
319
|
+
if (Object.hasOwn(parameters, name)) scope[name] = parameters[name];
|
|
320
|
+
else if (this.#fields.has(name) || this.#rowFields(row).has(name)) scope[name] = read(row, name) ?? null;
|
|
321
|
+
}
|
|
322
|
+
if (Object.values(scope).some(value => value == null)) return null;
|
|
323
|
+
const decimal = Object.values(scope).find(isDecimal);
|
|
324
|
+
if (definition.arithmetic) {
|
|
325
|
+
if (decimal === undefined) return evaluateNumber(definition.arithmetic, scope);
|
|
326
|
+
requireDecimalBackend();
|
|
327
|
+
return evaluateDecimal(definition.arithmetic, scope, decimal.constructor);
|
|
328
|
+
}
|
|
329
|
+
if (decimal !== undefined) throw new Error('Decimal grid formulas support arithmetic expressions only');
|
|
330
|
+
const names = [...Object.keys(scope), '_currcell', '_rowNode'];
|
|
331
|
+
// Inline code rule: only a formula text received with the grid's Source is compiled.
|
|
332
|
+
const run = this.runtime.inlineCompiler.compile(this.source, `struct.${definition.field}.formula`,
|
|
333
|
+
definition.formula, names, `return (${definition.formula}\n);`);
|
|
334
|
+
return run({...scope, _currcell:definition.column, _rowNode:row});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
#rowFields(row) {
|
|
338
|
+
if (this.#store.rowMode === 'attributes') return new Set(Object.keys(row.getAttr()));
|
|
339
|
+
return new Set(row.staticValue.keys());
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
#write(row, field, value) {
|
|
343
|
+
if (sameValue(this.#store.value(row, field), value)) return;
|
|
344
|
+
if (this.#store.rowMode === 'attributes') row.setAttr({[field]:value});
|
|
345
|
+
else row.staticValue.setItem(field, value);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Recalculate the data formulas among `fields`, on `rows` or on every resident row. */
|
|
349
|
+
recalculate(fields, rows = null) {
|
|
350
|
+
if (this.#running || !fields.size || this.#store === null) return;
|
|
351
|
+
this.#running = true;
|
|
352
|
+
const read = (row, field) => this.#store.value(row, field);
|
|
353
|
+
try {
|
|
354
|
+
const resident = this.#store.getRows();
|
|
355
|
+
for (const field of this.formulas.order) {
|
|
356
|
+
const definition = this.formulas.definitions.get(field);
|
|
357
|
+
if (!fields.has(field) || definition.view) continue;
|
|
358
|
+
let aggregate = null, failure = null;
|
|
359
|
+
if (definition.special?.kind === 'percent') {
|
|
360
|
+
try {
|
|
361
|
+
aggregate = 0;
|
|
362
|
+
for (const row of resident) aggregate = addNumeric(aggregate, read(row, definition.special.field));
|
|
363
|
+
} catch (error) { aggregate = null; failure = error; }
|
|
364
|
+
}
|
|
365
|
+
for (const row of rows ?? resident) {
|
|
366
|
+
let value = null;
|
|
367
|
+
try { value = this.#evaluate(definition, row, null, aggregate, read); }
|
|
368
|
+
catch (error) { failure = error; }
|
|
369
|
+
this.#write(row, field, value);
|
|
370
|
+
}
|
|
371
|
+
if (failure) this.#errors.set(field, failure);
|
|
372
|
+
else this.#errors.delete(field);
|
|
373
|
+
}
|
|
374
|
+
} finally {
|
|
375
|
+
this.#running = false;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** View values of the visible rows in this grid's view order, cached until the next store or view change. */
|
|
380
|
+
#computeViewValues() {
|
|
381
|
+
if (this.#viewValues !== null) return this.#viewValues;
|
|
382
|
+
const ordered = this.#view.getRows();
|
|
383
|
+
const values = new Map(ordered.map(row => [row, {}]));
|
|
384
|
+
const read = (row, field) => this.viewFields.has(field) ? values.get(row)[field] : this.#store.value(row, field);
|
|
385
|
+
for (const field of this.formulas.order) {
|
|
386
|
+
const definition = this.formulas.definitions.get(field);
|
|
387
|
+
if (!definition.view) continue;
|
|
388
|
+
let aggregate = 0, failure = null;
|
|
389
|
+
for (const [index, row] of ordered.entries()) {
|
|
390
|
+
let value = null;
|
|
391
|
+
try {
|
|
392
|
+
value = this.#evaluate(definition, row, index + 1, aggregate, read);
|
|
393
|
+
if (definition.special?.kind === 'running') aggregate = value;
|
|
394
|
+
} catch (error) { failure = error; }
|
|
395
|
+
values.get(row)[field] = value;
|
|
396
|
+
}
|
|
397
|
+
if (failure) this.#errors.set(field, failure);
|
|
398
|
+
else this.#errors.delete(field);
|
|
399
|
+
}
|
|
400
|
+
this.#viewValues = values;
|
|
401
|
+
return values;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
@@ -195,13 +195,142 @@
|
|
|
195
195
|
},
|
|
196
196
|
"has_default": true,
|
|
197
197
|
"default": false
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
"name": "check",
|
|
201
|
+
"kind": "keyword_only",
|
|
202
|
+
"role": "attribute",
|
|
203
|
+
"annotation": {
|
|
204
|
+
"kind": "dtype",
|
|
205
|
+
"dtype": "T"
|
|
206
|
+
},
|
|
207
|
+
"has_default": true,
|
|
208
|
+
"default": null
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"name": "checked",
|
|
212
|
+
"kind": "keyword_only",
|
|
213
|
+
"role": "attribute",
|
|
214
|
+
"annotation": {
|
|
215
|
+
"kind": "dtype",
|
|
216
|
+
"dtype": "T"
|
|
217
|
+
},
|
|
218
|
+
"has_default": true,
|
|
219
|
+
"default": null
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
"name": "checkedField",
|
|
223
|
+
"kind": "keyword_only",
|
|
224
|
+
"role": "attribute",
|
|
225
|
+
"annotation": {
|
|
226
|
+
"kind": "dtype",
|
|
227
|
+
"dtype": "T"
|
|
228
|
+
},
|
|
229
|
+
"has_default": true,
|
|
230
|
+
"default": null
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
"name": "title",
|
|
234
|
+
"kind": "keyword_only",
|
|
235
|
+
"role": "attribute",
|
|
236
|
+
"annotation": {
|
|
237
|
+
"kind": "dtype",
|
|
238
|
+
"dtype": "T"
|
|
239
|
+
},
|
|
240
|
+
"has_default": true,
|
|
241
|
+
"default": null
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
"name": "formula",
|
|
245
|
+
"kind": "keyword_only",
|
|
246
|
+
"role": "attribute",
|
|
247
|
+
"annotation": {
|
|
248
|
+
"kind": "dtype",
|
|
249
|
+
"dtype": "T"
|
|
250
|
+
},
|
|
251
|
+
"has_default": true,
|
|
252
|
+
"default": null
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"name": "calculated",
|
|
256
|
+
"kind": "keyword_only",
|
|
257
|
+
"role": "attribute",
|
|
258
|
+
"annotation": {
|
|
259
|
+
"kind": "dtype",
|
|
260
|
+
"dtype": "B"
|
|
261
|
+
},
|
|
262
|
+
"has_default": true,
|
|
263
|
+
"default": null
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
"name": "hidden",
|
|
267
|
+
"kind": "keyword_only",
|
|
268
|
+
"role": "attribute",
|
|
269
|
+
"annotation": {
|
|
270
|
+
"kind": "dtype",
|
|
271
|
+
"dtype": "B"
|
|
272
|
+
},
|
|
273
|
+
"has_default": true,
|
|
274
|
+
"default": null
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"name": "totalize",
|
|
278
|
+
"kind": "keyword_only",
|
|
279
|
+
"role": "attribute",
|
|
280
|
+
"annotation": {
|
|
281
|
+
"kind": "union",
|
|
282
|
+
"items": [
|
|
283
|
+
{
|
|
284
|
+
"kind": "dtype",
|
|
285
|
+
"dtype": "B"
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
"kind": "dtype",
|
|
289
|
+
"dtype": "T"
|
|
290
|
+
}
|
|
291
|
+
]
|
|
292
|
+
},
|
|
293
|
+
"has_default": true,
|
|
294
|
+
"default": null
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
"name": "footer_value",
|
|
298
|
+
"kind": "keyword_only",
|
|
299
|
+
"role": "attribute",
|
|
300
|
+
"annotation": {
|
|
301
|
+
"kind": "dtype",
|
|
302
|
+
"dtype": "T"
|
|
303
|
+
},
|
|
304
|
+
"has_default": true,
|
|
305
|
+
"default": null
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
"name": "footer_format",
|
|
309
|
+
"kind": "keyword_only",
|
|
310
|
+
"role": "attribute",
|
|
311
|
+
"annotation": {
|
|
312
|
+
"kind": "dtype",
|
|
313
|
+
"dtype": "T"
|
|
314
|
+
},
|
|
315
|
+
"has_default": true,
|
|
316
|
+
"default": null
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
"name": "attr",
|
|
320
|
+
"kind": "var_keyword",
|
|
321
|
+
"role": "attribute",
|
|
322
|
+
"annotation": {
|
|
323
|
+
"kind": "any"
|
|
324
|
+
},
|
|
325
|
+
"has_default": false
|
|
198
326
|
}
|
|
199
327
|
],
|
|
200
|
-
"accepts_var_keyword":
|
|
328
|
+
"accepts_var_keyword": true,
|
|
201
329
|
"accepts_var_positional": false
|
|
202
330
|
},
|
|
203
331
|
"_meta": {
|
|
204
|
-
"data_element": true
|
|
332
|
+
"data_element": true,
|
|
333
|
+
"keywordPattern": "^formula_[A-Za-z_$][A-Za-z0-9_$]*$"
|
|
205
334
|
}
|
|
206
335
|
},
|
|
207
336
|
"colgroup": {
|
|
@@ -46,11 +46,12 @@ export class GridHeaderInteraction {
|
|
|
46
46
|
}
|
|
47
47
|
#click = event => {
|
|
48
48
|
if (this.#suppressClick) { this.#suppressClick = false; return; }
|
|
49
|
-
if (event.target.closest('.column-resize')) return;
|
|
49
|
+
if (event.target.closest('.column-resize, .column-filter')) return;
|
|
50
50
|
const header = event.target.closest('th[data-field]');
|
|
51
51
|
if (header) this.#sort(header.dataset.field, event.shiftKey);
|
|
52
52
|
};
|
|
53
53
|
#dragStart = event => {
|
|
54
|
+
if (event.target.closest('.column-filter')) { event.preventDefault(); return; }
|
|
54
55
|
const header = event.target.closest('th[data-field]');
|
|
55
56
|
if (!header || this.#gesture) { event.preventDefault(); return; }
|
|
56
57
|
this.#suppressClick = true;
|
|
@@ -149,6 +150,7 @@ export class GridHeaderInteraction {
|
|
|
149
150
|
this.#focus(gesture.field);
|
|
150
151
|
};
|
|
151
152
|
#key = event => {
|
|
153
|
+
if (event.target.closest('.column-filter')) return;
|
|
152
154
|
if (event.key === 'Escape' && this.#gesture) {
|
|
153
155
|
this.cancel(); event.preventDefault(); return;
|
|
154
156
|
}
|
|
@@ -8,11 +8,11 @@ export class GridRows {
|
|
|
8
8
|
}
|
|
9
9
|
read(columns, nodes = this.store.getRows()) {
|
|
10
10
|
return nodes.map(node => ({node, cells:columns.map(column =>
|
|
11
|
-
cellText(this.store.value(node, column.field), column, this.format))}));
|
|
11
|
+
column.check ? '' : cellText(this.store.value(node, column.field), column, this.format))}));
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function cellText(value, column, format) {
|
|
15
|
+
export function cellText(value, column, format) {
|
|
16
16
|
if (value == null) return '';
|
|
17
17
|
const {dtype, format: pattern} = column;
|
|
18
18
|
if (dtype === 'T' && typeof value !== 'string' || dtype === 'B' && typeof value !== 'boolean') throw new TypeError(`grid ${column.field} does not match ${dtype}`);
|