From 9df0531ac338caa0c237424c77298fa420fa756c Mon Sep 17 00:00:00 2001 From: Jason Als Date: Mon, 12 Aug 2013 21:59:20 -0700 Subject: [PATCH 1/4] fix(ngTouch): replace event with originalEvent When jQuery is included touches are found on event.originalEvent --- src/ngTouch/directive/ngClick.js | 10 +++++++++- src/ngTouch/swipe.js | 7 +++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ngTouch/directive/ngClick.js b/src/ngTouch/directive/ngClick.js index 90274aba7322..8f2571a71e26 100644 --- a/src/ngTouch/directive/ngClick.js +++ b/src/ngTouch/directive/ngClick.js @@ -123,6 +123,8 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', return; // Too old. } + // Use JQuery originalEvent + event = event.originalEvent || event; var touches = event.touches && event.touches.length ? event.touches : [event]; var x = touches[0].clientX; var y = touches[0].clientY; @@ -165,6 +167,8 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', // Global touchstart handler that creates an allowable region for a click event. // This allowable region can be removed by preventGhostClick if we want to bust it. function onTouchStart(event) { + // Use JQuery originalEvent + event = event.originalEvent || event; var touches = event.touches && event.touches.length ? event.touches : [event]; var x = touches[0].clientX; var y = touches[0].clientY; @@ -210,6 +214,8 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', } element.on('touchstart', function(event) { + // Use JQuery originalEvent + event = event.originalEvent || event; tapping = true; tapElement = event.target ? event.target : event.srcElement; // IE uses srcElement. // Hack for Safari, which can target text nodes instead of containers. @@ -237,7 +243,9 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', element.on('touchend', function(event) { var diff = Date.now() - startTime; - + + // Use JQuery originalEvent + event = event.originalEvent || event; var touches = (event.changedTouches && event.changedTouches.length) ? event.changedTouches : ((event.touches && event.touches.length) ? event.touches : [event]); var e = touches[0].originalEvent || touches[0]; diff --git a/src/ngTouch/swipe.js b/src/ngTouch/swipe.js index 884e0800d83c..3f27246c3d1f 100644 --- a/src/ngTouch/swipe.js +++ b/src/ngTouch/swipe.js @@ -40,11 +40,10 @@ ngTouch.factory('$swipe', [function() { }; function getCoordinates(event) { + // Use JQuery originalEvent + event = event.originalEvent || event; var touches = event.touches && event.touches.length ? event.touches : [event]; - var e = (event.changedTouches && event.changedTouches[0]) || - (event.originalEvent && event.originalEvent.changedTouches && - event.originalEvent.changedTouches[0]) || - touches[0].originalEvent || touches[0]; + var e = (event.changedTouches && event.changedTouches[0]) || touches[0]; return { x: e.clientX, From 02d927442c5b38f7b5a87f2e8faaa46f6415ab76 Mon Sep 17 00:00:00 2001 From: Pavel Pomerantsev Date: Sun, 18 Jan 2015 23:40:04 +0300 Subject: [PATCH 2/4] Test that jQuery-wrapped touch event gets unwrapped --- test/ngTouch/directive/ngClickSpec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/ngTouch/directive/ngClickSpec.js b/test/ngTouch/directive/ngClickSpec.js index 921c64578b2b..ee7c5b4e8e79 100644 --- a/test/ngTouch/directive/ngClickSpec.js +++ b/test/ngTouch/directive/ngClickSpec.js @@ -49,6 +49,16 @@ describe('ngClick (touch)', function() { })); + it('should unwrap a jQuery-wrapped event object', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + + browserTrigger(element, 'touchstart'); + browserTrigger(element, 'touchend'); + expect($rootScope.event.originalEvent).toBeUndefined(); + })); + + it('should not click if the touch is held too long', inject(function($rootScope, $compile, $rootElement) { element = $compile('
')($rootScope); $rootElement.append(element); From 286593ae883d83701e2db12bbc209de8dd093e84 Mon Sep 17 00:00:00 2001 From: Pavel Pomerantsev Date: Sun, 18 Jan 2015 23:58:29 +0300 Subject: [PATCH 3/4] Remove trailing whitespace --- src/ngTouch/directive/ngClick.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngTouch/directive/ngClick.js b/src/ngTouch/directive/ngClick.js index 8f2571a71e26..78fb35f0e450 100644 --- a/src/ngTouch/directive/ngClick.js +++ b/src/ngTouch/directive/ngClick.js @@ -243,7 +243,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', element.on('touchend', function(event) { var diff = Date.now() - startTime; - + // Use JQuery originalEvent event = event.originalEvent || event; var touches = (event.changedTouches && event.changedTouches.length) ? event.changedTouches : From 9c59d7e0689b2885a761944cd67458143c1efd0f Mon Sep 17 00:00:00 2001 From: Pavel Pomerantsev Date: Fri, 23 Jan 2015 00:12:30 +0300 Subject: [PATCH 4/4] Additional tests for touchstart/end and click --- src/ngTouch/directive/ngClick.js | 3 +++ test/ngTouch/directive/ngClickSpec.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ngTouch/directive/ngClick.js b/src/ngTouch/directive/ngClick.js index 78fb35f0e450..4d4e9c02712a 100644 --- a/src/ngTouch/directive/ngClick.js +++ b/src/ngTouch/directive/ngClick.js @@ -283,6 +283,9 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', // - But the browser's follow-up slow click will be "busted" before it reaches this handler. // Therefore it's safe to use this directive on both mobile and desktop. element.on('click', function(event, touchend) { + // Use JQuery originalEvent + event = event.originalEvent || event; + scope.$apply(function() { clickHandler(scope, {$event: (touchend || event)}); }); diff --git a/test/ngTouch/directive/ngClickSpec.js b/test/ngTouch/directive/ngClickSpec.js index ee7c5b4e8e79..ea65f3f575a0 100644 --- a/test/ngTouch/directive/ngClickSpec.js +++ b/test/ngTouch/directive/ngClickSpec.js @@ -49,13 +49,24 @@ describe('ngClick (touch)', function() { })); - it('should unwrap a jQuery-wrapped event object', inject(function($rootScope, $compile) { + it('should unwrap a jQuery-wrapped event object on touchstart/touchend', inject(function($rootScope, $compile) { element = $compile('
')($rootScope); $rootScope.$digest(); browserTrigger(element, 'touchstart'); browserTrigger(element, 'touchend'); expect($rootScope.event.originalEvent).toBeUndefined(); + expect($rootScope.event.clientX).toBeDefined(); + expect($rootScope.event.clientY).toBeDefined(); + })); + + + it('should unwrap a jQuery-wrapped event object on click', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + + browserTrigger(element, 'click'); + expect($rootScope.event.originalEvent).toBeUndefined(); }));