Skip to content

Commit

Permalink
fix(ionicScrollDelegate): tapScrollToTop won't fire for button tap
Browse files Browse the repository at this point in the history
Closes #557
  • Loading branch information
ajoslin committed Feb 17, 2014
1 parent 17cc040 commit 70d9524
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions js/ext/angular/src/service/delegates/ionicScrollDelegate.js
Expand Up @@ -23,15 +23,24 @@ angular.module('ionic.ui.service.scrollDelegate', [])
anchorScroll: function(animate) {
$rootScope.$broadcast('scroll.anchorScroll', animate);
},
tapScrollToTop: function(element) {
tapScrollToTop: function(element, animate) {
var _this = this;
if (!angular.isDefined(animate)) {
animate = true;
}

ionic.on('tap', function(e) {
var target = e.target;
//Don't scroll to top for a button click
if (ionic.DomUtil.getParentOrSelfWithClass(target, 'button')) {
return;
}

var el = element[0];
var bounds = el.getBoundingClientRect();

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)) {
_this.scrollTop();
_this.scrollTop(animate);
}
}, element[0]);
},
Expand Down
32 changes: 32 additions & 0 deletions js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js
Expand Up @@ -35,6 +35,38 @@ describe('Ionic ScrollDelegate Service', function() {
testWithAnimate(false);
function testWithAnimate(animate) {
describe('with animate='+animate, function() {

it('should tapScrollToTop', function() {
var scope = rootScope.$new();
var el = angular.element('<div>' +
'<div class="not-button"></div>' +
'<div class="button"></div>' +
'</div>');
//ionic.trigger() REALLY doesnt want to work with tap,
//so we just mock on to catch the callback and use that...
var callback;
spyOn(ionic, 'on').andCallFake(function(eventName, cb) {
callback = cb;
});
del.tapScrollToTop(el, animate);

spyOn(del, 'scrollTop');
//Don't test the rectContains part, too much to mock
spyOn(ionic.DomUtil, 'rectContains').andCallFake(function() {
return true;
});
callback({
target: el[0].querySelector('.not-button'),
gesture:{ touches:[{}] }
});
expect(del.scrollTop).toHaveBeenCalledWith(animate);

del.scrollTop.reset();
callback({
target: el[0].querySelector('.button')
});
expect(del.scrollTop).not.toHaveBeenCalled();
});
it('should resize & scroll top', function() {
var scope = rootScope.$new();
var el = compile('<content start-y="100"></content>')(scope);
Expand Down

0 comments on commit 70d9524

Please sign in to comment.