From cb957fd7abbc8576411b29369621cfca311d9dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Wed, 14 Nov 2018 15:00:48 +0100 Subject: Add helper method to scroll to a given element 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 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/js/views/virtuallist.js b/js/views/virtuallist.js index 84ceae4f0..f55fb673f 100644 --- a/js/views/virtuallist.js +++ b/js/views/virtuallist.js @@ -884,6 +884,57 @@ this._$wrapper.appendTo(this._$container); }, + /** + * Scroll the list to the given element. + * + * The element will be aligned with the top of the list (or as far as + * possible, in case the element is at the bottom). + * + * @param {jQuery} $element the element of the list to scroll to. + */ + scrollTo: function($element) { + if (!this._isLoaded($element)) { + return; + } + + this._$container.scrollTop($element._top); + + // The visible elements are updated when the scroll event is + // handled. However, as the scroll event is asynchronous, it is not + // guaranteed that it will be handled before this method returns; as + // the caller could expect that the visibility of elements is + // updated when scrolling programatically this must be explicitly + // done. + // Note that, although the event is handled asynchronously (and in + // some cases several scrolls can be merged in a single event) the + // value returned by scrollTop() is always the expected one + // immediately after setting it with scrollTop(value). + this.updateVisibleElements(); + }, + + /** + * Returns whether the given element is loaded or not. + * + * @param {jQuery} $element the element to check. + * @return true if the element is loaded, false otherwise. + */ + _isLoaded: function($element) { + if (!this._$firstLoadedElement || !this._$lastLoadedElement) { + return false; + } + + var $currentElement = this._$firstLoadedElement; + while ($currentElement !== this._$lastLoadedElement._next) { + if ($currentElement === $element) { + return true; + } + + $currentElement = $currentElement._next; + } + + return false; + }, + }; OCA.SpreedMe.Views.VirtualList = VirtualList; -- cgit v1.2.3