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

fix(virtualRepeat): Add ability for container to resize after creation #5561

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
37 changes: 25 additions & 12 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var MAX_ELEMENT_SIZE = 1533917;
var NUM_EXTRA = 3;

/** @ngInject */
function VirtualRepeatContainerController($$rAF, $parse, $scope, $element, $attrs) {
function VirtualRepeatContainerController($$rAF, $parse, $window, $scope, $element, $attrs) {
this.$scope = $scope;
this.$element = $element;
this.$attrs = $attrs;
Expand Down Expand Up @@ -126,17 +126,30 @@ function VirtualRepeatContainerController($$rAF, $parse, $scope, $element, $attr
this.sizer = this.scroller.getElementsByClassName('md-virtual-repeat-sizer')[0];
this.offsetter = this.scroller.getElementsByClassName('md-virtual-repeat-offsetter')[0];

$$rAF(angular.bind(this, this.updateSize));

// TODO: Come up with a more robust (But hopefully also quick!) way of
// detecting that we're not visible.
if ($attrs.ngHide) {
$scope.$watch($attrs.ngHide, angular.bind(this, function(hidden) {
if (!hidden) {
$$rAF(angular.bind(this, this.updateSize));
}
}));
}
// $$rAF(angular.bind(this, this.updateSize));
//
// // TODO: Come up with a more robust (But hopefully also quick!) way of
// // detecting that we're not visible.
// if ($attrs.ngHide) {
// $scope.$watch($attrs.ngHide, angular.bind(this, function(hidden) {
// if (!hidden) {
// $$rAF(angular.bind(this, this.updateSize));
// }
// }));
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out code and just leave the TODO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to remove that and forgot. TODO is no longer relevant.


var boundUpdateSize = angular.bind(this, this.updateSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a comment to explain what's going on here (and why it needs an animation frame).

$$rAF(function() {
boundUpdateSize();

var jWindow = angular.element($window);
jWindow.on('resize', boundUpdateSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be debounced

$scope.$on('$destroy', function() {
jWindow.off('resize', boundUpdateSize);
});

$scope.$on('$md-resize', boundUpdateSize);
});
}


Expand Down
31 changes: 29 additions & 2 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ describe('<md-virtual-repeat>', function() {
' style="height: 10px; width: 10px; box-sizing: border-box;">' +
' {{i}} {{$index}}' +
'</div>';
var container, repeater, component, $$rAF, $compile, $document, scope,
var container, repeater, component, $$rAF, $compile, $document, $window, scope,
scroller, sizer, offsetter;

var NUM_ITEMS = 110,
VERTICAL_PX = 100,
HORIZONTAL_PX = 150,
ITEM_SIZE = 10;

beforeEach(inject(function(_$$rAF_, _$compile_, _$document_, $rootScope, _$material_) {
beforeEach(inject(function(_$$rAF_, _$compile_, _$document_, $rootScope, _$window_, _$material_) {
repeater = angular.element(REPEATER_HTML);
container = angular.element(CONTAINER_HTML).append(repeater);
component = null;
$$rAF = _$$rAF_;
$material = _$material_;
$compile = _$compile_;
$document = _$document_;
$window = _$window_;
scope = $rootScope.$new();
scope.startIndex = 0;
scroller = null;
Expand Down Expand Up @@ -520,6 +521,32 @@ describe('<md-virtual-repeat>', function() {
expect(scroller[0].scrollTop).toBe(25 * ITEM_SIZE);
});

it('should recheck container size on window resize', function() {
scope.items = createItems(100);
createRepeater();
// Expect 13 children (10 + 3 extra).
expect(offsetter.children().length).toBe(13);

container.css('height', '400px');
angular.element($window).triggerHandler('resize');

// Expect 43 children (40 + 3 extra).
expect(offsetter.children().length).toBe(43);
});

it('should recheck container size on $md-resize scope event', function() {
scope.items = createItems(100);
createRepeater();
// Expect 13 children (10 + 3 extra).
expect(offsetter.children().length).toBe(13);

container.css('height', '400px');
scope.$parent.$broadcast('$md-resize');

// Expect 43 children (40 + 3 extra).
expect(offsetter.children().length).toBe(43);
});

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