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.
Files changed (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +129 -4
  3. package/dist/designer-qy59eEux.js +3416 -0
  4. package/dist/designer.js +2 -3209
  5. package/dist/index.js +2 -2
  6. package/dist/react-designer.js +41 -0
  7. package/dist/react.js +60 -0
  8. package/dist/{viewer-D-c0lKzQ.js → viewer-xa17tyMJ.js} +682 -537
  9. package/package.json +80 -66
  10. package/src/cli/serve.js +135 -135
  11. package/src/designer/designer.js +2054 -1900
  12. package/src/designer/dialog.js +708 -708
  13. package/src/designer/fields.js +51 -2
  14. package/src/designer/icons.js +5 -0
  15. package/src/designer/index.html +28 -28
  16. package/src/designer/main.js +39 -39
  17. package/src/designer/menu.js +2 -2
  18. package/src/designer/panel.js +21 -1
  19. package/src/designer/sample-data.js +180 -9
  20. package/src/designer/structure.js +87 -6
  21. package/src/designer/styles.css +1654 -1616
  22. package/src/designer/tokens.js +9 -0
  23. package/src/engine/csv.js +350 -315
  24. package/src/engine/group.js +128 -65
  25. package/src/engine/index.js +35 -34
  26. package/src/engine/measure.js +13 -12
  27. package/src/engine/paginate.js +8 -4
  28. package/src/engine/resolve.js +37 -0
  29. package/src/engine/tables.js +87 -0
  30. package/src/engine/validate.js +97 -22
  31. package/src/index.html +79 -79
  32. package/src/index.js +51 -50
  33. package/src/preview/chrome.css +505 -505
  34. package/src/preview/chrome.js +147 -147
  35. package/src/preview/index.html +28 -28
  36. package/src/preview/viewer.js +763 -745
  37. package/src/react/designer.js +87 -0
  38. package/src/react/index.js +117 -0
  39. package/src/render/items.js +94 -1
  40. package/src/render/report.css +30 -0
  41. package/src/server/routes.js +331 -331
  42. package/src/shared/brand.css +29 -29
  43. package/src/shared/dropdown.css +136 -136
  44. package/src/shared/modal.css +125 -125
  45. package/src/shared/store.js +145 -145
  46. package/src/shared/theme.css +72 -72
  47. package/src/shared/window.js +138 -138
@@ -1,5 +1,6 @@
1
1
  /**
2
- * group.js partitions the dataset and resolves aggregate placeholders.
2
+ * group.js binds every table to its rows, partitions the grouped one, and
3
+ * resolves aggregate placeholders.
3
4
  *
4
5
  * An aggregate has to be worked out over some set of rows, and there are only
5
6
  * two sets a band can mean:
@@ -9,89 +10,112 @@
9
10
  * each copy has a different answer. Filled in per fragment by paginate.js,
10
11
  * where which group is being drawn is known.
11
12
  *
12
- * every other band - the whole dataset. Filled in here, once.
13
+ * every other band - a whole dataset. Filled in here, once.
13
14
  *
14
- * It used to be reportFooter and nothing else, which made a total under the
15
- * rows - the place a total is most often wanted, and the one a designer reaches
16
- * for first - print the raw `{sum(amount)}` back at you. There is no scope a
17
- * detail or a page footer could mean *other* than the whole report, so there
18
- * was never a reason to refuse them one.
15
+ * Which dataset, now that a report can hold more than one table: the one the
16
+ * expression names, as `{sum(sales.amount)}` or `{count(sales)}`, and the
17
+ * first table's when it names none - which is what every expression written
18
+ * before there could be a second table meant.
19
19
  */
20
20
 
21
21
  import { ReportError } from './validate.js';
22
+ import { tablesOf, datasetOf, groupingOf } from './tables.js';
22
23
 
23
24
 
24
25
  const REGEX = /\{([^{}]+)\}/g;
25
26
 
26
27
 
27
28
  export function group(resolvedJson, data) {
28
- let json = structuredClone(resolvedJson);
29
- const hasGroup = json.groupBy != null;
30
- if (hasGroup) {
31
- return withGroup(json, data);
32
- } else {
33
- return withoutGroup(json, data);
34
- }
35
- }
36
-
37
-
38
- function withGroup(json, data) {
29
+ const json = structuredClone(resolvedJson);
39
30
  const bands = json.bands;
40
- const tableBand = findTableBand(bands);
31
+ const tables = tablesOf(json);
41
32
 
42
- if (!tableBand) {
33
+ if (json.groupBy != null && tables.length === 0) {
43
34
  throw new ReportError(
44
35
  `group: layout.groupBy is "${json.groupBy}" but no band contains a table to group`
45
36
  );
46
37
  }
47
38
 
48
- const dataset = datasetFor(tableBand, json, data);
49
- const groupedData = groupedRows(dataset, json.groupBy);
50
- const groupedAggregates = groupedAggregate(bands, groupedData);
51
-
52
- tableBand.groups =
53
- Object.entries(groupedData).map(
54
- ([key, rows]) => ({
55
- key,
56
- rows,
57
- aggregates: groupedAggregates[key]
58
- })
59
- );
39
+ const grouping = groupingOf(json);
40
+ const named = namedDatasets(data);
41
+
42
+ /** the rows each table reads, bound as they are found */
43
+ const bound = tables.map(table => {
44
+ const rows = datasetFor(table, json, data);
45
+ named.set(datasetOf(table, json), rows);
46
+ return { table, rows };
47
+ });
60
48
 
61
- aggregate(bands, dataset);
49
+ for (const { table, rows } of bound) {
50
+ if (table === grouping?.table) {
51
+ bindGroups(table, rows, grouping.by, bands, datasetOf(table, json));
52
+ } else {
53
+ table.row = rows;
54
+ }
55
+ }
56
+
57
+ /** a report of nothing but text bands is legal - there is simply nothing to bind */
58
+ const scope = { rows: bound[0]?.rows ?? [], named };
59
+
60
+ aggregate(bands, scope);
62
61
  return json;
63
62
  }
64
63
 
65
64
 
66
- function withoutGroup(json, data) {
67
- const bands = json.bands;
68
- const tableBand = findTableBand(bands);
65
+ /**
66
+ * Splits the grouped table's rows into groups, each carrying its own totals.
67
+ * @param {object} table
68
+ * @param {object[]} rows
69
+ * @param {string} by the field that decides which group a row is in
70
+ * @param {object[]} bands
71
+ * @param {string|null} dataset the table's dataset name, which a group band's
72
+ * aggregate may carry as a prefix
73
+ */
74
+ function bindGroups(table, rows, by, bands, dataset) {
75
+ const groupedData = groupedRows(rows, by);
76
+ const groupedAggregates = groupedAggregate(bands, groupedData, dataset);
77
+
78
+ table.groups = Object.entries(groupedData).map(([key, groupRows]) => ({
79
+ key,
80
+ rows: groupRows,
81
+ aggregates: groupedAggregates[key]
82
+ }));
83
+ }
69
84
 
70
- /** a report of nothing but text bands is legal - there is simply nothing to bind */
71
- const dataset = tableBand ? datasetFor(tableBand, json, data) : [];
72
85
 
73
- if (tableBand) tableBand.row = dataset;
86
+ /**
87
+ * Every top-level array in the payload, by name - the datasets an aggregate
88
+ * can name. Not only the ones a table is bound to: a total of payments is a
89
+ * reasonable thing to print under an invoice that draws no table of them.
90
+ * @param {object} data
91
+ * @returns {Map<string, object[]>}
92
+ */
93
+ function namedDatasets(data) {
94
+ const found = new Map();
74
95
 
75
- aggregate(bands, dataset);
76
- return json;
96
+ for (const [name, value] of Object.entries(data ?? {})) {
97
+ if (Array.isArray(value)) found.set(name, value);
98
+ }
99
+
100
+ return found;
77
101
  }
78
102
 
79
103
 
80
104
  /**
81
105
  * The rows a table is bound to. A missing dataset is a layout/data mismatch
82
106
  * worth naming rather than a silent empty table.
83
- * @param {object} tableBand the table item
107
+ * @param {object} table the table item
84
108
  * @param {object} json
85
109
  * @param {object} data
86
110
  * @returns {object[]}
87
111
  */
88
- function datasetFor(tableBand, json, data) {
89
- const key = tableBand.dataset ?? json.dataset;
90
- const dataset = data[key];
112
+ function datasetFor(table, json, data) {
113
+ const key = datasetOf(table, json);
114
+ const dataset = key == null ? undefined : data[key];
91
115
 
92
116
  if (dataset === undefined) {
93
117
  throw new ReportError(
94
- `group: table "${tableBand.id}" is bound to dataset "${key}", which is not present in the data`
118
+ `group: table "${table.id}" is bound to dataset "${key}", which is not present in the data`
95
119
  );
96
120
  }
97
121
 
@@ -105,17 +129,6 @@ function datasetFor(tableBand, json, data) {
105
129
  }
106
130
 
107
131
 
108
- function findTableBand(bands) {
109
- for (const band of bands) {
110
- for (const item of band.items) {
111
- if (item.type == "table") {
112
- return item;
113
- }
114
- }
115
- }
116
- }
117
-
118
-
119
132
  /**
120
133
  * The bands whose aggregates belong to one group rather than to the report.
121
134
  *
@@ -129,9 +142,9 @@ const GROUP_SCOPED = ['groupHeader', 'groupFooter'];
129
142
  /**
130
143
  * Report-wide aggregates: every band except the two that mean a group.
131
144
  * @param {object[]} bands
132
- * @param {object[]} dataset
145
+ * @param {{rows: object[], named: Map<string, object[]>}} scope
133
146
  */
134
- function aggregate(bands, dataset) {
147
+ function aggregate(bands, scope) {
135
148
  for (const band of bands) {
136
149
  if (GROUP_SCOPED.includes(band.type)) continue;
137
150
 
@@ -144,7 +157,7 @@ function aggregate(bands, dataset) {
144
157
  * standing; re-resolving from `value` here would discard its work
145
158
  * and leave "{authorizer}" printed literally on the report.
146
159
  */
147
- item.text = fillAggregates(item.text ?? item.value, dataset);
160
+ item.text = fillAggregates(item.text ?? item.value, scope);
148
161
  }
149
162
  }
150
163
  }
@@ -156,20 +169,52 @@ function aggregate(bands, dataset) {
156
169
  * Non-aggregate placeholders were already handled by resolve.js and are left
157
170
  * as they are.
158
171
  * @param {string} value
159
- * @param {object[]} dataset
172
+ * @param {{rows: object[], named: Map<string, object[]>}} scope
160
173
  * @returns {string}
161
174
  */
162
- function fillAggregates(value, dataset) {
175
+ function fillAggregates(value, scope) {
163
176
  return value.replace(REGEX, (match, key) => {
164
177
  const expr = parseAggregate(key.trim());
165
178
  if (!expr) return match;
166
179
 
167
- const result = compute(expr, dataset);
180
+ const { rows, field } = scoped(expr.field, scope);
181
+ const result = compute({ key: expr.key, field }, rows);
168
182
  return result == null ? '' : String(result);
169
183
  });
170
184
  }
171
185
 
172
186
 
187
+ /**
188
+ * Which rows an aggregate's argument means, and which field of them.
189
+ *
190
+ * count(sales) every row of `sales`
191
+ * sum(sales.amount) `amount` over the rows of `sales`
192
+ * sum(amount) `amount` over the first table's rows, as ever
193
+ *
194
+ * A prefix is only a dataset if the payload has an array of that name. Rows are
195
+ * flat, so a dot has never meant anything inside a row field and nothing that
196
+ * worked before is read differently now.
197
+ *
198
+ * @param {string} field the expression's argument
199
+ * @param {{rows: object[], named: Map<string, object[]>}} scope
200
+ * @returns {{rows: object[], field: string}}
201
+ */
202
+ function scoped(field, scope) {
203
+ if (scope.named.has(field)) return { rows: scope.named.get(field), field: '' };
204
+
205
+ const dot = field.indexOf('.');
206
+
207
+ if (dot > 0) {
208
+ const name = field.slice(0, dot);
209
+ if (scope.named.has(name)) {
210
+ return { rows: scope.named.get(name), field: field.slice(dot + 1) };
211
+ }
212
+ }
213
+
214
+ return { rows: scope.rows, field };
215
+ }
216
+
217
+
173
218
  function parseAggregate(expr) {
174
219
  const match = expr.match(/^(\w+)\((.*?)\)$/);
175
220
  if (!match) return null;
@@ -189,12 +234,19 @@ function parseAggregate(expr) {
189
234
  * heading, and a count that worked in the footer and printed blank in the
190
235
  * header is the same fault this file exists to have stopped having.
191
236
  *
237
+ * Keyed on the expression's argument as written, because that is what
238
+ * paginate.js looks it up by - so `{sum(sales.amount)}` in a group footer is
239
+ * stored under "sales.amount" and computed over the group's `amount`. A group
240
+ * band only ever means its group's rows; naming the grouped dataset is allowed
241
+ * because it is the same thing said in full.
242
+ *
192
243
  * Shape: aggregates[groupKey][field] = { count, sum, avg, min, max }
193
244
  * @param {object[]} bands
194
245
  * @param {object} dataset grouped rows, keyed by group key
246
+ * @param {string|null} name the grouped table's dataset
195
247
  * @returns {object}
196
248
  */
197
- function groupedAggregate(bands, dataset) {
249
+ function groupedAggregate(bands, dataset, name) {
198
250
  const aggregates = {};
199
251
 
200
252
  /** every group gets a slot, even one no groupFooter expression touches */
@@ -209,6 +261,8 @@ function groupedAggregate(bands, dataset) {
209
261
  if (item.type != "text") continue;
210
262
 
211
263
  for (const expr of parseAll(item.value)) {
264
+ const field = withoutScope(expr.field, name);
265
+
212
266
  for (const [key, rows] of Object.entries(dataset)) {
213
267
  if (!aggregates[key][expr.field]) {
214
268
  aggregates[key][expr.field] = {
@@ -220,7 +274,8 @@ function groupedAggregate(bands, dataset) {
220
274
  };
221
275
  }
222
276
 
223
- aggregates[key][expr.field][expr.key] = compute(expr, rows);
277
+ aggregates[key][expr.field][expr.key] =
278
+ compute({ key: expr.key, field }, rows);
224
279
  }
225
280
  }
226
281
  }
@@ -230,6 +285,14 @@ function groupedAggregate(bands, dataset) {
230
285
  }
231
286
 
232
287
 
288
+ /** `sales.amount` to `amount`, and `sales` to nothing, when sales is the dataset */
289
+ function withoutScope(field, name) {
290
+ if (name == null) return field;
291
+ if (field === name) return '';
292
+ return field.startsWith(`${name}.`) ? field.slice(name.length + 1) : field;
293
+ }
294
+
295
+
233
296
  /** every aggregate expression in a value, not just the first */
234
297
  function parseAll(value) {
235
298
  const found = [];
@@ -1,34 +1,35 @@
1
- /**
2
- * The engine's public surface.
3
- *
4
- * Spec 4: buildPages(layout, data) -> PageList, in four ordered stages, each a
5
- * pure function over plain objects. Nothing here imports React, and nothing
6
- * here touches the DOM beyond the offscreen canvas used for text measurement.
7
- */
8
-
9
- import { resolve } from './resolve.js';
10
- import { group } from './group.js';
11
- import { measure } from './measure.js';
12
- import { paginate } from './paginate.js';
13
-
14
- export { resolve } from './resolve.js';
15
- export { group } from './group.js';
16
- export { measure } from './measure.js';
17
- export { paginate } from './paginate.js';
18
- export { validateLayout, ReportError } from './validate.js';
19
- export { search, searchPages } from './search.js';
20
- export { pageRegions, bandZoneHeight, resolveHeight, DEFAULT_BAND_HEIGHTS } from './regions.js';
21
- export { toCSV, toReportCSV, reportFilename } from './csv.js';
22
- export { measureText, wrapLines, textHeight, LINE_HEIGHT_RATIO } from './text-metrics.js';
23
-
24
-
25
- /**
26
- * Turns a layout file plus a data payload into a page list.
27
- * @param {object} layout the report layout file
28
- * @param {object} data the host application's data, keyed by dataset name
29
- * @returns {object} the layout, with a `pages` array of positioned bands
30
- * @throws {ReportError} when the layout or data cannot be used
31
- */
32
- export function buildPages(layout, data) {
33
- return paginate(measure(group(resolve(layout, data), data)));
34
- }
1
+ /**
2
+ * The engine's public surface.
3
+ *
4
+ * Spec 4: buildPages(layout, data) -> PageList, in four ordered stages, each a
5
+ * pure function over plain objects. Nothing here imports React, and nothing
6
+ * here touches the DOM beyond the offscreen canvas used for text measurement.
7
+ */
8
+
9
+ import { resolve } from './resolve.js';
10
+ import { group } from './group.js';
11
+ import { measure } from './measure.js';
12
+ import { paginate } from './paginate.js';
13
+
14
+ export { resolve } from './resolve.js';
15
+ export { group } from './group.js';
16
+ export { measure } from './measure.js';
17
+ export { paginate } from './paginate.js';
18
+ export { validateLayout, ReportError } from './validate.js';
19
+ export { search, searchPages } from './search.js';
20
+ export { pageRegions, bandZoneHeight, resolveHeight, DEFAULT_BAND_HEIGHTS } from './regions.js';
21
+ export { toCSV, toReportCSV, reportFilename, reportTables } from './csv.js';
22
+ export { tablesOf, datasetOf, groupingOf } from './tables.js';
23
+ export { measureText, wrapLines, textHeight, LINE_HEIGHT_RATIO } from './text-metrics.js';
24
+
25
+
26
+ /**
27
+ * Turns a layout file plus a data payload into a page list.
28
+ * @param {object} layout the report layout file
29
+ * @param {object} data the host application's data, keyed by dataset name
30
+ * @returns {object} the layout, with a `pages` array of positioned bands
31
+ * @throws {ReportError} when the layout or data cannot be used
32
+ */
33
+ export function buildPages(layout, data) {
34
+ return paginate(measure(group(resolve(layout, data), data)));
35
+ }
@@ -216,11 +216,9 @@ export function designHeight(item) {
216
216
 
217
217
  export function measure(json) {
218
218
  const measureJson = structuredClone(json);
219
- const grouped = measureJson.groupBy != null;
220
-
221
219
  for (const band of measureJson.bands) {
222
220
  if (!band.items) continue;
223
- measureBand(band, grouped, measureJson);
221
+ measureBand(band, measureJson);
224
222
  }
225
223
 
226
224
  return measureJson;
@@ -233,15 +231,14 @@ export function measure(json) {
233
231
  * independent, and handles items side by side on the same row as well as items
234
232
  * separated by a gap, neither of which a running total can express.
235
233
  * @param {object} band
236
- * @param {boolean} grouped
237
234
  * @param {object} json
238
235
  * @returns {object} the same band, measured
239
236
  */
240
- function measureBand(band, grouped, json) {
237
+ function measureBand(band, json) {
241
238
  let contentHeight = 0;
242
239
 
243
240
  for (const item of band.items) {
244
- item.measuredHeight = measureItem(item, grouped, json);
241
+ item.measuredHeight = measureItem(item, json);
245
242
  contentHeight = Math.max(contentHeight, (item.y ?? 0) + item.measuredHeight);
246
243
  }
247
244
 
@@ -264,15 +261,17 @@ function holdsTable(band) {
264
261
  }
265
262
 
266
263
 
267
- function measureItem(item, grouped, json) {
264
+ function measureItem(item, json) {
268
265
  switch (item.type) {
269
266
  case 'text':
270
267
  return measureTextItem(item);
271
268
  case 'table':
272
- return measureTableItem(item, grouped, json);
269
+ return measureTableItem(item, json);
273
270
  case 'line':
274
271
  return measureLineItem(item);
275
272
  case 'box':
273
+ case 'image':
274
+ /** a picture is scaled into its box, never the box grown round it */
276
275
  return item.h ?? 0;
277
276
  default:
278
277
  console.warn(`Unknown item type "${item.type}" on item "${item.id}"; treating as zero height.`);
@@ -317,15 +316,17 @@ function measureLineItem(item) {
317
316
 
318
317
 
319
318
  /**
320
- * headerHeight + rows x rowHeight. When the report is grouped, each group also
319
+ * headerHeight + rows x rowHeight. When the table is grouped, each group also
321
320
  * carries its own header and footer band, and a table header that repeats per
322
321
  * group - all of which occupy real space and were previously unaccounted for.
322
+ *
323
+ * Grouped is the table's own fact - group.js gave it `groups` - and not the
324
+ * report's: of two tables in a grouped report, only one is split into groups.
323
325
  * @param {object} item
324
- * @param {boolean} grouped
325
326
  * @param {object} json
326
327
  * @returns {number}
327
328
  */
328
- function measureTableItem(item, grouped, json) {
329
+ function measureTableItem(item, json) {
329
330
  const headerHgt = headerHeightFor(item);
330
331
 
331
332
  /** stamped on so the renderer draws the row at the height that was budgeted */
@@ -337,7 +338,7 @@ function measureTableItem(item, grouped, json) {
337
338
  * row at its own. Measuring the same wrap three times in three files is how
338
339
  * the three of them come to disagree.
339
340
  */
340
- if (!grouped) {
341
+ if (!Array.isArray(item.groups)) {
341
342
  item.rowHeights = rowHeightsFor(item, item.row);
342
343
 
343
344
  return headerHgt + total(item.rowHeights);
@@ -17,7 +17,6 @@ export function paginate(json) {
17
17
  let context = {
18
18
  page: paginateJSON.page,
19
19
  bands: paginateJSON.bands,
20
- group: paginateJSON.groupBy,
21
20
  assignedBands: {
22
21
  reportHeader: paginateJSON.bands.find(b => b.type === "reportHeader"),
23
22
  reportFooter: paginateJSON.bands.find(b => b.type === "reportFooter"),
@@ -124,7 +123,7 @@ function calPages(bands, context) {
124
123
  pages.push(currentPage);
125
124
 
126
125
  // spreading the report header and footer from the context.assignedBands
127
- const { assignedBands, availableHeight, group } = context;
126
+ const { assignedBands, availableHeight } = context;
128
127
  const { reportFooter, reportHeader, groupHeader, groupFooter, pageHeader, pageFooter } = assignedBands;
129
128
 
130
129
  /**
@@ -319,11 +318,16 @@ function calPages(bands, context) {
319
318
  * sits above it is on this page too. A continuation slice has
320
319
  * nothing above it, so it starts at the top of the zone rather
321
320
  * than leaving that gap blank on every page.
321
+ *
322
+ * pageY, not the designed y less the carry: a table under
323
+ * another table starts under however far the first one really
324
+ * reached, the same as a fixed item does. Placed at its designed
325
+ * y it drew straight over the rows above it.
322
326
  */
323
- let tableY = itemY - carry;
327
+ let tableY = pageY(itemY);
324
328
 
325
329
  // when the table has group
326
- if (group !== null) {
330
+ if (item.groups) {
327
331
  /** reserved hgt for group table at least i a page we should fit this
328
332
  * table header, group header, min of one row and the group footer
329
333
  * also provide the table header hgt , group header and footer hgt
@@ -96,6 +96,9 @@ export function resolve(rptJson, rptData) {
96
96
  for (const item of band.items) {
97
97
  if (item.type == "text")
98
98
  item.text = resolveTxt(item, rptData, deferUnknown);
99
+
100
+ if (item.type == "image")
101
+ item.resolvedSrc = resolveImage(item, rptData);
99
102
  }
100
103
  }
101
104
 
@@ -103,6 +106,40 @@ export function resolve(rptJson, rptData) {
103
106
  }
104
107
 
105
108
 
109
+ /**
110
+ * The picture an image item shows: the value at its `field` when the data has
111
+ * one there, and the `src` stored in the layout otherwise - so a report can
112
+ * carry a default logo that the host replaces per customer.
113
+ *
114
+ * Always written, if only as '', because its presence is what tells the
115
+ * renderer this item went through the engine. The designer canvas draws items
116
+ * straight from the layout and shows a placeholder for a bound image; a built
117
+ * report whose data had no picture should print an empty frame instead.
118
+ *
119
+ * The URL is not checked here. Every route to the page goes through the
120
+ * renderer, which is where it is made safe - checking twice is two lists of
121
+ * schemes to keep in step.
122
+ *
123
+ * @param {object} item an image item
124
+ * @param {object} rptData
125
+ * @returns {string}
126
+ */
127
+ function resolveImage(item, rptData) {
128
+ const field = typeof item.field === 'string' ? item.field.trim() : '';
129
+
130
+ if (field) {
131
+ const value = findVal(field, rptData);
132
+ if (typeof value === 'string' && value !== '') return value;
133
+
134
+ if (!item.src) {
135
+ console.warn(`Unresolved image field "${field}" on item "${item.id}".`);
136
+ }
137
+ }
138
+
139
+ return typeof item.src === 'string' ? item.src : '';
140
+ }
141
+
142
+
106
143
  /**
107
144
  * Resolves the text value of an item with the provided data
108
145
  * @param {*} item
@@ -0,0 +1,87 @@
1
+ /**
2
+ * tables.js answers the questions every stage asks about a report's tables:
3
+ * which there are, which dataset each reads, and which one is grouped.
4
+ *
5
+ * A report used to have one table, so each stage found "the" table on its own -
6
+ * and three copies of "the first table in the first band that has one" agreed
7
+ * only because there was never a second. With more than one, a stage that
8
+ * picked a different table from the others would bind rows to one table and
9
+ * group bands to another. So the rules live here, once.
10
+ *
11
+ * Nothing here throws or validates; validate.js says what is wrong with a
12
+ * layout, and these read whatever they are given as far as it goes.
13
+ */
14
+
15
+
16
+ /**
17
+ * Every table in the layout, in band order and then item order.
18
+ *
19
+ * Band order is the file's, which is page order for any layout the designer
20
+ * wrote - and it is the order "the first table" has always meant.
21
+ *
22
+ * @param {object} layout
23
+ * @returns {object[]} the table items themselves, not copies
24
+ */
25
+ export function tablesOf(layout) {
26
+ const found = [];
27
+
28
+ for (const band of (Array.isArray(layout?.bands) ? layout.bands : [])) {
29
+ for (const item of (Array.isArray(band?.items) ? band.items : [])) {
30
+ if (item?.type === 'table') found.push(item);
31
+ }
32
+ }
33
+
34
+ return found;
35
+ }
36
+
37
+
38
+ /**
39
+ * The dataset a table reads its rows from: its own, or the report's.
40
+ *
41
+ * @param {object} table
42
+ * @param {object} layout
43
+ * @returns {string|null}
44
+ */
45
+ export function datasetOf(table, layout) {
46
+ const own = typeof table?.dataset === 'string' && table.dataset !== '' ? table.dataset : null;
47
+ return own ?? layout?.dataset ?? null;
48
+ }
49
+
50
+
51
+ /**
52
+ * Whether a table asks to be grouped by a field of its own.
53
+ * @param {object} table
54
+ * @returns {boolean}
55
+ */
56
+ export function hasOwnGrouping(table) {
57
+ return typeof table?.groupBy === 'string' && table.groupBy !== '';
58
+ }
59
+
60
+
61
+ /**
62
+ * The grouped table, and the field it is grouped by.
63
+ *
64
+ * A table that names its own `groupBy` is the one. Failing that, a report-wide
65
+ * `layout.groupBy` - which is what every layout written before a report could
66
+ * hold two tables says - means the first table, since that was the only one it
67
+ * could have meant.
68
+ *
69
+ * One at most: groupHeader and groupFooter are the report's bands, not a
70
+ * table's, so two grouped tables would have to share one heading design.
71
+ * validate.js refuses a second; this takes the first.
72
+ *
73
+ * @param {object} layout
74
+ * @returns {{table: object, by: string}|null}
75
+ */
76
+ export function groupingOf(layout) {
77
+ const tables = tablesOf(layout);
78
+
79
+ const own = tables.find(hasOwnGrouping);
80
+ if (own) return { table: own, by: own.groupBy };
81
+
82
+ if (layout?.groupBy != null && tables.length > 0) {
83
+ return { table: tables[0], by: layout.groupBy };
84
+ }
85
+
86
+ return null;
87
+ }