report-studio 1.2.2 → 1.3.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/README.md +254 -3
- package/dist/designer-Dj330bWd.js +4690 -0
- package/dist/designer.js +1 -1
- package/dist/index.js +2 -2
- package/dist/react-designer.js +1 -1
- package/dist/react.js +1 -1
- package/dist/viewer-BewnkhfP.js +4028 -0
- package/package.json +2 -2
- package/src/designer/blank.js +0 -1
- package/src/designer/canvas.js +172 -54
- package/src/designer/designer.js +313 -12
- package/src/designer/dialog.js +609 -5
- package/src/designer/fields.js +128 -11
- package/src/designer/geometry.js +77 -4
- package/src/designer/grouping.js +556 -0
- package/src/designer/icons.js +9 -0
- package/src/designer/menu.js +50 -6
- package/src/designer/panel.js +478 -38
- package/src/designer/rulers.js +14 -6
- package/src/designer/sample-data.js +112 -31
- package/src/designer/select.js +3 -2
- package/src/designer/structure.js +126 -12
- package/src/designer/styles.css +661 -27
- package/src/designer/tokens.js +38 -11
- package/src/engine/calculate.js +487 -0
- package/src/engine/csv.js +17 -9
- package/src/engine/format.js +483 -0
- package/src/engine/group.js +350 -70
- package/src/engine/highlight.js +224 -0
- package/src/engine/index.js +8 -1
- package/src/engine/measure.js +53 -26
- package/src/engine/paginate.js +189 -92
- package/src/engine/resolve.js +117 -10
- package/src/engine/rows.js +71 -8
- package/src/engine/search.js +12 -4
- package/src/engine/tables.js +309 -11
- package/src/engine/validate.js +265 -8
- package/src/engine/xlsx-style.js +50 -4
- package/src/engine/xlsx.js +91 -21
- package/src/index.js +18 -0
- package/src/render/items.js +61 -10
- package/src/render/report.css +0 -5
- package/types/index.d.ts +96 -8
- package/types/layout.d.ts +156 -2
- package/dist/designer-DQlfKnTr.js +0 -3416
- package/dist/viewer-BSPNCNRU.js +0 -2912
package/src/engine/paginate.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
|
|
2
2
|
import { pageRegions, bandZoneHeight } from './regions.js';
|
|
3
|
-
import {
|
|
3
|
+
import { levelsOf, groupFrame, fragmentBandsOf, startsPage } from './tables.js';
|
|
4
|
+
import { designHeight, total, headerHeightFor, bandsHeight } from './measure.js';
|
|
5
|
+
import { formatValue, percentFormat } from './format.js';
|
|
6
|
+
import { PERCENTAGE, shownValue } from './group.js';
|
|
7
|
+
import { soleKey } from './resolve.js';
|
|
8
|
+
import { highlightFor } from './highlight.js';
|
|
4
9
|
|
|
5
10
|
|
|
6
11
|
const REGEX = /\{([^{}]+)\}/g;
|
|
@@ -23,8 +28,6 @@ export function paginate(json) {
|
|
|
23
28
|
pageHeader: paginateJSON.bands.find(b => b.type === "pageHeader"),
|
|
24
29
|
pageFooter: paginateJSON.bands.find(b => b.type === "pageFooter"),
|
|
25
30
|
detail: paginateJSON.bands.find(b => b.type === "detail"),
|
|
26
|
-
groupHeader: paginateJSON.bands.find(b => b.type === "groupHeader") || null,
|
|
27
|
-
groupFooter: paginateJSON.bands.find(b => b.type === "groupFooter") || null,
|
|
28
31
|
}
|
|
29
32
|
};
|
|
30
33
|
|
|
@@ -124,7 +127,7 @@ function calPages(bands, context) {
|
|
|
124
127
|
|
|
125
128
|
// spreading the report header and footer from the context.assignedBands
|
|
126
129
|
const { assignedBands, availableHeight } = context;
|
|
127
|
-
const { reportFooter, reportHeader,
|
|
130
|
+
const { reportFooter, reportHeader, pageHeader, pageFooter } = assignedBands;
|
|
128
131
|
|
|
129
132
|
/**
|
|
130
133
|
* The detail zone of a continuation page - no report header, no report
|
|
@@ -310,7 +313,13 @@ function calPages(bands, context) {
|
|
|
310
313
|
|
|
311
314
|
place(calBand, { ...structuredClone(item), y: pageY(itemY) });
|
|
312
315
|
}
|
|
313
|
-
|
|
316
|
+
/**
|
|
317
|
+
* A table goes down the splitting path when it overflows - or when
|
|
318
|
+
* one of its groups has to start a page, which a table placed whole
|
|
319
|
+
* on the page it starts on would never do.
|
|
320
|
+
*/
|
|
321
|
+
else if (item.type == "table"
|
|
322
|
+
&& (localBottom > detailZone() || breaksPages(item))) {
|
|
314
323
|
|
|
315
324
|
/**
|
|
316
325
|
* Where the next slice of this table starts inside the band. The
|
|
@@ -328,13 +337,26 @@ function calPages(bands, context) {
|
|
|
328
337
|
|
|
329
338
|
// when the table has group
|
|
330
339
|
if (item.groups) {
|
|
331
|
-
/**
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
340
|
+
/**
|
|
341
|
+
* What a fragment draws around its rows - column labels, the
|
|
342
|
+
* headers of every level it opens, the footers of every level
|
|
343
|
+
* it closes - is groupFrame's to say, the same rule measure.js
|
|
344
|
+
* sized the table by. A table grouped at one level frames every
|
|
345
|
+
* fragment the same way; a nested one does not, so the frame
|
|
346
|
+
* is asked for afresh for each slice rather than added up once.
|
|
347
|
+
*/
|
|
337
348
|
const tblHeaderHgt = item.headerMeasured ?? headerHeightFor(item);
|
|
349
|
+
const levels = levelsOf(item);
|
|
350
|
+
|
|
351
|
+
/** what the bands and labels around one fragment's rows cost */
|
|
352
|
+
const frameOf = (grp, firstOnPage, ends) => {
|
|
353
|
+
const frame = groupFrame(item, grp, firstOnPage, ends);
|
|
354
|
+
return {
|
|
355
|
+
frame,
|
|
356
|
+
above: (frame.columnHeader ? tblHeaderHgt : 0) + bandsHeight(frame.headers),
|
|
357
|
+
below: bandsHeight(frame.footers)
|
|
358
|
+
};
|
|
359
|
+
};
|
|
338
360
|
|
|
339
361
|
/** the tallest row anywhere in the table, since any group may hold it */
|
|
340
362
|
const tallest = Math.max(
|
|
@@ -342,19 +364,25 @@ function calPages(bands, context) {
|
|
|
342
364
|
...(item.groups ?? []).flatMap(grp =>
|
|
343
365
|
heightsFor(item, grp.rows, grp.rowHeights)));
|
|
344
366
|
|
|
345
|
-
|
|
346
|
-
|
|
367
|
+
/**
|
|
368
|
+
* The most a slice can need: the labels, every level's
|
|
369
|
+
* header, one row and every level's footer - a group that is
|
|
370
|
+
* the last of its region and of its city at once.
|
|
371
|
+
*/
|
|
372
|
+
const reservedSpace = tblHeaderHgt + tallest
|
|
373
|
+
+ total(levels.map(level => level?.header?.measuredHeight ?? 0))
|
|
374
|
+
+ total(levels.map(level => level?.footer?.measuredHeight ?? 0));
|
|
347
375
|
|
|
348
376
|
/**
|
|
349
377
|
* same trap as the ungrouped path: if the minimum viable slice
|
|
350
|
-
* (table header + group
|
|
351
|
-
* than an empty page we would allocate pages forever.
|
|
378
|
+
* (table header + group headers + one row + group footers) is
|
|
379
|
+
* taller than an empty page we would allocate pages forever.
|
|
352
380
|
*/
|
|
353
381
|
const sliceFitsAnyPage = reservedSpace <= freshPageHeight;
|
|
354
382
|
|
|
355
383
|
if (!sliceFitsAnyPage) {
|
|
356
384
|
console.warn(
|
|
357
|
-
`Table "${item.id}": table header + group
|
|
385
|
+
`Table "${item.id}": table header + group headers + one row + group footers ` +
|
|
358
386
|
`(${reservedSpace}px) exceeds the ${freshPageHeight}px printable page. ` +
|
|
359
387
|
`Falling back to one row per page; rows will overflow their page.`
|
|
360
388
|
);
|
|
@@ -363,28 +391,29 @@ function calPages(bands, context) {
|
|
|
363
391
|
/**
|
|
364
392
|
* How many rows of a group fit in {room} px.
|
|
365
393
|
*
|
|
366
|
-
* The group
|
|
367
|
-
* group, so
|
|
368
|
-
* finish it. Without that the last slice of a group
|
|
369
|
-
*
|
|
370
|
-
* grew past its zone straight over the page footer.
|
|
394
|
+
* The group footers only print on the fragment that finishes
|
|
395
|
+
* the group, so they are reserved only when this page could
|
|
396
|
+
* actually finish it. Without that the last slice of a group
|
|
397
|
+
* added footer height that nobody had budgeted for, and the
|
|
398
|
+
* detail band grew past its zone straight over the page footer.
|
|
371
399
|
*
|
|
372
|
-
* When the rows would fit but the
|
|
373
|
-
* deliberately held back so the
|
|
400
|
+
* When the rows would fit but the footers would not, one row is
|
|
401
|
+
* deliberately held back so the footers land on the next page
|
|
374
402
|
* beside a row rather than alone.
|
|
375
403
|
* @param {number[]} heights the group's row heights, in order
|
|
376
404
|
* @param {number} from the first row of this group still unplaced
|
|
377
405
|
* @param {number} room px left in the detail zone below the table top
|
|
378
406
|
* @param {number} rowsLeft rows still unplaced in this group
|
|
407
|
+
* @param {number} above labels and headers over this slice
|
|
408
|
+
* @param {number} below footers under it, if it ends the group
|
|
379
409
|
* @returns {number} rows to place now, possibly 0
|
|
380
410
|
*/
|
|
381
|
-
const fitRows = (heights, from, room, rowsLeft) => {
|
|
382
|
-
const body = room -
|
|
411
|
+
const fitRows = (heights, from, room, rowsLeft, above, below) => {
|
|
412
|
+
const body = room - above;
|
|
383
413
|
|
|
384
|
-
const withFooter = rowsThatFit(
|
|
385
|
-
heights, from, body - grpFooterHgt, rowsLeft);
|
|
414
|
+
const withFooter = rowsThatFit(heights, from, body - below, rowsLeft);
|
|
386
415
|
|
|
387
|
-
/** the group ends here, so its
|
|
416
|
+
/** the group ends here, so its footers have to fit here too */
|
|
388
417
|
if (rowsLeft <= withFooter) return rowsLeft;
|
|
389
418
|
|
|
390
419
|
return Math.min(
|
|
@@ -394,25 +423,37 @@ function calPages(bands, context) {
|
|
|
394
423
|
};
|
|
395
424
|
|
|
396
425
|
|
|
397
|
-
/**
|
|
398
|
-
* this groups variable helps us to know for current page how much groups are their
|
|
399
|
-
*/
|
|
426
|
+
/** the fragments placed on the page being built */
|
|
400
427
|
let calGroups = [];
|
|
401
428
|
|
|
402
429
|
let calItemHeight = 0; // used to cal the item hgt
|
|
403
430
|
|
|
404
431
|
|
|
405
|
-
|
|
406
432
|
/**
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
433
|
+
* Each innermost group in turn - for a one-level table, each
|
|
434
|
+
* group. A group that does not fit what is left of a page has
|
|
435
|
+
* as many rows placed as do, and carries on at the top of the
|
|
436
|
+
* next with its headers drawn again.
|
|
411
437
|
*/
|
|
438
|
+
for (const [n, grp] of item.groups.entries()) {
|
|
412
439
|
|
|
440
|
+
/**
|
|
441
|
+
* A group at a level that says `newPage` closes the page
|
|
442
|
+
* the group before it is on, whatever room is left.
|
|
443
|
+
*/
|
|
444
|
+
if (startsPage(item, grp, n) && calGroups.length > 0) {
|
|
445
|
+
const dupItem = structuredClone(item);
|
|
413
446
|
|
|
414
|
-
|
|
447
|
+
dupItem.groups = calGroups;
|
|
448
|
+
dupItem.y = tableY;
|
|
449
|
+
dupItem.measuredHeight = calItemHeight;
|
|
450
|
+
place(calBand, dupItem);
|
|
415
451
|
|
|
452
|
+
calBand = breakPage(calBand, band, itemY);
|
|
453
|
+
calGroups = [];
|
|
454
|
+
calItemHeight = 0;
|
|
455
|
+
tableY = 0;
|
|
456
|
+
}
|
|
416
457
|
|
|
417
458
|
let currIdx = 0;
|
|
418
459
|
const rowLen = grp.rows.length;
|
|
@@ -423,19 +464,24 @@ function calPages(bands, context) {
|
|
|
423
464
|
/** the table starts at tableY, so only what is below it is usable */
|
|
424
465
|
const roomHgt = detailZone() - tableY - calItemHeight;
|
|
425
466
|
|
|
467
|
+
const firstOnPage = calGroups.length === 0;
|
|
468
|
+
|
|
426
469
|
/** a page we have already emptied cannot be emptied again - take the slice as is */
|
|
427
|
-
const isFreshPage =
|
|
470
|
+
const isFreshPage = firstOnPage
|
|
428
471
|
&& calBand.items.length === 0
|
|
429
472
|
&& tableY === 0
|
|
430
473
|
&& detailZone() >= freshPageHeight;
|
|
431
474
|
|
|
475
|
+
const { above } = frameOf(grp, firstOnPage, false);
|
|
476
|
+
const { below } = frameOf(grp, firstOnPage, true);
|
|
477
|
+
|
|
432
478
|
/**
|
|
433
479
|
* Rows this page can take. fitRows reserves the group
|
|
434
|
-
*
|
|
435
|
-
* it hands back always fits the detail zone.
|
|
480
|
+
* footers whenever the group would end here, so the
|
|
481
|
+
* slice it hands back always fits the detail zone.
|
|
436
482
|
*/
|
|
437
483
|
let canFitRows = fitRows(
|
|
438
|
-
heights, currIdx, roomHgt, rowLen - currIdx);
|
|
484
|
+
heights, currIdx, roomHgt, rowLen - currIdx, above, below);
|
|
439
485
|
|
|
440
486
|
/**
|
|
441
487
|
* A page we have already emptied cannot be emptied again,
|
|
@@ -470,31 +516,12 @@ function calPages(bands, context) {
|
|
|
470
516
|
|
|
471
517
|
const hasMoreRows = currIdx < rowLen;
|
|
472
518
|
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
+ (!hasMoreRows ? grpFooterHgt : 0);
|
|
476
|
-
|
|
477
|
-
calItemHeight += occHgt;
|
|
519
|
+
const { frame, above: drawnAbove, below: drawnBelow } =
|
|
520
|
+
frameOf(grp, firstOnPage, !hasMoreRows);
|
|
478
521
|
|
|
479
|
-
|
|
480
|
-
* groupHeader / groupFooter are templates, not flow bands - they never
|
|
481
|
-
* land on a page of their own. Resolve them per fragment here, where the
|
|
482
|
-
* group's own rows and aggregates are in hand, so render/ only has to draw.
|
|
483
|
-
*/
|
|
484
|
-
let grpFragment = {
|
|
485
|
-
key: grp.key,
|
|
486
|
-
rows: rows,
|
|
487
|
-
rowHeights: slice,
|
|
488
|
-
aggregates: grp.aggregates,
|
|
489
|
-
showHeader: true,
|
|
490
|
-
showFooter: !hasMoreRows,
|
|
491
|
-
headerBand: resolveGroupBand(groupHeader, grp, rows[0]),
|
|
492
|
-
footerBand: !hasMoreRows
|
|
493
|
-
? resolveGroupBand(groupFooter, grp, rows[0])
|
|
494
|
-
: null
|
|
495
|
-
}
|
|
522
|
+
calItemHeight += total(slice) + drawnAbove + drawnBelow;
|
|
496
523
|
|
|
497
|
-
calGroups.push(
|
|
524
|
+
calGroups.push(groupFragment(grp, frame, rows, slice, !hasMoreRows));
|
|
498
525
|
}
|
|
499
526
|
}
|
|
500
527
|
|
|
@@ -600,7 +627,7 @@ function calPages(bands, context) {
|
|
|
600
627
|
* enough to overflow.
|
|
601
628
|
*/
|
|
602
629
|
const placed = item.type === "table" && item.groups
|
|
603
|
-
? { ...item, groups: wholeGroupFragments(item
|
|
630
|
+
? { ...item, groups: wholeGroupFragments(item) }
|
|
604
631
|
: item;
|
|
605
632
|
|
|
606
633
|
place(calBand, { ...structuredClone(placed), y: pageY(itemY) });
|
|
@@ -683,25 +710,71 @@ function anchorBands(pages) {
|
|
|
683
710
|
}
|
|
684
711
|
}
|
|
685
712
|
|
|
713
|
+
/**
|
|
714
|
+
* Whether any group of a table has to start a page of its own.
|
|
715
|
+
* @param {object} item a table
|
|
716
|
+
* @returns {boolean}
|
|
717
|
+
*/
|
|
718
|
+
function breaksPages(item) {
|
|
719
|
+
return Array.isArray(item.groups) && item.groups.some((grp, n) => startsPage(item, grp, n));
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
|
|
686
723
|
/**
|
|
687
724
|
* Every group of a table that fits on one page, as unsplit fragments. Gives the
|
|
688
725
|
* no-overflow path the same shape the splitting path produces.
|
|
689
|
-
* @param {object} item the table item carrying groups
|
|
690
|
-
* @param {object|null} groupHeader
|
|
691
|
-
* @param {object|null} groupFooter
|
|
726
|
+
* @param {object} item the table item carrying groups, and its own group bands
|
|
692
727
|
* @returns {object[]}
|
|
693
728
|
*/
|
|
694
|
-
function wholeGroupFragments(item
|
|
695
|
-
return item.groups.map(grp => (
|
|
729
|
+
function wholeGroupFragments(item) {
|
|
730
|
+
return item.groups.map((grp, n) => groupFragment(
|
|
731
|
+
grp, groupFrame(item, grp, n === 0, true), grp.rows, grp.rowHeights, true));
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* One group's rows on one page, with the bands drawn around them resolved for
|
|
737
|
+
* that group - which is everything render/ needs to draw it.
|
|
738
|
+
*
|
|
739
|
+
* showHeader whether the table's column labels head this fragment
|
|
740
|
+
* showFooter whether this fragment ends its group
|
|
741
|
+
* headerBands the group headers over the rows, outermost first
|
|
742
|
+
* footerBands the group footers under them, innermost first
|
|
743
|
+
* headerBand the innermost level's header, when it is drawn here, and
|
|
744
|
+
* footerBand its footer: all of a one-level table's group bands, under
|
|
745
|
+
* the names they have always had
|
|
746
|
+
*
|
|
747
|
+
* Each band in the lists carries `level`, 0 for the outermost.
|
|
748
|
+
*
|
|
749
|
+
* @param {object} grp the innermost group, from group.js
|
|
750
|
+
* @param {{columnHeader: boolean, headers: object[], footers: object[]}} frame from groupFrame
|
|
751
|
+
* @param {object[]} rows the rows placed
|
|
752
|
+
* @param {number[]} rowHeights their heights
|
|
753
|
+
* @param {boolean} ends whether this is the group's last fragment
|
|
754
|
+
* @returns {object}
|
|
755
|
+
*/
|
|
756
|
+
function groupFragment(grp, frame, rows, rowHeights, ends) {
|
|
757
|
+
const innermost = (grp.levels?.length ?? 1) - 1;
|
|
758
|
+
|
|
759
|
+
const resolved = ({ level, band }) => resolveGroupBand(
|
|
760
|
+
band, grp.levels?.[level] ?? grp, rows[0], level);
|
|
761
|
+
|
|
762
|
+
const headerBands = frame.headers.map(resolved);
|
|
763
|
+
const footerBands = frame.footers.map(resolved);
|
|
764
|
+
|
|
765
|
+
return {
|
|
696
766
|
key: grp.key,
|
|
697
|
-
|
|
698
|
-
|
|
767
|
+
path: grp.path ?? [grp.key],
|
|
768
|
+
rows,
|
|
769
|
+
rowHeights,
|
|
699
770
|
aggregates: grp.aggregates,
|
|
700
|
-
showHeader:
|
|
701
|
-
showFooter:
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
771
|
+
showHeader: frame.columnHeader,
|
|
772
|
+
showFooter: ends,
|
|
773
|
+
headerBands,
|
|
774
|
+
footerBands,
|
|
775
|
+
headerBand: headerBands.find(band => band.level === innermost) ?? null,
|
|
776
|
+
footerBand: footerBands.find(band => band.level === innermost) ?? null
|
|
777
|
+
};
|
|
705
778
|
}
|
|
706
779
|
|
|
707
780
|
|
|
@@ -709,15 +782,18 @@ function wholeGroupFragments(item, groupHeader, groupFooter) {
|
|
|
709
782
|
* Fills in a groupHeader / groupFooter template for one group fragment.
|
|
710
783
|
* Unlike the aggregate pass in group.js this keeps the literal text around a
|
|
711
784
|
* placeholder, so "Subtotal: {sum(price)}" stays "Subtotal: 160".
|
|
712
|
-
* @param {object|null} bandTemplate the
|
|
713
|
-
* @param {object} grp the group
|
|
785
|
+
* @param {object|null} bandTemplate the table's own groupHeader / groupFooter band
|
|
786
|
+
* @param {object} grp the group the band is drawn for, carrying its aggregates -
|
|
787
|
+
* for an outer level's band, that level's group
|
|
714
788
|
* @param {object} sampleRow any row of the group - all share the groupBy value
|
|
789
|
+
* @param {number} [level=0] the level the band is drawn for
|
|
715
790
|
* @returns {object|null} a resolved clone, or null when no template exists
|
|
716
791
|
*/
|
|
717
|
-
function resolveGroupBand(bandTemplate, grp, sampleRow) {
|
|
792
|
+
function resolveGroupBand(bandTemplate, grp, sampleRow, level = 0) {
|
|
718
793
|
if (!bandTemplate) return null;
|
|
719
794
|
|
|
720
795
|
const resolved = structuredClone(bandTemplate);
|
|
796
|
+
resolved.level = level;
|
|
721
797
|
|
|
722
798
|
for (const item of resolved.items) {
|
|
723
799
|
if (item.type !== "text") continue;
|
|
@@ -725,10 +801,26 @@ function resolveGroupBand(bandTemplate, grp, sampleRow) {
|
|
|
725
801
|
* Build on `text`, not `value` - resolve.js has already put the root
|
|
726
802
|
* data in and deliberately left everything group-scoped standing.
|
|
727
803
|
*/
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
804
|
+
const sole = soleKey(item.value);
|
|
805
|
+
|
|
806
|
+
item.text = (item.text ?? item.value).replace(REGEX, (match, key) => {
|
|
807
|
+
const found = groupPlaceholder(match, key.trim(), grp, sampleRow);
|
|
808
|
+
|
|
809
|
+
/** the value itself, for an item that prints nothing else - a subtotal */
|
|
810
|
+
if (sole === key.trim() && found.value !== undefined) {
|
|
811
|
+
item.raw = found.value;
|
|
812
|
+
if (found.share) item.rawFormat = percentFormat(item.format);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
if (found.value === undefined) return found.text;
|
|
816
|
+
if (found.value == null) return '';
|
|
817
|
+
if (found.share) return formatValue(found.value, percentFormat(item.format));
|
|
818
|
+
return formatValue(found.value, found.count ? null : item.format);
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
/** a subtotal's rules, asked of this group's total; a field reads the group's row */
|
|
822
|
+
const lit = highlightFor(item.highlight, shownValue(item), sampleRow);
|
|
823
|
+
if (lit) item.highlighted = lit;
|
|
732
824
|
}
|
|
733
825
|
|
|
734
826
|
return resolved;
|
|
@@ -744,21 +836,24 @@ function resolveGroupBand(bandTemplate, grp, sampleRow) {
|
|
|
744
836
|
* @param {string} key
|
|
745
837
|
* @param {object} grp
|
|
746
838
|
* @param {object} sampleRow
|
|
747
|
-
* @returns {string}
|
|
839
|
+
* @returns {{text?: string, value?: *, count?: boolean}} `text` for a token
|
|
840
|
+
* left standing, else the `value` it stands for - formatted by the caller
|
|
748
841
|
*/
|
|
749
842
|
function groupPlaceholder(match, key, grp, sampleRow) {
|
|
750
|
-
if (PAGE_KEYS.includes(key)) return match;
|
|
843
|
+
if (PAGE_KEYS.includes(key)) return { text: match };
|
|
751
844
|
|
|
752
845
|
const expr = key.match(/^(\w+)\((.*?)\)$/);
|
|
753
846
|
|
|
754
847
|
if (expr) {
|
|
755
848
|
const [, fn, field] = expr;
|
|
756
|
-
|
|
757
|
-
|
|
849
|
+
return {
|
|
850
|
+
value: grp.aggregates?.[field]?.[fn.trim()] ?? null,
|
|
851
|
+
count: fn.trim() === 'count',
|
|
852
|
+
share: fn.trim() === PERCENTAGE
|
|
853
|
+
};
|
|
758
854
|
}
|
|
759
855
|
|
|
760
|
-
|
|
761
|
-
return value == null ? '' : String(value);
|
|
856
|
+
return { value: sampleRow?.[key] ?? null };
|
|
762
857
|
}
|
|
763
858
|
|
|
764
859
|
|
|
@@ -857,8 +952,10 @@ function resolvePageKeys(band, pageNO, totalPages) {
|
|
|
857
952
|
|
|
858
953
|
if (item.type === "table" && item.groups) {
|
|
859
954
|
for (const fragment of item.groups) {
|
|
860
|
-
|
|
861
|
-
|
|
955
|
+
const { headers, footers } = fragmentBandsOf(fragment);
|
|
956
|
+
for (const groupBand of [...headers, ...footers]) {
|
|
957
|
+
resolvePageKeys(groupBand, pageNO, totalPages);
|
|
958
|
+
}
|
|
862
959
|
}
|
|
863
960
|
}
|
|
864
961
|
}
|
package/src/engine/resolve.js
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import { assertLayout, assertData } from './validate.js';
|
|
14
|
+
import { levelsOf, tablesOf } from './tables.js';
|
|
15
|
+
import { effectiveFormat, formatValue } from './format.js';
|
|
14
16
|
|
|
15
17
|
|
|
16
18
|
const REGEX = /\{([^{}]+)\}/g;
|
|
@@ -70,6 +72,60 @@ export function isRowScoped(type) {
|
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
|
|
75
|
+
/**
|
|
76
|
+
* The key of a text item that is one placeholder and nothing else - "{total}"
|
|
77
|
+
* or "{sum(amount)}" - or null. Such an item prints one value, and the stage
|
|
78
|
+
* that fills it in keeps that value as `raw`, so a spreadsheet can hold the
|
|
79
|
+
* number rather than the text it was printed as.
|
|
80
|
+
* @param {string} value a text item's value
|
|
81
|
+
* @returns {string|null}
|
|
82
|
+
*/
|
|
83
|
+
export function soleKey(value) {
|
|
84
|
+
const match = typeof value === 'string' ? value.trim().match(/^{([^{}]+)}$/) : null;
|
|
85
|
+
return match ? match[1].trim() : null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Every format in the layout, with the report's defaults filled in under it.
|
|
91
|
+
*
|
|
92
|
+
* Done once, here, on the copy every later stage works on - so measure.js, the
|
|
93
|
+
* renderer and the exporters read a text item's or a column's `format` as it
|
|
94
|
+
* stands, and none of them has to be handed the report's defaults as well.
|
|
95
|
+
* @param {object} layout the copy being resolved
|
|
96
|
+
*/
|
|
97
|
+
function settleFormats(layout) {
|
|
98
|
+
const defaults = layout.format;
|
|
99
|
+
const settle = (owner) => {
|
|
100
|
+
const settled = effectiveFormat(owner?.format, defaults);
|
|
101
|
+
if (settled) owner.format = settled;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const textIn = (band) => (Array.isArray(band?.items) ? band.items : [])
|
|
105
|
+
.filter(item => item?.type === 'text');
|
|
106
|
+
|
|
107
|
+
for (const band of layout.bands) textIn(band).forEach(settle);
|
|
108
|
+
|
|
109
|
+
for (const table of tablesOf(layout)) {
|
|
110
|
+
for (const column of (Array.isArray(table.columns) ? table.columns : [])) {
|
|
111
|
+
/**
|
|
112
|
+
* A column whose calculation is a share holds 0.25 for a quarter;
|
|
113
|
+
* printed as it comes that reads as a number, not a share.
|
|
114
|
+
*/
|
|
115
|
+
if (column && !column.format && /^\s*percentage\s*\(/.test(column.expression ?? '')) {
|
|
116
|
+
column.format = { type: 'percent' };
|
|
117
|
+
}
|
|
118
|
+
settle(column);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const level of levelsOf(table)) {
|
|
122
|
+
textIn(level?.header).forEach(settle);
|
|
123
|
+
textIn(level?.footer).forEach(settle);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
73
129
|
/**
|
|
74
130
|
* Resolves the report JSON with the provided data
|
|
75
131
|
* @param {*} rptJson
|
|
@@ -84,6 +140,8 @@ export function resolve(rptJson, rptData) {
|
|
|
84
140
|
// so that report will not affect
|
|
85
141
|
const resolved = structuredClone(rptJson);
|
|
86
142
|
|
|
143
|
+
settleFormats(resolved);
|
|
144
|
+
|
|
87
145
|
for (const band of resolved.bands) {
|
|
88
146
|
/**
|
|
89
147
|
* A group band's placeholders are mostly row fields ({name} is the
|
|
@@ -91,14 +149,17 @@ export function resolve(rptJson, rptData) {
|
|
|
91
149
|
* this stage cannot resolve there is deferred rather than blanked, so
|
|
92
150
|
* grouping still has it to work with.
|
|
93
151
|
*/
|
|
94
|
-
|
|
152
|
+
resolveBand(band, rptData, GROUP_BANDS.includes(band.type), resolved.format);
|
|
95
153
|
|
|
154
|
+
/** a table's own group bands are group bands just the same */
|
|
96
155
|
for (const item of band.items) {
|
|
97
|
-
if (item.type
|
|
98
|
-
item.text = resolveTxt(item, rptData, deferUnknown);
|
|
156
|
+
if (item.type !== 'table') continue;
|
|
99
157
|
|
|
100
|
-
|
|
101
|
-
|
|
158
|
+
for (const level of levelsOf(item)) {
|
|
159
|
+
for (const groupBand of [level?.header, level?.footer]) {
|
|
160
|
+
if (Array.isArray(groupBand?.items)) resolveBand(groupBand, rptData, true, resolved.format);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
102
163
|
}
|
|
103
164
|
}
|
|
104
165
|
|
|
@@ -106,6 +167,24 @@ export function resolve(rptJson, rptData) {
|
|
|
106
167
|
}
|
|
107
168
|
|
|
108
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Fills in what the root data can answer on one band's items.
|
|
172
|
+
* @param {object} band
|
|
173
|
+
* @param {object} rptData
|
|
174
|
+
* @param {boolean} deferUnknown leave an unresolvable key for the grouping stage
|
|
175
|
+
* @param {object} [defaults] the report's own format, for what an item leaves unsaid
|
|
176
|
+
*/
|
|
177
|
+
function resolveBand(band, rptData, deferUnknown, defaults) {
|
|
178
|
+
for (const item of band.items) {
|
|
179
|
+
if (item.type == "text")
|
|
180
|
+
item.text = resolveTxt(item, rptData, deferUnknown, defaults);
|
|
181
|
+
|
|
182
|
+
if (item.type == "image")
|
|
183
|
+
item.resolvedSrc = resolveImage(item, rptData);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
109
188
|
/**
|
|
110
189
|
* The picture an image item shows: the value at its `field` when the data has
|
|
111
190
|
* one there, and the `src` stored in the layout otherwise - so a report can
|
|
@@ -147,9 +226,18 @@ function resolveImage(item, rptData) {
|
|
|
147
226
|
* @param {boolean} deferUnknown leave an unresolvable key for a later stage
|
|
148
227
|
* @returns
|
|
149
228
|
*/
|
|
150
|
-
function resolveTxt(item, rptData, deferUnknown) {
|
|
229
|
+
function resolveTxt(item, rptData, deferUnknown, defaults) {
|
|
230
|
+
const sole = soleKey(item.value);
|
|
231
|
+
|
|
151
232
|
return item.value.replace(REGEX, (match, key) => {
|
|
152
|
-
|
|
233
|
+
const out = validatePlaceHolder(match, key.trim(), rptData, item, deferUnknown, defaults);
|
|
234
|
+
|
|
235
|
+
/** the value itself, for an item that prints nothing else */
|
|
236
|
+
if (sole === key.trim() && out !== match) {
|
|
237
|
+
item.raw = sole === 'today' ? new Date() : (findVal(sole, rptData) ?? null);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return out;
|
|
153
241
|
})
|
|
154
242
|
}
|
|
155
243
|
|
|
@@ -164,7 +252,7 @@ function resolveTxt(item, rptData, deferUnknown) {
|
|
|
164
252
|
* @param {boolean} deferUnknown
|
|
165
253
|
* @returns
|
|
166
254
|
*/
|
|
167
|
-
function validatePlaceHolder(match, key, rptData, item, deferUnknown) {
|
|
255
|
+
function validatePlaceHolder(match, key, rptData, item, deferUnknown, defaults) {
|
|
168
256
|
/**
|
|
169
257
|
* Handed on rather than reported. An aggregate belongs to grouping and a
|
|
170
258
|
* page key belongs to pagination, so neither is resolvable here and neither
|
|
@@ -176,7 +264,12 @@ function validatePlaceHolder(match, key, rptData, item, deferUnknown) {
|
|
|
176
264
|
|
|
177
265
|
if (PAGE_KEYS.includes(key)) return match;
|
|
178
266
|
|
|
179
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Today as the item's own date format, or the report's date pattern - in
|
|
269
|
+
* the local calendar, not UTC's, which before half past five in the
|
|
270
|
+
* morning in India was still yesterday.
|
|
271
|
+
*/
|
|
272
|
+
if (key == 'today') return formatValue(new Date(), todayFormat(item.format, defaults));
|
|
180
273
|
|
|
181
274
|
const value = findVal(key, rptData);
|
|
182
275
|
|
|
@@ -187,7 +280,21 @@ function validatePlaceHolder(match, key, rptData, item, deferUnknown) {
|
|
|
187
280
|
return '';
|
|
188
281
|
}
|
|
189
282
|
|
|
190
|
-
return
|
|
283
|
+
return formatValue(value, item.format);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The format {today} prints with: the item's, when it is a date format; else a
|
|
289
|
+
* plain date in the pattern the item's format carries from the report - which
|
|
290
|
+
* is ISO unless the report says otherwise.
|
|
291
|
+
* @param {object|undefined} format the item's, settled
|
|
292
|
+
* @param {object|undefined} defaults the report's
|
|
293
|
+
* @returns {object}
|
|
294
|
+
*/
|
|
295
|
+
function todayFormat(format, defaults) {
|
|
296
|
+
if (format?.type === 'date' || format?.type === 'datetime') return format;
|
|
297
|
+
return { type: 'date', pattern: format?.pattern ?? defaults?.pattern ?? 'yyyy-MM-dd' };
|
|
191
298
|
}
|
|
192
299
|
|
|
193
300
|
|