report-studio 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +129 -4
- package/dist/designer-qy59eEux.js +3416 -0
- package/dist/designer.js +2 -3209
- package/dist/index.js +2 -2
- package/dist/react-designer.js +41 -0
- package/dist/react.js +60 -0
- package/dist/{viewer-D-c0lKzQ.js → viewer-xa17tyMJ.js} +682 -537
- package/package.json +80 -66
- package/src/cli/serve.js +135 -135
- package/src/designer/designer.js +2054 -1900
- package/src/designer/dialog.js +708 -708
- package/src/designer/fields.js +51 -2
- package/src/designer/icons.js +5 -0
- package/src/designer/index.html +28 -28
- package/src/designer/main.js +39 -39
- package/src/designer/menu.js +2 -2
- package/src/designer/panel.js +21 -1
- package/src/designer/sample-data.js +180 -9
- package/src/designer/structure.js +87 -6
- package/src/designer/styles.css +1654 -1616
- package/src/designer/tokens.js +9 -0
- package/src/engine/csv.js +350 -315
- package/src/engine/group.js +128 -65
- package/src/engine/index.js +35 -34
- package/src/engine/measure.js +13 -12
- package/src/engine/paginate.js +8 -4
- package/src/engine/resolve.js +37 -0
- package/src/engine/tables.js +87 -0
- package/src/engine/validate.js +97 -22
- package/src/index.html +79 -79
- package/src/index.js +51 -50
- package/src/preview/chrome.css +505 -505
- package/src/preview/chrome.js +147 -147
- package/src/preview/index.html +28 -28
- package/src/preview/viewer.js +763 -745
- package/src/react/designer.js +87 -0
- package/src/react/index.js +117 -0
- package/src/render/items.js +94 -1
- package/src/render/report.css +30 -0
- package/src/server/routes.js +331 -331
- package/src/shared/brand.css +29 -29
- package/src/shared/dropdown.css +136 -136
- package/src/shared/modal.css +125 -125
- package/src/shared/store.js +145 -145
- package/src/shared/theme.css +72 -72
- package/src/shared/window.js +138 -138
package/src/preview/viewer.js
CHANGED
|
@@ -1,745 +1,763 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* viewer.js turns a page list into a page-at-a-time viewer: navigation, zoom,
|
|
3
|
-
* search, print and CSV.
|
|
4
|
-
*
|
|
5
|
-
* It takes one empty element and builds everything inside it - the chrome is
|
|
6
|
-
* chrome.js and the report is render.js - so an application mounts it the same
|
|
7
|
-
* way it mounts the designer, with a div and nothing else.
|
|
8
|
-
*
|
|
9
|
-
* All it knows about the report is the page list, which it hands to the
|
|
10
|
-
* engine's search. That is the seam in spec 2.2: a viewer for another framework
|
|
11
|
-
* reuses the same search, the same CSV and the same rendering, and replaces
|
|
12
|
-
* only the event handlers in this file.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { search, searchPages } from '../engine/search.js';
|
|
16
|
-
import { toReportCSV, reportFilename } from '../engine/csv.js';
|
|
17
|
-
import { openWindow, closeWithOpener } from '../shared/window.js';
|
|
18
|
-
import { createDropdown } from '../shared/dropdown.js';
|
|
19
|
-
import { render } from '../render/render.js';
|
|
20
|
-
import { chrome } from './chrome.js';
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The zoom levels the -/+ buttons step through, matching the menu beside them.
|
|
25
|
-
*
|
|
26
|
-
* Exported because the designer steps through the same ones. A report zoomed to
|
|
27
|
-
* 125% while it is being arranged and to 130% while it is being read would be
|
|
28
|
-
* two tools rather than two views of one.
|
|
29
|
-
*/
|
|
30
|
-
export const ZOOM_STEPS = [0.5, 0.75, 1, 1.25, 1.5, 2, 3];
|
|
31
|
-
|
|
32
|
-
export const MIN_ZOOM = ZOOM_STEPS[0];
|
|
33
|
-
export const MAX_ZOOM = ZOOM_STEPS[ZOOM_STEPS.length - 1];
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Mounts the viewer.
|
|
38
|
-
*
|
|
39
|
-
* @param {object} options
|
|
40
|
-
* @param {Element|string} options.mount the element to take over, or a selector
|
|
41
|
-
* @param {object} options.paginated the buildPages result
|
|
42
|
-
* @param {string} [options.title] what the brand corner reads
|
|
43
|
-
* @returns {object} a handle: paging, zoom, search, print, export and destroy
|
|
44
|
-
*/
|
|
45
|
-
/**
|
|
46
|
-
* Opens a report in a window of its own.
|
|
47
|
-
*
|
|
48
|
-
* The same viewer, mounted in a window this opens rather than in an element the
|
|
49
|
-
* host supplies - for an application that would rather the report were beside
|
|
50
|
-
* it than inside it. The host's stylesheets are carried across, so it looks the
|
|
51
|
-
* same either way.
|
|
52
|
-
*
|
|
53
|
-
* Call it from a click or a keystroke: a browser blocks a window opened from
|
|
54
|
-
* anything else, and a blocked one comes back as null rather than as a throw,
|
|
55
|
-
* so the caller can fall back to mounting in the page.
|
|
56
|
-
*
|
|
57
|
-
* @param {object} options what createViewer takes, less `mount`
|
|
58
|
-
* @param {object} options.paginated the buildPages result
|
|
59
|
-
* @param {string} [options.title] the window's title and the brand corner
|
|
60
|
-
* @param {number} [options.width]
|
|
61
|
-
* @param {number} [options.height]
|
|
62
|
-
* @param {string} [options.features] passed to window.open as given
|
|
63
|
-
* @returns {object|null} the viewer handle, with `window`; null if blocked
|
|
64
|
-
*/
|
|
65
|
-
export function openViewerWindow({
|
|
66
|
-
paginated, title, width, height, features, ...rest
|
|
67
|
-
} = {}) {
|
|
68
|
-
const opened = openWindow({
|
|
69
|
-
title: title ?? 'Report', width, height, features
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
if (!opened) return null;
|
|
73
|
-
|
|
74
|
-
const viewer = createViewer({
|
|
75
|
-
...rest, mount: opened.mount, paginated, title
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
/** the report was passed in memory, so the window outliving its opener shows nothing */
|
|
79
|
-
const unwatch = closeWithOpener(globalThis, opened.window, viewer.destroy);
|
|
80
|
-
|
|
81
|
-
return {
|
|
82
|
-
...viewer,
|
|
83
|
-
window: opened.window,
|
|
84
|
-
|
|
85
|
-
destroy() {
|
|
86
|
-
unwatch();
|
|
87
|
-
viewer.destroy();
|
|
88
|
-
opened.close();
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
export function createViewer({ mount, paginated, title } = {}) {
|
|
95
|
-
const root = typeof mount === 'string' ? document.querySelector(mount) : mount;
|
|
96
|
-
|
|
97
|
-
if (!root) {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`createViewer: no element for mount ${JSON.stringify(mount)}. ` +
|
|
100
|
-
`Add <div id="report"></div> to the page first.`
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* The document the viewer is mounted in, which is not always this one: a
|
|
106
|
-
* report opened in its own window lives in that window's document, and a
|
|
107
|
-
* listener - or a print stylesheet - left on the opener's would never reach
|
|
108
|
-
* the report it belongs to.
|
|
109
|
-
*/
|
|
110
|
-
const doc = root.ownerDocument;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* And the window it belongs to, which is the same point one step out.
|
|
114
|
-
*
|
|
115
|
-
* `window` in this module is whichever page loaded the script - the host's
|
|
116
|
-
* tab. A report opened in a window of its own is mounted from that tab, so
|
|
117
|
-
* every bare `window` here reached back into it: print() printed the
|
|
118
|
-
* application behind the report instead of the report, the print dialog
|
|
119
|
-
* appeared over the wrong window, and afterprint fired somewhere the
|
|
120
|
-
* highlights were not.
|
|
121
|
-
*
|
|
122
|
-
* A viewer mounted in the page resolves this to the same `window` it always
|
|
123
|
-
* was, so nothing changes there.
|
|
124
|
-
*/
|
|
125
|
-
const view = doc.defaultView ?? globalThis;
|
|
126
|
-
|
|
127
|
-
if (!paginated?.pages) {
|
|
128
|
-
throw new Error(
|
|
129
|
-
'createViewer: paginated must be a buildPages(layout, data) result.'
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
root.classList.add('report-viewer');
|
|
134
|
-
root.innerHTML = chrome({ title });
|
|
135
|
-
|
|
136
|
-
const viewport = root.querySelector('[data-role="viewport"]');
|
|
137
|
-
viewport.innerHTML = render(paginated);
|
|
138
|
-
|
|
139
|
-
const pages = [...viewport.querySelectorAll('.page')];
|
|
140
|
-
const total = pages.length;
|
|
141
|
-
|
|
142
|
-
const stack = viewport.querySelector('#main-page');
|
|
143
|
-
|
|
144
|
-
/** the report itself is what scrolls and what the highlighting sits in */
|
|
145
|
-
const mountEl = viewport;
|
|
146
|
-
|
|
147
|
-
const modal = root.querySelector('[data-role="export-modal"]');
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* One search root now that the viewer owns all of its chrome. A control is
|
|
151
|
-
* still found by its role rather than by which container holds it, so
|
|
152
|
-
* moving one between the toolbar and the pager stays a change to chrome.js
|
|
153
|
-
* alone.
|
|
154
|
-
*/
|
|
155
|
-
const find = (role) => root.querySelector(`[data-role="${role}"]`);
|
|
156
|
-
|
|
157
|
-
const ui = {
|
|
158
|
-
prev: find('prev'),
|
|
159
|
-
next: find('next'),
|
|
160
|
-
current: find('current'),
|
|
161
|
-
total: find('total'),
|
|
162
|
-
/** a listbox with the two bits of the <select> API this file used */
|
|
163
|
-
zoom: createDropdown(find('zoom-dropdown')),
|
|
164
|
-
zoomIn: find('zoom-in'),
|
|
165
|
-
zoomOut: find('zoom-out'),
|
|
166
|
-
print: find('print'),
|
|
167
|
-
exportOpen: find('export'),
|
|
168
|
-
query: find('query'),
|
|
169
|
-
hits: find('hits'),
|
|
170
|
-
hitPrev: find('hit-prev'),
|
|
171
|
-
hitNext: find('hit-next')
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const dialog = modal ? {
|
|
175
|
-
root: modal,
|
|
176
|
-
card: modal.querySelector('.modal-card'),
|
|
177
|
-
backdrop: modal.querySelector('[data-role="export-backdrop"]'),
|
|
178
|
-
summary: modal.querySelector('[data-role="export-summary"]'),
|
|
179
|
-
cancel: modal.querySelector('[data-role="export-cancel"]'),
|
|
180
|
-
confirm: modal.querySelector('[data-role="export-confirm"]'),
|
|
181
|
-
formats: [...modal.querySelectorAll('input[name="export-format"]')]
|
|
182
|
-
} : null;
|
|
183
|
-
|
|
184
|
-
let index = 0; // zero-based page index
|
|
185
|
-
let matches = []; // engine search results
|
|
186
|
-
let hitIndex = -1; // which match is focused
|
|
187
|
-
let zoom = 1; // the scale actually applied
|
|
188
|
-
let fitWidth = false; // recompute zoom from the viewport on resize
|
|
189
|
-
let returnFocusTo = null; // what had focus before the dialog opened
|
|
190
|
-
|
|
191
|
-
const afterPrintSupported = typeof view.onafterprint !== 'undefined';
|
|
192
|
-
|
|
193
|
-
ui.total.textContent = String(total);
|
|
194
|
-
ui.current.max = String(total);
|
|
195
|
-
|
|
196
|
-
/* ---------------- paging ---------------- */
|
|
197
|
-
|
|
198
|
-
function show(next) {
|
|
199
|
-
index = Math.min(Math.max(next, 0), total - 1);
|
|
200
|
-
|
|
201
|
-
pages.forEach((page, i) => page.classList.toggle('is-current', i === index));
|
|
202
|
-
|
|
203
|
-
ui.current.value = String(index + 1);
|
|
204
|
-
ui.prev.disabled = index === 0;
|
|
205
|
-
ui.next.disabled = index === total - 1;
|
|
206
|
-
|
|
207
|
-
paint();
|
|
208
|
-
/** absent in jsdom, so a consumer testing there is not stopped by it */
|
|
209
|
-
mountEl.scrollTo?.({ top: 0 });
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/* ---------------- zoom ---------------- */
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* A CSS transform is what keeps the report faithful - scaling font sizes
|
|
216
|
-
* instead would re-wrap text and stop matching the paginated layout, which
|
|
217
|
-
* is the whole point of a print preview.
|
|
218
|
-
*
|
|
219
|
-
* transform does not affect layout, so the stack is given the scaled
|
|
220
|
-
* footprint explicitly, otherwise there is nothing to scroll at 200%.
|
|
221
|
-
* Its size comes from the layout file rather than the DOM, so it is right
|
|
222
|
-
* before first paint and does not depend on a live layout engine.
|
|
223
|
-
*/
|
|
224
|
-
function applyZoom() {
|
|
225
|
-
mountEl.style.setProperty('--zoom', String(zoom));
|
|
226
|
-
|
|
227
|
-
if (stack) {
|
|
228
|
-
stack.style.width = `${paginated.page.width * zoom}px`;
|
|
229
|
-
stack.style.height = `${paginated.page.height * zoom}px`;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (!fitWidth) {
|
|
233
|
-
const exact = ZOOM_STEPS.find(step => Math.abs(step - zoom) < 1e-9);
|
|
234
|
-
ui.zoom.value = exact !== undefined ? String(exact) : 'fit';
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
ui.zoomOut.disabled = !fitWidth && zoom <= MIN_ZOOM;
|
|
238
|
-
ui.zoomIn.disabled = !fitWidth && zoom >= MAX_ZOOM;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function setZoom(next) {
|
|
242
|
-
fitWidth = false;
|
|
243
|
-
zoom = Math.min(Math.max(next, MIN_ZOOM), MAX_ZOOM);
|
|
244
|
-
applyZoom();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** the widest scale that still fits the viewport, ignoring the scrollbar */
|
|
248
|
-
function setFitWidth() {
|
|
249
|
-
fitWidth = true;
|
|
250
|
-
ui.zoom.value = 'fit';
|
|
251
|
-
|
|
252
|
-
const available = mountEl.clientWidth - 48;
|
|
253
|
-
if (available > 0) {
|
|
254
|
-
zoom = Math.min(Math.max(available / paginated.page.width, MIN_ZOOM), MAX_ZOOM);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
applyZoom();
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/** steps to the neighbouring preset rather than a blind multiply */
|
|
261
|
-
function stepZoom(direction) {
|
|
262
|
-
const from = zoom;
|
|
263
|
-
const next = direction > 0
|
|
264
|
-
? ZOOM_STEPS.find(step => step > from + 1e-9)
|
|
265
|
-
: [...ZOOM_STEPS].reverse().find(step => step < from - 1e-9);
|
|
266
|
-
|
|
267
|
-
setZoom(next ?? from);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/* ---------------- exporting ---------------- */
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* One entry per offered format. Adding a third means a line here and a radio
|
|
274
|
-
* in the dialog; nothing else in the flow needs to know about it.
|
|
275
|
-
*/
|
|
276
|
-
const FORMATS = {
|
|
277
|
-
pdf: {
|
|
278
|
-
available: () => true,
|
|
279
|
-
summary: () => `${total} page${total === 1 ? '' : 's'}, ` +
|
|
280
|
-
`${paginated.page.width}×${paginated.page.height}px each.`,
|
|
281
|
-
run: () => printReport()
|
|
282
|
-
},
|
|
283
|
-
csv: {
|
|
284
|
-
available: () => toReportCSV(paginated.pages) !== '',
|
|
285
|
-
summary: () => FORMATS.csv.available()
|
|
286
|
-
? `Downloads ${reportFilename(paginated, 'csv')}.`
|
|
287
|
-
: 'This report has nothing to write out.',
|
|
288
|
-
run: () => downloadCSV()
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Spec 6: PDF is window.print(). It costs nothing, and it matches the
|
|
294
|
-
* preview exactly because it *is* the preview - the print stylesheet just
|
|
295
|
-
* reveals every page and drops the chrome.
|
|
296
|
-
*
|
|
297
|
-
* Search highlights are cleared first so they do not end up in the file.
|
|
298
|
-
*/
|
|
299
|
-
function printReport() {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
const
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
ui.
|
|
613
|
-
ui.
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
if (
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
1
|
+
/**
|
|
2
|
+
* viewer.js turns a page list into a page-at-a-time viewer: navigation, zoom,
|
|
3
|
+
* search, print and CSV.
|
|
4
|
+
*
|
|
5
|
+
* It takes one empty element and builds everything inside it - the chrome is
|
|
6
|
+
* chrome.js and the report is render.js - so an application mounts it the same
|
|
7
|
+
* way it mounts the designer, with a div and nothing else.
|
|
8
|
+
*
|
|
9
|
+
* All it knows about the report is the page list, which it hands to the
|
|
10
|
+
* engine's search. That is the seam in spec 2.2: a viewer for another framework
|
|
11
|
+
* reuses the same search, the same CSV and the same rendering, and replaces
|
|
12
|
+
* only the event handlers in this file.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { search, searchPages } from '../engine/search.js';
|
|
16
|
+
import { toReportCSV, reportFilename } from '../engine/csv.js';
|
|
17
|
+
import { openWindow, closeWithOpener } from '../shared/window.js';
|
|
18
|
+
import { createDropdown } from '../shared/dropdown.js';
|
|
19
|
+
import { render } from '../render/render.js';
|
|
20
|
+
import { chrome } from './chrome.js';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The zoom levels the -/+ buttons step through, matching the menu beside them.
|
|
25
|
+
*
|
|
26
|
+
* Exported because the designer steps through the same ones. A report zoomed to
|
|
27
|
+
* 125% while it is being arranged and to 130% while it is being read would be
|
|
28
|
+
* two tools rather than two views of one.
|
|
29
|
+
*/
|
|
30
|
+
export const ZOOM_STEPS = [0.5, 0.75, 1, 1.25, 1.5, 2, 3];
|
|
31
|
+
|
|
32
|
+
export const MIN_ZOOM = ZOOM_STEPS[0];
|
|
33
|
+
export const MAX_ZOOM = ZOOM_STEPS[ZOOM_STEPS.length - 1];
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Mounts the viewer.
|
|
38
|
+
*
|
|
39
|
+
* @param {object} options
|
|
40
|
+
* @param {Element|string} options.mount the element to take over, or a selector
|
|
41
|
+
* @param {object} options.paginated the buildPages result
|
|
42
|
+
* @param {string} [options.title] what the brand corner reads
|
|
43
|
+
* @returns {object} a handle: paging, zoom, search, print, export and destroy
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* Opens a report in a window of its own.
|
|
47
|
+
*
|
|
48
|
+
* The same viewer, mounted in a window this opens rather than in an element the
|
|
49
|
+
* host supplies - for an application that would rather the report were beside
|
|
50
|
+
* it than inside it. The host's stylesheets are carried across, so it looks the
|
|
51
|
+
* same either way.
|
|
52
|
+
*
|
|
53
|
+
* Call it from a click or a keystroke: a browser blocks a window opened from
|
|
54
|
+
* anything else, and a blocked one comes back as null rather than as a throw,
|
|
55
|
+
* so the caller can fall back to mounting in the page.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} options what createViewer takes, less `mount`
|
|
58
|
+
* @param {object} options.paginated the buildPages result
|
|
59
|
+
* @param {string} [options.title] the window's title and the brand corner
|
|
60
|
+
* @param {number} [options.width]
|
|
61
|
+
* @param {number} [options.height]
|
|
62
|
+
* @param {string} [options.features] passed to window.open as given
|
|
63
|
+
* @returns {object|null} the viewer handle, with `window`; null if blocked
|
|
64
|
+
*/
|
|
65
|
+
export function openViewerWindow({
|
|
66
|
+
paginated, title, width, height, features, ...rest
|
|
67
|
+
} = {}) {
|
|
68
|
+
const opened = openWindow({
|
|
69
|
+
title: title ?? 'Report', width, height, features
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (!opened) return null;
|
|
73
|
+
|
|
74
|
+
const viewer = createViewer({
|
|
75
|
+
...rest, mount: opened.mount, paginated, title
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/** the report was passed in memory, so the window outliving its opener shows nothing */
|
|
79
|
+
const unwatch = closeWithOpener(globalThis, opened.window, viewer.destroy);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
...viewer,
|
|
83
|
+
window: opened.window,
|
|
84
|
+
|
|
85
|
+
destroy() {
|
|
86
|
+
unwatch();
|
|
87
|
+
viewer.destroy();
|
|
88
|
+
opened.close();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
export function createViewer({ mount, paginated, title } = {}) {
|
|
95
|
+
const root = typeof mount === 'string' ? document.querySelector(mount) : mount;
|
|
96
|
+
|
|
97
|
+
if (!root) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`createViewer: no element for mount ${JSON.stringify(mount)}. ` +
|
|
100
|
+
`Add <div id="report"></div> to the page first.`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The document the viewer is mounted in, which is not always this one: a
|
|
106
|
+
* report opened in its own window lives in that window's document, and a
|
|
107
|
+
* listener - or a print stylesheet - left on the opener's would never reach
|
|
108
|
+
* the report it belongs to.
|
|
109
|
+
*/
|
|
110
|
+
const doc = root.ownerDocument;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* And the window it belongs to, which is the same point one step out.
|
|
114
|
+
*
|
|
115
|
+
* `window` in this module is whichever page loaded the script - the host's
|
|
116
|
+
* tab. A report opened in a window of its own is mounted from that tab, so
|
|
117
|
+
* every bare `window` here reached back into it: print() printed the
|
|
118
|
+
* application behind the report instead of the report, the print dialog
|
|
119
|
+
* appeared over the wrong window, and afterprint fired somewhere the
|
|
120
|
+
* highlights were not.
|
|
121
|
+
*
|
|
122
|
+
* A viewer mounted in the page resolves this to the same `window` it always
|
|
123
|
+
* was, so nothing changes there.
|
|
124
|
+
*/
|
|
125
|
+
const view = doc.defaultView ?? globalThis;
|
|
126
|
+
|
|
127
|
+
if (!paginated?.pages) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
'createViewer: paginated must be a buildPages(layout, data) result.'
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
root.classList.add('report-viewer');
|
|
134
|
+
root.innerHTML = chrome({ title });
|
|
135
|
+
|
|
136
|
+
const viewport = root.querySelector('[data-role="viewport"]');
|
|
137
|
+
viewport.innerHTML = render(paginated);
|
|
138
|
+
|
|
139
|
+
const pages = [...viewport.querySelectorAll('.page')];
|
|
140
|
+
const total = pages.length;
|
|
141
|
+
|
|
142
|
+
const stack = viewport.querySelector('#main-page');
|
|
143
|
+
|
|
144
|
+
/** the report itself is what scrolls and what the highlighting sits in */
|
|
145
|
+
const mountEl = viewport;
|
|
146
|
+
|
|
147
|
+
const modal = root.querySelector('[data-role="export-modal"]');
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* One search root now that the viewer owns all of its chrome. A control is
|
|
151
|
+
* still found by its role rather than by which container holds it, so
|
|
152
|
+
* moving one between the toolbar and the pager stays a change to chrome.js
|
|
153
|
+
* alone.
|
|
154
|
+
*/
|
|
155
|
+
const find = (role) => root.querySelector(`[data-role="${role}"]`);
|
|
156
|
+
|
|
157
|
+
const ui = {
|
|
158
|
+
prev: find('prev'),
|
|
159
|
+
next: find('next'),
|
|
160
|
+
current: find('current'),
|
|
161
|
+
total: find('total'),
|
|
162
|
+
/** a listbox with the two bits of the <select> API this file used */
|
|
163
|
+
zoom: createDropdown(find('zoom-dropdown')),
|
|
164
|
+
zoomIn: find('zoom-in'),
|
|
165
|
+
zoomOut: find('zoom-out'),
|
|
166
|
+
print: find('print'),
|
|
167
|
+
exportOpen: find('export'),
|
|
168
|
+
query: find('query'),
|
|
169
|
+
hits: find('hits'),
|
|
170
|
+
hitPrev: find('hit-prev'),
|
|
171
|
+
hitNext: find('hit-next')
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const dialog = modal ? {
|
|
175
|
+
root: modal,
|
|
176
|
+
card: modal.querySelector('.modal-card'),
|
|
177
|
+
backdrop: modal.querySelector('[data-role="export-backdrop"]'),
|
|
178
|
+
summary: modal.querySelector('[data-role="export-summary"]'),
|
|
179
|
+
cancel: modal.querySelector('[data-role="export-cancel"]'),
|
|
180
|
+
confirm: modal.querySelector('[data-role="export-confirm"]'),
|
|
181
|
+
formats: [...modal.querySelectorAll('input[name="export-format"]')]
|
|
182
|
+
} : null;
|
|
183
|
+
|
|
184
|
+
let index = 0; // zero-based page index
|
|
185
|
+
let matches = []; // engine search results
|
|
186
|
+
let hitIndex = -1; // which match is focused
|
|
187
|
+
let zoom = 1; // the scale actually applied
|
|
188
|
+
let fitWidth = false; // recompute zoom from the viewport on resize
|
|
189
|
+
let returnFocusTo = null; // what had focus before the dialog opened
|
|
190
|
+
|
|
191
|
+
const afterPrintSupported = typeof view.onafterprint !== 'undefined';
|
|
192
|
+
|
|
193
|
+
ui.total.textContent = String(total);
|
|
194
|
+
ui.current.max = String(total);
|
|
195
|
+
|
|
196
|
+
/* ---------------- paging ---------------- */
|
|
197
|
+
|
|
198
|
+
function show(next) {
|
|
199
|
+
index = Math.min(Math.max(next, 0), total - 1);
|
|
200
|
+
|
|
201
|
+
pages.forEach((page, i) => page.classList.toggle('is-current', i === index));
|
|
202
|
+
|
|
203
|
+
ui.current.value = String(index + 1);
|
|
204
|
+
ui.prev.disabled = index === 0;
|
|
205
|
+
ui.next.disabled = index === total - 1;
|
|
206
|
+
|
|
207
|
+
paint();
|
|
208
|
+
/** absent in jsdom, so a consumer testing there is not stopped by it */
|
|
209
|
+
mountEl.scrollTo?.({ top: 0 });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* ---------------- zoom ---------------- */
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* A CSS transform is what keeps the report faithful - scaling font sizes
|
|
216
|
+
* instead would re-wrap text and stop matching the paginated layout, which
|
|
217
|
+
* is the whole point of a print preview.
|
|
218
|
+
*
|
|
219
|
+
* transform does not affect layout, so the stack is given the scaled
|
|
220
|
+
* footprint explicitly, otherwise there is nothing to scroll at 200%.
|
|
221
|
+
* Its size comes from the layout file rather than the DOM, so it is right
|
|
222
|
+
* before first paint and does not depend on a live layout engine.
|
|
223
|
+
*/
|
|
224
|
+
function applyZoom() {
|
|
225
|
+
mountEl.style.setProperty('--zoom', String(zoom));
|
|
226
|
+
|
|
227
|
+
if (stack) {
|
|
228
|
+
stack.style.width = `${paginated.page.width * zoom}px`;
|
|
229
|
+
stack.style.height = `${paginated.page.height * zoom}px`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (!fitWidth) {
|
|
233
|
+
const exact = ZOOM_STEPS.find(step => Math.abs(step - zoom) < 1e-9);
|
|
234
|
+
ui.zoom.value = exact !== undefined ? String(exact) : 'fit';
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
ui.zoomOut.disabled = !fitWidth && zoom <= MIN_ZOOM;
|
|
238
|
+
ui.zoomIn.disabled = !fitWidth && zoom >= MAX_ZOOM;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function setZoom(next) {
|
|
242
|
+
fitWidth = false;
|
|
243
|
+
zoom = Math.min(Math.max(next, MIN_ZOOM), MAX_ZOOM);
|
|
244
|
+
applyZoom();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** the widest scale that still fits the viewport, ignoring the scrollbar */
|
|
248
|
+
function setFitWidth() {
|
|
249
|
+
fitWidth = true;
|
|
250
|
+
ui.zoom.value = 'fit';
|
|
251
|
+
|
|
252
|
+
const available = mountEl.clientWidth - 48;
|
|
253
|
+
if (available > 0) {
|
|
254
|
+
zoom = Math.min(Math.max(available / paginated.page.width, MIN_ZOOM), MAX_ZOOM);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
applyZoom();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** steps to the neighbouring preset rather than a blind multiply */
|
|
261
|
+
function stepZoom(direction) {
|
|
262
|
+
const from = zoom;
|
|
263
|
+
const next = direction > 0
|
|
264
|
+
? ZOOM_STEPS.find(step => step > from + 1e-9)
|
|
265
|
+
: [...ZOOM_STEPS].reverse().find(step => step < from - 1e-9);
|
|
266
|
+
|
|
267
|
+
setZoom(next ?? from);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* ---------------- exporting ---------------- */
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* One entry per offered format. Adding a third means a line here and a radio
|
|
274
|
+
* in the dialog; nothing else in the flow needs to know about it.
|
|
275
|
+
*/
|
|
276
|
+
const FORMATS = {
|
|
277
|
+
pdf: {
|
|
278
|
+
available: () => true,
|
|
279
|
+
summary: () => `${total} page${total === 1 ? '' : 's'}, ` +
|
|
280
|
+
`${paginated.page.width}×${paginated.page.height}px each.`,
|
|
281
|
+
run: () => printReport()
|
|
282
|
+
},
|
|
283
|
+
csv: {
|
|
284
|
+
available: () => toReportCSV(paginated.pages) !== '',
|
|
285
|
+
summary: () => FORMATS.csv.available()
|
|
286
|
+
? `Downloads ${reportFilename(paginated, 'csv')}.`
|
|
287
|
+
: 'This report has nothing to write out.',
|
|
288
|
+
run: () => downloadCSV()
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Spec 6: PDF is window.print(). It costs nothing, and it matches the
|
|
294
|
+
* preview exactly because it *is* the preview - the print stylesheet just
|
|
295
|
+
* reveals every page and drops the chrome.
|
|
296
|
+
*
|
|
297
|
+
* Search highlights are cleared first so they do not end up in the file.
|
|
298
|
+
*/
|
|
299
|
+
function printReport() {
|
|
300
|
+
/**
|
|
301
|
+
* A picture still loading prints as a blank frame, and the print dialog
|
|
302
|
+
* snapshots the page the moment it opens. So a report with pictures on
|
|
303
|
+
* the way waits for them - loaded or failed, either way settled - and a
|
|
304
|
+
* report with none, which is most, prints at once as it always has.
|
|
305
|
+
*/
|
|
306
|
+
const pending = [...viewport.querySelectorAll('img')].filter(img => !img.complete);
|
|
307
|
+
|
|
308
|
+
if (pending.length > 0) {
|
|
309
|
+
Promise.all(pending.map(img => img.decode().catch(() => { })))
|
|
310
|
+
.then(printNow);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
printNow();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function printNow() {
|
|
318
|
+
clearMarks();
|
|
319
|
+
|
|
320
|
+
/** the window the report is in, which is not always the one this ran from */
|
|
321
|
+
view.print();
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Firefox and Safari return from print() before the dialog has taken
|
|
325
|
+
* its snapshot, so restoring the highlights on the next line would put
|
|
326
|
+
* them back too early and print them. afterprint is the reliable point;
|
|
327
|
+
* the immediate call below covers browsers that never fire it.
|
|
328
|
+
*/
|
|
329
|
+
if (!afterPrintSupported) restoreAfterPrint();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function restoreAfterPrint() {
|
|
333
|
+
if (ui.query.value.trim() !== '') paint();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The report already carries its own margins, so the sheet must add none -
|
|
339
|
+
* without this the browser's default print margin is added on top, the
|
|
340
|
+
* page no longer fits the paper, and every page spills a sliver onto a
|
|
341
|
+
* second sheet. The size comes from the layout, so a Letter or custom page
|
|
342
|
+
* prints as designed rather than as whatever A4 assumption was baked in.
|
|
343
|
+
*/
|
|
344
|
+
function applyPrintPageSize() {
|
|
345
|
+
const { width, height } = paginated.page;
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Into the viewer's own document: a report opened in its own window
|
|
349
|
+
* prints from that window, and an @page rule left in the opener would
|
|
350
|
+
* size the wrong sheet - or none.
|
|
351
|
+
*/
|
|
352
|
+
let style = doc.getElementById('report-page-size');
|
|
353
|
+
if (!style) {
|
|
354
|
+
style = doc.createElement('style');
|
|
355
|
+
style.id = 'report-page-size';
|
|
356
|
+
doc.head.appendChild(style);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
style.textContent = `@page { size: ${width}px ${height}px; margin: 0; }`;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* The report as text: every line and every table row, in the order the
|
|
364
|
+
* pages draw them. Boxes and rules are the only things left behind, having
|
|
365
|
+
* nothing to write.
|
|
366
|
+
*/
|
|
367
|
+
function downloadCSV() {
|
|
368
|
+
const csv = toReportCSV(paginated.pages);
|
|
369
|
+
if (csv === '') return false;
|
|
370
|
+
|
|
371
|
+
/** the BOM is what makes Excel read the file as UTF-8 rather than latin-1 */
|
|
372
|
+
download(
|
|
373
|
+
new Blob(['', csv], { type: 'text/csv;charset=utf-8' }),
|
|
374
|
+
reportFilename(paginated, 'csv')
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function download(blob, filename) {
|
|
381
|
+
const url = URL.createObjectURL(blob);
|
|
382
|
+
const link = doc.createElement('a');
|
|
383
|
+
|
|
384
|
+
link.href = url;
|
|
385
|
+
link.download = filename;
|
|
386
|
+
doc.body.appendChild(link);
|
|
387
|
+
link.click();
|
|
388
|
+
link.remove();
|
|
389
|
+
|
|
390
|
+
/** give the browser a tick to start the download before dropping the url */
|
|
391
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/* ---------------- the export dialog ---------------- */
|
|
395
|
+
|
|
396
|
+
function chosenFormat() {
|
|
397
|
+
return dialog?.formats.find(input => input.checked)?.value ?? 'pdf';
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** the note under the radios, plus whether Export can proceed at all */
|
|
401
|
+
function refreshDialog() {
|
|
402
|
+
if (!dialog) return;
|
|
403
|
+
|
|
404
|
+
for (const input of dialog.formats) {
|
|
405
|
+
const format = FORMATS[input.value];
|
|
406
|
+
input.disabled = format ? !format.available() : true;
|
|
407
|
+
input.closest('.format')?.classList.toggle('is-unavailable', input.disabled);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** never leave a disabled option selected */
|
|
411
|
+
const current = dialog.formats.find(i => i.checked);
|
|
412
|
+
if (current?.disabled) {
|
|
413
|
+
const fallback = dialog.formats.find(i => !i.disabled);
|
|
414
|
+
if (fallback) fallback.checked = true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const format = FORMATS[chosenFormat()];
|
|
418
|
+
dialog.summary.textContent = format ? format.summary() : '';
|
|
419
|
+
dialog.confirm.disabled = !format || !format.available();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function openExport() {
|
|
423
|
+
if (!dialog) return;
|
|
424
|
+
|
|
425
|
+
returnFocusTo = doc.activeElement;
|
|
426
|
+
dialog.root.hidden = false;
|
|
427
|
+
refreshDialog();
|
|
428
|
+
|
|
429
|
+
const focusOn = dialog.formats.find(i => i.checked && !i.disabled)
|
|
430
|
+
?? dialog.formats.find(i => !i.disabled)
|
|
431
|
+
?? dialog.cancel;
|
|
432
|
+
focusOn.focus();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function closeExport() {
|
|
436
|
+
if (!dialog || dialog.root.hidden) return;
|
|
437
|
+
|
|
438
|
+
dialog.root.hidden = true;
|
|
439
|
+
returnFocusTo?.focus?.();
|
|
440
|
+
returnFocusTo = null;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function confirmExport() {
|
|
444
|
+
const format = FORMATS[chosenFormat()];
|
|
445
|
+
if (!format?.available()) return;
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Close first. The print dialog is modal and blocking, so leaving our
|
|
449
|
+
* own overlay up would put it in the printed page and in the way.
|
|
450
|
+
*/
|
|
451
|
+
closeExport();
|
|
452
|
+
format.run();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** keeps Tab inside the card while the dialog is up */
|
|
456
|
+
function trapFocus(event) {
|
|
457
|
+
const focusable = [...dialog.card.querySelectorAll('button, input')]
|
|
458
|
+
.filter(node => !node.disabled);
|
|
459
|
+
if (!focusable.length) return;
|
|
460
|
+
|
|
461
|
+
const first = focusable[0];
|
|
462
|
+
const last = focusable[focusable.length - 1];
|
|
463
|
+
|
|
464
|
+
if (event.shiftKey && doc.activeElement === first) {
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
last.focus();
|
|
467
|
+
} else if (!event.shiftKey && doc.activeElement === last) {
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
first.focus();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/* ---------------- search ---------------- */
|
|
474
|
+
|
|
475
|
+
function runSearch() {
|
|
476
|
+
const query = ui.query.value.trim();
|
|
477
|
+
|
|
478
|
+
clearMarks();
|
|
479
|
+
|
|
480
|
+
if (query === '') {
|
|
481
|
+
matches = [];
|
|
482
|
+
hitIndex = -1;
|
|
483
|
+
ui.hits.textContent = '';
|
|
484
|
+
ui.hits.dataset.state = 'idle';
|
|
485
|
+
setHitButtons();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
matches = search(paginated.pages, query);
|
|
490
|
+
hitIndex = matches.length ? 0 : -1;
|
|
491
|
+
|
|
492
|
+
ui.hits.dataset.state = matches.length ? 'found' : 'empty';
|
|
493
|
+
setHitButtons();
|
|
494
|
+
|
|
495
|
+
if (!matches.length) {
|
|
496
|
+
ui.hits.textContent = 'no matches';
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Jump straight to the first hit. Reporting how many pages carry it is
|
|
502
|
+
* more useful than a raw count when the viewer shows one page at a time.
|
|
503
|
+
*/
|
|
504
|
+
const onPages = searchPages(paginated.pages, query).length;
|
|
505
|
+
ui.hits.textContent =
|
|
506
|
+
`${matches.length} on ${onPages} page${onPages === 1 ? '' : 's'}`;
|
|
507
|
+
|
|
508
|
+
goToHit(0);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function goToHit(next) {
|
|
512
|
+
if (!matches.length) return;
|
|
513
|
+
|
|
514
|
+
hitIndex = (next + matches.length) % matches.length;
|
|
515
|
+
const target = matches[hitIndex].pageNO - 1;
|
|
516
|
+
|
|
517
|
+
if (target !== index) show(target);
|
|
518
|
+
else paint();
|
|
519
|
+
|
|
520
|
+
ui.hits.textContent = `${hitIndex + 1} of ${matches.length}`;
|
|
521
|
+
focusMark();
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function setHitButtons() {
|
|
525
|
+
ui.hitPrev.disabled = matches.length === 0;
|
|
526
|
+
ui.hitNext.disabled = matches.length === 0;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/* ---------------- highlighting ---------------- */
|
|
530
|
+
|
|
531
|
+
function clearMarks() {
|
|
532
|
+
for (const mark of mountEl.querySelectorAll('mark.hit')) {
|
|
533
|
+
const parent = mark.parentNode;
|
|
534
|
+
parent.replaceChild(doc.createTextNode(mark.textContent), mark);
|
|
535
|
+
parent.normalize();
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** marks every occurrence on the page currently on screen */
|
|
540
|
+
function paint() {
|
|
541
|
+
clearMarks();
|
|
542
|
+
|
|
543
|
+
const query = ui.query.value.trim();
|
|
544
|
+
if (query === '') return;
|
|
545
|
+
|
|
546
|
+
const page = pages[index];
|
|
547
|
+
if (!page) return;
|
|
548
|
+
|
|
549
|
+
const needle = query.toLowerCase();
|
|
550
|
+
const walker = doc.createTreeWalker(page, NodeFilter.SHOW_TEXT);
|
|
551
|
+
const targets = [];
|
|
552
|
+
|
|
553
|
+
while (walker.nextNode()) {
|
|
554
|
+
const node = walker.currentNode;
|
|
555
|
+
if (node.nodeValue.toLowerCase().includes(needle)) targets.push(node);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
for (const node of targets) markNode(node, needle);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function markNode(node, needle) {
|
|
562
|
+
const text = node.nodeValue;
|
|
563
|
+
const fragment = doc.createDocumentFragment();
|
|
564
|
+
|
|
565
|
+
let from = 0;
|
|
566
|
+
|
|
567
|
+
for (; ;) {
|
|
568
|
+
const at = text.toLowerCase().indexOf(needle, from);
|
|
569
|
+
if (at === -1) break;
|
|
570
|
+
|
|
571
|
+
if (at > from) fragment.appendChild(doc.createTextNode(text.slice(from, at)));
|
|
572
|
+
|
|
573
|
+
const mark = doc.createElement('mark');
|
|
574
|
+
mark.className = 'hit';
|
|
575
|
+
mark.textContent = text.slice(at, at + needle.length);
|
|
576
|
+
fragment.appendChild(mark);
|
|
577
|
+
|
|
578
|
+
from = at + needle.length;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (from < text.length) fragment.appendChild(doc.createTextNode(text.slice(from)));
|
|
582
|
+
|
|
583
|
+
node.parentNode.replaceChild(fragment, node);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** the focused hit gets a stronger colour and is scrolled into view */
|
|
587
|
+
function focusMark() {
|
|
588
|
+
const marks = [...mountEl.querySelectorAll('mark.hit')];
|
|
589
|
+
if (!marks.length) return;
|
|
590
|
+
|
|
591
|
+
/** how many of the run of matches fall on this page before the focused one */
|
|
592
|
+
const onThisPage = matches.filter(m => m.pageNO === index + 1);
|
|
593
|
+
const offset = onThisPage.indexOf(matches[hitIndex]);
|
|
594
|
+
const mark = marks[Math.max(offset, 0)] ?? marks[0];
|
|
595
|
+
|
|
596
|
+
marks.forEach(m => m.classList.remove('is-focused'));
|
|
597
|
+
mark.classList.add('is-focused');
|
|
598
|
+
mark.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/* ---------------- wiring ---------------- */
|
|
602
|
+
|
|
603
|
+
ui.prev.addEventListener('click', () => show(index - 1));
|
|
604
|
+
ui.next.addEventListener('click', () => show(index + 1));
|
|
605
|
+
|
|
606
|
+
ui.current.addEventListener('change', () => {
|
|
607
|
+
const asked = Number(ui.current.value);
|
|
608
|
+
if (Number.isFinite(asked)) show(asked - 1);
|
|
609
|
+
else ui.current.value = String(index + 1);
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
ui.print?.addEventListener('click', printReport);
|
|
613
|
+
ui.exportOpen.addEventListener('click', openExport);
|
|
614
|
+
|
|
615
|
+
if (dialog) {
|
|
616
|
+
dialog.cancel.addEventListener('click', closeExport);
|
|
617
|
+
dialog.backdrop.addEventListener('click', closeExport);
|
|
618
|
+
dialog.confirm.addEventListener('click', confirmExport);
|
|
619
|
+
|
|
620
|
+
for (const input of dialog.formats) {
|
|
621
|
+
input.addEventListener('change', refreshDialog);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
dialog.root.addEventListener('keydown', (event) => {
|
|
625
|
+
if (event.key === 'Tab') trapFocus(event);
|
|
626
|
+
if (event.key === 'Enter') { event.preventDefault(); confirmExport(); }
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
ui.zoomIn.addEventListener('click', () => stepZoom(1));
|
|
631
|
+
ui.zoomOut.addEventListener('click', () => stepZoom(-1));
|
|
632
|
+
|
|
633
|
+
ui.zoom.addEventListener('change', () => {
|
|
634
|
+
if (ui.zoom.value === 'fit') setFitWidth();
|
|
635
|
+
else setZoom(Number(ui.zoom.value));
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
const onResize = () => { if (fitWidth) setFitWidth(); };
|
|
639
|
+
view.addEventListener('resize', onResize);
|
|
640
|
+
|
|
641
|
+
ui.hitPrev.addEventListener('click', () => goToHit(hitIndex - 1));
|
|
642
|
+
ui.hitNext.addEventListener('click', () => goToHit(hitIndex + 1));
|
|
643
|
+
|
|
644
|
+
let debounce;
|
|
645
|
+
ui.query.addEventListener('input', () => {
|
|
646
|
+
clearTimeout(debounce);
|
|
647
|
+
debounce = setTimeout(runSearch, 150);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
ui.query.addEventListener('keydown', (event) => {
|
|
651
|
+
if (event.key !== 'Enter') return;
|
|
652
|
+
event.preventDefault();
|
|
653
|
+
if (matches.length) goToHit(hitIndex + (event.shiftKey ? -1 : 1));
|
|
654
|
+
else runSearch();
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
/** paging keys, as long as the user is not typing in the search box */
|
|
658
|
+
const onKeyDown = (event) => {
|
|
659
|
+
/** while the dialog is up it owns the keyboard, bar Escape */
|
|
660
|
+
if (dialog && !dialog.root.hidden) {
|
|
661
|
+
if (event.key === 'Escape') { event.preventDefault(); closeExport(); }
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* ctrl+K reaches the search from anywhere, including from inside another
|
|
667
|
+
* field, so it sits above the guard that hands typing back to the inputs.
|
|
668
|
+
* ctrl+F comes along because it is what a browser's own find uses, and
|
|
669
|
+
* the report's search is more useful than searching the one page on screen.
|
|
670
|
+
*/
|
|
671
|
+
if ((event.ctrlKey || event.metaKey) && ['k', 'f'].includes(event.key.toLowerCase())) {
|
|
672
|
+
event.preventDefault();
|
|
673
|
+
ui.query.focus();
|
|
674
|
+
ui.query.select();
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* ctrl+P is taken from the browser deliberately: printing through
|
|
680
|
+
* printReport clears the search highlights first, so they do not end up
|
|
681
|
+
* in the PDF the way the browser's own shortcut would leave them.
|
|
682
|
+
*/
|
|
683
|
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'p') {
|
|
684
|
+
event.preventDefault();
|
|
685
|
+
printReport();
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Everything below is held back while a field has focus, which is what
|
|
691
|
+
* keeps ctrl+X available for cutting text out of the search box.
|
|
692
|
+
*/
|
|
693
|
+
if (event.target === ui.query || event.target === ui.current) return;
|
|
694
|
+
|
|
695
|
+
/** ctrl+X opens the export; ctrl+S too, since the browser's save is no use here */
|
|
696
|
+
if ((event.ctrlKey || event.metaKey) && ['x', 's'].includes(event.key.toLowerCase())) {
|
|
697
|
+
event.preventDefault();
|
|
698
|
+
openExport();
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (event.key === 'ArrowRight' || event.key === 'PageDown') show(index + 1);
|
|
703
|
+
if (event.key === 'ArrowLeft' || event.key === 'PageUp') show(index - 1);
|
|
704
|
+
if (event.key === 'Home') show(0);
|
|
705
|
+
if (event.key === 'End') show(total - 1);
|
|
706
|
+
|
|
707
|
+
/** ctrl +/-/0 zooms the report, the way the browser would zoom a page */
|
|
708
|
+
if (event.ctrlKey || event.metaKey) {
|
|
709
|
+
if (event.key === '+' || event.key === '=') { event.preventDefault(); stepZoom(1); }
|
|
710
|
+
if (event.key === '-') { event.preventDefault(); stepZoom(-1); }
|
|
711
|
+
if (event.key === '0') { event.preventDefault(); setZoom(1); }
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
doc.addEventListener('keydown', onKeyDown);
|
|
716
|
+
|
|
717
|
+
if (afterPrintSupported) {
|
|
718
|
+
view.addEventListener('afterprint', restoreAfterPrint);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* The listeners above are on the viewer's own document and window, which
|
|
723
|
+
* outlive this mount. Without a way to take them off, a second viewer on
|
|
724
|
+
* the same page - or a React <Preview /> remounting - leaves the first one
|
|
725
|
+
* still handling keys.
|
|
726
|
+
*/
|
|
727
|
+
function destroy() {
|
|
728
|
+
ui.zoom.destroy();
|
|
729
|
+
doc.removeEventListener('keydown', onKeyDown);
|
|
730
|
+
view.removeEventListener('resize', onResize);
|
|
731
|
+
view.removeEventListener('afterprint', restoreAfterPrint);
|
|
732
|
+
|
|
733
|
+
/** the viewer built everything in here, so it takes it all away again */
|
|
734
|
+
root.innerHTML = '';
|
|
735
|
+
root.classList.remove('report-viewer');
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
applyPrintPageSize();
|
|
739
|
+
applyZoom();
|
|
740
|
+
show(0);
|
|
741
|
+
setHitButtons();
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
show,
|
|
745
|
+
next: () => show(index + 1),
|
|
746
|
+
prev: () => show(index - 1),
|
|
747
|
+
find: (q) => { ui.query.value = q; runSearch(); },
|
|
748
|
+
print: printReport,
|
|
749
|
+
downloadCSV,
|
|
750
|
+
openExport,
|
|
751
|
+
closeExport,
|
|
752
|
+
confirmExport,
|
|
753
|
+
destroy,
|
|
754
|
+
get exportOpen() { return dialog ? !dialog.root.hidden : false; },
|
|
755
|
+
setZoom,
|
|
756
|
+
zoomIn: () => stepZoom(1),
|
|
757
|
+
zoomOut: () => stepZoom(-1),
|
|
758
|
+
fitWidth: setFitWidth,
|
|
759
|
+
get zoom() { return zoom; },
|
|
760
|
+
get page() { return index + 1; },
|
|
761
|
+
get pageCount() { return total; }
|
|
762
|
+
};
|
|
763
|
+
}
|