diff --git a/js/ext/angular/src/service/delegates/ionicScrollDelegate.js b/js/ext/angular/src/service/delegates/ionicScrollDelegate.js index 9b2ea289583..e3d6b4232d1 100644 --- a/js/ext/angular/src/service/delegates/ionicScrollDelegate.js +++ b/js/ext/angular/src/service/delegates/ionicScrollDelegate.js @@ -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]); }, diff --git a/js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js b/js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js index 3bf027b0675..33fc4831a39 100644 --- a/js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js +++ b/js/ext/angular/test/service/delegates/ionicScrollDelegate.unit.js @@ -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('
' + + '
' + + '
' + + '
'); + //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('')(scope);