athena-ui-js 2.5.1 → 2.6.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 +32 -2
- package/docs/migration-2.0.md +99 -83
- package/docs/styling.md +124 -123
- package/package.json +5 -1
- package/src/app.js +85 -0
- package/src/athena-ui.js +4 -79
- package/src/libs/config/config.js +1 -1
- package/src/libs/core/i18n.js +354 -352
- package/src/libs/hephaestus/MenuGenerator/menuGenerator.js +278 -265
- package/src/libs/hephaestus/chartGenerator/chartGenerator.js +772 -722
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +171 -161
- package/src/libs/hephaestus/formGenerator/formBuilder.js +470 -460
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +837 -825
- package/src/res/themes/default/style.css +3128 -3037
- package/types/app.d.ts +58 -0
- package/types/athena-ui.d.ts +1 -55
- package/types/libs/config/config.d.ts +96 -96
- package/types/libs/hephaestus/MenuGenerator/menuGenerator.d.ts +5 -0
- package/types/libs/hephaestus/chartGenerator/chartGenerator.d.ts +123 -117
- package/types/libs/hephaestus/dialogGenerator/dialogGenerator.d.ts +122 -122
- package/types/libs/hephaestus/feedbackGenerator/feedbackGenerator.d.ts +3 -0
- package/types/libs/hephaestus/formGenerator/authForms.d.ts +130 -130
- package/types/libs/hephaestus/formGenerator/formBuilder.d.ts +128 -128
- package/types/libs/hephaestus/tableGenerator/tableGenerator.d.ts +133 -129
|
@@ -1,825 +1,837 @@
|
|
|
1
|
-
//tableGenerator for Athena UI
|
|
2
|
-
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
-
//LICENCE: MIT
|
|
4
|
-
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
-
|
|
6
|
-
import { downloadFile, toCsv } from "../../core/csv.js";
|
|
7
|
-
import { asHandle, resolveElement } from "../../core/dom.js";
|
|
8
|
-
import { translate } from "../../core/i18n.js";
|
|
9
|
-
import { animate, fadeIn, prefersReducedMotion } from "../../core/motion.js";
|
|
10
|
-
import { clampWidth, virtualWindow } from "../../core/tableMath.js";
|
|
11
|
-
|
|
12
|
-
const el = (tag, className, text) => {
|
|
13
|
-
const element = document.createElement(tag);
|
|
14
|
-
if (className) element.className = className;
|
|
15
|
-
if (text != null) element.textContent = text;
|
|
16
|
-
return element;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const addClasses = (node, classes) => {
|
|
20
|
-
for (const name of [classes].flat()) if (name) node.classList.add(...String(name).split(/\s+/).filter(Boolean));
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const compare = (a, b) =>
|
|
24
|
-
typeof a === "number" && typeof b === "number"
|
|
25
|
-
? a - b
|
|
26
|
-
: String(a ?? "").localeCompare(String(b ?? ""), undefined, { numeric: true });
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Generates a table (optional filter box, sortable headers, pagination, row selection, inline
|
|
30
|
-
* editing, resizable columns, virtual scrolling, CSV export) and appends it to its parent.
|
|
31
|
-
*
|
|
32
|
-
* @param {object} options
|
|
33
|
-
* @param {string|Element} [options.parent] id or element, defaults to "wrapper"
|
|
34
|
-
* @param {string} [options.id] id of the table wrapper (default: "table")
|
|
35
|
-
* @param {string} [options.class] extra CSS class of the table
|
|
36
|
-
* @param {string} [options.caption]
|
|
37
|
-
* @param {{key: string, label?: string, width?: number, sortable?: boolean, editable?: boolean, align?: "left"|"center"|"right", title?: boolean,
|
|
38
|
-
* format?: (value, row) => string, render?: (row: object, value) => (string|Node)}[]} options.columns
|
|
39
|
-
* `format` writes the text of a cell; `render` builds its content instead (a badge, a link, buttons: an element, or text). Every cell carries its column
|
|
40
|
-
* label in `data-label`, so a stylesheet can show a row as a block on a narrow screen, with the label next to the value.
|
|
41
|
-
* @param {object[]} [options.rows] row objects, read by column key
|
|
42
|
-
* @param {boolean} [options.sortable=true] click on a header sorts by it
|
|
43
|
-
* @param {boolean} [options.filter=false] show a text filter above the table
|
|
44
|
-
* @param {number} [options.pageSize=0] rows per page, 0 disables pagination
|
|
45
|
-
* @param {boolean} [options.selectable=false] checkbox column; the header checkbox selects every filtered row
|
|
46
|
-
* @param {boolean} [options.resizableColumns=false] drag the edge of a header (or use the arrow keys on it) to resize;
|
|
47
|
-
* double click resets the width
|
|
48
|
-
* @param {{height?: number, rowHeight?: number, overscan?: number}|boolean} [options.virtual] render only the rows
|
|
49
|
-
* in view, for lists of thousands of rows. Rows have a fixed height (default 36 px) and the table scrolls
|
|
50
|
-
* inside `height` (default 400 px). Replaces pagination.
|
|
51
|
-
* @param {(key: string, width: number) => void} [options.onColumnResize]
|
|
52
|
-
* @param {(rows: object[]) => void} [options.onSelectionChange]
|
|
53
|
-
* @param {(row: object, key: string, value, oldValue) => (boolean|void)} [options.onEdit] called after an edit
|
|
54
|
-
* of an `editable` column (double click, or Enter / F2 on the focused cell). Return false to reject it.
|
|
55
|
-
* @param {(row: object) => void} [options.onRowClick]
|
|
56
|
-
* @param {string} [options.emptyText]
|
|
57
|
-
* @param {(row: object, column: object) => (string|string[]|null|undefined)} [options.cellClass] CSS class(es) for a cell
|
|
58
|
-
* @param {(row: object) => (string|string[]|null|undefined)} [options.rowClass] CSS class(es) for a row
|
|
59
|
-
* @param {boolean} [options.focusableRows=false] rows are reachable with Tab and, with `onRowClick`, opened with Enter and Space
|
|
60
|
-
* @param {(row: object) => string} [options.rowLabel] aria-label of a focusable row
|
|
61
|
-
* @param {"normal"|"compact"} [options.density="normal"]
|
|
62
|
-
* @param {boolean} [options.wrap] true lets long text wrap, false keeps one line with an ellipsis (default: wrap, except in virtual tables)
|
|
63
|
-
* @param {(query: {page: number, pageSize: number, cursor: *, sort: ({key: string, descending: boolean}|null), filter: string}) =>
|
|
64
|
-
* Promise<{rows: object[], total?: number, nextCursor?: *}>} [options.dataSource] load the rows from a server instead of
|
|
65
|
-
* keeping them all in the browser. Sorting, the filter box and the pager call it again. Answer with `total` for numbered pages
|
|
66
|
-
* (`page` is 0-based), or with `nextCursor` for a "Load more" button (`cursor` is what the last answer gave, undefined for the
|
|
67
|
-
* first request; the rows are appended). A failed request shows the error with a Retry button.
|
|
68
|
-
* Together with `virtual` the list scrolls like one of `total` rows (every answer must carry `total`): the rows in view are asked for
|
|
69
|
-
* in chunks of `chunkSize` (`page` is the chunk number), rows that have not arrived yet show a placeholder, and sorting or the filter
|
|
70
|
-
* start again from the top.
|
|
71
|
-
* @param {number} [options.chunkSize=50] rows per request of a data source with `virtual`
|
|
72
|
-
* @param {boolean} [options.animateRows=false] rows slide to their new place when the table is sorted, filtered or changes page, and new rows fade in
|
|
73
|
-
* (at most 60 rows are animated, none under reduced motion, none in a virtual table)
|
|
74
|
-
* @param {number} [options.
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
});
|
|
231
|
-
handle.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
th
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
input.
|
|
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
|
-
if (
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
tr.
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
if (
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
td
|
|
411
|
-
|
|
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
|
-
|
|
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
|
-
return tbody;
|
|
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
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
const
|
|
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
|
-
|
|
613
|
-
|
|
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
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
if (request
|
|
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
|
-
status.
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
const
|
|
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
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
if (generation
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
})
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
|
|
1
|
+
//tableGenerator for Athena UI
|
|
2
|
+
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
import { downloadFile, toCsv } from "../../core/csv.js";
|
|
7
|
+
import { asHandle, resolveElement } from "../../core/dom.js";
|
|
8
|
+
import { translate } from "../../core/i18n.js";
|
|
9
|
+
import { animate, fadeIn, prefersReducedMotion } from "../../core/motion.js";
|
|
10
|
+
import { clampWidth, virtualWindow } from "../../core/tableMath.js";
|
|
11
|
+
|
|
12
|
+
const el = (tag, className, text) => {
|
|
13
|
+
const element = document.createElement(tag);
|
|
14
|
+
if (className) element.className = className;
|
|
15
|
+
if (text != null) element.textContent = text;
|
|
16
|
+
return element;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const addClasses = (node, classes) => {
|
|
20
|
+
for (const name of [classes].flat()) if (name) node.classList.add(...String(name).split(/\s+/).filter(Boolean));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const compare = (a, b) =>
|
|
24
|
+
typeof a === "number" && typeof b === "number"
|
|
25
|
+
? a - b
|
|
26
|
+
: String(a ?? "").localeCompare(String(b ?? ""), undefined, { numeric: true });
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Generates a table (optional filter box, sortable headers, pagination, row selection, inline
|
|
30
|
+
* editing, resizable columns, virtual scrolling, CSV export) and appends it to its parent.
|
|
31
|
+
*
|
|
32
|
+
* @param {object} options
|
|
33
|
+
* @param {string|Element} [options.parent] id or element, defaults to "wrapper"
|
|
34
|
+
* @param {string} [options.id] id of the table wrapper (default: "table")
|
|
35
|
+
* @param {string} [options.class] extra CSS class of the table
|
|
36
|
+
* @param {string} [options.caption]
|
|
37
|
+
* @param {{key: string, label?: string, width?: number, sortable?: boolean, editable?: boolean, align?: "left"|"center"|"right", title?: boolean,
|
|
38
|
+
* format?: (value, row) => string, render?: (row: object, value) => (string|Node)}[]} options.columns
|
|
39
|
+
* `format` writes the text of a cell; `render` builds its content instead (a badge, a link, buttons: an element, or text). Every cell carries its column
|
|
40
|
+
* label in `data-label`, so a stylesheet can show a row as a block on a narrow screen, with the label next to the value.
|
|
41
|
+
* @param {object[]} [options.rows] row objects, read by column key
|
|
42
|
+
* @param {boolean} [options.sortable=true] click on a header sorts by it
|
|
43
|
+
* @param {boolean} [options.filter=false] show a text filter above the table
|
|
44
|
+
* @param {number} [options.pageSize=0] rows per page, 0 disables pagination
|
|
45
|
+
* @param {boolean} [options.selectable=false] checkbox column; the header checkbox selects every filtered row
|
|
46
|
+
* @param {boolean} [options.resizableColumns=false] drag the edge of a header (or use the arrow keys on it) to resize;
|
|
47
|
+
* double click resets the width
|
|
48
|
+
* @param {{height?: number, rowHeight?: number, overscan?: number}|boolean} [options.virtual] render only the rows
|
|
49
|
+
* in view, for lists of thousands of rows. Rows have a fixed height (default 36 px) and the table scrolls
|
|
50
|
+
* inside `height` (default 400 px). Replaces pagination.
|
|
51
|
+
* @param {(key: string, width: number) => void} [options.onColumnResize]
|
|
52
|
+
* @param {(rows: object[]) => void} [options.onSelectionChange]
|
|
53
|
+
* @param {(row: object, key: string, value, oldValue) => (boolean|void)} [options.onEdit] called after an edit
|
|
54
|
+
* of an `editable` column (double click, or Enter / F2 on the focused cell). Return false to reject it.
|
|
55
|
+
* @param {(row: object) => void} [options.onRowClick]
|
|
56
|
+
* @param {string} [options.emptyText]
|
|
57
|
+
* @param {(row: object, column: object) => (string|string[]|null|undefined)} [options.cellClass] CSS class(es) for a cell
|
|
58
|
+
* @param {(row: object) => (string|string[]|null|undefined)} [options.rowClass] CSS class(es) for a row
|
|
59
|
+
* @param {boolean} [options.focusableRows=false] rows are reachable with Tab and, with `onRowClick`, opened with Enter and Space
|
|
60
|
+
* @param {(row: object) => string} [options.rowLabel] aria-label of a focusable row
|
|
61
|
+
* @param {"normal"|"compact"} [options.density="normal"]
|
|
62
|
+
* @param {boolean} [options.wrap] true lets long text wrap, false keeps one line with an ellipsis (default: wrap, except in virtual tables)
|
|
63
|
+
* @param {(query: {page: number, pageSize: number, cursor: *, sort: ({key: string, descending: boolean}|null), filter: string}) =>
|
|
64
|
+
* Promise<{rows: object[], total?: number, nextCursor?: *}>} [options.dataSource] load the rows from a server instead of
|
|
65
|
+
* keeping them all in the browser. Sorting, the filter box and the pager call it again. Answer with `total` for numbered pages
|
|
66
|
+
* (`page` is 0-based), or with `nextCursor` for a "Load more" button (`cursor` is what the last answer gave, undefined for the
|
|
67
|
+
* first request; the rows are appended). A failed request shows the error with a Retry button.
|
|
68
|
+
* Together with `virtual` the list scrolls like one of `total` rows (every answer must carry `total`): the rows in view are asked for
|
|
69
|
+
* in chunks of `chunkSize` (`page` is the chunk number), rows that have not arrived yet show a placeholder, and sorting or the filter
|
|
70
|
+
* start again from the top.
|
|
71
|
+
* @param {number} [options.chunkSize=50] rows per request of a data source with `virtual`
|
|
72
|
+
* @param {boolean} [options.animateRows=false] rows slide to their new place when the table is sorted, filtered or changes page, and new rows fade in
|
|
73
|
+
* (at most 60 rows are animated, none under reduced motion, none in a virtual table)
|
|
74
|
+
* @param {boolean|number} [options.stack=false] a narrow table shows every row as a block, with the label of the column next to each value (no sideways scrolling).
|
|
75
|
+
* true: when the table is narrower than 600 px; a number: the width in px below which it stacks. It follows the width of the table, not of the screen,
|
|
76
|
+
* and the table gets the class `is-stacked` while it is stacked. Not for virtual tables (their rows have a fixed height).
|
|
77
|
+
* @param {number} [options.filterDelay=300] ms to wait after typing before the data source is asked
|
|
78
|
+
* @param {{label: string, onclick: (row: object) => void, danger?: boolean, hidden?: (row: object) => boolean, ariaLabel?: (row: object) => string}[]} [options.rowActions]
|
|
79
|
+
* a column of buttons at the end of every row
|
|
80
|
+
* Column extras: `render(row, value)` returns text or a Node (built by the caller, so safe by construction), `align`
|
|
81
|
+
* ("left" | "center" | "right"), `title` (false switches the tooltip with the full text off).
|
|
82
|
+
* @returns {{element: HTMLElement, update: Function, destroy: Function, setRows: Function, getSelected: () => object[], clearSelection: Function,
|
|
83
|
+
* getColumnWidths: () => Object<string, number>, toCsv: () => string, exportCsv: (filename?: string) => void}}
|
|
84
|
+
*/
|
|
85
|
+
export function generateTable(options = {}) {
|
|
86
|
+
const {
|
|
87
|
+
parent = "wrapper", id = "table", columns = [], sortable = true,
|
|
88
|
+
filter = false, selectable = false, resizableColumns = false, onRowClick, onSelectionChange
|
|
89
|
+
} = options;
|
|
90
|
+
const remote = typeof options.dataSource === "function";
|
|
91
|
+
const rowActions = options.rowActions?.length ? options.rowActions : null;
|
|
92
|
+
const virtual = options.virtual
|
|
93
|
+
? { height: 400, rowHeight: 36, overscan: 5, ...(options.virtual === true ? {} : options.virtual) }
|
|
94
|
+
: null;
|
|
95
|
+
const remoteVirtual = remote && Boolean(virtual);
|
|
96
|
+
const chunkSize = Math.max(1, options.chunkSize ?? 50);
|
|
97
|
+
const chunks = new Map(); // chunk number -> "loading" | "done" | "failed" (remote virtual)
|
|
98
|
+
let flushTimer = 0;
|
|
99
|
+
const pageSize = virtual ? 0 : (options.pageSize ?? (remote ? 25 : 0));
|
|
100
|
+
// What the data source told us: { loading, error, total, nextCursor, cursorMode }
|
|
101
|
+
const server = { loading: false, error: null, total: null, nextCursor: undefined, cursorMode: false, request: 0 };
|
|
102
|
+
const colCount = () => columns.length + (selectable ? 1 : 0) + (rowActions ? 1 : 0);
|
|
103
|
+
const host = resolveElement(parent) ?? document.body;
|
|
104
|
+
let rows = [...(options.rows ?? [])];
|
|
105
|
+
let current = rows; // filtered and sorted rows
|
|
106
|
+
const selected = new Set();
|
|
107
|
+
const widths = {}; // column key -> px, set by the user (or by options)
|
|
108
|
+
for (const col of columns) if (col.width) widths[col.key] = col.width;
|
|
109
|
+
const state = { sortKey: null, descending: false, page: 0, query: "" };
|
|
110
|
+
let windowKey = "";
|
|
111
|
+
|
|
112
|
+
const root = el("div", "athena-table-wrapper");
|
|
113
|
+
root.id = id;
|
|
114
|
+
const table = el("table", `athena-table ${options.class ?? ""}`.trim());
|
|
115
|
+
if (resizableColumns) table.classList.add("athena-table-resizable");
|
|
116
|
+
if (options.density === "compact") table.classList.add("athena-table-compact");
|
|
117
|
+
if (options.wrap === true) table.classList.add("athena-table-wrap");
|
|
118
|
+
else if (options.wrap === false) table.classList.add("athena-table-nowrap");
|
|
119
|
+
// `stack`: a row becomes a block while the table is narrower than the breakpoint (the style is in the theme: .athena-table-stackable.is-stacked)
|
|
120
|
+
const stackAt = options.stack === true ? 600 : Number(options.stack) || 0;
|
|
121
|
+
if (stackAt && !virtual) {
|
|
122
|
+
root.classList.add("athena-table-stackable");
|
|
123
|
+
const apply = () => root.classList.toggle("is-stacked", root.clientWidth > 0 && root.clientWidth < stackAt);
|
|
124
|
+
// Stacking changes the height of the table: do it in the next frame, or the observer reports a loop
|
|
125
|
+
if (typeof ResizeObserver !== "undefined") new ResizeObserver(() => requestAnimationFrame(apply)).observe(root);
|
|
126
|
+
apply();
|
|
127
|
+
}
|
|
128
|
+
let scroller = null;
|
|
129
|
+
if (virtual) {
|
|
130
|
+
table.classList.add("athena-table-virtual");
|
|
131
|
+
scroller = el("div", "athena-table-scroll");
|
|
132
|
+
scroller.style.maxHeight = `${virtual.height}px`;
|
|
133
|
+
scroller.style.setProperty("--row-height", `${virtual.rowHeight}px`);
|
|
134
|
+
scroller.tabIndex = 0;
|
|
135
|
+
scroller.setAttribute("role", "region");
|
|
136
|
+
scroller.setAttribute("aria-label", options.caption ?? id);
|
|
137
|
+
}
|
|
138
|
+
if (filter) {
|
|
139
|
+
const filterInput = el("input", "athena-table-filter");
|
|
140
|
+
filterInput.type = "search";
|
|
141
|
+
filterInput.placeholder = translate("filter");
|
|
142
|
+
filterInput.setAttribute("aria-label", translate("filter"));
|
|
143
|
+
let filterTimer = 0;
|
|
144
|
+
filterInput.addEventListener("input", () => {
|
|
145
|
+
state.query = remote ? filterInput.value.trim() : filterInput.value.toLowerCase();
|
|
146
|
+
state.page = 0;
|
|
147
|
+
if (scroller) scroller.scrollTop = 0;
|
|
148
|
+
if (!remote) return render();
|
|
149
|
+
clearTimeout(filterTimer);
|
|
150
|
+
filterTimer = setTimeout(() => load({ reset: true }), options.filterDelay ?? 300);
|
|
151
|
+
});
|
|
152
|
+
root.append(filterInput);
|
|
153
|
+
}
|
|
154
|
+
if (scroller) {
|
|
155
|
+
scroller.append(table);
|
|
156
|
+
root.append(scroller);
|
|
157
|
+
} else {
|
|
158
|
+
root.append(table);
|
|
159
|
+
}
|
|
160
|
+
const pager = pageSize > 0 ? el("div", "athena-table-pager") : null;
|
|
161
|
+
if (pager) root.append(pager);
|
|
162
|
+
host.append(root);
|
|
163
|
+
|
|
164
|
+
function visibleRows() {
|
|
165
|
+
if (remote) return rows; // the server filtered, sorted and paged them
|
|
166
|
+
let result = rows;
|
|
167
|
+
if (state.query) {
|
|
168
|
+
result = result.filter(row =>
|
|
169
|
+
columns.some(col => String(row[col.key] ?? "").toLowerCase().includes(state.query)));
|
|
170
|
+
}
|
|
171
|
+
if (state.sortKey) {
|
|
172
|
+
const dir = state.descending ? -1 : 1;
|
|
173
|
+
result = [...result].sort((a, b) => dir * compare(a[state.sortKey], b[state.sortKey]));
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const selectionChanged = () => onSelectionChange?.([...selected]);
|
|
179
|
+
|
|
180
|
+
/* ------------------COLUMN WIDTHS------------------- */
|
|
181
|
+
|
|
182
|
+
const totalWidth = () => Object.keys(widths).length === columns.length
|
|
183
|
+
? columns.reduce((sum, col) => sum + widths[col.key], 0) + (selectable ? 40 : 0)
|
|
184
|
+
: 0;
|
|
185
|
+
|
|
186
|
+
function renderColgroup() {
|
|
187
|
+
const group = el("colgroup");
|
|
188
|
+
if (selectable) {
|
|
189
|
+
const col = el("col");
|
|
190
|
+
col.style.width = "40px";
|
|
191
|
+
group.append(col);
|
|
192
|
+
}
|
|
193
|
+
for (const column of columns) {
|
|
194
|
+
const col = el("col");
|
|
195
|
+
if (widths[column.key]) col.style.width = `${widths[column.key]}px`;
|
|
196
|
+
group.append(col);
|
|
197
|
+
}
|
|
198
|
+
return group;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Freezes the current widths, so dragging one edge does not move the others.
|
|
202
|
+
function freezeWidths() {
|
|
203
|
+
if (Object.keys(widths).length === columns.length) return;
|
|
204
|
+
const heads = table.querySelectorAll("thead th:not(.athena-table-select)");
|
|
205
|
+
columns.forEach((col, i) => { widths[col.key] ??= clampWidth(heads[i]?.offsetWidth ?? 120); });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function applyWidths() {
|
|
209
|
+
const group = table.querySelector("colgroup");
|
|
210
|
+
if (!group) return;
|
|
211
|
+
const cols = [...group.children].slice(selectable ? 1 : 0);
|
|
212
|
+
columns.forEach((col, i) => { if (widths[col.key]) cols[i].style.width = `${widths[col.key]}px`; });
|
|
213
|
+
// With every width known the table is laid out by them, so dragging one edge moves nothing else.
|
|
214
|
+
const total = totalWidth();
|
|
215
|
+
table.style.tableLayout = total ? "fixed" : "";
|
|
216
|
+
table.style.width = total ? `${total}px` : "";
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function setWidth(col, width) {
|
|
220
|
+
freezeWidths();
|
|
221
|
+
widths[col.key] = clampWidth(width, col.minWidth ?? 40, col.maxWidth ?? 1200);
|
|
222
|
+
applyWidths();
|
|
223
|
+
options.onColumnResize?.(col.key, widths[col.key]);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function makeResizer(th, col) {
|
|
227
|
+
const handle = el("span", "athena-col-resizer");
|
|
228
|
+
handle.setAttribute("role", "separator");
|
|
229
|
+
handle.setAttribute("aria-orientation", "vertical");
|
|
230
|
+
handle.setAttribute("aria-label", `${translate("resizeColumn")}: ${col.label ?? col.key}`);
|
|
231
|
+
handle.tabIndex = 0;
|
|
232
|
+
// The handle sits inside a sortable header, so it must not trigger the sort.
|
|
233
|
+
handle.addEventListener("click", e => e.stopPropagation());
|
|
234
|
+
handle.addEventListener("keydown", e => {
|
|
235
|
+
e.stopPropagation();
|
|
236
|
+
const step = e.shiftKey ? 50 : 10;
|
|
237
|
+
if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
|
|
238
|
+
e.preventDefault();
|
|
239
|
+
freezeWidths();
|
|
240
|
+
setWidth(col, widths[col.key] + (e.key === "ArrowRight" ? step : -step));
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
handle.addEventListener("dblclick", e => {
|
|
244
|
+
e.stopPropagation();
|
|
245
|
+
delete widths[col.key];
|
|
246
|
+
if (col.width) widths[col.key] = col.width;
|
|
247
|
+
if (!Object.keys(widths).length) {
|
|
248
|
+
table.style.tableLayout = "";
|
|
249
|
+
table.style.width = "";
|
|
250
|
+
}
|
|
251
|
+
render();
|
|
252
|
+
});
|
|
253
|
+
handle.addEventListener("pointerdown", e => {
|
|
254
|
+
e.preventDefault();
|
|
255
|
+
e.stopPropagation();
|
|
256
|
+
freezeWidths();
|
|
257
|
+
const startX = e.clientX;
|
|
258
|
+
const startWidth = widths[col.key];
|
|
259
|
+
try {
|
|
260
|
+
handle.setPointerCapture(e.pointerId);
|
|
261
|
+
} catch { /* the pointer is gone already */ }
|
|
262
|
+
handle.classList.add("resizing");
|
|
263
|
+
const move = ev => setWidth(col, startWidth + (ev.clientX - startX));
|
|
264
|
+
const stop = () => {
|
|
265
|
+
handle.classList.remove("resizing");
|
|
266
|
+
handle.removeEventListener("pointermove", move);
|
|
267
|
+
handle.removeEventListener("pointerup", stop);
|
|
268
|
+
handle.removeEventListener("pointercancel", stop);
|
|
269
|
+
};
|
|
270
|
+
handle.addEventListener("pointermove", move);
|
|
271
|
+
handle.addEventListener("pointerup", stop);
|
|
272
|
+
handle.addEventListener("pointercancel", stop);
|
|
273
|
+
});
|
|
274
|
+
th.append(handle);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/* ------------------RENDER------------------- */
|
|
278
|
+
|
|
279
|
+
function renderHead(all) {
|
|
280
|
+
const tr = el("tr");
|
|
281
|
+
if (selectable) {
|
|
282
|
+
const th = el("th", "athena-table-select");
|
|
283
|
+
const box = el("input");
|
|
284
|
+
box.type = "checkbox";
|
|
285
|
+
box.setAttribute("aria-label", translate("selectAll"));
|
|
286
|
+
const chosen = all.filter(row => selected.has(row)).length;
|
|
287
|
+
box.checked = all.length > 0 && chosen === all.length;
|
|
288
|
+
box.indeterminate = chosen > 0 && chosen < all.length;
|
|
289
|
+
box.addEventListener("change", () => {
|
|
290
|
+
all.forEach(row => (box.checked ? selected.add(row) : selected.delete(row)));
|
|
291
|
+
selectionChanged();
|
|
292
|
+
render();
|
|
293
|
+
});
|
|
294
|
+
th.append(box);
|
|
295
|
+
tr.append(th);
|
|
296
|
+
}
|
|
297
|
+
for (const col of columns) {
|
|
298
|
+
const th = el("th", null, col.label ?? col.key);
|
|
299
|
+
th.scope = "col";
|
|
300
|
+
if (sortable && col.sortable !== false) {
|
|
301
|
+
th.classList.add("sortable");
|
|
302
|
+
th.tabIndex = 0;
|
|
303
|
+
const sort = () => {
|
|
304
|
+
state.descending = state.sortKey === col.key && !state.descending;
|
|
305
|
+
state.sortKey = col.key;
|
|
306
|
+
if (remote) load({ reset: true });
|
|
307
|
+
else render();
|
|
308
|
+
};
|
|
309
|
+
th.addEventListener("click", sort);
|
|
310
|
+
th.addEventListener("keydown", e => {
|
|
311
|
+
if (e.target === th && (e.key === "Enter" || e.key === " ")) {
|
|
312
|
+
e.preventDefault();
|
|
313
|
+
sort();
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
if (state.sortKey === col.key) {
|
|
317
|
+
th.append(el("span", "sort-indicator", state.descending ? " ▼" : " ▲"));
|
|
318
|
+
th.setAttribute("aria-sort", state.descending ? "descending" : "ascending");
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (resizableColumns) makeResizer(th, col);
|
|
322
|
+
tr.append(th);
|
|
323
|
+
}
|
|
324
|
+
if (rowActions) {
|
|
325
|
+
const th = el("th", "athena-table-actions");
|
|
326
|
+
th.scope = "col";
|
|
327
|
+
th.append(el("span", "athena-visually-hidden", translate("actions")));
|
|
328
|
+
tr.append(th);
|
|
329
|
+
}
|
|
330
|
+
const thead = el("thead");
|
|
331
|
+
thead.append(tr);
|
|
332
|
+
return thead;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Replaces the cell content with an input until the edit is confirmed (Enter, blur) or cancelled (Esc).
|
|
336
|
+
function startEdit(td, row, col) {
|
|
337
|
+
if (td.querySelector("input")) return;
|
|
338
|
+
const oldValue = row[col.key];
|
|
339
|
+
const input = el("input", "athena-table-edit");
|
|
340
|
+
input.type = typeof oldValue === "number" ? "number" : "text";
|
|
341
|
+
input.value = oldValue ?? "";
|
|
342
|
+
let finished = false;
|
|
343
|
+
const finish = commit => {
|
|
344
|
+
if (finished) return;
|
|
345
|
+
finished = true;
|
|
346
|
+
if (commit && input.value !== String(oldValue ?? "")) {
|
|
347
|
+
const value = typeof oldValue === "number" && input.value !== "" ? Number(input.value) : input.value;
|
|
348
|
+
row[col.key] = value;
|
|
349
|
+
if (options.onEdit?.(row, col.key, value, oldValue) === false) row[col.key] = oldValue;
|
|
350
|
+
}
|
|
351
|
+
render();
|
|
352
|
+
};
|
|
353
|
+
input.addEventListener("keydown", e => {
|
|
354
|
+
if (e.key === "Enter") finish(true);
|
|
355
|
+
else if (e.key === "Escape") {
|
|
356
|
+
e.stopPropagation();
|
|
357
|
+
finish(false);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
input.addEventListener("blur", () => finish(true));
|
|
361
|
+
td.replaceChildren(input);
|
|
362
|
+
input.focus();
|
|
363
|
+
input.select();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function spacer(height) {
|
|
367
|
+
const tr = el("tr", "athena-table-spacer");
|
|
368
|
+
tr.setAttribute("aria-hidden", "true");
|
|
369
|
+
tr.style.height = `${height}px`;
|
|
370
|
+
const td = el("td");
|
|
371
|
+
td.colSpan = colCount();
|
|
372
|
+
tr.append(td);
|
|
373
|
+
return tr;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const rowOf = new WeakMap(); // the element of a row -> its row object, to find the same row again after a redraw (animateRows)
|
|
377
|
+
let rendered = false;
|
|
378
|
+
|
|
379
|
+
function renderRow(row, index) {
|
|
380
|
+
const tr = el("tr");
|
|
381
|
+
if (row !== undefined) rowOf.set(tr, row);
|
|
382
|
+
if (virtual) tr.setAttribute("aria-rowindex", String(index + 2));
|
|
383
|
+
if (row === undefined) {
|
|
384
|
+
// A row of a data source that has not arrived yet
|
|
385
|
+
tr.className = "athena-table-placeholder";
|
|
386
|
+
tr.setAttribute("aria-busy", "true");
|
|
387
|
+
const td = el("td", null, "…");
|
|
388
|
+
td.colSpan = colCount();
|
|
389
|
+
tr.append(td);
|
|
390
|
+
return tr;
|
|
391
|
+
}
|
|
392
|
+
addClasses(tr, options.rowClass?.(row));
|
|
393
|
+
if (onRowClick) {
|
|
394
|
+
tr.classList.add("clickable");
|
|
395
|
+
tr.addEventListener("click", () => onRowClick(row));
|
|
396
|
+
}
|
|
397
|
+
if (options.focusableRows) {
|
|
398
|
+
tr.tabIndex = 0;
|
|
399
|
+
if (options.rowLabel) tr.setAttribute("aria-label", options.rowLabel(row));
|
|
400
|
+
if (onRowClick) {
|
|
401
|
+
tr.addEventListener("keydown", e => {
|
|
402
|
+
if (e.target === tr && (e.key === "Enter" || e.key === " ")) {
|
|
403
|
+
e.preventDefault();
|
|
404
|
+
onRowClick(row);
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (selectable) {
|
|
410
|
+
const td = el("td", "athena-table-select");
|
|
411
|
+
const box = el("input");
|
|
412
|
+
box.type = "checkbox";
|
|
413
|
+
box.checked = selected.has(row);
|
|
414
|
+
tr.classList.toggle("selected", box.checked);
|
|
415
|
+
box.addEventListener("click", e => e.stopPropagation());
|
|
416
|
+
box.addEventListener("change", () => {
|
|
417
|
+
if (box.checked) selected.add(row);
|
|
418
|
+
else selected.delete(row);
|
|
419
|
+
selectionChanged();
|
|
420
|
+
render();
|
|
421
|
+
});
|
|
422
|
+
td.append(box);
|
|
423
|
+
tr.append(td);
|
|
424
|
+
}
|
|
425
|
+
for (const col of columns) {
|
|
426
|
+
const value = row[col.key];
|
|
427
|
+
const content = col.render ? col.render(row, value) : (col.format ? col.format(value, row) : (value ?? ""));
|
|
428
|
+
const td = el("td");
|
|
429
|
+
td.dataset.label = col.label ?? col.key;
|
|
430
|
+
if (content instanceof Node) td.append(content);
|
|
431
|
+
else {
|
|
432
|
+
td.textContent = content;
|
|
433
|
+
// Text that may be cut off gets its full text as a tooltip.
|
|
434
|
+
if (col.title !== false && String(content).length > 24) td.title = String(content);
|
|
435
|
+
}
|
|
436
|
+
if (col.align) td.classList.add(`athena-align-${col.align}`);
|
|
437
|
+
addClasses(td, options.cellClass?.(row, col));
|
|
438
|
+
if (col.editable) {
|
|
439
|
+
td.classList.add("editable");
|
|
440
|
+
td.tabIndex = 0;
|
|
441
|
+
td.addEventListener("dblclick", () => startEdit(td, row, col));
|
|
442
|
+
td.addEventListener("keydown", e => {
|
|
443
|
+
if ((e.key === "Enter" || e.key === "F2") && e.target === td) {
|
|
444
|
+
e.preventDefault();
|
|
445
|
+
startEdit(td, row, col);
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
tr.append(td);
|
|
450
|
+
}
|
|
451
|
+
if (rowActions) {
|
|
452
|
+
const td = el("td", "athena-table-actions");
|
|
453
|
+
for (const action of rowActions) {
|
|
454
|
+
if (action.hidden?.(row)) continue;
|
|
455
|
+
const button = el("button", `athena-button button-${action.danger ? "danger" : "secondary"} button-small`, action.label);
|
|
456
|
+
button.type = "button";
|
|
457
|
+
if (action.ariaLabel) button.setAttribute("aria-label", action.ariaLabel(row));
|
|
458
|
+
button.addEventListener("click", event => {
|
|
459
|
+
event.stopPropagation();
|
|
460
|
+
action.onclick(row);
|
|
461
|
+
});
|
|
462
|
+
td.append(button);
|
|
463
|
+
}
|
|
464
|
+
tr.append(td);
|
|
465
|
+
}
|
|
466
|
+
return tr;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function messageRow(className, text, retry) {
|
|
470
|
+
const td = el("td", className, text);
|
|
471
|
+
td.colSpan = Math.max(colCount(), 1);
|
|
472
|
+
if (retry) {
|
|
473
|
+
const button = el("button", "athena-button button-secondary button-small", translate("retry"));
|
|
474
|
+
button.type = "button";
|
|
475
|
+
button.addEventListener("click", () => load({}));
|
|
476
|
+
td.append(" ", button);
|
|
477
|
+
}
|
|
478
|
+
const tr = el("tr");
|
|
479
|
+
tr.append(td);
|
|
480
|
+
return tr;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function renderBody() {
|
|
484
|
+
const tbody = el("tbody");
|
|
485
|
+
if (remote && server.error && !current.length) {
|
|
486
|
+
const row = messageRow("athena-table-error", server.error, true);
|
|
487
|
+
row.firstChild.setAttribute("role", "alert");
|
|
488
|
+
tbody.append(row);
|
|
489
|
+
return tbody;
|
|
490
|
+
}
|
|
491
|
+
if (remote && server.loading && !current.length) {
|
|
492
|
+
tbody.append(messageRow("athena-table-loading", translate("loading")));
|
|
493
|
+
return tbody;
|
|
494
|
+
}
|
|
495
|
+
if (!current.length) {
|
|
496
|
+
tbody.append(messageRow("athena-table-empty", options.emptyText ?? translate("noData")));
|
|
497
|
+
return tbody;
|
|
498
|
+
}
|
|
499
|
+
if (virtual) {
|
|
500
|
+
const win = virtualWindow({
|
|
501
|
+
scrollTop: scroller.scrollTop, viewportHeight: viewportHeight(),
|
|
502
|
+
rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
|
|
503
|
+
});
|
|
504
|
+
windowKey = `${win.start}:${win.end}`;
|
|
505
|
+
if (remoteVirtual) ensureChunks();
|
|
506
|
+
if (win.offsetTop) tbody.append(spacer(win.offsetTop));
|
|
507
|
+
for (let i = win.start; i < win.end; i++) tbody.append(renderRow(current[i], i));
|
|
508
|
+
if (win.offsetBottom) tbody.append(spacer(win.offsetBottom));
|
|
509
|
+
return tbody;
|
|
510
|
+
}
|
|
511
|
+
const pageRows = remote ? current : pageSize > 0 ? current.slice(state.page * pageSize, (state.page + 1) * pageSize) : current;
|
|
512
|
+
for (const row of pageRows) tbody.append(renderRow(row));
|
|
513
|
+
return tbody;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function renderRemotePager() {
|
|
517
|
+
if (server.cursorMode) {
|
|
518
|
+
pager.hidden = !server.nextCursor && !server.error;
|
|
519
|
+
const more = el("button", "athena-button button-secondary", translate("loadMore"));
|
|
520
|
+
more.type = "button";
|
|
521
|
+
more.disabled = server.loading;
|
|
522
|
+
more.addEventListener("click", () => load({ more: true }));
|
|
523
|
+
pager.replaceChildren(more);
|
|
524
|
+
showPagerError();
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const pages = server.total == null ? state.page + 2 : Math.max(1, Math.ceil(server.total / pageSize));
|
|
528
|
+
pager.hidden = pages === 1;
|
|
529
|
+
const go = delta => () => {
|
|
530
|
+
state.page += delta;
|
|
531
|
+
load({});
|
|
532
|
+
};
|
|
533
|
+
const prev = el("button", null, `‹ ${translate("previous")}`);
|
|
534
|
+
prev.type = "button";
|
|
535
|
+
prev.disabled = state.page === 0 || server.loading;
|
|
536
|
+
prev.addEventListener("click", go(-1));
|
|
537
|
+
const next = el("button", null, `${translate("next")} ›`);
|
|
538
|
+
next.type = "button";
|
|
539
|
+
next.disabled = state.page >= pages - 1 || server.loading;
|
|
540
|
+
next.addEventListener("click", go(1));
|
|
541
|
+
pager.replaceChildren(prev, el("span", null, ` ${translate("page", { page: state.page + 1, pages: server.total == null ? "?" : pages })} `), next);
|
|
542
|
+
showPagerError();
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// A failed request while rows are on the screen (the next page, "Load more"): say so next to the buttons.
|
|
546
|
+
function showPagerError() {
|
|
547
|
+
if (!server.error || !current.length) return;
|
|
548
|
+
pager.hidden = false;
|
|
549
|
+
const message = el("span", "athena-table-error", server.error);
|
|
550
|
+
message.setAttribute("role", "alert");
|
|
551
|
+
pager.append(" ", message);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function renderPager(total) {
|
|
555
|
+
if (!pager) return;
|
|
556
|
+
if (remote) return renderRemotePager();
|
|
557
|
+
const pages = Math.max(1, Math.ceil(total / pageSize));
|
|
558
|
+
state.page = Math.min(state.page, pages - 1);
|
|
559
|
+
pager.hidden = pages === 1;
|
|
560
|
+
const button = (text, disabled, delta) => {
|
|
561
|
+
const b = el("button", null, text);
|
|
562
|
+
b.type = "button";
|
|
563
|
+
b.disabled = disabled;
|
|
564
|
+
b.addEventListener("click", () => {
|
|
565
|
+
state.page += delta;
|
|
566
|
+
render();
|
|
567
|
+
});
|
|
568
|
+
return b;
|
|
569
|
+
};
|
|
570
|
+
pager.replaceChildren(
|
|
571
|
+
button(`‹ ${translate("previous")}`, state.page === 0, -1),
|
|
572
|
+
el("span", null, ` ${translate("page", { page: state.page + 1, pages })} `),
|
|
573
|
+
button(`${translate("next")} ›`, state.page >= pages - 1, 1)
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function render() {
|
|
578
|
+
const scrollTop = scroller?.scrollTop ?? 0;
|
|
579
|
+
current = visibleRows();
|
|
580
|
+
root.toggleAttribute("aria-busy", remote && server.loading);
|
|
581
|
+
root.classList.toggle("athena-table-is-loading", remote && server.loading);
|
|
582
|
+
renderPager(current.length);
|
|
583
|
+
if (virtual) table.setAttribute("aria-rowcount", String(current.length + 1));
|
|
584
|
+
// Where the rows were before (FLIP): the same row object is the same row
|
|
585
|
+
const before = options.animateRows && rendered && !virtual && !prefersReducedMotion()
|
|
586
|
+
? new Map([...(table.tBodies[0]?.rows ?? [])].slice(0, 60).map(tr => [rowOf.get(tr), tr.getBoundingClientRect()]).filter(([row]) => row !== undefined))
|
|
587
|
+
: null;
|
|
588
|
+
const parts = [renderHead(current), renderBody()];
|
|
589
|
+
if (resizableColumns || Object.keys(widths).length) parts.unshift(renderColgroup());
|
|
590
|
+
if (options.caption) parts.unshift(el("caption", null, options.caption));
|
|
591
|
+
table.replaceChildren(...parts);
|
|
592
|
+
if (before?.size) {
|
|
593
|
+
for (const tr of [...table.tBodies[0].rows].slice(0, 60)) {
|
|
594
|
+
const row = rowOf.get(tr);
|
|
595
|
+
if (row === undefined) continue;
|
|
596
|
+
const old = before.get(row);
|
|
597
|
+
if (!old) {
|
|
598
|
+
fadeIn(tr);
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
const now = tr.getBoundingClientRect();
|
|
602
|
+
if (Math.abs(old.top - now.top) > 1) animate(tr, [{ translate: `0 ${old.top - now.top}px` }, { translate: "0 0" }], { easing: "cubic-bezier(0.2, 0, 0, 1)" });
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
rendered = true;
|
|
606
|
+
if (Object.keys(widths).length) applyWidths();
|
|
607
|
+
if (scroller) scroller.scrollTop = scrollTop;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// Scrolling only swaps the body, and only when the window of rows really changed.
|
|
611
|
+
// The scroll area is as high as the rows, up to `virtual.height`. That is also the least it can show, so a list that
|
|
612
|
+
// starts empty (a tiny scroll area when the rows are set) still draws a full window.
|
|
613
|
+
const viewportHeight = () => Math.max(scroller.clientHeight, Math.min(virtual.height, current.length * virtual.rowHeight));
|
|
614
|
+
|
|
615
|
+
function refreshWindow(force = false) {
|
|
616
|
+
if (remoteVirtual) ensureChunks();
|
|
617
|
+
const win = virtualWindow({
|
|
618
|
+
scrollTop: scroller.scrollTop, viewportHeight: viewportHeight(),
|
|
619
|
+
rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
|
|
620
|
+
});
|
|
621
|
+
if (!force && `${win.start}:${win.end}` === windowKey) return;
|
|
622
|
+
const focused = document.activeElement;
|
|
623
|
+
const keepFocus = focused && table.tBodies[0]?.contains(focused);
|
|
624
|
+
table.tBodies[0].replaceWith(renderBody());
|
|
625
|
+
if (keepFocus) scroller.focus({ preventScroll: true });
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
if (scroller) {
|
|
629
|
+
let queued = false;
|
|
630
|
+
scroller.addEventListener("scroll", () => {
|
|
631
|
+
if (queued) return;
|
|
632
|
+
queued = true;
|
|
633
|
+
// Once per frame; the timer is for pages where frames are paused or late (a hidden tab, a busy page).
|
|
634
|
+
let timer = 0;
|
|
635
|
+
const run = () => {
|
|
636
|
+
if (!queued) return;
|
|
637
|
+
queued = false;
|
|
638
|
+
clearTimeout(timer);
|
|
639
|
+
refreshWindow();
|
|
640
|
+
};
|
|
641
|
+
requestAnimationFrame(run);
|
|
642
|
+
timer = setTimeout(run, 100);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Asks the data source. { reset } starts again from the first page, { more } appends the next cursor page.
|
|
648
|
+
* An answer that arrives after a newer request was made is ignored.
|
|
649
|
+
*/
|
|
650
|
+
async function load({ reset = false, more = false } = {}) {
|
|
651
|
+
if (remoteVirtual) return loadVirtual();
|
|
652
|
+
if (reset) {
|
|
653
|
+
state.page = 0;
|
|
654
|
+
server.nextCursor = undefined;
|
|
655
|
+
}
|
|
656
|
+
const request = ++server.request;
|
|
657
|
+
server.loading = true;
|
|
658
|
+
server.error = null;
|
|
659
|
+
render();
|
|
660
|
+
try {
|
|
661
|
+
const answer = await options.dataSource({
|
|
662
|
+
page: state.page,
|
|
663
|
+
pageSize,
|
|
664
|
+
cursor: more ? server.nextCursor : undefined,
|
|
665
|
+
sort: state.sortKey ? { key: state.sortKey, descending: state.descending } : null,
|
|
666
|
+
filter: state.query
|
|
667
|
+
});
|
|
668
|
+
if (request !== server.request) return;
|
|
669
|
+
const loaded = answer?.rows ?? [];
|
|
670
|
+
server.cursorMode = answer?.nextCursor !== undefined || (more && server.cursorMode);
|
|
671
|
+
server.nextCursor = answer?.nextCursor ?? undefined;
|
|
672
|
+
server.total = answer?.total ?? null;
|
|
673
|
+
rows = more ? [...rows, ...loaded] : [...loaded];
|
|
674
|
+
if (!more) selected.clear();
|
|
675
|
+
} catch (error) {
|
|
676
|
+
if (request !== server.request) return;
|
|
677
|
+
server.error = error?.message ?? String(error);
|
|
678
|
+
if (!more) rows = [];
|
|
679
|
+
} finally {
|
|
680
|
+
if (request === server.request) {
|
|
681
|
+
server.loading = false;
|
|
682
|
+
render();
|
|
683
|
+
if (!more) selectionChanged();
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/* ------------------REMOTE + VIRTUAL------------------- */
|
|
689
|
+
|
|
690
|
+
const status = remoteVirtual ? el("div", "athena-table-status") : null;
|
|
691
|
+
if (status) {
|
|
692
|
+
status.setAttribute("role", "status");
|
|
693
|
+
root.append(status);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function updateStatus() {
|
|
697
|
+
if (!status) return;
|
|
698
|
+
const failed = [...chunks.values()].includes("failed");
|
|
699
|
+
status.replaceChildren();
|
|
700
|
+
status.hidden = !failed;
|
|
701
|
+
if (!failed) return;
|
|
702
|
+
status.append(el("span", "athena-table-error", server.error ?? translate("noData")), " ");
|
|
703
|
+
const retry = el("button", "athena-button button-secondary button-small", translate("retry"));
|
|
704
|
+
retry.type = "button";
|
|
705
|
+
retry.addEventListener("click", () => {
|
|
706
|
+
for (const [chunk, value] of chunks) if (value === "failed") chunks.delete(chunk);
|
|
707
|
+
server.error = null;
|
|
708
|
+
updateStatus();
|
|
709
|
+
ensureChunks();
|
|
710
|
+
});
|
|
711
|
+
status.append(retry);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const query = () => ({ pageSize: chunkSize, cursor: undefined, sort: state.sortKey ? { key: state.sortKey, descending: state.descending } : null, filter: state.query });
|
|
715
|
+
|
|
716
|
+
function place(chunk, list) {
|
|
717
|
+
list.forEach((row, i) => {
|
|
718
|
+
const index = chunk * chunkSize + i;
|
|
719
|
+
if (index < rows.length) rows[index] = row;
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Starts again: the first chunk tells how many rows there are, the others come while the person scrolls.
|
|
724
|
+
async function loadVirtual() {
|
|
725
|
+
const generation = ++server.request;
|
|
726
|
+
clearTimeout(flushTimer);
|
|
727
|
+
chunks.clear();
|
|
728
|
+
rows = [];
|
|
729
|
+
selected.clear();
|
|
730
|
+
if (scroller) scroller.scrollTop = 0;
|
|
731
|
+
server.loading = true;
|
|
732
|
+
server.error = null;
|
|
733
|
+
render();
|
|
734
|
+
try {
|
|
735
|
+
const answer = await options.dataSource({ page: 0, ...query() });
|
|
736
|
+
if (generation !== server.request) return;
|
|
737
|
+
if (!Number.isInteger(answer?.total) || answer.total < 0) throw new Error("A data source with virtual scrolling has to answer with total");
|
|
738
|
+
rows = new Array(answer.total);
|
|
739
|
+
place(0, answer.rows ?? []);
|
|
740
|
+
chunks.set(0, "done");
|
|
741
|
+
server.total = answer.total;
|
|
742
|
+
} catch (error) {
|
|
743
|
+
if (generation !== server.request) return;
|
|
744
|
+
server.error = error?.message ?? String(error);
|
|
745
|
+
rows = [];
|
|
746
|
+
} finally {
|
|
747
|
+
if (generation === server.request) {
|
|
748
|
+
server.loading = false;
|
|
749
|
+
render();
|
|
750
|
+
selectionChanged();
|
|
751
|
+
updateStatus();
|
|
752
|
+
if (!server.error) ensureChunks();
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async function loadChunk(chunk, generation) {
|
|
758
|
+
chunks.set(chunk, "loading");
|
|
759
|
+
try {
|
|
760
|
+
const answer = await options.dataSource({ page: chunk, ...query() });
|
|
761
|
+
if (generation !== server.request) return;
|
|
762
|
+
place(chunk, answer?.rows ?? []);
|
|
763
|
+
chunks.set(chunk, "done");
|
|
764
|
+
} catch (error) {
|
|
765
|
+
if (generation !== server.request) return;
|
|
766
|
+
chunks.set(chunk, "failed");
|
|
767
|
+
server.error = error?.message ?? String(error);
|
|
768
|
+
} finally {
|
|
769
|
+
if (generation === server.request) {
|
|
770
|
+
refreshWindow(true);
|
|
771
|
+
updateStatus();
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// Asks for the chunks under the rows in view (and the rows around them). Waits a moment, so scrolling past does not ask for everything.
|
|
777
|
+
function ensureChunks() {
|
|
778
|
+
clearTimeout(flushTimer);
|
|
779
|
+
const generation = server.request;
|
|
780
|
+
flushTimer = setTimeout(() => {
|
|
781
|
+
if (generation !== server.request || !rows.length) return;
|
|
782
|
+
const win = virtualWindow({
|
|
783
|
+
scrollTop: scroller.scrollTop, viewportHeight: viewportHeight(),
|
|
784
|
+
rowHeight: virtual.rowHeight, total: rows.length, overscan: virtual.overscan
|
|
785
|
+
});
|
|
786
|
+
const first = Math.floor(win.start / chunkSize);
|
|
787
|
+
const last = Math.floor((win.end - 1) / chunkSize);
|
|
788
|
+
for (let chunk = first; chunk <= last; chunk++) if (!chunks.has(chunk)) loadChunk(chunk, generation);
|
|
789
|
+
}, 40);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
// The window of rows depends on the height of the scroll area, which is unknown while the list is empty or
|
|
793
|
+
// not on screen yet: measure again whenever it changes size.
|
|
794
|
+
if (scroller && typeof ResizeObserver !== "undefined") {
|
|
795
|
+
let lastHeight = 0;
|
|
796
|
+
new ResizeObserver(() => {
|
|
797
|
+
const height = scroller.clientHeight;
|
|
798
|
+
if (height === lastHeight) return;
|
|
799
|
+
lastHeight = height;
|
|
800
|
+
refreshWindow();
|
|
801
|
+
}).observe(scroller);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
render();
|
|
805
|
+
if (remote) load({ reset: true });
|
|
806
|
+
return asHandle({
|
|
807
|
+
element: root,
|
|
808
|
+
/** Asks the data source again (the first page, keeping sort and filter). Resolves when the answer is shown. */
|
|
809
|
+
reload: () => (remote ? load({ reset: true }) : Promise.resolve()),
|
|
810
|
+
// update() draws the table again; update({ rows }) also replaces the rows.
|
|
811
|
+
update(next) {
|
|
812
|
+
if (next?.rows) this.setRows(next.rows);
|
|
813
|
+
else render();
|
|
814
|
+
return this;
|
|
815
|
+
},
|
|
816
|
+
setRows(newRows) {
|
|
817
|
+
rows = [...newRows];
|
|
818
|
+
selected.clear();
|
|
819
|
+
state.page = 0;
|
|
820
|
+
if (scroller) scroller.scrollTop = 0;
|
|
821
|
+
render();
|
|
822
|
+
selectionChanged();
|
|
823
|
+
},
|
|
824
|
+
getSelected: () => [...selected],
|
|
825
|
+
clearSelection() {
|
|
826
|
+
selected.clear();
|
|
827
|
+
render();
|
|
828
|
+
selectionChanged();
|
|
829
|
+
},
|
|
830
|
+
getColumnWidths: () => ({ ...widths }),
|
|
831
|
+
// The filtered and sorted rows as CSV text
|
|
832
|
+
toCsv: () => toCsv(columns, current.filter(row => row !== undefined)),
|
|
833
|
+
exportCsv(filename = "table.csv") {
|
|
834
|
+
downloadFile(filename, toCsv(columns, current.filter(row => row !== undefined)));
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
}
|