bug: native scrolling broken on ios because event.preventDefault() being called on drag gestures by default when inst.options.prevent_default_directions is empty
Type: bug
Platform: ios 8 webview
@perrygovier (and the rest of the team but the commit below is Perry's)The current nightly build breaks native scrolling in ion-contents when they don't have an ion-list or an ion-scroll wrapping the contents. The view is not scrollable.
To reproduce build a project with the code ~~in this codepen: http://codepen.io/jskrzypek/pen/oXpPmG~~ in this repo: https://github.com/jskrzypek/4008-preventDefault and then follow the instructions on the readme (or below).
I think this commit introduced the bug: https://github.com/driftyco/ionic/commit/56ab0f2b5d7f5bd35ed6252749cfa1867c3b0f6a, it's on line https://github.com/driftyco/ionic/blob/master/js/utils/gestures.js#L1174.
It seems this is more likely what was meant (and fixes the bug):
// Prevent gestures that are not intended for this event handler from firing subsequent times
- if (inst.options.prevent_default_directions.length === 0
- || inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
+ if (inst.options.prevent_default_directions.length > 0
+ && inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
ev.srcEvent.preventDefault();
Fix submitted as a PR.
This bug seems to be present on android as well as iOS, which makes sense given that it's coming from the drag gesture code and not from platform-specific code or implementations.
See #4022 for documentation of the issue on Android 4.4 webview
Just tested this on ios and android. Seems to be working fine for me with 1.0.1. Is there any other information you could provide? I can't seem to replicate it.
@mhartington I'll be in the office tomorrow and push up a repo. I'm happy to help you reproduce on Monday.
I'm having trouble replicating the problem. Fixing the code the way I mentioned fixes the bug we see in our app, but I am having trouble recreating the issue in a demonstration app. I will update once I manage that.
Ok I got it. (The problem with replication was happening because of some very strange behaviour when the html template from my pen is in the state passed to $urlRouterProvider.otherwise(). But I'll document to that later. Also I cannot replicate the error on my android device, but it looks like the same thing that is blocking the bug from the $urlRouterProvider might be (incorrectly) preventing the error from happening on android.)
Here is a repo that demonstrates the bug we see: https://github.com/jskrzypek/4008-preventDefault.
Shameless plug side-note: It's built with
generator-mcflyour yeoman generator that gives you a pre-wired gulp & browserify build system for angular apps and options to automatically integrate ionic.
Steps to reproduce:
-
Clone the repo &
cdinto the directorygit clone https://github.com/jskrzypek/4008-preventDefault#master && cd 4008-preventDefault -
Install npm & bower dependencies
npm install && bower install -
Use git to apply a patch to stick some
console.logmessages on theDraghandlergit apply ionic.js.patch -
Use our gulp task to build the distributed code (using browserify)
gulp dist -
cdinto the directory where the ionic project sitscd dist/app/dev -
Add platforms
ionic platform add android ios -
Run/build for either platform with
ionic run .... Note that I am currently only able to reproduce this bug in ios. -
Attach your console (safari developer view for
iosor chrome://inspect#devices forandroid) -
In the tab labeled
ion-listyou should be able to scroll fine via dragging, and should see messages logged by the drag handler from our patch. -
Switch to the tab labeled
ion-content. On ios you will not be able to scroll via dragging, but messages will be logged to the console showing us that theev.preventDefault()method is being called on these legitimate drag events. -
Click the red heart in the upper-right corner. This will run the
drag-handler.jsscript that swaps out the current version of the handler for the one with my suggested fixes. -
Now you can scroll just fine in the
ion-contenttab andev.preventDefault()is not being called when it shouldn't be.
@mhartington Just letting you know that the repo is live with the bug replication. The other issue that was blocking it, I'm not really sure how I should start documenting...
For me it is working on android devices, but still not scrolling on ios
http://forum.ionicframework.com/t/overflow-scroll-true-ios-scrolling-not-working/28007
@jskrzypek i checked your repo out and tested it with IOS and i love your red heart but I have no idea what it does. It would fix my Problem.
+1
@Gunbit All that button does is run a script that replaces ionic.Gestures.gestures.Drag.handler with a nearly identical function that has this change in it:
// Prevent gestures that are not intended for this event handler from firing subsequent times
- if (inst.options.prevent_default_directions.length === 0
- || inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
+ if (inst.options.prevent_default_directions.length > 0
+ && inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
ev.srcEvent.preventDefault();
Those lines are found here: https://github.com/driftyco/ionic/blob/master/js/utils/gestures.js#L1174-L1175 Thanks should go to @ghaiat for locating the bug :smile:
A workaround to implement this fix would be to add the following run block. You might put also want to wrap it in a deviceready or $ionicPlatform.ready() listener to make sure this happens after ionic is attached to the window.
.run(function() {
ionic.Gestures.gestures.Drag.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') {
// Prevent gestures that are not intended for this event handler from firing subsequent times
if(inst.options.prevent_default_directions.length > 0 && inst.options.prevent_default_directions.indexOf(ev.direction) != -1) {
ev.srcEvent.preventDefault();
}
this.preventedFirstMove = true;
}
// current gesture isnt drag, but dragged is true
// this means an other gesture is busy. now call dragend
if(ionic.Gestures.detection.current.name != this.name && this.triggered) {
inst.trigger(this.name + 'end', ev);
this.triggered = false;
return;
}
// max touches
if(inst.options.drag_max_touches > 0 &&
ev.touches.length > inst.options.drag_max_touches) {
return;
}
switch(ev.eventType) {
case ionic.Gestures.EVENT_START:
this.triggered = false;
break;
case ionic.Gestures.EVENT_MOVE:
// when the distance we moved is too small we skip this gesture
// or we can be already in dragging
if(ev.distance < inst.options.drag_min_distance &&
ionic.Gestures.detection.current.name != this.name) {
return;
}
// we are dragging!
if(ionic.Gestures.detection.current.name != this.name) {
ionic.Gestures.detection.current.name = this.name;
if(inst.options.correct_for_drag_min_distance) {
// When a drag is triggered, set the event center to drag_min_distance pixels from the original event center.
// Without this correction, the dragged distance would jumpstart at drag_min_distance pixels instead of at 0.
// It might be useful to save the original start point somewhere
var factor = Math.abs(inst.options.drag_min_distance / ev.distance);
ionic.Gestures.detection.current.startEvent.center.pageX += ev.deltaX * factor;
ionic.Gestures.detection.current.startEvent.center.pageY += ev.deltaY * factor;
// recalculate event data using new start point
ev = ionic.Gestures.detection.extendEventData(ev);
}
}
// lock drag to axis?
if(ionic.Gestures.detection.current.lastEvent.drag_locked_to_axis || (inst.options.drag_lock_to_axis && inst.options.drag_lock_min_distance <= ev.distance)) {
ev.drag_locked_to_axis = true;
}
var last_direction = ionic.Gestures.detection.current.lastEvent.direction;
if(ev.drag_locked_to_axis && last_direction !== ev.direction) {
// keep direction on the axis that the drag gesture started on
if(ionic.Gestures.utils.isVertical(last_direction)) {
ev.direction = (ev.deltaY < 0) ? ionic.Gestures.DIRECTION_UP : ionic.Gestures.DIRECTION_DOWN;
} else {
ev.direction = (ev.deltaX < 0) ? ionic.Gestures.DIRECTION_LEFT : ionic.Gestures.DIRECTION_RIGHT;
}
}
// first time, trigger dragstart event
if(!this.triggered) {
inst.trigger(this.name + 'start', ev);
this.triggered = true;
}
// trigger normal event
inst.trigger(this.name, ev);
// direction event, like dragdown
inst.trigger(this.name + ev.direction, ev);
// block the browser events
if((inst.options.drag_block_vertical && ionic.Gestures.utils.isVertical(ev.direction)) ||
(inst.options.drag_block_horizontal && !ionic.Gestures.utils.isVertical(ev.direction))) {
ev.preventDefault();
}
break;
case ionic.Gestures.EVENT_END:
// trigger dragend
if(this.triggered) {
inst.trigger(this.name + 'end', ev);
}
this.triggered = false;
break;
}
};
})
@mhartington Any progress on checking this out?
Merged this into our 1.0.2 branch e10b5d23c9cb3e04c6374574303e55aef6aefee4
@mhartington Awesome, thanks!
@jskrzypek Your fixed have a side effects(ion-fresher can only work once)
@huip can you elaborate what you mean
@jskrzypek I mean if ion-content contains ion-fresher tag, when pull down to refresh item-list,the pull-down events can not work.
I just updated the ionic lib to 1.0.1 and got the same problem on Android. :/ How do I downgrade to ionic 1.0.0 ?
I tested on android v5.0.2 ...i dont know if the problem is because of the version or because I use the pull to refresh function.. any ideas?