ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

bug: native scrolling broken on ios because event.preventDefault() being called on drag gestures by default when inst.options.prevent_default_directions is empty

Open jskrzypek opened this issue 11 years ago • 42 comments

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();

jskrzypek avatar Jun 29 '15 21:06 jskrzypek

Fix submitted as a PR.

jskrzypek avatar Jul 01 '15 11:07 jskrzypek

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

jskrzypek avatar Jul 03 '15 13:07 jskrzypek

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 avatar Jul 03 '15 14:07 mhartington

@mhartington I'll be in the office tomorrow and push up a repo. I'm happy to help you reproduce on Monday.

jskrzypek avatar Jul 04 '15 17:07 jskrzypek

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.

jskrzypek avatar Jul 07 '15 10:07 jskrzypek

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-mcfly our yeoman generator that gives you a pre-wired gulp & browserify build system for angular apps and options to automatically integrate ionic.

Steps to reproduce:

  1. Clone the repo & cd into the directory

    git clone https://github.com/jskrzypek/4008-preventDefault#master && cd 4008-preventDefault
    
  2. Install npm & bower dependencies

    npm install && bower install
    
  3. Use git to apply a patch to stick some console.log messages on the Drag handler

    git apply ionic.js.patch
    
  4. Use our gulp task to build the distributed code (using browserify)

    gulp dist
    
  5. cd into the directory where the ionic project sits

    cd dist/app/dev
    
  6. Add platforms

    ionic platform add android ios
    
  7. Run/build for either platform with ionic run .... Note that I am currently only able to reproduce this bug in ios.

  8. Attach your console (safari developer view for ios or chrome://inspect#devices for android)

  9. In the tab labeled ion-list you should be able to scroll fine via dragging, and should see messages logged by the drag handler from our patch.

  10. 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 the ev.preventDefault() method is being called on these legitimate drag events.

  11. Click the red heart in the upper-right corner. This will run the drag-handler.js script that swaps out the current version of the handler for the one with my suggested fixes.

  12. Now you can scroll just fine in the ion-content tab and ev.preventDefault() is not being called when it shouldn't be.

jskrzypek avatar Jul 07 '15 13:07 jskrzypek

@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...

jskrzypek avatar Jul 07 '15 14:07 jskrzypek

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

KillerCodeMonkey avatar Jul 09 '15 10:07 KillerCodeMonkey

@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.

Gunbit avatar Jul 09 '15 13:07 Gunbit

+1

krlwlfrt avatar Jul 09 '15 19:07 krlwlfrt

@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:

jskrzypek avatar Jul 10 '15 12:07 jskrzypek

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;
        }
    };
})

jskrzypek avatar Jul 10 '15 12:07 jskrzypek

@mhartington Any progress on checking this out?

jskrzypek avatar Jul 14 '15 10:07 jskrzypek

Merged this into our 1.0.2 branch e10b5d23c9cb3e04c6374574303e55aef6aefee4

mhartington avatar Jul 14 '15 14:07 mhartington

@mhartington Awesome, thanks!

jskrzypek avatar Jul 14 '15 14:07 jskrzypek

@jskrzypek Your fixed have a side effects(ion-fresher can only work once)

huip avatar Jul 27 '15 06:07 huip

@huip can you elaborate what you mean

jskrzypek avatar Jul 27 '15 08:07 jskrzypek

@jskrzypek I mean if ion-content contains ion-fresher tag, when pull down to refresh item-list,the pull-down events can not work.

huip avatar Jul 27 '15 09:07 huip

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 ?

lucasalmeida92 avatar Jul 27 '15 14:07 lucasalmeida92

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?

lucasalmeida92 avatar Jul 27 '15 21:07 lucasalmeida92