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

Commit d6d7b08

Browse files
kseamonThomasBurleson
authored andcommitted
fix(virtualRepeat): Add ability for container to resize after creation
Closes #5561.
1 parent 0690e1b commit d6d7b08

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/components/virtualRepeat/virtual-repeater.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var MAX_ELEMENT_SIZE = 1533917;
7878
var NUM_EXTRA = 3;
7979

8080
/** @ngInject */
81-
function VirtualRepeatContainerController($$rAF, $parse, $scope, $element, $attrs) {
81+
function VirtualRepeatContainerController($$rAF, $parse, $window, $scope, $element, $attrs) {
8282
this.$scope = $scope;
8383
this.$element = $element;
8484
this.$attrs = $attrs;
@@ -126,17 +126,30 @@ function VirtualRepeatContainerController($$rAF, $parse, $scope, $element, $attr
126126
this.sizer = this.scroller.getElementsByClassName('md-virtual-repeat-sizer')[0];
127127
this.offsetter = this.scroller.getElementsByClassName('md-virtual-repeat-offsetter')[0];
128128

129-
$$rAF(angular.bind(this, this.updateSize));
130-
131-
// TODO: Come up with a more robust (But hopefully also quick!) way of
132-
// detecting that we're not visible.
133-
if ($attrs.ngHide) {
134-
$scope.$watch($attrs.ngHide, angular.bind(this, function(hidden) {
135-
if (!hidden) {
136-
$$rAF(angular.bind(this, this.updateSize));
137-
}
138-
}));
139-
}
129+
// $$rAF(angular.bind(this, this.updateSize));
130+
//
131+
// // TODO: Come up with a more robust (But hopefully also quick!) way of
132+
// // detecting that we're not visible.
133+
// if ($attrs.ngHide) {
134+
// $scope.$watch($attrs.ngHide, angular.bind(this, function(hidden) {
135+
// if (!hidden) {
136+
// $$rAF(angular.bind(this, this.updateSize));
137+
// }
138+
// }));
139+
// }
140+
141+
var boundUpdateSize = angular.bind(this, this.updateSize);
142+
$$rAF(function() {
143+
boundUpdateSize();
144+
145+
var jWindow = angular.element($window);
146+
jWindow.on('resize', boundUpdateSize);
147+
$scope.$on('$destroy', function() {
148+
jWindow.off('resize', boundUpdateSize);
149+
});
150+
151+
$scope.$on('$md-resize', boundUpdateSize);
152+
});
140153
}
141154

142155

src/components/virtualRepeat/virtual-repeater.spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ describe('<md-virtual-repeat>', function() {
1313
' style="height: 10px; width: 10px; box-sizing: border-box;">' +
1414
' {{i}} {{$index}}' +
1515
'</div>';
16-
var container, repeater, component, $$rAF, $compile, $document, scope,
16+
var container, repeater, component, $$rAF, $compile, $document, $window, scope,
1717
scroller, sizer, offsetter;
1818

1919
var NUM_ITEMS = 110,
2020
VERTICAL_PX = 100,
2121
HORIZONTAL_PX = 150,
2222
ITEM_SIZE = 10;
2323

24-
beforeEach(inject(function(_$$rAF_, _$compile_, _$document_, $rootScope, _$material_) {
24+
beforeEach(inject(function(_$$rAF_, _$compile_, _$document_, $rootScope, _$window_, _$material_) {
2525
repeater = angular.element(REPEATER_HTML);
2626
container = angular.element(CONTAINER_HTML).append(repeater);
2727
component = null;
2828
$$rAF = _$$rAF_;
2929
$material = _$material_;
3030
$compile = _$compile_;
3131
$document = _$document_;
32+
$window = _$window_;
3233
scope = $rootScope.$new();
3334
scope.startIndex = 0;
3435
scroller = null;
@@ -520,6 +521,32 @@ describe('<md-virtual-repeat>', function() {
520521
expect(scroller[0].scrollTop).toBe(25 * ITEM_SIZE);
521522
});
522523

524+
it('should recheck container size on window resize', function() {
525+
scope.items = createItems(100);
526+
createRepeater();
527+
// Expect 13 children (10 + 3 extra).
528+
expect(offsetter.children().length).toBe(13);
529+
530+
container.css('height', '400px');
531+
angular.element($window).triggerHandler('resize');
532+
533+
// Expect 43 children (40 + 3 extra).
534+
expect(offsetter.children().length).toBe(43);
535+
});
536+
537+
it('should recheck container size on $md-resize scope event', function() {
538+
scope.items = createItems(100);
539+
createRepeater();
540+
// Expect 13 children (10 + 3 extra).
541+
expect(offsetter.children().length).toBe(13);
542+
543+
container.css('height', '400px');
544+
scope.$parent.$broadcast('$md-resize');
545+
546+
// Expect 43 children (40 + 3 extra).
547+
expect(offsetter.children().length).toBe(43);
548+
});
549+
523550
/**
524551
* Facade to access transform properly even when jQuery is used;
525552
* since jQuery's css function is obtaining the computed style (not wanted)

0 commit comments

Comments
 (0)