Skip to content

Commit 70d9524

Browse files
committed
fix(ionicScrollDelegate): tapScrollToTop won't fire for button tap
Closes #557
1 parent 17cc040 commit 70d9524

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

js/ext/angular/src/service/delegates/ionicScrollDelegate.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ angular.module('ionic.ui.service.scrollDelegate', [])
2323
anchorScroll: function(animate) {
2424
$rootScope.$broadcast('scroll.anchorScroll', animate);
2525
},
26-
tapScrollToTop: function(element) {
26+
tapScrollToTop: function(element, animate) {
2727
var _this = this;
28+
if (!angular.isDefined(animate)) {
29+
animate = true;
30+
}
2831

2932
ionic.on('tap', function(e) {
33+
var target = e.target;
34+
//Don't scroll to top for a button click
35+
if (ionic.DomUtil.getParentOrSelfWithClass(target, 'button')) {
36+
return;
37+
}
38+
3039
var el = element[0];
3140
var bounds = el.getBoundingClientRect();
3241

3342
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)) {
34-
_this.scrollTop();
43+
_this.scrollTop(animate);
3544
}
3645
}, element[0]);
3746
},

js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js

+32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ describe('Ionic ScrollDelegate Service', function() {
3535
testWithAnimate(false);
3636
function testWithAnimate(animate) {
3737
describe('with animate='+animate, function() {
38+
39+
it('should tapScrollToTop', function() {
40+
var scope = rootScope.$new();
41+
var el = angular.element('<div>' +
42+
'<div class="not-button"></div>' +
43+
'<div class="button"></div>' +
44+
'</div>');
45+
//ionic.trigger() REALLY doesnt want to work with tap,
46+
//so we just mock on to catch the callback and use that...
47+
var callback;
48+
spyOn(ionic, 'on').andCallFake(function(eventName, cb) {
49+
callback = cb;
50+
});
51+
del.tapScrollToTop(el, animate);
52+
53+
spyOn(del, 'scrollTop');
54+
//Don't test the rectContains part, too much to mock
55+
spyOn(ionic.DomUtil, 'rectContains').andCallFake(function() {
56+
return true;
57+
});
58+
callback({
59+
target: el[0].querySelector('.not-button'),
60+
gesture:{ touches:[{}] }
61+
});
62+
expect(del.scrollTop).toHaveBeenCalledWith(animate);
63+
64+
del.scrollTop.reset();
65+
callback({
66+
target: el[0].querySelector('.button')
67+
});
68+
expect(del.scrollTop).not.toHaveBeenCalled();
69+
});
3870
it('should resize & scroll top', function() {
3971
var scope = rootScope.$new();
4072
var el = compile('<content start-y="100"></content>')(scope);

0 commit comments

Comments
 (0)