Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngTouch): fix ghost click on small touch delta #6995

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/ngTouch/directive/ngClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,16 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
// Actual linking function.
return function(scope, element, attr) {
var clickHandler = $parse(attr.ngClick),
tapping = false,
tapElement, // Used to blur the element after a tap.
startTime, // Used to check if the tap was held too long.
touchStartX,
touchStartY;

function resetState() {
tapping = false;
element.removeClass(ACTIVE_CLASS_NAME);
}

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

if (tapping && diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
if (diff < TAP_DURATION && dist < MOVE_TOLERANCE) {
// Call preventGhostClick so the clickbuster will catch the corresponding click.
preventGhostClick(x, y);

Expand Down
9 changes: 5 additions & 4 deletions test/ngTouch/directive/ngClickSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('ngClick (touch)', function() {
x: 10,
y: 10
});
browserTrigger(element, 'touchmove');
browserTrigger(element, 'touchend',{
keys: [],
x: 400,
Expand All @@ -97,7 +98,7 @@ describe('ngClick (touch)', function() {
}));


it('should not click if a touchmove comes before touchend', inject(function($rootScope, $compile, $rootElement) {
it('should click if the touchend is close', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();
Expand All @@ -112,11 +113,11 @@ describe('ngClick (touch)', function() {
browserTrigger(element, 'touchmove');
browserTrigger(element, 'touchend',{
keys: [],
x: 400,
y: 400
x: 15,
y: 15
});

expect($rootScope.tapped).toBeUndefined();
expect($rootScope.tapped).toBe(true);
}));

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