report-studio 1.2.3 → 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
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* grouping.js is the designer's side of a table's own grouping.
|
|
3
|
+
*
|
|
4
|
+
* A grouped table carries the bands its groups are drawn with, one pair for
|
|
5
|
+
* each level it is grouped at, outermost first:
|
|
6
|
+
*
|
|
7
|
+
* { type: 'table', id: 'sales', grouping: [
|
|
8
|
+
* { by: 'region', header, footer },
|
|
9
|
+
* { by: 'city', header, footer } within each region
|
|
10
|
+
* ] }
|
|
11
|
+
*
|
|
12
|
+
* Everything else in the designer addresses a band by a string - its type,
|
|
13
|
+
* which was enough when a report had one band of each type. A table's group
|
|
14
|
+
* band is addressed the same way, by a key naming the table as well, and the
|
|
15
|
+
* level when it is not the outermost:
|
|
16
|
+
*
|
|
17
|
+
* 'groupHeader:sales' the header of the outermost level of table "sales"
|
|
18
|
+
* 'groupHeader@1:sales' the header of its second level
|
|
19
|
+
*
|
|
20
|
+
* The outermost level's key is the one a table grouped at one level has always
|
|
21
|
+
* had. The level sits before the separator, where a table id cannot reach, so
|
|
22
|
+
* an id with a colon in it is still read whole.
|
|
23
|
+
*
|
|
24
|
+
* So the selection, the canvas's data-band-type, the band rail and every
|
|
25
|
+
* structure.js call keep taking one string, and only the few places that need
|
|
26
|
+
* to know what *kind* of band a key means ask kindOf().
|
|
27
|
+
*
|
|
28
|
+
* Nothing here touches the document, so all of it is specified without one.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
tablesOf, levelsOf, hasOwnGrouping, normalizeGrouping, GROUP_BAND_TYPES,
|
|
33
|
+
MAX_GROUP_LEVELS, GROUP_SORTS
|
|
34
|
+
} from '../engine/tables.js';
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/** the two kinds of group band, in the order a group draws them */
|
|
38
|
+
export const GROUP_KINDS = GROUP_BAND_TYPES;
|
|
39
|
+
|
|
40
|
+
/** what a group band is worth when it is first switched on - as a report band's */
|
|
41
|
+
export const NEW_GROUP_BAND_HEIGHT = 36;
|
|
42
|
+
|
|
43
|
+
const SEPARATOR = ':';
|
|
44
|
+
|
|
45
|
+
/** between a band's kind and its level, in the key of any level but the outermost */
|
|
46
|
+
const LEVEL_MARK = '@';
|
|
47
|
+
|
|
48
|
+
/** which property of a grouping level holds each kind of band */
|
|
49
|
+
const SLOT = { groupHeader: 'header', groupFooter: 'footer' };
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The key a table's group band is addressed by.
|
|
54
|
+
* @param {'groupHeader'|'groupFooter'} kind
|
|
55
|
+
* @param {string} tableId
|
|
56
|
+
* @param {number} [level=0] 0 is the outermost
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
export function groupBandKey(kind, tableId, level = 0) {
|
|
60
|
+
const mark = level > 0 ? `${LEVEL_MARK}${level}` : '';
|
|
61
|
+
return `${kind}${mark}${SEPARATOR}${tableId}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What a band key names, when it names a table's group band.
|
|
67
|
+
*
|
|
68
|
+
* Split at the first separator only, so a table whose id has a colon in it is
|
|
69
|
+
* still found.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} key
|
|
72
|
+
* @returns {{kind: string, table: string, level: number}|null} null for any other key
|
|
73
|
+
*/
|
|
74
|
+
export function parseBandKey(key) {
|
|
75
|
+
if (typeof key !== 'string') return null;
|
|
76
|
+
|
|
77
|
+
const at = key.indexOf(SEPARATOR);
|
|
78
|
+
if (at === -1) return null;
|
|
79
|
+
|
|
80
|
+
const [kind, mark, ...rest] = key.slice(0, at).split(LEVEL_MARK);
|
|
81
|
+
if (!GROUP_KINDS.includes(kind) || rest.length) return null;
|
|
82
|
+
|
|
83
|
+
let level = 0;
|
|
84
|
+
|
|
85
|
+
if (mark !== undefined) {
|
|
86
|
+
if (!/^[1-9]\d*$/.test(mark)) return null;
|
|
87
|
+
level = Number(mark);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { kind, table: key.slice(at + SEPARATOR.length), level };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The band type a key means: `groupHeader:refunds` is a groupHeader, and every
|
|
96
|
+
* other key is its own type.
|
|
97
|
+
* @param {string} key
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
100
|
+
export function kindOf(key) {
|
|
101
|
+
return parseBandKey(key)?.kind ?? key;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
/** whether a key means a band drawn once per group, either kind of key */
|
|
106
|
+
export function isGroupKind(key) {
|
|
107
|
+
return GROUP_KINDS.includes(kindOf(key));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What to call a band where a person reads it - the gutter, the band rail, the
|
|
113
|
+
* placeholder dialog. A table's group band says whose it is, and which level
|
|
114
|
+
* when it is not the outermost.
|
|
115
|
+
* @param {string} key
|
|
116
|
+
* @returns {string}
|
|
117
|
+
*/
|
|
118
|
+
export function bandLabel(key) {
|
|
119
|
+
const parsed = parseBandKey(key);
|
|
120
|
+
if (!parsed) return String(key ?? '');
|
|
121
|
+
|
|
122
|
+
const level = parsed.level > 0 ? ` ${parsed.level + 1}` : '';
|
|
123
|
+
return `${parsed.kind}${level} · ${parsed.table}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/** the table with this id, or null */
|
|
128
|
+
export function tableById(layout, id) {
|
|
129
|
+
return tablesOf(layout).find(table => table.id === id) ?? null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
/** a table's level, when it is a level the designer can edit */
|
|
134
|
+
function levelAt(table, n) {
|
|
135
|
+
const level = levelsOf(table)[n];
|
|
136
|
+
return level && typeof level === 'object' ? level : null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The band behind a table's group band key.
|
|
142
|
+
* @param {object} layout
|
|
143
|
+
* @param {string} key
|
|
144
|
+
* @returns {object|null} null when the key is not a group band key, the table
|
|
145
|
+
* is not there or not grouped that deep, or the level has no band of that kind
|
|
146
|
+
*/
|
|
147
|
+
export function groupBandAt(layout, key) {
|
|
148
|
+
const parsed = parseBandKey(key);
|
|
149
|
+
if (!parsed) return null;
|
|
150
|
+
|
|
151
|
+
const band = levelAt(tableById(layout, parsed.table), parsed.level)?.[SLOT[parsed.kind]];
|
|
152
|
+
|
|
153
|
+
return band && typeof band === 'object' ? band : null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Every group band key a table has room for, in the order a group draws them:
|
|
159
|
+
* the headers outermost first, then the footers innermost first.
|
|
160
|
+
* @param {object} table
|
|
161
|
+
* @returns {{key: string, kind: string, level: number}[]}
|
|
162
|
+
*/
|
|
163
|
+
function tableKeys(table) {
|
|
164
|
+
const depth = levelsOf(table).length;
|
|
165
|
+
const levels = Array.from({ length: depth }, (_, n) => n);
|
|
166
|
+
|
|
167
|
+
return [
|
|
168
|
+
...levels.map(level => ({ kind: 'groupHeader', level })),
|
|
169
|
+
...levels.reverse().map(level => ({ kind: 'groupFooter', level }))
|
|
170
|
+
].map(({ kind, level }) => ({ key: groupBandKey(kind, table.id, level), kind, level }));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Every table group band the report has, in table order and, within a table,
|
|
176
|
+
* in the order a group draws them.
|
|
177
|
+
* @param {object} layout
|
|
178
|
+
* @returns {{key: string, kind: string, table: string, level: number, band: object}[]}
|
|
179
|
+
*/
|
|
180
|
+
export function groupBandsIn(layout) {
|
|
181
|
+
const found = [];
|
|
182
|
+
|
|
183
|
+
for (const table of tablesOf(layout)) {
|
|
184
|
+
for (const { key, kind, level } of tableKeys(table)) {
|
|
185
|
+
const band = levelAt(table, level)?.[SLOT[kind]];
|
|
186
|
+
|
|
187
|
+
if (band && typeof band === 'object') {
|
|
188
|
+
found.push({ key, kind, table: table.id, level, band });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return found;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Every grouped table, with its group band keys - whether or not the band is
|
|
199
|
+
* switched on. What the band rail offers a checkbox for.
|
|
200
|
+
*
|
|
201
|
+
* `by` and `keys` are the outermost level's; `levels` has every level's,
|
|
202
|
+
* outermost first.
|
|
203
|
+
*
|
|
204
|
+
* @param {object} layout
|
|
205
|
+
* @returns {{table: string, by: string, keys: string[],
|
|
206
|
+
* levels: {by: string, keys: string[]}[]}[]}
|
|
207
|
+
*/
|
|
208
|
+
export function groupedTables(layout) {
|
|
209
|
+
return tablesOf(layout)
|
|
210
|
+
.filter(table => levelsOf(table).length > 0)
|
|
211
|
+
.map(table => {
|
|
212
|
+
const levels = levelsOf(table).map((level, n) => ({
|
|
213
|
+
by: level?.by ?? '',
|
|
214
|
+
keys: GROUP_KINDS.map(kind => groupBandKey(kind, table.id, n))
|
|
215
|
+
}));
|
|
216
|
+
|
|
217
|
+
return { table: table.id, by: levels[0].by, keys: levels[0].keys, levels };
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Switches one of a table's group bands on or off.
|
|
224
|
+
*
|
|
225
|
+
* Switching off the last one is allowed, though a group with neither a header
|
|
226
|
+
* nor a footer is a layout validate.js refuses - the same as switching off the
|
|
227
|
+
* report's group band under a report-wide groupBy always was. A half-made
|
|
228
|
+
* report is the normal state of one being designed; the problem is said in the
|
|
229
|
+
* corner, and the switch beside it is the fix. Clearing the table's *Group by*
|
|
230
|
+
* is how grouping is turned off.
|
|
231
|
+
*
|
|
232
|
+
* @param {object} layout
|
|
233
|
+
* @param {string} key
|
|
234
|
+
* @param {boolean} on
|
|
235
|
+
* @returns {object|boolean} the band switched on, true for one switched off,
|
|
236
|
+
* false when nothing changed
|
|
237
|
+
*/
|
|
238
|
+
export function setGroupBand(layout, key, on) {
|
|
239
|
+
const parsed = parseBandKey(key);
|
|
240
|
+
if (!parsed) return false;
|
|
241
|
+
|
|
242
|
+
const level = levelAt(tableById(layout, parsed.table), parsed.level);
|
|
243
|
+
if (!level) return false;
|
|
244
|
+
|
|
245
|
+
const slot = SLOT[parsed.kind];
|
|
246
|
+
|
|
247
|
+
if (on) {
|
|
248
|
+
if (level[slot]) return false;
|
|
249
|
+
|
|
250
|
+
level[slot] = { type: parsed.kind, height: NEW_GROUP_BAND_HEIGHT, items: [] };
|
|
251
|
+
return level[slot];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!level[slot]) return false;
|
|
255
|
+
|
|
256
|
+
level[slot] = null;
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The groupings a table had before its *Group by* was cleared - every level.
|
|
263
|
+
*
|
|
264
|
+
* Clearing the box and typing a new field arrives as an empty value between
|
|
265
|
+
* the two, and the header and footer someone designed should not be the price
|
|
266
|
+
* of retyping a name. Kept beside the table rather than on it, so it is never
|
|
267
|
+
* saved with the file.
|
|
268
|
+
*/
|
|
269
|
+
const cleared = new WeakMap();
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Sets the field one level of a table is grouped by.
|
|
274
|
+
*
|
|
275
|
+
* The outermost level's is the table's *Group by*, which is how grouping is
|
|
276
|
+
* switched on and off from the rail:
|
|
277
|
+
*
|
|
278
|
+
* a field, on a table not yet grouped grouped, with a header showing the
|
|
279
|
+
* group's value
|
|
280
|
+
* another field regrouped; a `{old}` in its bands
|
|
281
|
+
* becomes `{new}`
|
|
282
|
+
* nothing no longer grouped, at any level
|
|
283
|
+
*
|
|
284
|
+
* An inner level is only ever renamed. Emptying its box leaves it there with
|
|
285
|
+
* no field - which validate.js names - rather than taking away the level and
|
|
286
|
+
* the box being typed in with it; addGroupLevel and removeGroupLevel are how
|
|
287
|
+
* levels come and go. An inner level given its first field gets a header
|
|
288
|
+
* showing it, as a new grouping does, if its header is still empty.
|
|
289
|
+
*
|
|
290
|
+
* @param {object} table mutated in place
|
|
291
|
+
* @param {string|null} by
|
|
292
|
+
* @param {number} [n=0] the level
|
|
293
|
+
* @returns {boolean} whether anything changed
|
|
294
|
+
*/
|
|
295
|
+
export function setGroupBy(table, by, n = 0) {
|
|
296
|
+
if (n > 0) return renameLevel(table, n, by ?? '');
|
|
297
|
+
|
|
298
|
+
const level = levelAt(table, 0);
|
|
299
|
+
|
|
300
|
+
if (by == null || by === '') {
|
|
301
|
+
if (!level && !hasOwnGrouping(table)) return false;
|
|
302
|
+
|
|
303
|
+
if (level) cleared.set(table, levelsOf(table));
|
|
304
|
+
|
|
305
|
+
delete table.grouping;
|
|
306
|
+
delete table.groupBy;
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (level) return renameLevel(table, 0, by);
|
|
311
|
+
|
|
312
|
+
delete table.groupBy;
|
|
313
|
+
|
|
314
|
+
const before = cleared.get(table);
|
|
315
|
+
|
|
316
|
+
if (before?.length) {
|
|
317
|
+
cleared.delete(table);
|
|
318
|
+
const [outer, ...inner] = before;
|
|
319
|
+
renameField([outer.header, outer.footer], outer.by, by);
|
|
320
|
+
table.grouping = [{ ...outer, by }, ...inner];
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
table.grouping = [newLevel(by)];
|
|
325
|
+
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
/** a level's field changed, and its bands' `{old}` with it */
|
|
331
|
+
function renameLevel(table, n, by) {
|
|
332
|
+
const level = levelAt(table, n);
|
|
333
|
+
if (!level || level.by === by) return false;
|
|
334
|
+
|
|
335
|
+
if (!level.by && by && level.header && !(level.header.items ?? []).length) {
|
|
336
|
+
level.header.items = [groupLabel(by)];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
renameField([level.header, level.footer], level.by, by);
|
|
340
|
+
level.by = by;
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Groups a grouped table again, within the groups of its innermost level - by
|
|
347
|
+
* city within region. The new level starts with a header, as a new grouping
|
|
348
|
+
* does, and with no field until one is typed.
|
|
349
|
+
*
|
|
350
|
+
* @param {object} table mutated in place; must already be grouped
|
|
351
|
+
* @param {string} [by='']
|
|
352
|
+
* @returns {number} the new level's index, or -1 when the table is not grouped
|
|
353
|
+
*/
|
|
354
|
+
export function addGroupLevel(table, by = '') {
|
|
355
|
+
if (!Array.isArray(table?.grouping) || table.grouping.length === 0) return -1;
|
|
356
|
+
if (table.grouping.length >= MAX_GROUP_LEVELS) return -1;
|
|
357
|
+
|
|
358
|
+
table.grouping.push(by ? newLevel(by) : {
|
|
359
|
+
by: '',
|
|
360
|
+
header: { type: 'groupHeader', height: NEW_GROUP_BAND_HEIGHT, items: [] },
|
|
361
|
+
footer: null
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
return table.grouping.length - 1;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Sets how one level's groups are put in order and placed:
|
|
370
|
+
*
|
|
371
|
+
* sort 'asc' or 'desc' by the group's value; '' or null for the order
|
|
372
|
+
* the values are first seen in, which is left out of the file
|
|
373
|
+
* newPage true to start each of the level's groups, after the table's
|
|
374
|
+
* first, on a page of its own; false is left out of the file
|
|
375
|
+
*
|
|
376
|
+
* @param {object} table mutated in place
|
|
377
|
+
* @param {number} n the level
|
|
378
|
+
* @param {'sort'|'newPage'} option
|
|
379
|
+
* @param {*} value
|
|
380
|
+
* @returns {boolean} whether anything changed
|
|
381
|
+
*/
|
|
382
|
+
export function setGroupOption(table, n, option, value) {
|
|
383
|
+
const level = levelAt(table, n);
|
|
384
|
+
if (!level) return false;
|
|
385
|
+
|
|
386
|
+
let next;
|
|
387
|
+
|
|
388
|
+
if (option === 'sort') next = GROUP_SORTS.includes(value) ? value : undefined;
|
|
389
|
+
else if (option === 'newPage') next = value === true ? true : undefined;
|
|
390
|
+
else return false;
|
|
391
|
+
|
|
392
|
+
if (level[option] === next) return false;
|
|
393
|
+
|
|
394
|
+
if (next === undefined) delete level[option];
|
|
395
|
+
else level[option] = next;
|
|
396
|
+
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Takes one level out of a table's grouping, with its bands. The levels inside
|
|
403
|
+
* it move out one; taking out the only level ungroups the table, as clearing
|
|
404
|
+
* its *Group by* does.
|
|
405
|
+
*
|
|
406
|
+
* @param {object} table mutated in place
|
|
407
|
+
* @param {number} n
|
|
408
|
+
* @returns {boolean} whether there was such a level
|
|
409
|
+
*/
|
|
410
|
+
export function removeGroupLevel(table, n) {
|
|
411
|
+
if (!levelAt(table, n)) return false;
|
|
412
|
+
|
|
413
|
+
if (levelsOf(table).length === 1) return setGroupBy(table, null);
|
|
414
|
+
|
|
415
|
+
table.grouping.splice(n, 1);
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Swaps a level with the one outside it: grouping by city and then region
|
|
422
|
+
* rather than region and then city. Each level keeps its own bands.
|
|
423
|
+
*
|
|
424
|
+
* @param {object} table mutated in place
|
|
425
|
+
* @param {number} n the level to move out; 0 cannot be
|
|
426
|
+
* @returns {boolean} whether anything moved
|
|
427
|
+
*/
|
|
428
|
+
export function moveGroupLevelOut(table, n) {
|
|
429
|
+
if (n < 1 || !levelAt(table, n) || !levelAt(table, n - 1)) return false;
|
|
430
|
+
|
|
431
|
+
const [level] = table.grouping.splice(n, 1);
|
|
432
|
+
table.grouping.splice(n - 1, 0, level);
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Gives one grouped table the group headers and footers another one is drawn
|
|
439
|
+
* with - every level of them.
|
|
440
|
+
*
|
|
441
|
+
* For two tables that should look alike - a grouping's design is not shared,
|
|
442
|
+
* so this is a copy, and the two are edited apart afterwards. Each level of the
|
|
443
|
+
* copy says `{by}` for the level it lands on wherever the original said its
|
|
444
|
+
* own. Levels the source has and the target does not are left out, and a
|
|
445
|
+
* level the target has and the source does not keeps its own bands.
|
|
446
|
+
*
|
|
447
|
+
* @param {object} layout
|
|
448
|
+
* @param {string} toId the table to copy onto, which must be grouped
|
|
449
|
+
* @param {string} fromId the grouped table to copy from
|
|
450
|
+
* @returns {boolean} whether anything was copied
|
|
451
|
+
*/
|
|
452
|
+
export function copyGroupDesign(layout, toId, fromId) {
|
|
453
|
+
if (toId === fromId) return false;
|
|
454
|
+
|
|
455
|
+
const targets = levelsOf(tableById(layout, toId));
|
|
456
|
+
const sources = levelsOf(tableById(layout, fromId));
|
|
457
|
+
|
|
458
|
+
let copied = false;
|
|
459
|
+
|
|
460
|
+
targets.forEach((target, n) => {
|
|
461
|
+
const source = sources[n];
|
|
462
|
+
if (!target || typeof target !== 'object' || !source || typeof source !== 'object') return;
|
|
463
|
+
|
|
464
|
+
target.header = source.header ? structuredClone(source.header) : null;
|
|
465
|
+
target.footer = source.footer ? structuredClone(source.footer) : null;
|
|
466
|
+
|
|
467
|
+
renameField([target.header, target.footer], source.by, target.by);
|
|
468
|
+
copied = true;
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
return copied;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Whether a layout still says its grouping the older way - `groupBy` on the
|
|
477
|
+
* report or a table - and so has to be moved onto its table before the
|
|
478
|
+
* designer can show it.
|
|
479
|
+
* @param {object} layout
|
|
480
|
+
* @returns {boolean}
|
|
481
|
+
*/
|
|
482
|
+
export function needsNormalizing(layout) {
|
|
483
|
+
if (layout == null || typeof layout !== 'object') return false;
|
|
484
|
+
|
|
485
|
+
return layout.groupBy != null || tablesOf(layout).some(hasOwnGrouping);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* The layout the designer edits, for a layout it was given.
|
|
491
|
+
*
|
|
492
|
+
* The older form of grouping is moved onto its table; anything else is the
|
|
493
|
+
* very object that was passed in, because a host that handed the designer its
|
|
494
|
+
* layout may be holding on to it. A report-level group band that nothing is
|
|
495
|
+
* grouped by is left where it is: it was drawn by somebody, and it is theirs
|
|
496
|
+
* to remove from the band list.
|
|
497
|
+
*
|
|
498
|
+
* @param {object} layout
|
|
499
|
+
* @returns {object}
|
|
500
|
+
*/
|
|
501
|
+
export function openLayout(layout) {
|
|
502
|
+
return needsNormalizing(layout) ? normalizeGrouping(layout) : layout;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
/** `{old}` to `{new}` in every text item of the given bands */
|
|
507
|
+
function renameField(bands, from, to) {
|
|
508
|
+
if (!from || !to || from === to) return;
|
|
509
|
+
|
|
510
|
+
for (const band of bands) {
|
|
511
|
+
for (const item of (band?.items ?? [])) {
|
|
512
|
+
if (item?.type === 'text' && typeof item.value === 'string') {
|
|
513
|
+
item.value = item.value.split(`{${from}}`).join(`{${to}}`);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
/** a level grouped by `by`, with a header showing the group's value */
|
|
521
|
+
function newLevel(by) {
|
|
522
|
+
return {
|
|
523
|
+
by,
|
|
524
|
+
header: {
|
|
525
|
+
type: 'groupHeader',
|
|
526
|
+
height: NEW_GROUP_BAND_HEIGHT,
|
|
527
|
+
items: [groupLabel(by)]
|
|
528
|
+
},
|
|
529
|
+
footer: null
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
/** the header a new grouping starts with: the group's own value, in bold */
|
|
535
|
+
function groupLabel(by) {
|
|
536
|
+
return {
|
|
537
|
+
id: 'group-label',
|
|
538
|
+
type: 'text',
|
|
539
|
+
x: 0,
|
|
540
|
+
y: 8,
|
|
541
|
+
w: 300,
|
|
542
|
+
h: 20,
|
|
543
|
+
value: `{${by}}`,
|
|
544
|
+
style: {
|
|
545
|
+
fontFamily: 'Helvetica, Arial, sans-serif',
|
|
546
|
+
fontSize: 12,
|
|
547
|
+
fontWeight: 'bold',
|
|
548
|
+
fontStyle: 'normal',
|
|
549
|
+
color: '#000000',
|
|
550
|
+
align: 'left',
|
|
551
|
+
background: null,
|
|
552
|
+
border: null,
|
|
553
|
+
padding: 0
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
}
|
package/src/designer/icons.js
CHANGED
|
@@ -107,6 +107,15 @@ export const ICONS = {
|
|
|
107
107
|
a.75.75 0 0 0-.75-.75H9.75"/>
|
|
108
108
|
<rect x="6.25" y="1.75" width="3.5" height="2.75" rx=".85"/>`),
|
|
109
109
|
|
|
110
|
+
/** a percent sign: how a value prints, number, money or date */
|
|
111
|
+
format: wrap(`<path d="M12.25 3.75 3.75 12.25"/>
|
|
112
|
+
<circle cx="4.75" cy="4.75" r="1.75"/>
|
|
113
|
+
<circle cx="11.25" cy="11.25" r="1.75"/>`),
|
|
114
|
+
|
|
115
|
+
/** a highlighter pen over the stroke it leaves */
|
|
116
|
+
highlight: wrap(`<path d="M9.75 2.75 13.25 6.25 8 11.5H4.5V8z"/>
|
|
117
|
+
<path d="M2.25 13.75h11.5"/>`),
|
|
118
|
+
|
|
110
119
|
/** a disk: the shutter above, the label below */
|
|
111
120
|
save: wrap(`<path d="M2.75 3.25h8.5l2.5 2.5v7a.5.5 0 0 1-.5.5h-11
|
|
112
121
|
a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5z"/>
|
package/src/designer/menu.js
CHANGED
|
@@ -32,10 +32,17 @@ export const MENU_EDGE_GAP = 8;
|
|
|
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
34
|
* would be one the report cannot hold
|
|
35
|
+
* @param {{label: string, index?: number}|null} [options.format] what a Format
|
|
36
|
+
* entry would format, when what was right-clicked has values to format - a
|
|
37
|
+
* text item, or the table column under the pointer; null offers none
|
|
38
|
+
* @param {{label: string, index?: number}|null} [options.highlight] the same,
|
|
39
|
+
* for a Highlight entry
|
|
40
|
+
* @param {boolean} [options.rows] offer to highlight the table's rows
|
|
35
41
|
* @returns {object[]} entries, in the order they are drawn
|
|
36
42
|
*/
|
|
37
43
|
export function menuActions({
|
|
38
|
-
count = 0, canPaste = false, canDuplicate = true
|
|
44
|
+
count = 0, canPaste = false, canDuplicate = true, format = null, highlight = null,
|
|
45
|
+
rows = false
|
|
39
46
|
} = {}) {
|
|
40
47
|
const many = count > 1 ? ` ${count} items` : '';
|
|
41
48
|
|
|
@@ -44,12 +51,40 @@ export function menuActions({
|
|
|
44
51
|
glyph: 'paste',
|
|
45
52
|
label: 'Paste',
|
|
46
53
|
hint: 'ctrl+V',
|
|
54
|
+
keys: 'Ctrl+V',
|
|
47
55
|
disabled: !canPaste
|
|
48
56
|
};
|
|
49
57
|
|
|
50
58
|
if (count < 1) return [paste];
|
|
51
59
|
|
|
60
|
+
/** first, being about this item rather than about the clipboard */
|
|
61
|
+
const about = [
|
|
62
|
+
format && {
|
|
63
|
+
action: 'edit-format',
|
|
64
|
+
glyph: 'format',
|
|
65
|
+
label: format.label,
|
|
66
|
+
hint: 'number, currency, percent or date',
|
|
67
|
+
index: format.index
|
|
68
|
+
},
|
|
69
|
+
highlight && {
|
|
70
|
+
action: 'edit-highlight',
|
|
71
|
+
glyph: 'highlight',
|
|
72
|
+
label: highlight.label,
|
|
73
|
+
hint: 'colour values by what they are',
|
|
74
|
+
index: highlight.index
|
|
75
|
+
},
|
|
76
|
+
rows && {
|
|
77
|
+
action: 'edit-row-highlight',
|
|
78
|
+
glyph: 'highlight',
|
|
79
|
+
label: 'Highlight rows…',
|
|
80
|
+
hint: 'shade whole rows by a field'
|
|
81
|
+
}
|
|
82
|
+
].filter(Boolean);
|
|
83
|
+
|
|
84
|
+
const formatting = about.length ? [...about, { separator: true }] : [];
|
|
85
|
+
|
|
52
86
|
return [
|
|
87
|
+
...formatting,
|
|
53
88
|
{
|
|
54
89
|
action: 'duplicate-item',
|
|
55
90
|
glyph: 'duplicate',
|
|
@@ -57,13 +92,15 @@ export function menuActions({
|
|
|
57
92
|
? `Duplicate${many}`
|
|
58
93
|
: 'This cannot be duplicated',
|
|
59
94
|
hint: 'ctrl+D, or alt-drag',
|
|
95
|
+
keys: 'Ctrl+D',
|
|
60
96
|
disabled: !canDuplicate
|
|
61
97
|
},
|
|
62
98
|
{
|
|
63
99
|
action: 'copy-items',
|
|
64
100
|
glyph: 'copy',
|
|
65
101
|
label: `Copy${many}`,
|
|
66
|
-
hint: 'ctrl+C'
|
|
102
|
+
hint: 'ctrl+C',
|
|
103
|
+
keys: 'Ctrl+C'
|
|
67
104
|
},
|
|
68
105
|
paste,
|
|
69
106
|
{ separator: true },
|
|
@@ -72,6 +109,7 @@ export function menuActions({
|
|
|
72
109
|
glyph: 'delete',
|
|
73
110
|
label: `Delete${many}`,
|
|
74
111
|
hint: 'del',
|
|
112
|
+
keys: 'Del',
|
|
75
113
|
tone: 'danger'
|
|
76
114
|
}
|
|
77
115
|
];
|
|
@@ -79,9 +117,10 @@ export function menuActions({
|
|
|
79
117
|
|
|
80
118
|
|
|
81
119
|
/**
|
|
82
|
-
* The
|
|
83
|
-
*
|
|
84
|
-
*
|
|
120
|
+
* The menu's markup: one row per action - its icon, its name, and the keys
|
|
121
|
+
* that do the same from anywhere. Named rather than icon-only: a copy and a
|
|
122
|
+
* duplicate are two overlapping squares either way, and a menu is read, not
|
|
123
|
+
* recognised. The longer hint stays in the tooltip.
|
|
85
124
|
*
|
|
86
125
|
* @param {object[]} actions from menuActions
|
|
87
126
|
* @returns {string} markup
|
|
@@ -92,10 +131,15 @@ export function drawMenu(actions) {
|
|
|
92
131
|
: `<button type="button" role="menuitem"
|
|
93
132
|
class="dz-menu-item${one.tone ? ` is-${esc(one.tone)}` : ''}"
|
|
94
133
|
data-action="${esc(one.action)}"
|
|
134
|
+
${one.index != null ? `data-index="${esc(one.index)}"` : ''}
|
|
95
135
|
title="${esc(one.label)} (${esc(one.hint)})"
|
|
96
136
|
aria-label="${esc(one.label)}"
|
|
97
137
|
${one.disabled ? 'disabled' : ''}
|
|
98
|
-
>${
|
|
138
|
+
>${[
|
|
139
|
+
`<span class="dz-menu-icon" aria-hidden="true">${icon(one.glyph)}</span>`,
|
|
140
|
+
`<span class="dz-menu-label">${esc(one.label)}</span>`,
|
|
141
|
+
one.keys ? `<kbd class="dz-menu-keys">${esc(one.keys)}</kbd>` : ''
|
|
142
|
+
].join('')}</button>`;
|
|
99
143
|
|
|
100
144
|
return `<div class="dz-menu" data-role="menu" role="menu"
|
|
101
145
|
aria-label="Item actions">${actions.map(entry).join('')}</div>`;
|