Skip to content

Commit

Permalink
fix(scrolling): Prevent gestures from breaking native scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygovier committed Mar 19, 2015
1 parent 7bf1207 commit e917cae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions js/angular/directive/sideMenuContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ function($timeout, $ionicGesture, $window) {

// add gesture handlers
var gestureOpts = { stop_browser_behavior: false };
if (ionic.DomUtil.getParentOrSelfWithClass($element[0],'overflow-scroll')) {
gestureOpts.prevent_default_directions = ['left','right'];
}
var contentTapGesture = $ionicGesture.on('tap', onContentTap, $element, gestureOpts);
var dragRightGesture = $ionicGesture.on('dragright', onDragX, $element, gestureOpts);
var dragLeftGesture = $ionicGesture.on('dragleft', onDragX, $element, gestureOpts);
Expand Down
9 changes: 6 additions & 3 deletions js/utils/gestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,16 +1160,19 @@
drag_lock_to_axis : false,
// drag lock only kicks in when distance > drag_lock_min_distance
// This way, locking occurs only when the distance has become large enough to reliably determine the direction
drag_lock_min_distance : 25
drag_lock_min_distance : 25,
// prevent default if the gesture is going the given direction
prevent_default_directions : []
},
triggered: false,
handler: function dragGesture(ev, inst) {

if (ev.srcEvent.type == 'touchstart' || ev.srcEvent.type == 'touchend') {
this.preventedFirstMove = false;

} else if (!this.preventedFirstMove && ev.srcEvent.type == 'touchmove') {
ev.srcEvent.preventDefault();
if (inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
ev.srcEvent.preventDefault();
}

This comment has been minimized.

Copy link
@MartinMa

MartinMa Apr 1, 2015

I am experiencing a regression here concerning issue 1729. On Android 4.4.2 drag gestures will be canceled immediately, unless I pass {prevent_default_directions = ['left', 'right']} as fourth argument to $ionicGesture.on. Although I don't use native scrolling.

I think preventDefault(); should be the default, if 'overflow-scroll' isn't present? With the current behavior it breaks drag gestures when using Ionic scroll.

this.preventedFirstMove = true;
}

Expand Down
10 changes: 8 additions & 2 deletions js/views/listView.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,19 @@
self.onRefreshOpening = opts.onRefreshOpening || function() {};
self.onRefreshHolding = opts.onRefreshHolding || function() {};

var gestureOpts = {};
// don't prevent native scrolling
if (ionic.DomUtil.getParentOrSelfWithClass(self.el,'overflow-scroll')) {
gestureOpts.prevent_default_directions = ['left','right'];
}

window.ionic.onGesture('release', function(e) {
self._handleEndDrag(e);
}, self.el);
}, self.el, gestureOpts);

window.ionic.onGesture('drag', function(e) {
self._handleDrag(e);
}, self.el);
}, self.el, gestureOpts);
// Start the drag states
self._initDrag();
},
Expand Down
16 changes: 16 additions & 0 deletions test/unit/angular/directive/list.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ describe('ionList directive', function() {
expect(el.children().html()).toBe('<hello></hello>');
});

it('should provide gesture prevent_default_directions if native scrolling', function() {
spyOn(window.ionic,'onGesture');

var el = setup('', '<hello></hello>');
flush();
expect(window.ionic.onGesture).toHaveBeenCalled();
args = window.ionic.onGesture.mostRecentCall.args;
expect(args[3]).toEqual({});

var el = setup('class="overflow-scroll"', '<hello></hello>');
flush();
var gestureOpts = {prevent_default_directions: ['left','right']};
args = window.ionic.onGesture.mostRecentCall.args;
expect(args[3]).toEqual(gestureOpts);
});

it('should give options to listView after init', function() {
var options;
spyOn(ionic.views, 'ListView').andCallFake(function(o) {
Expand Down

0 comments on commit e917cae

Please sign in to comment.