From acefd0800dd2a1db6039407f1a313e3a16854842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 13 Nov 2018 16:01:00 +0100 Subject: Extract the load of previous elements to its own method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- js/views/virtuallist.js | 57 +++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/js/views/virtuallist.js b/js/views/virtuallist.js index e2f0a017a..0c5a0ff7b 100644 --- a/js/views/virtuallist.js +++ b/js/views/virtuallist.js @@ -166,6 +166,7 @@ this._prependedElementsBuffer = document.createDocumentFragment(); delete this._$firstPrependedElement; + delete this._$lastPrependedElement; }, appendElementStart: function() { @@ -193,6 +194,7 @@ if (!this._$firstPrependedElement) { this._$firstPrependedElement = $element; } + this._$lastPrependedElement = $element; }, appendElement: function($element) { @@ -217,10 +219,34 @@ }, prependElementEnd: function() { + this._loadPreviousElements( + this._$firstPrependedElement, + this._$lastPrependedElement, + this._prependedElementsBuffer + ); + + delete this._prependedElementsBuffer; + + this.updateVisibleElements(); + }, + + appendElementEnd: function() { + this._loadNextElements( + this._$firstAppendedElement, + this._$lastAppendedElement, + this._appendedElementsBuffer + ); + + delete this._appendedElementsBuffer; + + this.updateVisibleElements(); + }, + + _loadPreviousElements: function($firstElementToLoad, $lastElementToLoad, elementsBuffer) { var $wrapper = $('
'); $wrapper._top = 0; - var $firstExistingElement = this._$firstPrependedElement._next; + var $firstExistingElement = $firstElementToLoad._next; if ($firstExistingElement) { // The wrapper is already at the top, so no need to set its @@ -235,8 +261,7 @@ var previousWrapperHeight = this._getElementHeight($wrapper); - $wrapper.prepend(this._prependedElementsBuffer); - delete this._prependedElementsBuffer; + $wrapper.prepend(elementsBuffer); var wrapperHeightDifference = this._getElementHeight($wrapper) - previousWrapperHeight; @@ -245,10 +270,10 @@ // number. this._$wrapperBackground.height(this._getElementHeight(this._$wrapperBackground) + wrapperHeightDifference); - while (this._$firstPrependedElement) { - this._updateCache(this._$firstPrependedElement, $wrapper); + while ($firstElementToLoad !== $lastElementToLoad._previous) { + this._updateCache($firstElementToLoad, $wrapper); - this._$firstPrependedElement = this._$firstPrependedElement._previous; + $firstElementToLoad = $firstElementToLoad._previous; } // Remove the temporal wrapper used to layout and get the height of @@ -284,27 +309,13 @@ this._$container.scrollTop(Math.round(this._$container.scrollTop() + wrapperHeightDifference)); // Update the position of the wrapper with the visible elements. - // This is needed even if "updateVisibleElements()" is called, as it - // could "short circuit" before reaching the point where the wrapper - // position is updated. + // This is needed even if "updateVisibleElements()" is called later, + // as it could "short circuit" before reaching the point where the + // wrapper position is updated. if (this._$firstVisibleElement) { this._$wrapper._top += wrapperHeightDifference; this._$wrapper.css('top', this._$wrapper._top); } - - this.updateVisibleElements(); - }, - - appendElementEnd: function() { - this._loadNextElements( - this._$firstAppendedElement, - this._$lastAppendedElement, - this._appendedElementsBuffer - ); - - delete this._appendedElementsBuffer; - - this.updateVisibleElements(); }, _loadNextElements: function($firstElementToLoad, $lastElementToLoad, elementsBuffer) { -- cgit v1.2.3