report-studio 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +129 -4
- package/dist/designer-qy59eEux.js +3416 -0
- package/dist/designer.js +2 -3209
- package/dist/index.js +2 -2
- package/dist/react-designer.js +41 -0
- package/dist/react.js +60 -0
- package/dist/{viewer-D-c0lKzQ.js → viewer-xa17tyMJ.js} +682 -537
- package/package.json +80 -66
- package/src/cli/serve.js +135 -135
- package/src/designer/designer.js +2054 -1900
- package/src/designer/dialog.js +708 -708
- package/src/designer/fields.js +51 -2
- package/src/designer/icons.js +5 -0
- package/src/designer/index.html +28 -28
- package/src/designer/main.js +39 -39
- package/src/designer/menu.js +2 -2
- package/src/designer/panel.js +21 -1
- package/src/designer/sample-data.js +180 -9
- package/src/designer/structure.js +87 -6
- package/src/designer/styles.css +1654 -1616
- package/src/designer/tokens.js +9 -0
- package/src/engine/csv.js +350 -315
- package/src/engine/group.js +128 -65
- package/src/engine/index.js +35 -34
- package/src/engine/measure.js +13 -12
- package/src/engine/paginate.js +8 -4
- package/src/engine/resolve.js +37 -0
- package/src/engine/tables.js +87 -0
- package/src/engine/validate.js +97 -22
- package/src/index.html +79 -79
- package/src/index.js +51 -50
- package/src/preview/chrome.css +505 -505
- package/src/preview/chrome.js +147 -147
- package/src/preview/index.html +28 -28
- package/src/preview/viewer.js +763 -745
- package/src/react/designer.js +87 -0
- package/src/react/index.js +117 -0
- package/src/render/items.js +94 -1
- package/src/render/report.css +30 -0
- package/src/server/routes.js +331 -331
- package/src/shared/brand.css +29 -29
- package/src/shared/dropdown.css +136 -136
- package/src/shared/modal.css +125 -125
- package/src/shared/store.js +145 -145
- package/src/shared/theme.css +72 -72
- package/src/shared/window.js +138 -138
package/src/designer/fields.js
CHANGED
|
@@ -46,6 +46,12 @@ const ALIGNMENTS = [
|
|
|
46
46
|
{ label: 'Right', value: 'right' }
|
|
47
47
|
];
|
|
48
48
|
|
|
49
|
+
const IMAGE_FITS = [
|
|
50
|
+
{ label: 'Fit inside', value: 'contain' },
|
|
51
|
+
{ label: 'Fill and crop', value: 'cover' },
|
|
52
|
+
{ label: 'Stretch', value: 'fill' }
|
|
53
|
+
];
|
|
54
|
+
|
|
49
55
|
const ORIENTATIONS = [
|
|
50
56
|
{ label: 'Horizontal', value: 'horizontal' },
|
|
51
57
|
{ label: 'Vertical', value: 'vertical' }
|
|
@@ -155,6 +161,32 @@ export function fieldsFor(item) {
|
|
|
155
161
|
];
|
|
156
162
|
}
|
|
157
163
|
|
|
164
|
+
if (item.type === 'image') {
|
|
165
|
+
return [
|
|
166
|
+
{
|
|
167
|
+
title: 'Picture',
|
|
168
|
+
fields: [
|
|
169
|
+
{
|
|
170
|
+
key: 'src', label: 'Image', type: 'image',
|
|
171
|
+
hint: 'Stored in the layout file. Keep it small - a logo, ' +
|
|
172
|
+
'not a photograph'
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
key: 'field', label: 'From data', type: 'text', nullable: true,
|
|
176
|
+
hint: 'A data path such as company.logo, whose value is the ' +
|
|
177
|
+
"picture's URL. It replaces the stored image when present"
|
|
178
|
+
},
|
|
179
|
+
{ key: 'fit', label: 'Fit', type: 'choice', options: IMAGE_FITS },
|
|
180
|
+
{
|
|
181
|
+
key: 'alt', label: 'Description', type: 'text',
|
|
182
|
+
hint: 'Read out by a screen reader in place of the picture'
|
|
183
|
+
}
|
|
184
|
+
]
|
|
185
|
+
},
|
|
186
|
+
box
|
|
187
|
+
];
|
|
188
|
+
}
|
|
189
|
+
|
|
158
190
|
if (item.type === 'line') {
|
|
159
191
|
return [
|
|
160
192
|
box,
|
|
@@ -179,6 +211,21 @@ export function fieldsFor(item) {
|
|
|
179
211
|
if (item.type === 'table') {
|
|
180
212
|
return [
|
|
181
213
|
box,
|
|
214
|
+
{
|
|
215
|
+
title: 'Data',
|
|
216
|
+
fields: [
|
|
217
|
+
{
|
|
218
|
+
key: 'dataset', label: 'Dataset', type: 'text', nullable: true,
|
|
219
|
+
hint: "The key in the data object whose rows this table " +
|
|
220
|
+
"draws. Blank reads the report's dataset"
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: 'groupBy', label: 'Group by', type: 'text', nullable: true,
|
|
224
|
+
hint: 'A field to group these rows by, or blank. One table ' +
|
|
225
|
+
'in a report can be grouped'
|
|
226
|
+
}
|
|
227
|
+
]
|
|
228
|
+
},
|
|
182
229
|
{
|
|
183
230
|
title: 'Rows',
|
|
184
231
|
fields: [num('rowHeight', 'Row height', { min: 1 })]
|
|
@@ -452,11 +499,13 @@ export function reportFields(layout) {
|
|
|
452
499
|
{ key: 'name', label: 'Name', type: 'text' },
|
|
453
500
|
{
|
|
454
501
|
key: 'dataset', label: 'Dataset', type: 'text', nullable: true,
|
|
455
|
-
hint: 'The key in the data object that drives the rows'
|
|
502
|
+
hint: 'The key in the data object that drives the rows, ' +
|
|
503
|
+
'for every table that does not name its own'
|
|
456
504
|
},
|
|
457
505
|
{
|
|
458
506
|
key: 'groupBy', label: 'Group by', type: 'text', nullable: true,
|
|
459
|
-
hint: 'A field name, or blank for no grouping'
|
|
507
|
+
hint: 'A field name, or blank for no grouping. Groups the ' +
|
|
508
|
+
'first table, unless a table sets its own'
|
|
460
509
|
}
|
|
461
510
|
]
|
|
462
511
|
},
|
package/src/designer/icons.js
CHANGED
|
@@ -26,6 +26,11 @@ export const ICONS = {
|
|
|
26
26
|
table: wrap(`<rect x="2.25" y="3.25" width="11.5" height="9.5" rx="1.25"/>
|
|
27
27
|
<path d="M2.25 6.5h11.5M6.5 6.5v6.25M10 6.5v6.25"/>`),
|
|
28
28
|
|
|
29
|
+
/** a frame with a hill and a sun in it - a picture, not a box */
|
|
30
|
+
image: wrap(`<rect x="2.25" y="3.25" width="11.5" height="9.5" rx="1.25"/>
|
|
31
|
+
<path d="m2.75 11.5 3.5-3.5 2.5 2.5 1.5-1.5 3 3"/>
|
|
32
|
+
<circle cx="10.25" cy="6.25" r="1"/>`),
|
|
33
|
+
|
|
29
34
|
/** an empty rectangle: the frame, and nothing in it */
|
|
30
35
|
box: wrap(`<rect x="2.5" y="3.75" width="11" height="8.5" rx="1.25"/>`),
|
|
31
36
|
|
package/src/designer/index.html
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
|
|
4
|
-
<head>
|
|
5
|
-
<meta charset="UTF-8">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>Report Designer</title>
|
|
8
|
-
<link rel="icon" type="image/png" href="../asset/images/icon.png">
|
|
9
|
-
<link rel="stylesheet" href="./styles.css">
|
|
10
|
-
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
11
|
-
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
12
|
-
<link
|
|
13
|
-
href="https://fonts.googleapis.com/css2?family=Courier+Prime:ital,wght@0,400;0,700;1,400;1,700&family=Instrument+Sans:ital,wght@0,400..700;1,400..700&display=swap"
|
|
14
|
-
rel="stylesheet">
|
|
15
|
-
</head>
|
|
16
|
-
|
|
17
|
-
<body>
|
|
18
|
-
<!--
|
|
19
|
-
The whole host-facing API: one empty element. createDesigner builds its
|
|
20
|
-
own bar, gutter and canvas inside this, so a consumer never copies
|
|
21
|
-
markup out of here to get a working designer.
|
|
22
|
-
-->
|
|
23
|
-
<div id="report-designer"></div>
|
|
24
|
-
|
|
25
|
-
<script type="module" src="./main.js"></script>
|
|
26
|
-
</body>
|
|
27
|
-
|
|
28
|
-
</html>
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
+
<title>Report Designer</title>
|
|
8
|
+
<link rel="icon" type="image/png" href="../asset/images/icon.png">
|
|
9
|
+
<link rel="stylesheet" href="./styles.css">
|
|
10
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
11
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
12
|
+
<link
|
|
13
|
+
href="https://fonts.googleapis.com/css2?family=Courier+Prime:ital,wght@0,400;0,700;1,400;1,700&family=Instrument+Sans:ital,wght@0,400..700;1,400..700&display=swap"
|
|
14
|
+
rel="stylesheet">
|
|
15
|
+
</head>
|
|
16
|
+
|
|
17
|
+
<body>
|
|
18
|
+
<!--
|
|
19
|
+
The whole host-facing API: one empty element. createDesigner builds its
|
|
20
|
+
own bar, gutter and canvas inside this, so a consumer never copies
|
|
21
|
+
markup out of here to get a working designer.
|
|
22
|
+
-->
|
|
23
|
+
<div id="report-designer"></div>
|
|
24
|
+
|
|
25
|
+
<script type="module" src="./main.js"></script>
|
|
26
|
+
</body>
|
|
27
|
+
|
|
28
|
+
</html>
|
package/src/designer/main.js
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The designer page's entry.
|
|
3
|
-
*
|
|
4
|
-
* It opens whatever the URL asks for:
|
|
5
|
-
*
|
|
6
|
-
* /designer/ a blank report
|
|
7
|
-
* /designer/?report=<id> a saved report, through the dev server
|
|
8
|
-
* /designer/?fixture the sales-summary fixture, for poking at
|
|
9
|
-
*
|
|
10
|
-
* The fixture is loaded dynamically and its failure is survivable, because it
|
|
11
|
-
* is a development file that sits outside what the CLI serves. A static import
|
|
12
|
-
* of it would make this page work under `npm run dev` and break under
|
|
13
|
-
* `npx report-studio design`, which is the shipped path of the two.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { createDesigner } from './designer.js';
|
|
17
|
-
import { createStore } from '../shared/store.js';
|
|
18
|
-
|
|
19
|
-
const params = new URLSearchParams(location.search);
|
|
20
|
-
const wanted = params.get('report');
|
|
21
|
-
|
|
22
|
-
const designer = createDesigner({ mount: '#report-designer', layout: {} });
|
|
23
|
-
|
|
24
|
-
if (wanted) {
|
|
25
|
-
/**
|
|
26
|
-
* Loaded through the store rather than fetched here, so a missing report
|
|
27
|
-
* reports itself the way it would for anyone else.
|
|
28
|
-
*/
|
|
29
|
-
createStore().load(wanted)
|
|
30
|
-
.then(layout => designer.open(layout, wanted))
|
|
31
|
-
.catch(error => console.warn(`could not open "${wanted}": ${error.message}`));
|
|
32
|
-
} else if (params.has('fixture')) {
|
|
33
|
-
import('../../fixtures/test-data.js')
|
|
34
|
-
.then(({ rptJson }) => designer.open(structuredClone(rptJson)))
|
|
35
|
-
.catch(() => console.warn('the fixtures are only there under `npm run dev`'));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** handy from the devtools console while poking at a layout */
|
|
39
|
-
window.designer = designer;
|
|
1
|
+
/**
|
|
2
|
+
* The designer page's entry.
|
|
3
|
+
*
|
|
4
|
+
* It opens whatever the URL asks for:
|
|
5
|
+
*
|
|
6
|
+
* /designer/ a blank report
|
|
7
|
+
* /designer/?report=<id> a saved report, through the dev server
|
|
8
|
+
* /designer/?fixture the sales-summary fixture, for poking at
|
|
9
|
+
*
|
|
10
|
+
* The fixture is loaded dynamically and its failure is survivable, because it
|
|
11
|
+
* is a development file that sits outside what the CLI serves. A static import
|
|
12
|
+
* of it would make this page work under `npm run dev` and break under
|
|
13
|
+
* `npx report-studio design`, which is the shipped path of the two.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createDesigner } from './designer.js';
|
|
17
|
+
import { createStore } from '../shared/store.js';
|
|
18
|
+
|
|
19
|
+
const params = new URLSearchParams(location.search);
|
|
20
|
+
const wanted = params.get('report');
|
|
21
|
+
|
|
22
|
+
const designer = createDesigner({ mount: '#report-designer', layout: {} });
|
|
23
|
+
|
|
24
|
+
if (wanted) {
|
|
25
|
+
/**
|
|
26
|
+
* Loaded through the store rather than fetched here, so a missing report
|
|
27
|
+
* reports itself the way it would for anyone else.
|
|
28
|
+
*/
|
|
29
|
+
createStore().load(wanted)
|
|
30
|
+
.then(layout => designer.open(layout, wanted))
|
|
31
|
+
.catch(error => console.warn(`could not open "${wanted}": ${error.message}`));
|
|
32
|
+
} else if (params.has('fixture')) {
|
|
33
|
+
import('../../fixtures/test-data.js')
|
|
34
|
+
.then(({ rptJson }) => designer.open(structuredClone(rptJson)))
|
|
35
|
+
.catch(() => console.warn('the fixtures are only there under `npm run dev`'));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** handy from the devtools console while poking at a layout */
|
|
39
|
+
window.designer = designer;
|
package/src/designer/menu.js
CHANGED
|
@@ -31,7 +31,7 @@ export const MENU_EDGE_GAP = 8;
|
|
|
31
31
|
* @param {number} [options.count] how many items are selected
|
|
32
32
|
* @param {boolean} [options.canPaste] whether the clipboard holds anything
|
|
33
33
|
* @param {boolean} [options.canDuplicate] false when a copy of the selection
|
|
34
|
-
* would be one the report cannot hold
|
|
34
|
+
* would be one the report cannot hold
|
|
35
35
|
* @returns {object[]} entries, in the order they are drawn
|
|
36
36
|
*/
|
|
37
37
|
export function menuActions({
|
|
@@ -55,7 +55,7 @@ export function menuActions({
|
|
|
55
55
|
glyph: 'duplicate',
|
|
56
56
|
label: canDuplicate
|
|
57
57
|
? `Duplicate${many}`
|
|
58
|
-
: '
|
|
58
|
+
: 'This cannot be duplicated',
|
|
59
59
|
hint: 'ctrl+D, or alt-drag',
|
|
60
60
|
disabled: !canDuplicate
|
|
61
61
|
},
|
package/src/designer/panel.js
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
COLUMN_FIELDS, readField, writeField
|
|
23
23
|
} from './fields.js';
|
|
24
24
|
import { BAND_TYPES, BAND_NOTES, hasBand, findBand } from './structure.js';
|
|
25
|
-
import { esc } from '../render/items.js';
|
|
25
|
+
import { esc, imageSrc } from '../render/items.js';
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -175,6 +175,26 @@ function input(id, bind, field, value) {
|
|
|
175
175
|
return `<input type="checkbox" id="${id}" ${bind}
|
|
176
176
|
${value ? 'checked' : ''}>`;
|
|
177
177
|
|
|
178
|
+
case 'image':
|
|
179
|
+
/**
|
|
180
|
+
* Buttons rather than a bound input: a picture arrives through a
|
|
181
|
+
* file dialog, which the designer opens, and the rail is rebuilt
|
|
182
|
+
* once it lands. No data-field, so syncPanel has nothing here to
|
|
183
|
+
* write a data: URI into.
|
|
184
|
+
*/
|
|
185
|
+
return `
|
|
186
|
+
<div class="dz-image" id="${id}">
|
|
187
|
+
${value
|
|
188
|
+
? `<img class="dz-image-thumb" src="${esc(imageSrc(value))}" alt="">`
|
|
189
|
+
: '<span class="dz-image-none">No image stored</span>'}
|
|
190
|
+
<div class="dz-image-actions">
|
|
191
|
+
<button type="button" class="dz-add-row"
|
|
192
|
+
data-action="upload-image">${value ? 'Replace' : 'Upload'}</button>
|
|
193
|
+
${value ? `<button type="button" class="dz-add-row"
|
|
194
|
+
data-action="clear-image">Remove</button>` : ''}
|
|
195
|
+
</div>
|
|
196
|
+
</div>`;
|
|
197
|
+
|
|
178
198
|
case 'text':
|
|
179
199
|
return `<input type="text" id="${id}" ${bind}
|
|
180
200
|
value="${esc(value ?? '')}" spellcheck="false">`;
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import { PLACEHOLDER, isEngineKey, isRowScoped } from '../engine/resolve.js';
|
|
22
|
+
import { groupingOf, datasetOf } from '../engine/tables.js';
|
|
22
23
|
|
|
23
24
|
|
|
24
25
|
/** enough rows to show a group break, few enough to read at a glance */
|
|
@@ -34,6 +35,16 @@ const AGGREGATE = /\{\s*(\w+)\(([^)]*)\)\s*\}/g;
|
|
|
34
35
|
/** and ones that want a date */
|
|
35
36
|
const DATEISH = /(date|day|when)$/i;
|
|
36
37
|
|
|
38
|
+
/**
|
|
39
|
+
* What a bound image previews with: a small grey PNG, stored inline.
|
|
40
|
+
*
|
|
41
|
+
* Inline rather than a placeholder service's URL, so a designer opened on a
|
|
42
|
+
* machine with no network still previews - and so opening one does not tell a
|
|
43
|
+
* third party that it has been.
|
|
44
|
+
*/
|
|
45
|
+
export const SAMPLE_IMAGE = 'data:image/png;base64,' +
|
|
46
|
+
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAAAAADRSSBWAAAAC0lEQVR4nGO4eRcAApIBt+wBgUAAAAAASUVORK5CYII=';
|
|
47
|
+
|
|
37
48
|
|
|
38
49
|
/**
|
|
39
50
|
* Every placeholder key a layout asks the data for, with the scope it is read in.
|
|
@@ -72,6 +83,32 @@ export function requiredKeys(layout) {
|
|
|
72
83
|
}
|
|
73
84
|
|
|
74
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The data paths the layout's images are bound to.
|
|
88
|
+
*
|
|
89
|
+
* Kept apart from requiredKeys because the answer is a different kind of
|
|
90
|
+
* value: a picture's URL, not a word or a figure. Always read from the root -
|
|
91
|
+
* resolve.js looks an image's field up in the payload whatever band it is in.
|
|
92
|
+
*
|
|
93
|
+
* @param {object} layout
|
|
94
|
+
* @returns {string[]} paths, de-duplicated
|
|
95
|
+
*/
|
|
96
|
+
export function imageKeys(layout) {
|
|
97
|
+
const found = new Set();
|
|
98
|
+
|
|
99
|
+
for (const band of (Array.isArray(layout?.bands) ? layout.bands : [])) {
|
|
100
|
+
for (const item of (band.items || [])) {
|
|
101
|
+
if (item.type !== 'image' || typeof item.field !== 'string') continue;
|
|
102
|
+
|
|
103
|
+
const key = item.field.trim();
|
|
104
|
+
if (key) found.add(key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return [...found];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
75
112
|
/**
|
|
76
113
|
* The dataset key rows are read from, and the fields each row needs.
|
|
77
114
|
*
|
|
@@ -103,15 +140,28 @@ export function datasets(layout) {
|
|
|
103
140
|
|
|
104
141
|
/**
|
|
105
142
|
* A report can group and place row placeholders without ever drawing a
|
|
106
|
-
* table, so the
|
|
143
|
+
* table, so the rows they read are created if anything needs them at all.
|
|
144
|
+
* They are the grouped table's: a group band's `{region}` is read off the
|
|
145
|
+
* rows of the table being grouped, whichever table that is.
|
|
107
146
|
*/
|
|
108
147
|
const { row } = requiredKeys(layout);
|
|
148
|
+
const grouping = groupingOf(layout);
|
|
149
|
+
const by = grouping?.by ?? layout?.groupBy ?? null;
|
|
109
150
|
|
|
110
|
-
if (row.length ||
|
|
111
|
-
const fields = fieldsOf(
|
|
151
|
+
if (row.length || by) {
|
|
152
|
+
const fields = fieldsOf(
|
|
153
|
+
(grouping && datasetOf(grouping.table, layout)) || fallback);
|
|
112
154
|
|
|
113
155
|
for (const key of row) fields.add(key);
|
|
114
|
-
if (
|
|
156
|
+
if (by) fields.add(by);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* `{sum(payments.amount)}` names a dataset of its own, which the report
|
|
161
|
+
* may total without drawing a table of it - so it needs rows as well.
|
|
162
|
+
*/
|
|
163
|
+
for (const scoped of scopedAggregates(layout)) {
|
|
164
|
+
fieldsOf(scoped.dataset).add(scoped.field);
|
|
115
165
|
}
|
|
116
166
|
|
|
117
167
|
return found;
|
|
@@ -134,16 +184,82 @@ export function sampleData(layout) {
|
|
|
134
184
|
const { root } = requiredKeys(layout);
|
|
135
185
|
const summed = summedFields(layout);
|
|
136
186
|
|
|
187
|
+
/** first, so a text placeholder on the same path does not take it as a word */
|
|
188
|
+
for (const path of imageKeys(layout)) place(data, path, SAMPLE_IMAGE);
|
|
189
|
+
|
|
137
190
|
for (const path of root) place(data, path);
|
|
138
191
|
|
|
192
|
+
const groupedBy = groupedFieldsByDataset(layout);
|
|
193
|
+
|
|
139
194
|
for (const [name, fields] of datasets(layout)) {
|
|
140
|
-
data[name] = rowsFor([...fields],
|
|
195
|
+
data[name] = rowsFor([...fields], groupedBy.get(name) ?? null, summed);
|
|
141
196
|
}
|
|
142
197
|
|
|
143
198
|
return data;
|
|
144
199
|
}
|
|
145
200
|
|
|
146
201
|
|
|
202
|
+
/**
|
|
203
|
+
* A data payload with whatever the layout now asks for and it lacks filled in -
|
|
204
|
+
* and nothing it already has touched.
|
|
205
|
+
*
|
|
206
|
+
* The data file is written once, on the first save, and after that it is the
|
|
207
|
+
* user's: real figures typed into it must survive every later save. But a
|
|
208
|
+
* report goes on growing after that save - a second table, a new placeholder,
|
|
209
|
+
* an image bound to a field - and a file that never learned about them made the
|
|
210
|
+
* preview fail on a dataset "which is not present in the data". So what is
|
|
211
|
+
* missing is added from the sample, and what is there is left as it is.
|
|
212
|
+
*
|
|
213
|
+
* "Missing" is `undefined`, walked down through plain objects. An existing
|
|
214
|
+
* array is a dataset the user owns, so its rows are never added to or edited,
|
|
215
|
+
* even if a column has appeared since; and a value that is present, `null`
|
|
216
|
+
* included, is an answer rather than a gap.
|
|
217
|
+
*
|
|
218
|
+
* @param {object|null} data what is on disk, or nothing
|
|
219
|
+
* @param {object} layout
|
|
220
|
+
* @returns {{data: object, added: boolean}} a new object, never the one given;
|
|
221
|
+
* `added` says whether anything was filled in
|
|
222
|
+
*/
|
|
223
|
+
export function completeData(data, layout) {
|
|
224
|
+
const sample = sampleData(layout);
|
|
225
|
+
|
|
226
|
+
if (data == null || typeof data !== 'object' || Array.isArray(data)) {
|
|
227
|
+
return { data: sample, added: true };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const out = structuredClone(data);
|
|
231
|
+
const added = fillMissing(out, sample);
|
|
232
|
+
|
|
233
|
+
return { data: out, added };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
/** copies into `target` every key of `source` it lacks; says whether it did */
|
|
238
|
+
function fillMissing(target, source) {
|
|
239
|
+
let added = false;
|
|
240
|
+
|
|
241
|
+
for (const [key, value] of Object.entries(source)) {
|
|
242
|
+
const current = target[key];
|
|
243
|
+
|
|
244
|
+
if (current === undefined) {
|
|
245
|
+
target[key] = structuredClone(value);
|
|
246
|
+
added = true;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const bothObjects = isPlainObject(current) && isPlainObject(value);
|
|
251
|
+
if (bothObjects && fillMissing(current, value)) added = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return added;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
function isPlainObject(value) {
|
|
259
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
|
|
147
263
|
/**
|
|
148
264
|
* The fields the layout takes an aggregate over.
|
|
149
265
|
*
|
|
@@ -167,7 +283,13 @@ export function summedFields(layout) {
|
|
|
167
283
|
if (item.type !== 'text') continue;
|
|
168
284
|
|
|
169
285
|
for (const [, , field] of String(item.value ?? '').matchAll(AGGREGATE)) {
|
|
170
|
-
|
|
286
|
+
const name = field.trim();
|
|
287
|
+
if (!name) continue;
|
|
288
|
+
|
|
289
|
+
found.add(name);
|
|
290
|
+
|
|
291
|
+
/** `sales.amount` sums the `amount` of each row of sales */
|
|
292
|
+
if (name.includes('.')) found.add(name.slice(name.indexOf('.') + 1));
|
|
171
293
|
}
|
|
172
294
|
}
|
|
173
295
|
}
|
|
@@ -176,8 +298,57 @@ export function summedFields(layout) {
|
|
|
176
298
|
}
|
|
177
299
|
|
|
178
300
|
|
|
179
|
-
/**
|
|
180
|
-
|
|
301
|
+
/**
|
|
302
|
+
* The field each dataset is grouped by - one at most, the grouped table's.
|
|
303
|
+
* @param {object} layout
|
|
304
|
+
* @returns {Map<string, string>}
|
|
305
|
+
*/
|
|
306
|
+
function groupedFieldsByDataset(layout) {
|
|
307
|
+
const found = new Map();
|
|
308
|
+
const grouping = groupingOf(layout);
|
|
309
|
+
|
|
310
|
+
if (grouping) {
|
|
311
|
+
found.set(datasetOf(grouping.table, layout) || layout?.dataset || 'rows', grouping.by);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return found;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Aggregates that name a dataset, as `{sum(payments.amount)}`: the dataset,
|
|
320
|
+
* and the field of it. The prefix is taken to be a dataset whether or not a
|
|
321
|
+
* table reads it - that is what the engine does when the payload has one.
|
|
322
|
+
* @param {object} layout
|
|
323
|
+
* @returns {{dataset: string, field: string}[]}
|
|
324
|
+
*/
|
|
325
|
+
function scopedAggregates(layout) {
|
|
326
|
+
const found = [];
|
|
327
|
+
|
|
328
|
+
for (const band of (Array.isArray(layout?.bands) ? layout.bands : [])) {
|
|
329
|
+
for (const item of (band.items || [])) {
|
|
330
|
+
if (item.type !== 'text') continue;
|
|
331
|
+
|
|
332
|
+
for (const [, , arg] of String(item.value ?? '').matchAll(AGGREGATE)) {
|
|
333
|
+
const [dataset, ...rest] = arg.trim().split('.');
|
|
334
|
+
const field = rest.join('.');
|
|
335
|
+
|
|
336
|
+
if (dataset && field) found.push({ dataset, field });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return found;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Writes a value at a dotted path, building the objects on the way down.
|
|
347
|
+
* @param {object} data
|
|
348
|
+
* @param {string} path
|
|
349
|
+
* @param {*} [value] what to write; a value made up from the key when absent
|
|
350
|
+
*/
|
|
351
|
+
function place(data, path, value) {
|
|
181
352
|
const parts = path.split('.').filter(Boolean);
|
|
182
353
|
if (!parts.length) return;
|
|
183
354
|
|
|
@@ -189,7 +360,7 @@ function place(data, path) {
|
|
|
189
360
|
node = node[part];
|
|
190
361
|
}
|
|
191
362
|
|
|
192
|
-
if (node[last] === undefined) node[last] = valueFor(last, 0);
|
|
363
|
+
if (node[last] === undefined) node[last] = value ?? valueFor(last, 0);
|
|
193
364
|
}
|
|
194
365
|
|
|
195
366
|
|
|
@@ -83,8 +83,7 @@ function bandList(layout) {
|
|
|
83
83
|
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
* How many tables the report has.
|
|
87
|
-
* validate.js), so this is what says whether another may be added.
|
|
86
|
+
* How many tables the report has.
|
|
88
87
|
*
|
|
89
88
|
* @param {object} layout
|
|
90
89
|
* @returns {number}
|
|
@@ -97,9 +96,60 @@ export function tableCount(layout) {
|
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
|
|
100
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* Whether this report can take another table. Always, now that each table
|
|
101
|
+
* binds a dataset of its own; kept as the one place to say otherwise, should a
|
|
102
|
+
* limit ever come back, rather than a check at every call site.
|
|
103
|
+
*
|
|
104
|
+
* @param {object} layout
|
|
105
|
+
* @returns {boolean}
|
|
106
|
+
*/
|
|
101
107
|
export function canAddTable(layout) {
|
|
102
|
-
return
|
|
108
|
+
return Array.isArray(layout?.bands);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The dataset a new table reads.
|
|
114
|
+
*
|
|
115
|
+
* The first table reads the report's, as it always did. Every one after it
|
|
116
|
+
* gets a name of its own - `items2`, `items3` - because a second table on
|
|
117
|
+
* the same rows is rarely what anyone draws a second table for, and a name
|
|
118
|
+
* that is already distinct is one the sample data can fill with its own rows.
|
|
119
|
+
*
|
|
120
|
+
* @param {object} layout
|
|
121
|
+
* @returns {string|null}
|
|
122
|
+
*/
|
|
123
|
+
function datasetForNewTable(layout) {
|
|
124
|
+
const count = tableCount(layout);
|
|
125
|
+
if (count === 0) return layout.dataset ?? null;
|
|
126
|
+
|
|
127
|
+
const base = layout.dataset || 'rows';
|
|
128
|
+
const taken = new Set(bandList(layout)
|
|
129
|
+
.flatMap(b => b.items || [])
|
|
130
|
+
.filter(i => i?.type === 'table')
|
|
131
|
+
.map(i => i.dataset ?? layout.dataset));
|
|
132
|
+
|
|
133
|
+
for (let n = count + 1; ; n++) {
|
|
134
|
+
if (!taken.has(`${base}${n}`)) return `${base}${n}`;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A copied table, made fit to sit beside its original.
|
|
141
|
+
*
|
|
142
|
+
* Tables are stacked rather than overlapped - validate.js refuses two that
|
|
143
|
+
* share any height of a band - so the copy goes below everything on the band
|
|
144
|
+
* rather than a step down and across. And it is not grouped by a field of its
|
|
145
|
+
* own, since a report groups one table and the original is already that one.
|
|
146
|
+
*
|
|
147
|
+
* @param {object} copy mutated in place
|
|
148
|
+
* @param {object} band the band it is going onto, before the copy is added
|
|
149
|
+
*/
|
|
150
|
+
function settleTable(copy, band) {
|
|
151
|
+
copy.y = nextY(band);
|
|
152
|
+
delete copy.groupBy;
|
|
103
153
|
}
|
|
104
154
|
|
|
105
155
|
|
|
@@ -294,6 +344,34 @@ export function createBox(layout, band) {
|
|
|
294
344
|
}
|
|
295
345
|
|
|
296
346
|
|
|
347
|
+
/**
|
|
348
|
+
* A new image, below whatever is already on the band.
|
|
349
|
+
*
|
|
350
|
+
* Empty: no picture and no field. The canvas draws it as a dashed frame to be
|
|
351
|
+
* filled from the rail - either a picture uploaded into the layout, or a data
|
|
352
|
+
* field the host supplies a URL in. Logo-sized, because a logo is what goes in
|
|
353
|
+
* a report more often than anything else.
|
|
354
|
+
*
|
|
355
|
+
* @param {object} layout
|
|
356
|
+
* @param {object} band the band it is going onto, for its vertical place
|
|
357
|
+
* @returns {object} the item; it has not been added to anything yet
|
|
358
|
+
*/
|
|
359
|
+
export function createImage(layout, band) {
|
|
360
|
+
return {
|
|
361
|
+
id: nextItemId(layout, 'image'),
|
|
362
|
+
type: 'image',
|
|
363
|
+
x: 0,
|
|
364
|
+
y: nextY(band),
|
|
365
|
+
w: 160,
|
|
366
|
+
h: 80,
|
|
367
|
+
src: null,
|
|
368
|
+
field: null,
|
|
369
|
+
fit: 'contain',
|
|
370
|
+
alt: ''
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
|
|
297
375
|
/** a column at its default width, numbered by where it sits */
|
|
298
376
|
export function makeColumn(n) {
|
|
299
377
|
return {
|
|
@@ -326,7 +404,7 @@ export function createTable(layout, band, columns = DEFAULT_COLUMNS) {
|
|
|
326
404
|
x: 0,
|
|
327
405
|
y: nextY(band),
|
|
328
406
|
w: contentWidth(layout),
|
|
329
|
-
dataset: layout
|
|
407
|
+
dataset: datasetForNewTable(layout),
|
|
330
408
|
rowHeight: 24,
|
|
331
409
|
headerHeight: 28,
|
|
332
410
|
showHeader: true,
|
|
@@ -419,7 +497,6 @@ export function duplicateItem(layout, bandType, id) {
|
|
|
419
497
|
const original = (band.items || []).find(i => i.id === id);
|
|
420
498
|
if (!original) return null;
|
|
421
499
|
|
|
422
|
-
/** a copy of the table would be the second one, which the engine cannot bind */
|
|
423
500
|
if (original.type === 'table' && !canAddTable(layout)) return null;
|
|
424
501
|
|
|
425
502
|
const copy = structuredClone(original);
|
|
@@ -428,6 +505,8 @@ export function duplicateItem(layout, bandType, id) {
|
|
|
428
505
|
copy.x = clampX(layout, (original.x ?? 0) + DUPLICATE_OFFSET, copy.w);
|
|
429
506
|
copy.y = Math.max(0, (original.y ?? 0) + DUPLICATE_OFFSET);
|
|
430
507
|
|
|
508
|
+
if (copy.type === 'table') settleTable(copy, band);
|
|
509
|
+
|
|
431
510
|
band.items ??= [];
|
|
432
511
|
band.items.push(copy);
|
|
433
512
|
|
|
@@ -497,6 +576,8 @@ export function pasteItems(layout, bandType, entries) {
|
|
|
497
576
|
copy.x = clampX(layout, (item.x ?? 0) + DUPLICATE_OFFSET, copy.w);
|
|
498
577
|
copy.y = from === bandType ? y : clampY(layout, bandType, y, copy);
|
|
499
578
|
|
|
579
|
+
if (copy.type === 'table') settleTable(copy, band);
|
|
580
|
+
|
|
500
581
|
band.items.push(copy);
|
|
501
582
|
made.push(copy);
|
|
502
583
|
}
|