-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
virtual-scroll.tsx
464 lines (408 loc) · 13.5 KB
/
virtual-scroll.tsx
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
import { Component, ComponentInterface, Element, FunctionalComponent, Host, Listen, Method, Prop, State, Watch, h, readTask, writeTask } from '@stencil/core';
import { Cell, DomRenderFn, FooterHeightFn, HeaderFn, HeaderHeightFn, ItemHeightFn, ItemRenderFn, VirtualNode } from '../../interface';
import { CELL_TYPE_FOOTER, CELL_TYPE_HEADER, CELL_TYPE_ITEM } from './constants';
import { Range, calcCells, calcHeightIndex, doRender, findCellIndex, getRange, getShouldUpdate, getViewport, inplaceUpdate, positionForIndex, resizeBuffer, updateVDom } from './virtual-scroll-utils';
@Component({
tag: 'ion-virtual-scroll',
styleUrl: 'virtual-scroll.scss'
})
export class VirtualScroll implements ComponentInterface {
private contentEl?: HTMLElement;
private scrollEl?: HTMLElement;
private range: Range = { offset: 0, length: 0 };
private timerUpdate: any;
private heightIndex?: Uint32Array;
private viewportHeight = 0;
private cells: Cell[] = [];
private virtualDom: VirtualNode[] = [];
private isEnabled = false;
private viewportOffset = 0;
private currentScrollTop = 0;
private indexDirty = 0;
private lastItemLen = 0;
private rmEvent: (() => void) | undefined;
@Element() el!: HTMLIonVirtualScrollElement;
@State() totalHeight = 0;
/**
* It is important to provide this
* if virtual item height will be significantly larger than the default
* The approximate height of each virtual item template's cell.
* This dimension is used to help determine how many cells should
* be created when initialized, and to help calculate the height of
* the scrollable area. This height value can only use `px` units.
* Note that the actual rendered size of each cell comes from the
* app's CSS, whereas this approximation is used to help calculate
* initial dimensions before the item has been rendered.
*/
@Prop() approxItemHeight = 45;
/**
* The approximate height of each header template's cell.
* This dimension is used to help determine how many cells should
* be created when initialized, and to help calculate the height of
* the scrollable area. This height value can only use `px` units.
* Note that the actual rendered size of each cell comes from the
* app's CSS, whereas this approximation is used to help calculate
* initial dimensions before the item has been rendered.
*/
@Prop() approxHeaderHeight = 30;
/**
* The approximate width of each footer template's cell.
* This dimension is used to help determine how many cells should
* be created when initialized, and to help calculate the height of
* the scrollable area. This height value can only use `px` units.
* Note that the actual rendered size of each cell comes from the
* app's CSS, whereas this approximation is used to help calculate
* initial dimensions before the item has been rendered.
*/
@Prop() approxFooterHeight = 30;
/**
* Section headers and the data used within its given
* template can be dynamically created by passing a function to `headerFn`.
* For example, a large list of contacts usually has dividers between each
* letter in the alphabet. App's can provide their own custom `headerFn`
* which is called with each record within the dataset. The logic within
* the header function can decide if the header template should be used,
* and what data to give to the header template. The function must return
* `null` if a header cell shouldn't be created.
*/
@Prop() headerFn?: HeaderFn;
/**
* Section footers and the data used within its given
* template can be dynamically created by passing a function to `footerFn`.
* The logic within the footer function can decide if the footer template
* should be used, and what data to give to the footer template. The function
* must return `null` if a footer cell shouldn't be created.
*/
@Prop() footerFn?: HeaderFn;
/**
* The data that builds the templates within the virtual scroll.
* It's important to note that when this data has changed, then the
* entire virtual scroll is reset, which is an expensive operation and
* should be avoided if possible.
*/
@Prop() items?: any[];
/**
* An optional function that maps each item within their height.
* When this function is provides, heavy optimizations and fast path can be taked by
* `ion-virtual-scroll` leading to massive performance improvements.
*
* This function allows to skip all DOM reads, which can be Doing so leads
* to massive performance
*/
@Prop() itemHeight?: ItemHeightFn;
/**
* An optional function that maps each item header within their height.
*/
@Prop() headerHeight?: HeaderHeightFn;
/**
* An optional function that maps each item footer within their height.
*/
@Prop() footerHeight?: FooterHeightFn;
/**
* NOTE: only JSX API for stencil.
*
* Provide a render function for the items to be rendered. Returns a JSX virtual-dom.
*/
@Prop() renderItem?: (item: any, index: number) => any;
/**
* NOTE: only JSX API for stencil.
*
* Provide a render function for the header to be rendered. Returns a JSX virtual-dom.
*/
@Prop() renderHeader?: (item: any, index: number) => any;
/**
* NOTE: only JSX API for stencil.
*
* Provide a render function for the footer to be rendered. Returns a JSX virtual-dom.
*/
@Prop() renderFooter?: (item: any, index: number) => any;
/**
* NOTE: only Vanilla JS API.
*/
@Prop() nodeRender?: ItemRenderFn;
/** @internal */
@Prop() domRender?: DomRenderFn;
@Watch('itemHeight')
@Watch('headerHeight')
@Watch('footerHeight')
@Watch('items')
itemsChanged() {
this.calcCells();
this.updateVirtualScroll();
}
async componentDidLoad() {
const contentEl = this.el.closest('ion-content');
if (!contentEl) {
console.error('virtual-scroll must be used inside ion-content');
return;
}
await contentEl.componentOnReady();
this.contentEl = contentEl;
this.scrollEl = await contentEl.getScrollElement();
this.calcCells();
this.updateState();
}
componentDidUpdate() {
this.updateState();
}
componentDidUnload() {
this.scrollEl = undefined;
}
@Listen('resize', { target: 'window' })
onResize() {
this.calcCells();
this.updateVirtualScroll();
}
/**
* Returns the position of the virtual item at the given index.
*/
@Method()
positionForItem(index: number): Promise<number> {
return Promise.resolve(positionForIndex(index, this.cells, this.getHeightIndex()));
}
/**
* This method marks a subset of items as dirty, so they can be re-rendered. Items should be marked as
* dirty any time the content or their style changes.
*
* The subset of items to be updated can are specifing by an offset and a length.
*/
@Method()
async checkRange(offset: number, len = -1) {
// TODO: kind of hacky how we do in-place updated of the cells
// array. this part needs a complete refactor
if (!this.items) {
return;
}
const length = (len === -1)
? this.items.length - offset
: len;
const cellIndex = findCellIndex(this.cells, offset);
const cells = calcCells(
this.items,
this.itemHeight,
this.headerHeight,
this.footerHeight,
this.headerFn,
this.footerFn,
this.approxHeaderHeight,
this.approxFooterHeight,
this.approxItemHeight,
cellIndex, offset, length
);
this.cells = inplaceUpdate(this.cells, cells, cellIndex);
this.lastItemLen = this.items.length;
this.indexDirty = Math.max(offset - 1, 0);
this.scheduleUpdate();
}
/**
* This method marks the tail the items array as dirty, so they can be re-rendered.
*
* It's equivalent to calling:
*
* ```js
* virtualScroll.checkRange(lastItemLen);
* ```
*/
@Method()
async checkEnd() {
if (this.items) {
this.checkRange(this.lastItemLen);
}
}
private onScroll = () => {
this.updateVirtualScroll();
}
private updateVirtualScroll() {
// do nothing if virtual-scroll is disabled
if (!this.isEnabled || !this.scrollEl) {
return;
}
// unschedule future updates
if (this.timerUpdate) {
clearTimeout(this.timerUpdate);
this.timerUpdate = undefined;
}
// schedule DOM operations into the stencil queue
readTask(this.readVS.bind(this));
writeTask(this.writeVS.bind(this));
}
private readVS() {
const { contentEl, scrollEl, el } = this;
let topOffset = 0;
let node: HTMLElement | null = el;
while (node && node !== contentEl) {
topOffset += node.offsetTop;
node = node.parentElement;
}
this.viewportOffset = topOffset;
if (scrollEl) {
this.viewportHeight = scrollEl.offsetHeight;
this.currentScrollTop = scrollEl.scrollTop;
}
}
private writeVS() {
const dirtyIndex = this.indexDirty;
// get visible viewport
const scrollTop = this.currentScrollTop - this.viewportOffset;
const viewport = getViewport(scrollTop, this.viewportHeight, 100);
// compute lazily the height index
const heightIndex = this.getHeightIndex();
// get array bounds of visible cells base in the viewport
const range = getRange(heightIndex, viewport, 2);
// fast path, do nothing
const shouldUpdate = getShouldUpdate(dirtyIndex, this.range, range);
if (!shouldUpdate) {
return;
}
this.range = range;
// in place mutation of the virtual DOM
updateVDom(
this.virtualDom,
heightIndex,
this.cells,
range
);
// Write DOM
// Different code paths taken depending of the render API used
if (this.nodeRender) {
doRender(this.el, this.nodeRender, this.virtualDom, this.updateCellHeight.bind(this));
} else if (this.domRender) {
this.domRender(this.virtualDom);
} else if (this.renderItem) {
this.el.forceUpdate();
}
}
private updateCellHeight(cell: Cell, node: any) {
const update = () => {
if ((node as any)['$ionCell'] === cell) {
const style = window.getComputedStyle(node);
const height = node.offsetHeight + parseFloat(style.getPropertyValue('margin-bottom'));
this.setCellHeight(cell, height);
}
};
if (node && node.componentOnReady) {
node.componentOnReady().then(update);
} else {
update();
}
}
private setCellHeight(cell: Cell, height: number) {
const index = cell.i;
// the cell might changed since the height update was scheduled
if (cell !== this.cells[index]) {
return;
}
if (cell.height !== height || cell.visible !== true) {
cell.visible = true;
cell.height = height;
this.indexDirty = Math.min(this.indexDirty, index);
this.scheduleUpdate();
}
}
private scheduleUpdate() {
clearTimeout(this.timerUpdate);
this.timerUpdate = setTimeout(() => this.updateVirtualScroll(), 100);
}
private updateState() {
const shouldEnable = !!(
this.scrollEl &&
this.cells
);
if (shouldEnable !== this.isEnabled) {
this.enableScrollEvents(shouldEnable);
if (shouldEnable) {
this.updateVirtualScroll();
}
}
}
private calcCells() {
if (!this.items) {
return;
}
this.lastItemLen = this.items.length;
this.cells = calcCells(
this.items,
this.itemHeight,
this.headerHeight,
this.footerHeight,
this.headerFn,
this.footerFn,
this.approxHeaderHeight,
this.approxFooterHeight,
this.approxItemHeight,
0, 0, this.lastItemLen
);
this.indexDirty = 0;
}
private getHeightIndex(): Uint32Array {
if (this.indexDirty !== Infinity) {
this.calcHeightIndex(this.indexDirty);
}
return this.heightIndex!;
}
private calcHeightIndex(index = 0) {
// TODO: optimize, we don't need to calculate all the cells
this.heightIndex = resizeBuffer(this.heightIndex, this.cells.length);
this.totalHeight = calcHeightIndex(this.heightIndex, this.cells, index);
this.indexDirty = Infinity;
}
private enableScrollEvents(shouldListen: boolean) {
if (this.rmEvent) {
this.rmEvent();
this.rmEvent = undefined;
}
const scrollEl = this.scrollEl;
if (scrollEl) {
this.isEnabled = shouldListen;
scrollEl.addEventListener('scroll', this.onScroll);
this.rmEvent = () => {
scrollEl.removeEventListener('scroll', this.onScroll);
};
}
}
private renderVirtualNode(node: VirtualNode) {
const { type, value, index } = node.cell;
switch (type) {
case CELL_TYPE_ITEM: return this.renderItem!(value, index);
case CELL_TYPE_HEADER: return this.renderHeader!(value, index);
case CELL_TYPE_FOOTER: return this.renderFooter!(value, index);
}
}
render() {
return (
<Host
style={{
height: `${this.totalHeight}px`
}}
>
{this.renderItem && (
<VirtualProxy dom={this.virtualDom}>
{this.virtualDom.map(node => this.renderVirtualNode(node))}
</VirtualProxy>
)}
</Host>
);
}
}
const VirtualProxy: FunctionalComponent<{dom: VirtualNode[]}> = ({ dom }, children, utils) => {
return utils.map(children, (child, i) => {
const node = dom[i];
const vattrs = child.vattrs || {};
let classes = vattrs.class || '';
classes += 'virtual-item ';
if (!node.visible) {
classes += 'virtual-loading';
}
return {
...child,
vattrs: {
...vattrs,
class: classes,
style: {
...vattrs.style,
transform: `translate3d(0,${node.top}px,0)`
}
}
};
});
};