diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-11-14 15:00:48 +0100 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-11-20 12:18:15 +0100 |
commit | cb957fd7abbc8576411b29369621cfca311d9dd7 (patch) | |
tree | 25b2d1af7e8b7569dcf7720199ce87c4507da9c5 | |
parent | 9482deadac7be26ca644574bcc90f59c2faecfe3 (diff) |
Add helper method to scroll to a given element
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
-rw-r--r-- | js/views/virtuallist.js | 51 |
1 files changed, 51 insertions, 0 deletions
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; |