-
Notifications
You must be signed in to change notification settings - Fork 76
/
radar.js
783 lines (636 loc) · 26.5 KB
/
radar.js
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
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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import { A } from '@ember/array';
import { set, get } from '@ember/object';
import { assert } from '@ember/debug';
import { run } from '@ember/runloop';
import { DEBUG } from '@glimmer/env';
import { Token, scheduler } from 'ember-raf-scheduler';
import VirtualComponent from '../elements/virtual-component';
import OccludedContent from '../elements/occluded-content';
import insertRangeBefore from '../utils/insert-range-before';
import objectAt from '../utils/object-at';
import roundTo from '../utils/round-to';
import { isPrepend, isAppend } from '../utils/mutation-checkers';
import {
addScrollHandler,
removeScrollHandler
} from '../utils/scroll-handler';
import ViewportContainer from '../viewport-container';
import closestElement from '../../utils/element/closest';
import estimateElementHeight from '../../utils/element/estimate-element-height';
import getScaledClientRect from '../../utils/element/get-scaled-client-rect';
import keyForItem from '../../ember-internals/key-for-item';
import document from '../../utils/document-shim';
export default class Radar {
constructor(
parentToken,
{
bufferSize,
containerSelector,
estimateHeight,
initialRenderCount,
items,
key,
renderAll,
renderFromLast,
shouldRecycle,
startingIndex,
occlusionTagName
}
) {
this.token = new Token(parentToken);
// Public API
this.bufferSize = bufferSize;
this.containerSelector = containerSelector;
this.estimateHeight = estimateHeight;
this.initialRenderCount = initialRenderCount;
this.items = items;
this.key = key;
this.renderAll = renderAll;
this.renderFromLast = renderFromLast;
this.shouldRecycle = shouldRecycle;
this.startingIndex = startingIndex;
// defaults to a no-op intentionally, actions will only be sent if they
// are passed into the component
this.sendAction = () => {};
// Calculated constants
this._itemContainer = null;
this._scrollContainer = null;
this._prependOffset = 0;
this._calculatedEstimateHeight = 0;
this._collectionOffset = 0;
this._calculatedScrollContainerHeight = 0;
this._transformScale = 1;
// Event handler
this._scrollHandler = ({ top }) => {
// debounce scheduling updates by checking to make sure we've moved a minimum amount
if (this._didEarthquake(Math.abs(this._scrollTop - top))) {
this.scheduleUpdate();
}
};
this._resizeHandler = this.scheduleUpdate.bind(this);
// Run state
this._nextUpdate = null;
this._nextLayout = null;
this._started = false;
this._didReset = true;
this._didUpdateItems = false;
// Cache state
this._scrollTop = 0;
// Setting these values to infinity starts us in a guaranteed good state for the radar,
// so it knows that it needs to run certain measurements, etc.
this._prevFirstItemIndex = Infinity;
this._prevLastItemIndex = -Infinity;
this._prevFirstVisibleIndex = 0;
this._prevLastVisibleIndex = 0;
this._firstReached = false;
this._lastReached = false;
this._prevTotalItems = 0;
this._prevFirstKey = 0;
this._prevLastKey = 0;
this._componentPool = [];
this._prependComponentPool = [];
this._appendComponentPool = []; // https://github.com/html-next/vertical-collection/issues/296
// Boundaries
this._occludedContentBefore = new OccludedContent(occlusionTagName);
this._occludedContentAfter = new OccludedContent(occlusionTagName);
this._pageUpHandler = this.pageUp.bind(this);
this._occludedContentBefore.addEventListener('click', this._pageUpHandler);
this._pageDownHandler = this.pageDown.bind(this);
this._occludedContentAfter.addEventListener('click', this._pageDownHandler);
// Element to hold pooled component DOM when not in use
if (document) {
this._domPool = document.createDocumentFragment();
}
// Initialize virtual components
this.virtualComponents = A([this._occludedContentBefore, this._occludedContentAfter]);
this.orderedComponents = [];
this._updateVirtualComponents();
// In older versions of Ember/IE, binding anything on an object in the template
// adds observers which creates __ember_meta__
this.__ember_meta__ = null; // eslint-disable-line camelcase
if (DEBUG) {
this._debugDidUpdate = null;
}
}
destroy() {
this.token.cancel();
for (let i = 0; i < this.orderedComponents.length; i++) {
this.orderedComponents[i].destroy();
}
// Boundaries
this._occludedContentBefore.removeEventListener('click', this._pageUpHandler);
this._occludedContentAfter.removeEventListener('click', this._pageDownHandler);
this._occludedContentBefore.destroy();
this._occludedContentAfter.destroy();
this.orderedComponents = null;
set(this, 'virtualComponents', null);
if (this._started) {
removeScrollHandler(this._scrollContainer, this._scrollHandler);
ViewportContainer.removeEventListener('resize', this._resizeHandler);
}
}
schedule(queueName, job) {
return scheduler.schedule(queueName, job, this.token);
}
/**
* Start the Radar. Does initial measurements, adds event handlers,
* sets up initial scroll state, and
*/
start() {
const {
startingIndex,
containerSelector,
_occludedContentBefore
} = this;
// Use the occluded content element, which has been inserted into the DOM,
// to find the item container and the scroll container
this._itemContainer = _occludedContentBefore.element.parentNode;
this._scrollContainer = containerSelector === 'body' ? ViewportContainer : closestElement(this._itemContainer, containerSelector);
this._updateConstants();
// Setup initial scroll state
if (startingIndex !== 0) {
const {
renderFromLast,
_calculatedEstimateHeight,
_collectionOffset,
_calculatedScrollContainerHeight
} = this;
let startingScrollTop = startingIndex * _calculatedEstimateHeight;
if (renderFromLast) {
startingScrollTop -= (_calculatedScrollContainerHeight - _calculatedEstimateHeight);
}
// initialize the scrollTop value, which will be applied to the
// scrollContainer after the collection has been initialized
this._scrollTop = startingScrollTop + _collectionOffset;
this._prevFirstVisibleIndex = startingIndex;
} else {
this._scrollTop = this._scrollContainer.scrollTop;
}
this._started = true;
this.update();
// Setup event handlers
addScrollHandler(this._scrollContainer, this._scrollHandler);
ViewportContainer.addEventListener('resize', this._resizeHandler);
}
/*
* Schedules an update for the next RAF
*
* This will first run _updateVirtualComponents in the sync phase, which figures out what
* components need to be rerendered and updates the appropriate VCs and moves their associated
* DOM. At the end of the `sync` phase the runloop is flushed and Glimmer renders the changes.
*
* By the `affect` phase the Radar should have had time to measure, meaning it has all of the
* current info and we can send actions for any changes.
*
* @private
*/
scheduleUpdate(didUpdateItems, promiseResolve) {
if (didUpdateItems === true) {
// Set the update items flag first, in case scheduleUpdate has already been called
// but the RAF hasn't yet run
this._didUpdateItems = true;
}
if (this._nextUpdate !== null || this._started === false) {
return;
}
this._nextUpdate = this.schedule('sync', () => {
this._nextUpdate = null;
this._scrollTop = this._scrollContainer.scrollTop;
this.update(promiseResolve);
});
}
update(promiseResolve) {
if (this._didUpdateItems === true) {
this._determineUpdateType();
this._didUpdateItems = false;
}
this._updateConstants();
this._updateIndexes();
this._updateVirtualComponents();
this.schedule('measure', () => {
if (promiseResolve) {
promiseResolve();
}
this.afterUpdate();
});
}
afterUpdate() {
const { _prevTotalItems: totalItems } = this;
const scrollDiff = this._calculateScrollDiff();
if (scrollDiff !== 0) {
this._scrollContainer.scrollTop += scrollDiff;
}
// Re-sync scrollTop, since Chrome may have intervened
this._scrollTop = this._scrollContainer.scrollTop;
// Unset prepend offset, we're done with any prepend changes at this point
this._prependOffset = 0;
if (totalItems !== 0) {
this._sendActions();
}
// Cache previous values
this._prevFirstItemIndex = this.firstItemIndex;
this._prevLastItemIndex = this.lastItemIndex;
this._prevFirstVisibleIndex = this.firstVisibleIndex;
this._prevLastVisibleIndex = this.lastVisibleIndex;
// Clear the reset flag
this._didReset = false;
if (DEBUG && this._debugDidUpdate !== null) {
// Hook to update the visual debugger
this._debugDidUpdate(this);
}
}
/*
* The scroll diff is the difference between where we want the container's scrollTop to be,
* and where it actually is right now. By default it accounts for the `_prependOffset`, which
* is set when items are added to the front of the collection, as well as any discrepancies
* that may have arisen between the cached `_scrollTop` value and the actually container's
* scrollTop. The container's scrollTop may be modified by the browser when we manipulate DOM
* (Chrome specifically does this a lot), so `_scrollTop` should be considered the canonical
* scroll top.
*
* Subclasses should override this method to provide any difference between expected item size
* pre-render and actual item size post-render.
*/
_calculateScrollDiff() {
return (this._prependOffset + this._scrollTop) - this._scrollContainer.scrollTop;
}
_determineUpdateType() {
const {
items,
key,
totalItems,
_prevTotalItems,
_prevFirstKey,
_prevLastKey
} = this;
const lenDiff = totalItems - _prevTotalItems;
if (isPrepend(lenDiff, items, key, _prevFirstKey, _prevLastKey) === true) {
this.prepend(lenDiff);
} else if (isAppend(lenDiff, items, key, _prevFirstKey, _prevLastKey) === true) {
this.append(lenDiff);
} else {
this.reset();
}
const firstItem = objectAt(this.items, 0);
const lastItem = objectAt(this.items, this.totalItems - 1);
this._prevTotalItems = totalItems;
this._prevFirstKey = totalItems > 0 ? keyForItem(firstItem, key, 0) : 0;
this._prevLastKey = totalItems > 0 ? keyForItem(lastItem, key, totalItems - 1) : 0;
}
_updateConstants() {
const {
estimateHeight,
_occludedContentBefore,
_itemContainer,
_scrollContainer
} = this;
assert('Must provide a `estimateHeight` value to vertical-collection', estimateHeight !== null);
assert('itemContainer must be set on Radar before scheduling an update', _itemContainer !== null);
assert('scrollContainer must be set on Radar before scheduling an update', _scrollContainer !== null);
// The scroll container's offsetHeight will reflect the actual height of the element, while
// it's measured height via bounding client rect will reflect the height with any transformations
// applied. We use this to find out the scale of the items so we can store measurements at the
// correct heights.
const scrollContainerOffsetHeight = _scrollContainer.offsetHeight;
const { height: scrollContainerRenderedHeight } = _scrollContainer.getBoundingClientRect();
let transformScale;
// transformScale represents the opposite of the scale, if any, applied to the collection. Check for equality
// to guard against floating point errors, and check to make sure we're not dividing by zero (default to scale 1 if so)
if (scrollContainerOffsetHeight === scrollContainerRenderedHeight || scrollContainerRenderedHeight === 0) {
transformScale = 1;
} else {
transformScale = scrollContainerOffsetHeight / scrollContainerRenderedHeight;
}
const { top: scrollContentTop } = getScaledClientRect(_occludedContentBefore, transformScale);
const { top: scrollContainerTop } = getScaledClientRect(_scrollContainer, transformScale);
let scrollContainerMaxHeight = 0;
if (_scrollContainer instanceof Element) {
const maxHeightStyle = window.getComputedStyle(_scrollContainer).maxHeight;
if (maxHeightStyle !== 'none') {
scrollContainerMaxHeight = estimateElementHeight(_scrollContainer.parentElement, maxHeightStyle);
}
}
const calculatedEstimateHeight = typeof estimateHeight === 'string'
? estimateElementHeight(_itemContainer, estimateHeight)
: estimateHeight;
assert(`calculatedEstimateHeight must be greater than 0, instead was "${calculatedEstimateHeight}" based on estimateHeight: ${estimateHeight}`, calculatedEstimateHeight > 0);
this._transformScale = transformScale;
this._calculatedEstimateHeight = calculatedEstimateHeight;
this._calculatedScrollContainerHeight = roundTo(Math.max(scrollContainerOffsetHeight, scrollContainerMaxHeight));
// The offset between the top of the collection and the top of the scroll container. Determined by finding
// the distance from the collection is from the top of the scroll container's content (scrollTop + actual position)
// and subtracting the scroll containers actual top.
this._collectionOffset = roundTo((_scrollContainer.scrollTop + scrollContentTop) - scrollContainerTop);
}
/*
* Updates virtualComponents, which is meant to be a static pool of components that we render to.
* In order to decrease the time spent rendering and diffing, we pull the {{each}} out of the DOM
* and only replace the content of _virtualComponents which are removed/added.
*
* For instance, if we start with the following and scroll down, items 2 and 3 do not need to be
* rerendered, only item 1 needs to be removed and only item 4 needs to be added. So we replace
* item 1 with item 4, and then manually move the DOM:
*
* 1 4 2
* 2 -> replace 1 with 4 -> 2 -> manually move DOM -> 3
* 3 3 4
*
* However, _virtualComponents is still out of order. Rather than keep track of the state of
* things in _virtualComponents, we track the visually ordered components in the
* _orderedComponents array. This is possible because all of our operations are relatively simple,
* popping some number of components off one end and pushing them onto the other.
*
* @private
*/
_updateVirtualComponents() {
const {
items,
orderedComponents,
virtualComponents,
_componentPool,
shouldRecycle,
renderAll,
_started,
_didReset,
_occludedContentBefore,
_occludedContentAfter,
totalItems
} = this;
let renderedFirstItemIndex, renderedLastItemIndex, renderedTotalBefore, renderedTotalAfter;
if (renderAll === true) {
// All items should be rendered, set indexes based on total item count
renderedFirstItemIndex = 0;
renderedLastItemIndex = totalItems - 1;
renderedTotalBefore = 0;
renderedTotalAfter = 0;
} else if (_started === false) {
// The Radar hasn't been started yet, render the initialRenderCount if it exists
renderedFirstItemIndex = this.startingIndex;
renderedLastItemIndex = this.startingIndex + this.initialRenderCount - 1;
renderedTotalBefore = 0;
renderedTotalAfter = 0;
} else {
renderedFirstItemIndex = this.firstItemIndex;
renderedLastItemIndex = this.lastItemIndex;
renderedTotalBefore = this.totalBefore;
renderedTotalAfter = this.totalAfter;
}
// If there are less items available than rendered, we drop the last rendered item index
renderedLastItemIndex = Math.min(renderedLastItemIndex, totalItems - 1);
// Add components to be recycled to the pool
while (orderedComponents.length > 0 && orderedComponents[0].index < renderedFirstItemIndex) {
_componentPool.push(orderedComponents.shift());
}
while (orderedComponents.length > 0 && orderedComponents[orderedComponents.length - 1].index > renderedLastItemIndex) {
_componentPool.unshift(orderedComponents.pop());
}
if (_didReset) {
if (shouldRecycle === true) {
for (let i = 0; i < orderedComponents.length; i++) {
// If the underlying array has changed, the indexes could be the same but
// the content may have changed, so recycle the remaining components
const component = orderedComponents[i];
component.recycle(objectAt(items, component.index), component.index);
}
} else {
while (orderedComponents.length > 0) {
// If recycling is disabled we need to delete all components and clear the array
_componentPool.push(orderedComponents.shift());
}
}
}
let firstIndexInList = orderedComponents.length > 0 ? orderedComponents[0].index : renderedFirstItemIndex;
let lastIndexInList = orderedComponents.length > 0 ? orderedComponents[orderedComponents.length - 1].index : renderedFirstItemIndex - 1;
// Append as many items as needed to the rendered components
while (lastIndexInList < renderedLastItemIndex) {
let component;
if (shouldRecycle === true) {
component = _componentPool.pop() || new VirtualComponent();
} else {
component = new VirtualComponent();
}
const itemIndex = ++lastIndexInList;
component.recycle(objectAt(items, itemIndex), itemIndex);
this._appendComponent(component);
orderedComponents.push(component);
}
// Prepend as many items as needed to the rendered components
while (firstIndexInList > renderedFirstItemIndex) {
let component;
if (shouldRecycle === true) {
component = _componentPool.pop() || new VirtualComponent();
} else {
component = new VirtualComponent();
}
const itemIndex = --firstIndexInList;
component.recycle(objectAt(items, itemIndex), itemIndex);
this._prependComponent(component);
orderedComponents.unshift(component);
}
// If there are any items remaining in the pool, remove them
if (_componentPool.length > 0) {
if (shouldRecycle === true) {
// Grab the DOM of the remaining components and move it to temporary node disconnected from
// the body if the item can be reused later otherwise delete the component to avoid virtual re-rendering of the
// deleted item. If we end up using these components again, we'll grab their DOM and put it back
for (let i = _componentPool.length - 1; i >= 0; i--) {
const component = _componentPool[i];
const item = objectAt(items, component.index);
if (item) {
insertRangeBefore(this._domPool, null, component.realUpperBound, component.realLowerBound);
} else {
// Insert the virtual component bound back to make sure Glimmer is
// not confused about the state of the DOM.
insertRangeBefore(this._itemContainer, null, component.realUpperBound, component.realLowerBound);
run(() => {
virtualComponents.removeObject(component);
});
_componentPool.splice(i, 1);
}
}
} else {
virtualComponents.removeObjects(_componentPool);
_componentPool.length = 0;
}
}
const totalItemsBefore = renderedFirstItemIndex;
const totalItemsAfter = totalItems - renderedLastItemIndex - 1;
const beforeItemsText = totalItemsBefore === 1 ? 'item' : 'items';
const afterItemsText = totalItemsAfter === 1 ? 'item' : 'items';
// Set padding element heights.
_occludedContentBefore.style.height = `${Math.max(renderedTotalBefore, 0)}px`;
_occludedContentBefore.innerHTML = totalItemsBefore > 0 ? `And ${totalItemsBefore} ${beforeItemsText} before` : '';
_occludedContentAfter.style.height = `${Math.max(renderedTotalAfter, 0)}px`;
_occludedContentAfter.innerHTML = totalItemsAfter > 0 ? `And ${totalItemsAfter} ${afterItemsText} after` : '';
}
_appendComponent(component) {
const {
virtualComponents,
_occludedContentAfter,
_appendComponentPool,
shouldRecycle,
_itemContainer
} = this;
const relativeNode = _occludedContentAfter.realUpperBound;
if (component.rendered === true) {
insertRangeBefore(_itemContainer, relativeNode, component.realUpperBound, component.realLowerBound);
} else {
virtualComponents.insertAt(virtualComponents.length - 1, component);
component.rendered = true;
// shouldRecycle=false breaks UI when scrolling the elements fast.
// Reference https://github.com/html-next/vertical-collection/issues/296
// Components that are both new and appended still need to be rendered at the end because Glimmer.
// We have to move them _after_ they render, so we schedule that if they exist
if(!shouldRecycle) {
_appendComponentPool.unshift(component);
if (this._nextLayout === null) {
this._nextLayout = this.schedule('layout', () => {
this._nextLayout = null;
while (_appendComponentPool.length > 0) {
const component = _appendComponentPool.pop();
// Changes with each inserted component
const relativeNode = _occludedContentAfter.realUpperBound;
insertRangeBefore(this._itemContainer, relativeNode, component.realUpperBound, component.realLowerBound);
}
});
}
}
}
}
_prependComponent(component) {
const {
virtualComponents,
_occludedContentBefore,
_prependComponentPool,
_itemContainer
} = this;
const relativeNode = _occludedContentBefore.realLowerBound.nextSibling;
if (component.rendered === true) {
insertRangeBefore(_itemContainer, relativeNode, component.realUpperBound, component.realLowerBound);
} else {
virtualComponents.insertAt(virtualComponents.length - 1, component);
component.rendered = true;
// Components that are both new and prepended still need to be rendered at the end because Glimmer.
// We have to move them _after_ they render, so we schedule that if they exist
_prependComponentPool.unshift(component);
if (this._nextLayout === null) {
this._nextLayout = this.schedule('layout', () => {
this._nextLayout = null;
while (_prependComponentPool.length > 0) {
const component = _prependComponentPool.pop();
// Changes with each inserted component
const relativeNode = _occludedContentBefore.realLowerBound.nextSibling;
insertRangeBefore(_itemContainer, relativeNode, component.realUpperBound, component.realLowerBound);
}
});
}
}
}
_sendActions() {
const {
firstItemIndex,
lastItemIndex,
firstVisibleIndex,
lastVisibleIndex,
_prevFirstVisibleIndex,
_prevLastVisibleIndex,
totalItems,
_firstReached,
_lastReached,
_didReset
} = this;
if (_didReset || firstVisibleIndex !== _prevFirstVisibleIndex) {
this.sendAction('firstVisibleChanged', firstVisibleIndex);
}
if (_didReset || lastVisibleIndex !== _prevLastVisibleIndex) {
this.sendAction('lastVisibleChanged', lastVisibleIndex);
}
if (_firstReached === false && firstItemIndex === 0) {
this.sendAction('firstReached', firstItemIndex);
this._firstReached = true;
}
if (_lastReached === false && lastItemIndex === totalItems - 1) {
this.sendAction('lastReached', lastItemIndex);
this._lastReached = true;
}
}
prepend(numPrepended) {
this._prevFirstItemIndex += numPrepended;
this._prevLastItemIndex += numPrepended;
this.orderedComponents.forEach((c) => set(c, 'index', get(c, 'index') + numPrepended));
this._firstReached = false;
this._prependOffset = numPrepended * this._calculatedEstimateHeight;
}
append() {
this._lastReached = false;
}
reset() {
this._firstReached = false;
this._lastReached = false;
this._didReset = true;
}
pageUp() {
if (this.renderAll) {
return; // All items rendered, no need to page up
}
const {
bufferSize,
firstItemIndex,
totalComponents
} = this;
if (firstItemIndex !== 0) {
const newFirstItemIndex = Math.max(firstItemIndex - totalComponents + bufferSize, 0);
const offset = this.getOffsetForIndex(newFirstItemIndex);
this._scrollContainer.scrollTop = offset + this._collectionOffset;
this.scheduleUpdate();
}
}
pageDown() {
if (this.renderAll) {
return; // All items rendered, no need to page down
}
const {
bufferSize,
lastItemIndex,
totalComponents,
totalItems
} = this;
if (lastItemIndex !== totalItems - 1) {
const newFirstItemIndex = Math.min(lastItemIndex + bufferSize + 1, totalItems - totalComponents);
const offset = this.getOffsetForIndex(newFirstItemIndex);
this._scrollContainer.scrollTop = offset + this._collectionOffset;
this.scheduleUpdate();
}
}
get totalComponents() {
return Math.min(this.totalItems, (this.lastItemIndex - this.firstItemIndex) + 1);
}
/*
* `prependOffset` exists because there are times when we need to do the following in this exact
* order:
*
* 1. Prepend, which means we need to adjust the scroll position by `estimateHeight * numPrepended`
* 2. Calculate the items that will be displayed after the prepend, and move VCs around as
* necessary (`scheduleUpdate`).
* 3. Actually add the amount prepended to `scrollContainer.scrollTop`
*
* This is due to some strange behavior in Chrome where it will modify `scrollTop` on it's own
* when prepending item elements. We seem to avoid this behavior by doing these things in a RAF
* in this exact order.
*/
get visibleTop() {
return Math.max(this._scrollTop - this._collectionOffset + this._prependOffset, 0);
}
get visibleMiddle() {
return this.visibleTop + (this._calculatedScrollContainerHeight / 2);
}
get visibleBottom() {
// There is a case where the container of this vertical collection could have height 0 at
// initial render step but will be updated later. We want to return visibleBottom to be 0 rather
// than -1.
return Math.max(this.visibleTop + this._calculatedScrollContainerHeight - 1, 0);
}
get totalItems() {
return this.items ? get(this.items, 'length') : 0;
}
}