Skip to content

Commit ea289b8

Browse files
committed
fix(ionicScrollDelegate): trigger resize before scrolling to top/bottom
Closes #522
1 parent 0fe4486 commit ea289b8

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

Diff for: js/ext/angular/src/service/delegates/ionicScrollDelegate.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ angular.module('ionic.ui.service.scrollDelegate', [])
2626

2727
if(ionic.DomUtil.rectContains(e.gesture.touches[0].pageX, e.gesture.touches[0].pageY, bounds.left, bounds.top, bounds.left + bounds.width, bounds.top + 20)) {
2828
_this.scrollTop();
29-
}
29+
}
3030
}, element[0]);
3131
},
3232

@@ -47,6 +47,14 @@ angular.module('ionic.ui.service.scrollDelegate', [])
4747
* $scope {Scope} the scope to register and listen for events
4848
*/
4949
register: function($scope, $element) {
50+
51+
function scrollViewResize() {
52+
// Run the resize after this digest
53+
return $timeout(function() {
54+
$scope.$parent.scrollView && $scope.$parent.scrollView.resize();
55+
});
56+
}
57+
5058
$element.bind('scroll', function(e) {
5159
$scope.onScroll({
5260
event: e,
@@ -56,10 +64,7 @@ angular.module('ionic.ui.service.scrollDelegate', [])
5664
});
5765

5866
$scope.$parent.$on('scroll.resize', function(e) {
59-
// Run the resize after this digest
60-
$timeout(function() {
61-
$scope.$parent.scrollView && $scope.$parent.scrollView.resize();
62-
});
67+
scrollViewResize();
6368
});
6469

6570
// Called to stop refreshing on the scroll view
@@ -73,14 +78,18 @@ angular.module('ionic.ui.service.scrollDelegate', [])
7378
* @param animate {boolean} whether to animate or just snap
7479
*/
7580
$scope.$parent.$on('scroll.scrollTop', function(e, animate) {
76-
$scope.$parent.scrollView && $scope.$parent.scrollView.scrollTo(0, 0, animate === false ? false : true);
81+
scrollViewResize().then(function() {
82+
$scope.$parent.scrollView && $scope.$parent.scrollView.scrollTo(0, 0, animate === false ? false : true);
83+
});
7784
});
7885
$scope.$parent.$on('scroll.scrollBottom', function(e, animate) {
79-
var sv = $scope.$parent.scrollView;
80-
var max;
81-
if(!sv) { return; }
82-
max = sv.getScrollMax();
83-
sv.scrollTo(0, max.top, animate === false ? false : true);
86+
scrollViewResize().then(function() {
87+
var sv = $scope.$parent.scrollView;
88+
if (sv) {
89+
var max = sv.getScrollMax();
90+
sv.scrollTo(0, max.top, animate === false ? false : true);
91+
}
92+
});
8493
});
8594
}
8695
};

Diff for: js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js

+35-21
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,59 @@ describe('Ionic ScrollDelegate Service', function() {
2727
expect(sv).not.toBe(undefined);
2828
});
2929

30-
it('Should scroll top', function() {
31-
spyOn(del, 'register');
30+
it('should resize', function() {
31+
var scope = rootScope.$new();
32+
var el = compile('<content></content>')(scope);
33+
34+
var sv = del.getScrollView(scope);
35+
spyOn(sv, 'resize');
3236

37+
del.resize();
38+
timeout.flush();
39+
expect(sv.resize).toHaveBeenCalled();
40+
});
41+
42+
it('Should resize & scroll top', function() {
3343
var scope = rootScope.$new();
3444
var el = compile('<content start-y="100"></content>')(scope);
3545

3646
var sv = del.getScrollView(scope);
47+
spyOn(sv, 'resize');
3748

38-
var v = sv.getValues();
49+
expect(sv.getValues().top).toBe(100);
3950

40-
expect(v.top).toBe(100);
41-
42-
del.scrollTop();
51+
del.scrollTop(false);
52+
timeout.flush();
53+
expect(sv.resize).toHaveBeenCalled();
4354

44-
expect(v.top).toBe(100);
55+
expect(sv.getValues().top).toBe(0);
4556
});
4657

47-
/*
48-
it('Should scroll bottom', function() {
49-
spyOn(del, 'register');
50-
58+
it('Should resize & scroll top', function() {
5159
var scope = rootScope.$new();
52-
var el = compile('<content start-y="100"><div style="height:1000px; width:100px;"></div></content>')(scope);
60+
var el = compile('<content start-y="100"></content>')(scope);
5361

5462
var sv = del.getScrollView(scope);
55-
timeout.flush();
56-
sv.resize();
63+
spyOn(sv, 'resize');
5764

58-
var v = sv.getValues();
65+
expect(sv.getValues().top).toBe(100);
5966

67+
del.scrollBottom(false);
68+
timeout.flush();
69+
expect(sv.resize).toHaveBeenCalled();
6070

61-
expect(v.top).toBe(100);
71+
expect(sv.getValues().top).toBe(sv.getScrollMax().top);
72+
});
6273

74+
it('should finish refreshing', function() {
75+
var scope = rootScope.$new();
76+
var el = compile('<content start-y="100"></content>')(scope);
6377

64-
console.log(sv.getScrollMax());
65-
del.scrollBottom();
78+
var sv = del.getScrollView(scope);
79+
spyOn(sv, 'finishPullToRefresh');
6680

67-
expect(v.top).toBe(100);
68-
});
69-
*/
81+
del.finishRefreshing(scope);
82+
expect(sv.finishPullToRefresh).toHaveBeenCalled();
83+
});
7084
});
7185

0 commit comments

Comments
 (0)