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

fix(repeater): resize scroller correctly #5031

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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