Skip to content

Commit 0b9e6f7

Browse files
committed
refactor(repeat): cleanup repeat
1 parent 632b939 commit 0b9e6f7

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

src/virtual-repeat.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class VirtualRepeat extends AbstractRepeater {
214214
_prevItemsCount: number;
215215

216216
/**@internal */
217-
scrollContainer: HTMLElement;
217+
containerEl: HTMLElement;
218218

219219
/**@internal */
220220
private scrollListener: () => any;
@@ -339,7 +339,7 @@ export class VirtualRepeat extends AbstractRepeater {
339339
const scrollListener = this.scrollListener = () => {
340340
this._onScroll();
341341
};
342-
const scrollContainer = this.scrollContainer = templateStrategy.getScrollContainer(element);
342+
const containerEl = this.containerEl = templateStrategy.getScrollContainer(element);
343343
const [topBufferEl, bottomBufferEl] = templateStrategy.createBuffers(element);
344344

345345
this.topBufferEl = topBufferEl;
@@ -359,14 +359,14 @@ export class VirtualRepeat extends AbstractRepeater {
359359
const firstElement = templateStrategy.getFirstElement(topBufferEl, bottomBufferEl);
360360
this.distanceToTop = firstElement === null ? 0 : getElementDistanceToTopOfDocument(firstElement);
361361

362-
if (hasOverflowScroll(scrollContainer)) {
362+
if (hasOverflowScroll(containerEl)) {
363363
this._fixedHeightContainer = true;
364-
scrollContainer.addEventListener('scroll', scrollListener);
364+
containerEl.addEventListener('scroll', scrollListener);
365365
} else {
366366
DOM.addEventListener('scroll', scrollListener, false);
367367
}
368368
if (this.items.length < this.elementsInView && this.isLastIndex === undefined) {
369-
this._getMore(true);
369+
this._getMore(/*force?*/true);
370370
}
371371
}
372372

@@ -377,34 +377,34 @@ export class VirtualRepeat extends AbstractRepeater {
377377

378378
/**@override */
379379
detached(): void {
380-
const scrollCt = this.scrollContainer;
380+
const scrollCt = this.containerEl;
381381
const scrollListener = this.scrollListener;
382382
if (hasOverflowScroll(scrollCt)) {
383383
scrollCt.removeEventListener('scroll', scrollListener);
384384
} else {
385385
DOM.removeEventListener('scroll', scrollListener, false);
386386
}
387387
this.isLastIndex = undefined;
388-
this._fixedHeightContainer = false;
388+
this._isAttached
389+
= this._fixedHeightContainer = false;
390+
this._unsubscribeCollection();
389391
this._resetCalculation();
390-
this._isAttached = false;
391-
this._prevItemsCount = 0;
392392
this.templateStrategy.removeBuffers(this.element, this.topBufferEl, this.bottomBufferEl);
393-
this.topBufferEl = this.bottomBufferEl = this.scrollContainer = this.scrollListener = null;
394-
this.distanceToTop = 0;
393+
this.topBufferEl = this.bottomBufferEl = this.containerEl = this.scrollListener = null;
395394
this.removeAllViews(/*return to cache?*/true, /*skip animation?*/false);
396-
this._unsubscribeCollection();
397395
const $clearInterval = PLATFORM.global.clearInterval;
398396
$clearInterval(this._calcDistanceToTopInterval);
399397
$clearInterval(this._sizeInterval);
400-
this._sizeInterval = this._calcDistanceToTopInterval = 0;
398+
this._prevItemsCount
399+
= this.distanceToTop
400+
= this._sizeInterval
401+
= this._calcDistanceToTopInterval = 0;
401402
}
402403

403404
/**@override */
404405
unbind(): void {
405406
this.scope = null;
406407
this.items = null;
407-
this._prevItemsCount = 0;
408408
}
409409

410410
/**
@@ -439,7 +439,7 @@ export class VirtualRepeat extends AbstractRepeater {
439439
throw new Error('Value is not iterateable for virtual repeat.');
440440
}
441441

442-
const scroller = this.scrollContainer;
442+
const scroller = this.containerEl;
443443
if (shouldCalculateSize) {
444444
const currentItemCount = items.length;
445445
if (currentItemCount > 0 && this.viewCount() === 0) {
@@ -462,9 +462,10 @@ export class VirtualRepeat extends AbstractRepeater {
462462
strategy.instanceChanged(this, items, this._first);
463463

464464
if (shouldCalculateSize) {
465+
const firstIndex = this._first;
465466
const currentItemCount = items.length;
466467
// Reset rebinding
467-
this._lastRebind = this._first;
468+
this._lastRebind = firstIndex;
468469

469470
if (reducingItems && previousLastViewIndex > currentItemCount - 1) {
470471
// Do we need to set scrolltop so that we appear at the bottom of the list to match scrolling as far as we could?
@@ -474,7 +475,7 @@ export class VirtualRepeat extends AbstractRepeater {
474475
}
475476
if (!reducingItems) {
476477
// If we're expanding our items, then we need to reset our previous first for the next go around of scroll handling
477-
this._previousFirst = this._first;
478+
this._previousFirst = firstIndex;
478479
// Simulating the down scroll event to load up data appropriately
479480
this._scrollingDown = true;
480481
this._scrollingUp = false;
@@ -527,7 +528,7 @@ export class VirtualRepeat extends AbstractRepeater {
527528
*/
528529
getScroller(): HTMLElement {
529530
return this._fixedHeightContainer
530-
? this.scrollContainer
531+
? this.containerEl
531532
: document.documentElement;
532533
}
533534

@@ -594,13 +595,8 @@ export class VirtualRepeat extends AbstractRepeater {
594595
return;
595596
}
596597
const topBufferEl = this.topBufferEl;
597-
const scrollerEl = this.scrollContainer;
598+
const scrollerEl = this.containerEl;
598599
const itemHeight = this.itemHeight;
599-
// If offset parent of top buffer is the scroll container
600-
// its actual offsetTop is just the offset top itself
601-
// If not, then the offset top is calculated based on the parent offsetTop as well
602-
const topBufferDistance = getDistanceToParent(topBufferEl, scrollerEl);
603-
const isFixedHeightContainer = this._fixedHeightContainer;
604600
/**
605601
* Real scroll top calculated based on current scroll top of scroller and top buffer {height + distance to top}
606602
* as there could be elements before top buffer and it affects real scroll top of the selected repeat
@@ -609,15 +605,18 @@ export class VirtualRepeat extends AbstractRepeater {
609605
* - not document: the scroll top of this repeat is calculated based on current scroller.scrollTop and the distance to top of `top buffer`
610606
* - document: the scroll top is the substraction of `pageYOffset` and distance to top of current buffer element (logic needs revised)
611607
*/
612-
const scrollTop = isFixedHeightContainer
613-
? scrollerEl.scrollTop
614-
: (pageYOffset - this.distanceToTop);
615-
const realScrollTop = Math$max(
616-
0,
617-
isFixedHeightContainer
618-
? scrollTop - Math$abs(topBufferDistance)
619-
: scrollTop
620-
);
608+
let realScrollTop = 0;
609+
const isFixedHeightContainer = this._fixedHeightContainer;
610+
if (isFixedHeightContainer) {
611+
// If offset parent of top buffer is the scroll container
612+
// its actual offsetTop is just the offset top itself
613+
// If not, then the offset top is calculated based on the parent offsetTop as well
614+
const topBufferDistance = getDistanceToParent(topBufferEl, scrollerEl);
615+
const scrollerScrollTop = scrollerEl.scrollTop;
616+
realScrollTop = Math$max(0, scrollerScrollTop - Math$abs(topBufferDistance));
617+
} else {
618+
realScrollTop = pageYOffset - this.distanceToTop;
619+
}
621620
const elementsInView = this.elementsInView;
622621

623622
// Calculate the index of first view
@@ -695,9 +694,7 @@ export class VirtualRepeat extends AbstractRepeater {
695694
}
696695
}
697696
this._previousFirst = firstIndex;
698-
this._isScrolling
699-
= this._scrollingUp
700-
= this._scrollingDown = false;
697+
this._isScrolling = false;
701698
}
702699

703700
/**@internal*/
@@ -930,7 +927,7 @@ export class VirtualRepeat extends AbstractRepeater {
930927
this._prevItemsCount = itemsLength;
931928
const itemHeight = this.itemHeight;
932929
const scrollContainerHeight = this._fixedHeightContainer
933-
? calcScrollHeight(this.scrollContainer)
930+
? calcScrollHeight(this.containerEl)
934931
: document.documentElement.clientHeight;
935932
const elementsInView = this.elementsInView = Math$ceil(scrollContainerHeight / itemHeight) + 1;
936933
const viewsCount = this._viewsLength = elementsInView * 2;

0 commit comments

Comments
 (0)