report-studio 0.0.1 → 1.0.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 -0
- package/README.md +238 -4
- package/dist/designer.js +3209 -0
- package/dist/index.js +2 -4
- package/dist/viewer-D-c0lKzQ.js +1576 -0
- package/package.json +66 -33
- package/src/asset/images/icon.png +0 -0
- package/src/asset/images/logo.png +0 -0
- package/src/cli/index.js +282 -0
- package/src/cli/serve.js +135 -0
- package/src/designer/blank.js +53 -0
- package/src/designer/canvas.js +518 -0
- package/src/designer/designer.js +1900 -0
- package/src/designer/dialog.js +708 -0
- package/src/designer/fields.js +584 -0
- package/src/designer/geometry.js +154 -0
- package/src/designer/guides.js +370 -0
- package/src/designer/history.js +113 -0
- package/src/designer/icons.js +118 -0
- package/src/designer/index.html +28 -0
- package/src/designer/main.js +39 -0
- package/src/designer/menu.js +126 -0
- package/src/designer/panel.js +478 -0
- package/src/designer/preview.js +95 -0
- package/src/designer/rulers.js +231 -0
- package/src/designer/sample-data.js +240 -0
- package/src/designer/select.js +699 -0
- package/src/designer/structure.js +604 -0
- package/src/designer/styles.css +1616 -0
- package/src/designer/toast.js +62 -0
- package/src/designer/tokens.js +284 -0
- package/src/engine/csv.js +315 -0
- package/src/engine/group.js +304 -0
- package/src/engine/index.js +34 -0
- package/src/engine/measure.js +390 -0
- package/src/engine/paginate.js +861 -0
- package/src/engine/regions.js +175 -0
- package/src/engine/resolve.js +171 -0
- package/src/engine/search.js +129 -0
- package/src/engine/text-metrics.js +206 -0
- package/src/engine/validate.js +328 -0
- package/src/index.html +79 -0
- package/src/index.js +50 -0
- package/src/preview/chrome.css +505 -0
- package/src/preview/chrome.js +147 -0
- package/src/preview/index.html +28 -0
- package/src/preview/main.js +66 -0
- package/src/preview/styles.css +16 -0
- package/src/preview/viewer.js +745 -0
- package/src/render/items.js +425 -0
- package/src/render/render.js +98 -0
- package/src/render/report.css +202 -0
- package/src/server/routes.js +331 -0
- package/src/shared/brand.css +29 -0
- package/src/shared/dropdown.css +136 -0
- package/src/shared/dropdown.js +166 -0
- package/src/shared/modal.css +125 -0
- package/src/shared/prefix.js +13 -0
- package/src/shared/store.js +145 -0
- package/src/shared/theme.css +72 -0
- package/src/shared/window.js +138 -0
- package/src/vite/index.js +53 -0
- package/dist/react.js +0 -4
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* items.js draws the things that live inside a band: text boxes and tables.
|
|
3
|
+
*
|
|
4
|
+
* Split out of render.js because the designer canvas needs exactly this and
|
|
5
|
+
* nothing more. The preview wraps these in anchored, paginated bands; the
|
|
6
|
+
* designer wraps them in band zones on a single unpaginated page. Spec 5.2 asks
|
|
7
|
+
* for one drawing path so that what is designed looks like what is printed -
|
|
8
|
+
* two paths drift, and the drift is always found by a user rather than a test.
|
|
9
|
+
*
|
|
10
|
+
* Every element carries data-item-id and data-item-type. The preview ignores
|
|
11
|
+
* them; the designer uses them to take a pointer event back to a layout item
|
|
12
|
+
* without maintaining a parallel element map that can fall out of step.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/** items are placed against the band box, which the caller has made relative */
|
|
17
|
+
export const position = (p) => `position:absolute;top:${px(p?.y)}px;left:${px(p?.x)}px;`;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A number on its way into a style attribute.
|
|
22
|
+
*
|
|
23
|
+
* A layout is JSON, and JSON says nothing about what type a field holds: a `w`
|
|
24
|
+
* that arrived as a string writes `width:undefinedpx` at best and closes the
|
|
25
|
+
* attribute at worst. Anything that is not a finite number is the fallback.
|
|
26
|
+
*
|
|
27
|
+
* @param {*} value
|
|
28
|
+
* @param {number} [fallback]
|
|
29
|
+
* @returns {number}
|
|
30
|
+
*/
|
|
31
|
+
export function px(value, fallback = 0) {
|
|
32
|
+
const n = Number(value);
|
|
33
|
+
return Number.isFinite(n) ? n : fallback;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A style *value* on its way into a `style="..."` attribute.
|
|
39
|
+
*
|
|
40
|
+
* `esc` is for text between tags; this is for the other half. Item text comes
|
|
41
|
+
* from the host's data and is escaped on the way in, but a colour, a font
|
|
42
|
+
* family or a background comes from the layout - and a layout is a file, which
|
|
43
|
+
* an application may well let its own users author. Written raw, a colour of
|
|
44
|
+
* `red" onmouseover="alert(1)` closed the attribute and put a live event
|
|
45
|
+
* handler on the element.
|
|
46
|
+
*
|
|
47
|
+
* Removed rather than entity-escaped: `"` inside a style attribute is a
|
|
48
|
+
* quote again by the time CSS parses it, so escaping would move the hole
|
|
49
|
+
* rather than close it. None of these characters appear in a colour, a length
|
|
50
|
+
* or a font stack, so nothing legitimate is lost - and `;` and `{}` go too, so
|
|
51
|
+
* a value cannot append a declaration of its own to the ones around it.
|
|
52
|
+
*
|
|
53
|
+
* Brackets stay, because `rgb(...)` and `linear-gradient(...)` are ordinary
|
|
54
|
+
* things to write in a layout. `url(` alone does not: it is the one CSS
|
|
55
|
+
* function that fetches, and a report that quietly calls home when it is
|
|
56
|
+
* opened is not a report anybody asked for.
|
|
57
|
+
*
|
|
58
|
+
* @param {*} value
|
|
59
|
+
* @param {string} [fallback] what an empty or absent value becomes
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
export function css(value, fallback = '') {
|
|
63
|
+
if (value == null) return fallback;
|
|
64
|
+
|
|
65
|
+
const clean = String(value)
|
|
66
|
+
.replace(/[<>"'`&;{}\\]/g, '')
|
|
67
|
+
.replace(/url\s*\(/gi, '')
|
|
68
|
+
.replace(/\s+/g, ' ')
|
|
69
|
+
.trim();
|
|
70
|
+
|
|
71
|
+
return clean === '' ? fallback : clean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Everything that reaches the page came from user data, so it is escaped on the
|
|
77
|
+
* way into the markup. Without this a value like "<script>" is executed rather
|
|
78
|
+
* than printed.
|
|
79
|
+
* @param {*} value
|
|
80
|
+
* @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
export function esc(value) {
|
|
83
|
+
if (value == null) return '';
|
|
84
|
+
return String(value)
|
|
85
|
+
.replace(/&/g, '&')
|
|
86
|
+
.replace(/</g, '<')
|
|
87
|
+
.replace(/>/g, '>')
|
|
88
|
+
.replace(/"/g, '"')
|
|
89
|
+
.replace(/'/g, ''');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Draws every item in a band, in layout order.
|
|
95
|
+
* @param {object[]} list the band's items
|
|
96
|
+
* @returns {string} markup
|
|
97
|
+
*/
|
|
98
|
+
export function items(list) {
|
|
99
|
+
return (list || []).map(i => {
|
|
100
|
+
switch (i.type) {
|
|
101
|
+
case "text":
|
|
102
|
+
return text(i);
|
|
103
|
+
case "table":
|
|
104
|
+
return table(i)
|
|
105
|
+
case "line":
|
|
106
|
+
return line(i)
|
|
107
|
+
case "box":
|
|
108
|
+
return box(i)
|
|
109
|
+
default:
|
|
110
|
+
console.warn(`Unknown item type "${i?.type}"; it is not drawn.`);
|
|
111
|
+
return '';
|
|
112
|
+
}
|
|
113
|
+
}).join("");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
function text(i) {
|
|
118
|
+
/**
|
|
119
|
+
* `?? {}` the way box and line have always had it. A text item with no
|
|
120
|
+
* style block is a layout validateLayout accepts - it asks for a style to
|
|
121
|
+
* be an object *if there is one* - and reading straight through it threw a
|
|
122
|
+
* TypeError out of the renderer for a layout the validator had just called
|
|
123
|
+
* fine. Written by hand or generated by an API rather than drawn, but the
|
|
124
|
+
* two halves of the package have to agree about what they accept.
|
|
125
|
+
*/
|
|
126
|
+
const style = i.style ?? {};
|
|
127
|
+
|
|
128
|
+
return `
|
|
129
|
+
<p id="${esc(i.id)}" style="
|
|
130
|
+
width:${px(i.w)}px;
|
|
131
|
+
height:${px(i.h)}px;
|
|
132
|
+
${position(i)}
|
|
133
|
+
font-size:${px(style.fontSize, 12)}px;
|
|
134
|
+
font-family:${css(style.fontFamily, 'inherit')};
|
|
135
|
+
font-weight:${css(style.fontWeight, 'normal')};
|
|
136
|
+
font-style:${css(style.fontStyle, 'normal')};
|
|
137
|
+
text-align:${css(style.align, 'left')};
|
|
138
|
+
color:${css(style.color, 'inherit')};
|
|
139
|
+
" data-item-id="${esc(i.id)}" data-item-type="text">${esc(i.text)}</p>
|
|
140
|
+
`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A rectangle: a rule bent round four sides, with an optional fill.
|
|
146
|
+
*
|
|
147
|
+
* `box-sizing: border-box` in the stylesheet, so `w` and `h` are the outside of
|
|
148
|
+
* the box however thick its border - which is what the designer draws round it
|
|
149
|
+
* and what the engine paginated against. A border that added to them would put
|
|
150
|
+
* the two a few pixels apart at every thickness.
|
|
151
|
+
*
|
|
152
|
+
* Drawn before the items above it, because paginate orders a band by `y` and a
|
|
153
|
+
* backdrop is placed at the top of what it sits behind.
|
|
154
|
+
*/
|
|
155
|
+
function box(i) {
|
|
156
|
+
const style = i.style ?? {};
|
|
157
|
+
const width = Math.max(0, Number(style.borderWidth ?? 1)) || 1;
|
|
158
|
+
|
|
159
|
+
const pen = RULE_STYLES.includes(style.borderStyle) ? style.borderStyle : 'solid';
|
|
160
|
+
const border = `${width}px ${pen} ${css(style.borderColor, '#000000')}`;
|
|
161
|
+
|
|
162
|
+
const radius = Math.max(0, Number(style.radius ?? 0)) || 0;
|
|
163
|
+
|
|
164
|
+
const background = css(style.background);
|
|
165
|
+
|
|
166
|
+
return `
|
|
167
|
+
<div id="${esc(i.id)}" class="box" style="
|
|
168
|
+
width:${px(i.w)}px;
|
|
169
|
+
height:${px(i.h)}px;
|
|
170
|
+
${position(i)}
|
|
171
|
+
border:${border};
|
|
172
|
+
${radius ? `border-radius:${radius}px;` : ''}
|
|
173
|
+
${background ? `background:${background};` : ''}
|
|
174
|
+
" data-item-id="${esc(i.id)}" data-item-type="box"></div>
|
|
175
|
+
`
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The pen a line is drawn with, as one CSS border shorthand.
|
|
181
|
+
*
|
|
182
|
+
* `lineStyle` is whitelisted rather than passed through, the way tableRule
|
|
183
|
+
* already whitelists the grid's: the designer canvas draws straight from the
|
|
184
|
+
* layout without asking the validator, so a value it does not recognise should
|
|
185
|
+
* fall back to a rule rather than land in a style attribute.
|
|
186
|
+
*/
|
|
187
|
+
function stroke(style) {
|
|
188
|
+
const thickness = Math.max(0, Number(style?.thickness ?? 1)) || 1;
|
|
189
|
+
const pen = LINE_STYLES.includes(style?.lineStyle) ? style.lineStyle : 'solid';
|
|
190
|
+
|
|
191
|
+
return `${thickness}px ${pen} ${css(style?.color, '#000000')}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A rule, drawn down the middle of the item's box.
|
|
197
|
+
*
|
|
198
|
+
* The box is not the rule: a 1px hairline that *was* its box would be a 1px
|
|
199
|
+
* target in the designer, and an item nobody can click is an item nobody can
|
|
200
|
+
* edit. So the box is whatever `h` (or `w`, running vertically) says, and the
|
|
201
|
+
* rule sits centred inside it - which is also where an eye expects a rule that
|
|
202
|
+
* separates one thing from another to fall.
|
|
203
|
+
*
|
|
204
|
+
* The border goes on the inner span rather than the box, so that `w` and `h`
|
|
205
|
+
* keep meaning what they mean for every other item.
|
|
206
|
+
*/
|
|
207
|
+
function line(i) {
|
|
208
|
+
const vertical = i.orientation === 'vertical';
|
|
209
|
+
const pen = stroke(i.style);
|
|
210
|
+
|
|
211
|
+
return `
|
|
212
|
+
<div id="${esc(i.id)}" class="line${vertical ? ' is-vertical' : ''}" style="
|
|
213
|
+
width:${px(i.w)}px;
|
|
214
|
+
height:${px(i.h)}px;
|
|
215
|
+
${position(i)}
|
|
216
|
+
" data-item-id="${esc(i.id)}" data-item-type="line"><span class="line-mark"
|
|
217
|
+
style="${vertical ? `border-left:${pen}` : `border-top:${pen}`}"></span></div>
|
|
218
|
+
`
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
/** what a rule may be drawn in; anything else is not a rule */
|
|
223
|
+
const LINE_STYLES = ['solid', 'dashed', 'dotted', 'double'];
|
|
224
|
+
|
|
225
|
+
/** a table's grid may also be drawn in nothing at all */
|
|
226
|
+
const RULE_STYLES = [...LINE_STYLES, 'none'];
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The widest rule a table can be drawn with and still measure what it declares.
|
|
231
|
+
*
|
|
232
|
+
* .cell is box-sizing: border-box with a fixed height, so a rule is drawn
|
|
233
|
+
* inside the row rather than added to it - until the two rules of a cell are
|
|
234
|
+
* together taller than the row, at which point the cell has to grow and the
|
|
235
|
+
* table stops being headerHeight + rows x rowHeight. Half the shallowest row
|
|
236
|
+
* is the point where that starts, so that is the limit.
|
|
237
|
+
*
|
|
238
|
+
* @param {object} item the table
|
|
239
|
+
* @returns {number} pixels
|
|
240
|
+
*/
|
|
241
|
+
function maxRuleWidth(item) {
|
|
242
|
+
const rowHeight = item.rowHeight ?? 0;
|
|
243
|
+
const headerHeight = item.showHeader ? (item.headerHeight ?? rowHeight) : rowHeight;
|
|
244
|
+
|
|
245
|
+
const shallowest = Math.min(rowHeight || Infinity, headerHeight || Infinity);
|
|
246
|
+
|
|
247
|
+
return Number.isFinite(shallowest) ? Math.max(1, Math.floor(shallowest / 2)) : 1;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A table's grid, as custom properties on the table itself.
|
|
253
|
+
*
|
|
254
|
+
* Set here and read by .cell rather than written onto every cell: a five
|
|
255
|
+
* hundred row table has fifteen hundred cells, and the same border string
|
|
256
|
+
* repeated that many times is markup nobody needs to send. Custom properties
|
|
257
|
+
* inherit, so one declaration on the table reaches all of them.
|
|
258
|
+
*
|
|
259
|
+
* Only what the table actually declares is emitted, so a table that says
|
|
260
|
+
* nothing keeps taking its colour from the stylesheet's own variable.
|
|
261
|
+
*
|
|
262
|
+
* @param {object} item the table
|
|
263
|
+
* @returns {string} css declarations, or empty
|
|
264
|
+
*/
|
|
265
|
+
function tableRule(item) {
|
|
266
|
+
const style = item.style ?? {};
|
|
267
|
+
const out = [];
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Whitelisted rather than passed through: the designer canvas draws
|
|
271
|
+
* straight from the layout without asking the validator, and a border style
|
|
272
|
+
* it does not recognise should fall back to the stylesheet rather than land
|
|
273
|
+
* in a style attribute.
|
|
274
|
+
*/
|
|
275
|
+
if (RULE_STYLES.includes(style.borderStyle)) {
|
|
276
|
+
out.push(`--rs-rule-style:${style.borderStyle}`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (style.borderColor) out.push(`--rs-rule-color:${css(style.borderColor)}`);
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The header's own colours, set here for the same reason the grid is: they
|
|
283
|
+
* belong to every `th` in the table, and a table that says nothing about
|
|
284
|
+
* them keeps taking the stylesheet's.
|
|
285
|
+
*/
|
|
286
|
+
if (style.headerBackground) {
|
|
287
|
+
out.push(`--rs-head-bg:${css(style.headerBackground)}`);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (style.headerColor) out.push(`--rs-head-color:${css(style.headerColor)}`);
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The cell font, which used to go nowhere at all: the rail offered a size
|
|
294
|
+
* and a colour, report.css hard-coded 12px, and the two never met. It
|
|
295
|
+
* matters more than it looks - the engine wraps a cell against this font,
|
|
296
|
+
* so a table drawn in one and measured in another paginates to a row count
|
|
297
|
+
* it does not print.
|
|
298
|
+
*/
|
|
299
|
+
if (style.fontFamily) out.push(`--rs-cell-family:${css(style.fontFamily)}`);
|
|
300
|
+
|
|
301
|
+
const size = Number(style.fontSize);
|
|
302
|
+
if (Number.isFinite(size) && size > 0) out.push(`--rs-cell-size:${size}px`);
|
|
303
|
+
|
|
304
|
+
if (style.color) out.push(`--rs-cell-color:${css(style.color)}`);
|
|
305
|
+
|
|
306
|
+
/** one line and an ellipsis, for a column of figures that must not wrap */
|
|
307
|
+
if (item.wrap === false) out.push('--rs-cell-wrap:nowrap');
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Clamped rather than trusted: the rail will not offer a width past this,
|
|
311
|
+
* but a hand-written layout can, and a rule that outgrows its row would
|
|
312
|
+
* quietly undo the arithmetic pagination was done with.
|
|
313
|
+
*/
|
|
314
|
+
const width = Number(style.borderWidth);
|
|
315
|
+
|
|
316
|
+
if (Number.isFinite(width) && width > 0) {
|
|
317
|
+
out.push(`--rs-rule-width:${Math.min(width, maxRuleWidth(item))}px`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return out.length ? `${out.join(';')};` : '';
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
function table(i) {
|
|
325
|
+
return `
|
|
326
|
+
<table id="${esc(i.id)}" style="width:${px(i.w)}px;height:${px(i.measuredHeight)}px;${position(i)}${tableRule(i)}" data-item-id="${esc(i.id)}" data-item-type="table">
|
|
327
|
+
${i.groups ? groupedBody(i) : flatBody(i)}
|
|
328
|
+
</table>
|
|
329
|
+
`
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* A cell's content, in a box of exactly the row's height.
|
|
335
|
+
*
|
|
336
|
+
* `height` on a `<tr>` is a minimum, not a height: a cell whose padding and
|
|
337
|
+
* line box need more than that makes the row taller, and the table then outgrows
|
|
338
|
+
* the `headerHeight + rows x rowHeight` the engine paginated against. A few
|
|
339
|
+
* pixels a row is invisible until the fortieth row has pushed the detail band
|
|
340
|
+
* over the page footer. So the padding lives on a box of a known height inside
|
|
341
|
+
* the cell, and the cell itself adds nothing.
|
|
342
|
+
*
|
|
343
|
+
* @param {number} height the row height the engine budgeted
|
|
344
|
+
* @param {string} align the column's text alignment
|
|
345
|
+
* @param {*} value what the cell shows
|
|
346
|
+
* @returns {string} markup
|
|
347
|
+
*/
|
|
348
|
+
function cell(height, align, value) {
|
|
349
|
+
return `<div class="cell" style="height:${px(height)}px;text-align:${css(align, 'left')}">` +
|
|
350
|
+
`${esc(value)}</div>`;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
/** the column header row, shared by both table shapes */
|
|
355
|
+
function columnHeader(i) {
|
|
356
|
+
/** what measure.js budgeted, which is taller than declared if a label wrapped */
|
|
357
|
+
const height = i.headerMeasured ?? i.headerHeight ?? i.rowHeight;
|
|
358
|
+
|
|
359
|
+
return `<thead>
|
|
360
|
+
<tr style="height:${px(height)}px">
|
|
361
|
+
${i.columns.map(c => `<th style="width:${px(c.width)}px;">${cell(height, c.align, c.label)}</th>`).join("")}
|
|
362
|
+
</tr>
|
|
363
|
+
</thead>`;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @param {object} i the table
|
|
369
|
+
* @param {object[]} rows
|
|
370
|
+
* @param {number[]} [heights] one per row, from measure.js; absent on the
|
|
371
|
+
* designer canvas, where nothing has been measured and every row is its
|
|
372
|
+
* declared height
|
|
373
|
+
*/
|
|
374
|
+
function dataRows(i, rows, heights) {
|
|
375
|
+
return (rows || []).map((r, n) => {
|
|
376
|
+
const height = heights?.[n] ?? i.rowHeight;
|
|
377
|
+
|
|
378
|
+
return `<tr style="height:${px(height)}px">
|
|
379
|
+
${i.columns.map(c => `<td style="width:${px(c.width)}px;">${cell(height, c.align, r[c.field])}</td>`).join("")}
|
|
380
|
+
</tr>`;
|
|
381
|
+
}).join("");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* A group band is a designed band with absolutely positioned items, so it is
|
|
387
|
+
* dropped into a full-width cell that recreates the band box inside the table.
|
|
388
|
+
*/
|
|
389
|
+
function groupBandRow(band, colSpan, cls) {
|
|
390
|
+
if (!band) return '';
|
|
391
|
+
const height = band.measuredHeight > 0 ? band.measuredHeight : (band.height ?? 0);
|
|
392
|
+
return `<tr class="${cls}">
|
|
393
|
+
<td colspan="${colSpan}" class="group-band-cell">
|
|
394
|
+
<div class="band" style="position:relative;height:${px(height)}px">${items(band.items)}</div>
|
|
395
|
+
</td>
|
|
396
|
+
</tr>`;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
function flatBody(i) {
|
|
401
|
+
return `
|
|
402
|
+
${i.showHeader ? columnHeader(i) : ""}
|
|
403
|
+
<tbody>
|
|
404
|
+
${dataRows(i, i.row, i.rowHeights)}
|
|
405
|
+
</tbody>
|
|
406
|
+
`;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* One tbody per group fragment, so a group that survives a page break keeps its
|
|
412
|
+
* header on the next page and only prints its footer on the fragment that ends it.
|
|
413
|
+
*/
|
|
414
|
+
function groupedBody(i) {
|
|
415
|
+
const colSpan = i.columns.length;
|
|
416
|
+
|
|
417
|
+
return i.groups.map(g => `
|
|
418
|
+
${g.showHeader && i.showHeader ? columnHeader(i) : ""}
|
|
419
|
+
<tbody>
|
|
420
|
+
${groupBandRow(g.headerBand, colSpan, "group-header")}
|
|
421
|
+
${dataRows(i, g.rows, g.rowHeights)}
|
|
422
|
+
${g.showFooter ? groupBandRow(g.footerBand, colSpan, "group-footer") : ""}
|
|
423
|
+
</tbody>
|
|
424
|
+
`).join("");
|
|
425
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* render.js takes the pages json and turns it into the preview's markup.
|
|
3
|
+
*
|
|
4
|
+
* It owns page and band composition only - where a band sits on a paginated
|
|
5
|
+
* page, and how the page box is drawn. Drawing the items inside a band lives in
|
|
6
|
+
* items.js, because the designer canvas needs that half and none of this one.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { items, esc, px } from './items.js';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The page margins, as padding.
|
|
14
|
+
*
|
|
15
|
+
* Every length goes through px() on the way into a style attribute. The page
|
|
16
|
+
* block is the layout's, buildPages does not run the validator over it, and a
|
|
17
|
+
* margin that arrived as a string writes `padding-top:undefinedpx` - or worse,
|
|
18
|
+
* closes the attribute.
|
|
19
|
+
*/
|
|
20
|
+
const padding = (m) => `padding-top:${px(m?.top)}px;padding-left:${px(m?.left)}px;` +
|
|
21
|
+
`padding-bottom:${px(m?.bottom)}px;padding-right:${px(m?.right)}px;`
|
|
22
|
+
|
|
23
|
+
/** display order */
|
|
24
|
+
const order = ['pageHeader', 'reportHeader', 'detail', 'reportFooter', 'pageFooter'];
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export function render(json) {
|
|
28
|
+
const pageConf = json.page;
|
|
29
|
+
let html = `
|
|
30
|
+
<main id="main-page">
|
|
31
|
+
${json.pages.map(p =>
|
|
32
|
+
`
|
|
33
|
+
<section id="page-${esc(p.pageNO)}" class="page" style="height:${px(pageConf.height)}px; width:${px(pageConf.width)}px; ${padding(pageConf.margin)};position:relative">
|
|
34
|
+
${page(p, pageConf)}
|
|
35
|
+
</section>
|
|
36
|
+
`
|
|
37
|
+
).join('')}
|
|
38
|
+
</main>
|
|
39
|
+
`;
|
|
40
|
+
return html;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
function page(page, pageConf) {
|
|
45
|
+
/**
|
|
46
|
+
* copy before sorting - the page list is the seam every other consumer
|
|
47
|
+
* reads, and render has no business reordering it in place.
|
|
48
|
+
* A band type render does not know about sorts to the end, not the front.
|
|
49
|
+
*/
|
|
50
|
+
const rank = (type) => {
|
|
51
|
+
const i = order.indexOf(type);
|
|
52
|
+
return i === -1 ? order.length : i;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const sortedPage = [...page.bands].sort(
|
|
56
|
+
({ type: a }, { type: b }) => rank(a) - rank(b)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
let pageHtmlStr = [];
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
for (const band of sortedPage) {
|
|
63
|
+
pageHtmlStr.push(bandConversion(band, page.pageNO, pageConf))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return pageHtmlStr.join('');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Bands are anchored, not stacked: the engine gave each one the top of its zone,
|
|
72
|
+
* so a page whose detail ran short still prints its footer on the bottom edge.
|
|
73
|
+
* Without a `top` - an older page list, or a band the engine did not place - it
|
|
74
|
+
* falls back to flowing, which is what this used to do for everything.
|
|
75
|
+
*/
|
|
76
|
+
function bandConversion(comp, pageNO, pageConf) {
|
|
77
|
+
const anchored = Number.isFinite(comp.top);
|
|
78
|
+
const margin = pageConf?.margin ?? { top: 0, left: 0, right: 0 };
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An absolutely positioned child anchors to its ancestor's *padding* box,
|
|
82
|
+
* and the page draws its margins as padding - so the margins have to be
|
|
83
|
+
* added back here, or every band would sit inside them.
|
|
84
|
+
*/
|
|
85
|
+
const box = anchored
|
|
86
|
+
? `position:absolute;top:${px(comp.top) + px(margin.top)}px;` +
|
|
87
|
+
`left:${px(margin.left)}px;right:${px(margin.right)}px;`
|
|
88
|
+
: `position:relative;`;
|
|
89
|
+
|
|
90
|
+
/** the zone is the height; a band that outgrew it keeps its measured height */
|
|
91
|
+
const height = Math.max(comp.zoneHeight ?? 0, comp.measuredHeight ?? 0);
|
|
92
|
+
|
|
93
|
+
return `
|
|
94
|
+
<div class="band" id="${esc(comp.type)}-${esc(pageNO)}" style="${box}${height > 0 ? `height:${px(height)}px` : ""}" data-band-type="${esc(comp.type)}">
|
|
95
|
+
${items(comp.items)}
|
|
96
|
+
</div>
|
|
97
|
+
`
|
|
98
|
+
}
|