Skip to content

Commit a8a0921

Browse files
author
Justin Vanderheide
committed
fix(ngTouch): fix ghost click on small touch delta
ngClick was leaking ghost click events when the touch Startmoved a small distance. This is because if there was a touchMove event between the touchStart and touchEnd preventGhostClick would not be called. This also caused the MOVE_TOLERANCE threshold to be ignored because any change in position would stop preventGhostClick from being run. Cancelling a tap is now correctly based on the MOVE_TOLERANCE, and ghost clicks are not leaked. Closes angular#6251
1 parent 708f2ba commit a8a0921

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/ngTouch/directive/ngClick.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,16 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
195195
// Actual linking function.
196196
return function(scope, element, attr) {
197197
var clickHandler = $parse(attr.ngClick),
198-
tapping = false,
199198
tapElement, // Used to blur the element after a tap.
200199
startTime, // Used to check if the tap was held too long.
201200
touchStartX,
202201
touchStartY;
203202

204203
function resetState() {
205-
tapping = false;
206204
element.removeClass(ACTIVE_CLASS_NAME);
207205
}
208206

209207
element.on('touchstart', function(event) {
210-
tapping = true;
211208
tapElement = event.target ? event.target : event.srcElement; // IE uses srcElement.
212209
// Hack for Safari, which can target text nodes instead of containers.
213210
if(tapElement.nodeType == 3) {
@@ -242,7 +239,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
242239
var y = e.clientY;
243240
var dist = Math.sqrt( Math.pow(x - touchStartX, 2) + Math.pow(y - touchStartY, 2) );
244241

245-
if (tapping && diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
242+
if (diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
246243
// Call preventGhostClick so the clickbuster will catch the corresponding click.
247244
preventGhostClick(x, y);
248245

test/ngTouch/directive/ngClickSpec.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ describe('ngClick (touch)', function() {
8787
x: 10,
8888
y: 10
8989
});
90+
browserTrigger(element, 'touchmove');
9091
browserTrigger(element, 'touchend',{
9192
keys: [],
9293
x: 400,
@@ -97,7 +98,7 @@ describe('ngClick (touch)', function() {
9798
}));
9899

99100

100-
it('should not click if a touchmove comes before touchend', inject(function($rootScope, $compile, $rootElement) {
101+
it('should click if the touchend is close', inject(function($rootScope, $compile, $rootElement) {
101102
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
102103
$rootElement.append(element);
103104
$rootScope.$digest();
@@ -112,11 +113,11 @@ describe('ngClick (touch)', function() {
112113
browserTrigger(element, 'touchmove');
113114
browserTrigger(element, 'touchend',{
114115
keys: [],
115-
x: 400,
116-
y: 400
116+
x: 15,
117+
y: 15
117118
});
118119

119-
expect($rootScope.tapped).toBeUndefined();
120+
expect($rootScope.tapped).toBe(true);
120121
}));
121122

122123
it('should add the CSS class while the element is held down, and then remove it', inject(function($rootScope, $compile, $rootElement) {

0 commit comments

Comments
 (0)