-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathscroll_panel.js
More file actions
561 lines (522 loc) · 20.1 KB
/
scroll_panel.js
File metadata and controls
561 lines (522 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
283
284
285
286
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
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
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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/******************************************************************************
*
* Copyright (c) 2020, the Regular Table Authors.
*
* This file is part of the Regular Table library, distributed under the terms
* of the Apache License 2.0. The full license can be found in the LICENSE
* file.
*
*/
import { log_perf, html, throttle_tag, flush_tag } from "./utils";
import { DEBUG, BROWSER_MAX_HEIGHT } from "./constants";
import container_css from "../../dist/css/container.css";
import sub_cell_offsets from "../../dist/css/sub-cell-offsets.css";
/**
* Handles the virtual scroll pane, as well as the double buffering
* of the underlying <table>. This DOM structure looks a little like
* this:
*
* +------------------------+ <- regular-table
* | +----------------------|------<- div.rt-virtual-panel
* | | +------------------+ | <- div.rt-scroll-table-clip
* | | | +----------------|-|--+ <- table |
* | | | | 1 A Alabama | | | |
* | | | | 2 B Arizona | | | |
* | | | | 3 C Arkansas | | | |
* | | | | 4 D Californi| | | |
* | | | | 5 E Colorado | | | |
* | | +------------------+ | | |
* +------------------------+ | |
* | | 8 H District of C | |
* | +---------------------+ |
* | |
* | |
* | |
* | |
* | |
* +--------------------------------------------------+
*
* `overflow: auto` is applied to `.rt-scroll-container`, and `.rt-virtual-pane`
* is sized to match the estimated "virtual" size of the `table`; estimated,
* because it's true size can't be known until all columns dimensions are known,
* which may be deferred in the case of auto-sized tables.
*
* Double buffering can be enabled on "column scroll", "row scroll" and/or
* "column schema change". When enabled and a redraw is requested for the case,
* the existing table is cloned with `cloneNode()` and swapped with the real
* `table`, which is then updated offscreen and swapped back in. While this is
* much slower to render, it prevents draw-in.
*
* @class RegularVirtualTableViewModel
*/
export class RegularVirtualTableViewModel extends HTMLElement {
/**
* Create the DOM for this `shadowRoot`.
*
* TODO deprecated
* `MATERIAL_STYLE` is needed both here, and in the document `<head>`, due
* to double buffering, which may read incorrect position/size values as the
* double buffered `<table>` is rendered in the shadow DOM before being
* swapped in.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
*/
create_shadow_dom() {
this.attachShadow({ mode: "open" });
const slot = `<slot></slot>`;
this.shadowRoot.innerHTML = html`
<style>
${container_css}
</style>
<style>
${sub_cell_offsets}
</style>
<div class="rt-virtual-panel"></div>
<div class="rt-scroll-table-clip">${slot}</div>
`;
const [, style, virtual_panel, table_clip] = this.shadowRoot.children;
this._sub_cell_style = style;
this._table_clip = table_clip;
this._virtual_panel = virtual_panel;
this._setup_virtual_scroll();
}
_setup_virtual_scroll() {
if (this._table_clip) {
if (
this._virtual_mode === "both" ||
this._virtual_mode === "vertical"
) {
this._table_clip.style.top = "0px";
} else {
this._table_clip.style.removeProperty("top");
}
if (
this._virtual_mode === "both" ||
this._virtual_mode === "horizontal"
) {
this._table_clip.style.left = "0px";
} else {
this._table_clip.style.removeProperty("left");
}
if (this._virtual_mode !== "both") {
this._table_clip.style.contain = "none";
} else {
this._table_clip.style.removeProperty("contain");
}
}
}
/**
* Calculates the `viewport` argument for perspective's `to_columns` method.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @param {*} nrows
* @returns
*/
_calculate_viewport(nrows, num_columns) {
const { start_row, end_row } = this._calculate_row_range(nrows);
const { start_col, end_col } =
this._calculate_column_range(num_columns);
this._nrows = nrows;
return { start_col, end_col, start_row, end_row };
}
/**
* Calculate `start_row` and `end_row` for the viewport. We do this by
* first calculating `total_scroll_height`, the px height of the
* scrollable page, from the `_virtual_panel.offsetHeight`.
*
* 0px +------------+-------------+ - virtual_panel.offsetHeight
* | | . | . 600px
* | viewport | . | .
* | | . | .
* 200px +------------+ - height | . - total_scroll_height
* | 200px | . . 400px
* | | . .
* | | . .
* | | . .
* | | . .
* 600px +--------------------------+ - -
*
* `percent_scroll` can be calculated from this value and `scrollTop`,
* which we can then apply to the new calculated height to preserve scroll
* position when the height has changed since previous render.
*
* 0px +--------------------------+ -
* | | .
* | | .
* | | . scrollable area
* 300px +------------+ | .
* | | | .
* - - - | viewport | - - - - - - | - total_scroll_height
* | | | 400px
* 500px +------------+ |
* | |
* 600px +--------------------------+
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @param {*} nrows
* @returns
*/
_calculate_row_range(nrows) {
const { height, containerHeight } = this._container_size;
const row_height = this._column_sizes.row_height || 19;
const header_levels = this._view_cache.config.column_pivots.length;
const total_scroll_height = Math.max(
1,
this._virtual_panel.offsetHeight - containerHeight,
);
const percent_scroll =
Math.max(Math.ceil(this.scrollTop), 0) / total_scroll_height;
const clip_panel_row_height = height / row_height - header_levels;
const relative_nrows = nrows || 0;
const scrollable_rows = Math.max(
0,
relative_nrows - clip_panel_row_height,
);
const start_row = scrollable_rows * percent_scroll;
const end_row = Math.max(
0,
Math.min(start_row + clip_panel_row_height, nrows),
);
return { start_row, end_row };
}
_calc_start_column() {
const scroll_index_offset = this._view_cache.config.row_pivots.length;
let start_col = 0;
let offset_width = 0;
let diff = 0;
while (offset_width < this.scrollLeft) {
const new_val =
this._column_sizes.indices[start_col + scroll_index_offset];
diff = this.scrollLeft - offset_width;
start_col += 1;
offset_width += new_val !== undefined ? new_val : 60;
}
start_col +=
diff /
(this._column_sizes.indices[start_col + scroll_index_offset - 1] ||
60);
return Math.max(0, start_col - 1);
}
/**
* Calculates `start_col` and `end_col` for the viewport - most of the
* details of which are actually calculated in `_max_column`, the equivalent
* of `total_scroll_height` from `_calculate_row_range`.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @returns
*/
_calculate_column_range(num_columns) {
if (
this._virtual_mode === "none" ||
this._virtual_mode === "vertical"
) {
return { start_col: 0, end_col: Infinity };
} else {
const start_col = this._calc_start_column();
const vis_cols =
this.table_model.num_columns() ||
Math.min(
num_columns,
Math.ceil(this._container_size.width / 60),
);
let end_col = start_col + vis_cols + 1;
return { start_col, end_col };
}
}
/**
* Calculates the minimum possible starting column index for which the last
* column is completely visible (e.g. not occluded by the container clip).
* This is assumed to be the # of columns until the column widths are
* calculated as they are scrolled into view by the user, which requires
* special synchronization with _update_virtual_panel_width`
* as the scrollable width will change as the user scrolls left to right.
*
* Once `_column_sizes.indices` has enough column widths populated from
* user scrolling, it calulates the cumulative sum of column widths from
* last visible column backwards, until the sum is larger than the viewport
* px width, which is 1 below the max scroll column
*
* width = 290 = 210 = 100 = 0
* 0px V V V 500px
* +-----------------+-------+---------+--------+
* | ..ol B) (Col C) | Col D | Col E | Col F |
* | | 80px | 110px | 100px |
* | | | | |
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @returns
*/
_max_scroll_column(num_columns) {
let width = 0;
if (this._view_cache.config.row_pivots.length > 0) {
for (const w of this._column_sizes.indices.slice(
0,
this._view_cache.config.row_pivots.length,
)) {
width += w;
}
}
let scroll_index_offset = this._view_cache.config.row_pivots.length;
let max_scroll_column = num_columns;
while (width < this._container_size.width && max_scroll_column >= 0) {
max_scroll_column--;
width +=
this._column_sizes.indices[
max_scroll_column + scroll_index_offset
] || 60;
}
return Math.min(num_columns - 1, max_scroll_column + 1);
}
/**
* Determines whether the viewport is identical in row and column axes to
* the previous viewport rendered, for throttling identical render requests,
* e.g. when the logical (row-wise) viewport does not change, but the pixel
* viewport has moved a few px.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @param {*} {start_col, end_col, start_row, end_row}
* @returns
*/
_validate_viewport({ start_col, end_col, start_row, end_row }) {
start_row = Math.floor(start_row);
end_row = Math.ceil(end_row);
start_col = Math.floor(start_col);
end_col = Math.ceil(end_col);
const invalid_column = this._start_col !== start_col;
const invalid_row =
this._start_row !== start_row ||
this._end_row !== end_row ||
this._end_col !== end_col;
this._start_col = start_col;
this._end_col = end_col;
this._start_row = start_row;
this._end_row = end_row;
return { invalid_column, invalid_row };
}
_calc_scrollable_column_width(num_columns) {
let scroll_index_offset = this._view_cache.config.row_pivots.length;
const max_scroll_column = this._max_scroll_column(num_columns);
let cidx = scroll_index_offset,
virtual_width = 0;
while (cidx < max_scroll_column + scroll_index_offset) {
virtual_width += this._column_sizes.indices[cidx] || 60;
cidx++;
}
if (cidx < this._column_sizes.indices.length) {
let viewport_width = this._column_sizes.indices
.slice(0, this._view_cache.config.row_pivots.length)
.reduce((x, y) => x + y, 0);
virtual_width += Math.max(
0,
this._column_sizes.indices[cidx] -
(this._container_size.width - viewport_width) || 0,
);
}
return virtual_width;
}
/**
* Updates the `virtual_panel` width based on view state.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @param {*} invalid
*/
_update_virtual_panel_width(invalid, num_columns) {
if (invalid) {
if (
this._virtual_mode === "vertical" ||
this._virtual_mode === "none"
) {
this._virtual_panel.style.width =
this._column_sizes.indices.reduce((x, y) => x + y, 0) +
"px";
} else {
const virtual_width =
this._calc_scrollable_column_width(num_columns);
if (virtual_width !== 0) {
const panel_width =
this._container_size.width + virtual_width + 2;
this._virtual_panel.style.width = panel_width + "px";
} else {
this._virtual_panel.style.width = "1px";
}
}
}
}
/**
* Updates the `virtual_panel` height based on the view state.
*
* @internal
* @private
* @memberof RegularVirtualTableViewModel
* @param {*} nrows
*/
_update_virtual_panel_height(nrows) {
const { row_height = 19 } = this._column_sizes;
const header_height =
this._view_cache.config.column_pivots.length * row_height;
let virtual_panel_px_size;
virtual_panel_px_size = Math.min(
BROWSER_MAX_HEIGHT,
nrows * row_height + header_height,
);
this._virtual_panel.style.height = `${virtual_panel_px_size}px`;
}
/**
* Draws this virtual panel, given an object of render options that allow
* the implementor to fine tune the individual render frames based on the
* interaction and previous render state.
*
* @public
* @memberof RegularVirtualTableViewModel
* @param {DrawOptions} [options]
* @param {boolean} [options.invalid_viewport=true]
* @param {boolean} [options.preserve_width=false]
* @param {boolean} [options.throttle=true]
*/
async draw(options = {}) {
if (typeof options.throttle !== "undefined" && !options.throttle) {
return await internal_draw.call(this, options);
} else {
return await throttle_tag(this, () =>
internal_draw.call(this, options),
);
}
}
async _draw_flush() {
await flush_tag(this);
}
update_sub_cell_offset(viewport) {
const y_offset =
this._column_sizes.row_height * (viewport.start_row % 1) || 0;
const x_offset =
this._column_sizes.indices[
(this.table_model._row_headers_length || 0) +
Math.floor(viewport.start_col)
] *
(viewport.start_col % 1) || 0;
let style = this._sub_cell_style.sheet?.cssRules[0].style;
if (style) {
style.setProperty(`--regular-table--clip-x`, `${x_offset}px`);
style.setProperty(`--regular-table--clip-y`, `${y_offset}px`);
style.setProperty(`--regular-table--transform-x`, `-${x_offset}px`);
style.setProperty(`--regular-table--transform-y`, `-${y_offset}px`);
}
}
}
async function internal_draw(options) {
const __debug_start_time__ = DEBUG && performance.now();
const { invalid_viewport = true, preserve_width = false } = options;
const {
num_columns,
num_rows,
num_row_headers,
num_column_headers,
row_height,
} = await this._view_cache.view(0, 0, 0, 0);
this._column_sizes.row_height = row_height || this._column_sizes.row_height;
if (num_row_headers !== undefined) {
this._view_cache.row_pivots = Array(num_row_headers).fill(0);
}
if (num_column_headers !== undefined) {
this._view_cache.column_pivots = Array(num_column_headers).fill(0);
}
this._container_size = {
width:
this._virtual_mode === "none" || this._virtual_mode === "vertical"
? Infinity
: this._table_clip.clientWidth,
height:
this._virtual_mode === "none" || this._virtual_mode === "horizontal"
? Infinity
: this._table_clip.clientHeight,
containerHeight:
this._virtual_mode === "none" || this._virtual_mode === "horizontal"
? Infinity
: this.clientHeight,
};
this._update_virtual_panel_height(num_rows);
if (!preserve_width) {
this._update_virtual_panel_width(invalid_viewport, num_columns);
}
const viewport = this._calculate_viewport(num_rows, num_columns);
const { invalid_row, invalid_column } = this._validate_viewport(viewport);
if (
this._invalid_schema ||
invalid_row ||
invalid_column ||
invalid_viewport
) {
let autosize_cells = [],
needs_sub_cell_update = true;
for await (let last_cells of this.table_model.draw(
this._container_size,
this._view_cache,
this._selected_id,
preserve_width,
viewport,
num_columns,
)) {
if (last_cells !== undefined) {
autosize_cells = autosize_cells.concat(last_cells);
}
// We want to perform this before the next event loop so there
// is no scroll jitter, but only on the first iteration as
// subsequent viewports are incorrect.
if (needs_sub_cell_update) {
this.update_sub_cell_offset(viewport);
needs_sub_cell_update = false;
}
this._is_styling = true;
const callbacks = this._style_callbacks;
for (const callback of callbacks) {
await callback({ detail: this });
}
this._is_styling = false;
if (!this._invalidated && last_cells !== undefined) {
break;
}
this._invalidated = false;
}
const old_height = this.table_model._column_sizes.row_height;
this.table_model.autosize_cells(autosize_cells, row_height);
this.table_model.header.reset_header_cache();
if (old_height !== this.table_model._column_sizes.row_height) {
this._update_virtual_panel_height(num_rows);
}
if (!preserve_width) {
this._update_virtual_panel_width(
this._invalid_schema || invalid_column,
num_columns,
);
}
this._invalid_schema = false;
} else {
this.update_sub_cell_offset(viewport);
}
if (DEBUG) {
log_perf(performance.now() - __debug_start_time__);
}
}
/**
* Options for the draw method.
*
* @public
* @typedef DrawOptions
* @type {object}
* @property {boolean} [invalid_viewport]
* @property {boolean} [preserve_width]
*/