Skip to content

Commit

Permalink
Add scrollToItem method
Browse files Browse the repository at this point in the history
  • Loading branch information
indolering authored and Emmanuel Garcia committed Jun 14, 2016
1 parent b99a2aa commit 43eea45
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"list",
"virtual-list"
],
"version": "1.3.2",
"version": "1.3.3",
"homepage": "https://github.com/PolymerElements/iron-list",
"authors": [
"The Polymer Authors"
Expand Down
16 changes: 15 additions & 1 deletion iron-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -1033,12 +1033,14 @@
if (!el || el._templateInstance.__key__ !== key) {
return;
}

if (dot >= 0) {
path = this.as + '.' + path.substring(dot+1);
el._templateInstance.notifyPath(path, value, true);
} else {
el._templateInstance[this.as] = value;
}

},

/**
Expand Down Expand Up @@ -1340,15 +1342,27 @@
this._scrollHeight = this._estScrollHeight;
}
},

/**
* Scroll to a specific item in the virtual list regardless
* of the physical items in the DOM tree.
*
* @method scrollToItem
* @param {(Object)} item The item to be scrolled to
*/
scrollToItem: function(item){
return this.scrollToIndex(this.items.indexOf(item));
},

/**
* Scroll to a specific index in the virtual list regardless
* of the physical items in the DOM tree.
*
* @method scrollToIndex
* @param {number} idx The index of the item
*/
scrollToIndex: function(idx) {
if (typeof idx !== 'number') {
if (typeof idx !== 'number' || idx < 0 || idx > this.items.length - 1) {
return;
}

Expand Down
29 changes: 29 additions & 0 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@
}, 100);
});

test('scroll to item', function(done) {
list.items = buildDataSet(100);

setTimeout(function() {
list.scrollToItem(list.items[30]);
assert.equal(list.firstVisibleIndex, 30);

list.scrollToItem(list.items[0]);
assert.equal(list.firstVisibleIndex, 0);

var rowHeight = getFirstItemFromList(list).offsetHeight;
var viewportHeight = list.offsetHeight;
var itemsPerViewport = Math.floor(viewportHeight/rowHeight);

list.scrollToItem(list.items[99]);
assert.equal(list.firstVisibleIndex, list.items.length - itemsPerViewport);

// make the height of the viewport same as the height of the row
// and scroll to the last item
list.style.height = list._physicalItems[0].offsetHeight + 'px';

setTimeout(function() {
list.scrollToItem(list.items[99]);
assert.equal(list.firstVisibleIndex, 99);
done();
}, 100);
}, 100);
});

test('scroll to top', function(done) {
list.items = buildDataSet(100);
Polymer.dom.flush();
Expand Down

0 comments on commit 43eea45

Please sign in to comment.