Skip to content

Commit

Permalink
Merge pull request #250 from PolymerElements/issue-243
Browse files Browse the repository at this point in the history
Fix #243
  • Loading branch information
Emmanuel Garcia committed Jun 1, 2016
2 parents d3cef7d + 5342ec5 commit 46cca14
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 60 deletions.
23 changes: 10 additions & 13 deletions iron-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@
if (physicalOffset > this._scrollPosition) {
return this.grid ? vidx - (vidx % this._itemsPerRow) : vidx;
}

// Handle a partially rendered final row in grid mode
if (this.grid && this._virtualCount - 1 === vidx) {
return vidx - (vidx % this._itemsPerRow);
Expand All @@ -639,21 +638,17 @@
if (this._lastVisibleIndexVal === null) {
if (this.grid) {
var lastIndex = this.firstVisibleIndex + this._estRowsInView * this._itemsPerRow - 1;
this._lastVisibleIndexVal = lastIndex > this._virtualCount ? this._virtualCount : lastIndex;
this._lastVisibleIndexVal = Math.min(this._virtualCount, lastIndex);
} else {
var physicalOffset = this._physicalTop;

this._iterateItems(function(pidx, vidx) {
physicalOffset += this._getPhysicalSizeIncrement(pidx);

if(physicalOffset <= this._scrollBottom) {
if (this.grid) {
var lastIndex = vidx - vidx % this._itemsPerRow + this._itemsPerRow - 1;
this._lastVisibleIndexVal = lastIndex > this._virtualCount ? this._virtualCount : lastIndex;
} else {
this._lastVisibleIndexVal = vidx;
}
if (physicalOffset < this._scrollBottom) {
this._lastVisibleIndexVal = vidx;
} else {
// Break _iterateItems
return true;
}
physicalOffset += this._getPhysicalSizeIncrement(pidx);
});
}
}
Expand Down Expand Up @@ -1067,10 +1062,11 @@
this._virtualCount = this.items ? this.items.length : 0;
this._collection = this.items ? Polymer.Collection.get(this.items) : null;
this._physicalIndexForKey = {};
this._firstVisibleIndexVal = null;
this._lastVisibleIndexVal = null;

this._resetScrollPosition(0);
this._removeFocusedItem();

// create the initial physical items
if (!this._physicalItems) {
this._physicalCount = Math.max(1, Math.min(DEFAULT_PHYSICAL_COUNT, this._virtualCount));
Expand All @@ -1081,6 +1077,7 @@
this._physicalStart = 0;

} else if (change.path === 'items.splices') {

this._adjustVirtualIndex(change.value.indexSplices);
this._virtualCount = this.items ? this.items.length : 0;

Expand Down
83 changes: 36 additions & 47 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,55 +81,44 @@
});
});

test('first visible index', function(done) {
test('first visible index', function() {
container.data = buildDataSet(100);
Polymer.dom.flush();
assert.equal(list.firstVisibleIndex, 0);
list.scroll(0, container.itemHeight * 10);
list.fire('scroll');
assert.equal(list.firstVisibleIndex, 10);
list.scroll(0, container.itemHeight * 50);
list.fire('scroll');
assert.equal(list.firstVisibleIndex, 50);
list.scrollToIndex(60);
Polymer.dom.flush();
assert.equal(list.firstVisibleIndex, 60);
list.scrollToIndex(0);
Polymer.dom.flush();
assert.equal(list.firstVisibleIndex, 0);
});

flush(function() {
var setSize = list.items.length;
var rowHeight = container.itemHeight;
var viewportHeight = list.offsetHeight;
var scrollToItem;

function checkFirstVisible() {
assert.equal(list.firstVisibleIndex, scrollToItem);
assert.equal(getFirstItemFromList(list).textContent, scrollToItem);
}

function checkLastVisible() {
var visibleItemsCount = Math.floor(viewportHeight / rowHeight);
assert.equal(list.lastVisibleIndex, scrollToItem + visibleItemsCount - 1);
assert.equal(getLastItemFromList(list).textContent, scrollToItem + visibleItemsCount - 1);
}

function doneScrollDown() {
checkFirstVisible();
checkLastVisible();
scrollToItem = 1;
flush(function() {
simulateScroll({
list: list,
contribution: rowHeight,
target: scrollToItem*rowHeight,
onScrollEnd: doneScrollUp
});
});
}

function doneScrollUp() {
checkFirstVisible();
checkLastVisible();
done();
}
scrollToItem = 50;

simulateScroll({
list: list,
contribution: 50,
target: scrollToItem*rowHeight,
onScrollEnd: doneScrollDown
});

});
test('last visible index', function() {
container.data = buildDataSet(1);
container.itemHeight = 1000;
Polymer.dom.flush();
assert.equal(list.lastVisibleIndex, 0);
container.data = buildDataSet(2);
container.itemHeight = 50;
Polymer.dom.flush();
assert.equal(list.lastVisibleIndex, 1);
container.data = buildDataSet(10);
Polymer.dom.flush();
list.scrollToIndex(8);
Polymer.dom.flush();
assert.equal(list.lastVisibleIndex, 9);
container.itemHeight = 50;
container.data = buildDataSet(100);
Polymer.dom.flush();
list.scroll(0, 100);
list.fire('scroll');
assert.equal(list.lastVisibleIndex, ((list._scrollTop + container.listHeight) / container.itemHeight) - 1);
});

test('scroll to index', function(done) {
Expand Down

0 comments on commit 46cca14

Please sign in to comment.