Skip to content

Commit

Permalink
fix(virtualRepeat): Do not scroll past bottom
Browse files Browse the repository at this point in the history
Fixes angular#6279
Might also fix angular#4169
  • Loading branch information
kseamon committed Feb 2, 2016
1 parent 4fb92e3 commit d27de6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,23 +346,23 @@ VirtualRepeatContainerController.prototype.resetScroll = function() {

VirtualRepeatContainerController.prototype.handleScroll_ = function() {
var offset = this.isHorizontal() ? this.scroller.scrollLeft : this.scroller.scrollTop;
if (offset === this.scrollOffset) return;
if (offset === this.scrollOffset || offset > this.scrollSize - this.size) return;

var itemSize = this.repeater.getItemSize();
if (!itemSize) return;

var numItems = Math.max(0, Math.floor(offset / itemSize) - NUM_EXTRA);

var transform = this.isHorizontal() ? 'translateX(' : 'translateY(';
transform += (numItems * itemSize) + 'px)';
var transform = (this.isHorizontal() ? 'translateX(' : 'translateY(') +
(numItems * itemSize) + 'px)';

this.scrollOffset = offset;
this.offsetter.style.webkitTransform = transform;
this.offsetter.style.transform = transform;

if (this.bindTopIndex) {
var topIndex = Math.floor(offset / itemSize);
if (topIndex !== this.topIndex && topIndex < this.repeater.itemsLength) {
if (topIndex !== this.topIndex && topIndex < this.repeater.getItemCount()) {
this.topIndex = topIndex;
this.bindTopIndex.assign(this.$scope, topIndex);
if (!this.$rootScope.$$phase) this.$scope.$digest();
Expand Down Expand Up @@ -627,6 +627,15 @@ VirtualRepeatController.prototype.getItemSize = function() {
};


/**
* Called by the container. Returns the size of a single repeated item.
* @return {?number} Size of a repeated item.
*/
VirtualRepeatController.prototype.getItemCount = function() {
return this.itemsLength;
};


/**
* Updates the order and visible offset of repeated blocks in response to scrolling
* or items updates.
Expand Down
15 changes: 15 additions & 0 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,21 @@ describe('<md-virtual-repeat>', function() {
expect(offsetter.children().length).toBe(3);
}));

it('should not scroll past the bottom', function() {
scope.items = createItems(101);
createRepeater();

scroller[0].scrollTop = ITEM_SIZE * 91;
scroller.triggerHandler('scroll');

expect(getTransform(offsetter)).toBe('translateY(880px)');

scroller[0].scrollTop++;
scroller.triggerHandler('scroll');

expect(getTransform(offsetter)).toBe('translateY(880px)');
});

/**
* Facade to access transform properly even when jQuery is used;
* since jQuery's css function is obtaining the computed style (not wanted)
Expand Down

0 comments on commit d27de6d

Please sign in to comment.