Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(repeater) resize scroller correctly,
Browse files Browse the repository at this point in the history
There was some recursion inside VirtualRepeatController.prototype.virtualRepeatUpdate_ which was causing the scroller not to shrink properly when items were removed.

Fixes #5027. Closes #5031.

In addition, in onDemand mode an undefined length would stop the container from sizing correctly. An undefined length will now be changed to zero.#
It could also possibly fix #4950 and may be a better solution than #5009, but I haven't yet tested it against that issue.#
fixes #5027#   (use "git push" to publish your local commits)
  • Loading branch information
colinskow authored and ThomasBurleson committed Oct 8, 2015
1 parent 88952d6 commit b22ab96
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,21 +574,21 @@ VirtualRepeatController.prototype.getItemSize = function() {
* @private
*/
VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItems) {
var itemsLength = items ? items.length : 0;
var itemsLength = items && items.length || 0;
var lengthChanged = false;

if (itemsLength !== this.itemsLength) {
lengthChanged = true;
this.itemsLength = itemsLength;
}

// If the number of items shrank, scroll up to the top.
if (this.items && itemsLength < this.items.length && this.container.getScrollOffset() !== 0) {
this.items = items;
this.container.resetScroll();
return;
}

if (itemsLength !== this.itemsLength) {
lengthChanged = true;
this.itemsLength = itemsLength;
}

this.items = items;
if (items !== oldItems || lengthChanged) {
this.updateIndexes_();
Expand Down
68 changes: 68 additions & 0 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,74 @@ describe('<md-virtual-repeat>', function() {
expect(getRepeated().length).toBe(numItemRenderers);
});

it('should resize the scroller correctly when item length changes (vertical)', function() {
scope.items = createItems(200);
createRepeater();
scope.$apply();
$$rAF.flush();
expect(sizer[0].offsetHeight).toBe(200 * ITEM_SIZE);

// Scroll down half way
scroller[0].scrollTop = 100 * ITEM_SIZE;
scroller.triggerHandler('scroll');
scope.$apply();
$$rAF.flush();

// Remove some items
scope.items = createItems(20);
scope.$apply();
$$rAF.flush();
expect(scroller[0].scrollTop).toBe(0);
expect(sizer[0].offsetHeight).toBe(20 * ITEM_SIZE);

// Scroll down half way
scroller[0].scrollTop = 10 * ITEM_SIZE;
scroller.triggerHandler('scroll');
scope.$apply();
$$rAF.flush();

// Add more items
scope.items = createItems(250);
scope.$apply();
$$rAF.flush();
expect(scroller[0].scrollTop).toBe(100);
expect(sizer[0].offsetHeight).toBe(250 * ITEM_SIZE);
});

it('should resize the scroller correctly when item length changes (horizontal)', function() {
container.attr({'md-orient-horizontal': ''});
scope.items = createItems(200);
createRepeater();
scope.$apply();
$$rAF.flush();
expect(sizer[0].offsetWidth).toBe(200 * ITEM_SIZE);

// Scroll right half way
scroller[0].scrollLeft = 100 * ITEM_SIZE;
scroller.triggerHandler('scroll');
scope.$apply();
$$rAF.flush();

// Remove some items
scope.items = createItems(20);
scope.$apply();
$$rAF.flush();
expect(scroller[0].scrollLeft).toBe(0);
expect(sizer[0].offsetWidth).toBe(20 * ITEM_SIZE);

// Scroll right half way
scroller[0].scrollLeft = 10 * ITEM_SIZE;
scroller.triggerHandler('scroll');
scope.$apply();
$$rAF.flush();

// Add more items
scope.items = createItems(250);
scope.$apply();
$$rAF.flush();
expect(sizer[0].offsetWidth).toBe(250 * ITEM_SIZE);
});

it('should update topIndex when scrolling', function() {
container.attr({'md-top-index': 'topIndex'});
scope.items = createItems(NUM_ITEMS);
Expand Down

0 comments on commit b22ab96

Please sign in to comment.