Skip to content

Commit

Permalink
fix(tap): Deactivate elements during scroll at the same time click is…
Browse files Browse the repository at this point in the history
… ignored, #997

Previously I disabled the activation class immediately on a touchmove,
where as the click will still work if you touchstart and touchend
within a few pixels of each other. So visually it may have looked like
the click shouldn't have worked. I just updated it so the use the same
numbers. For example, if you hold down an item and move just 5 pixels,
the item will stay active (before it wouldn't have), and the click will
fire. But at the same time, if you hold down an item, and move a larger
distance, once it realizes that it went farther than 6 pixels it'll not
allow a click to happen, AND it'll not show the item as being active.
  • Loading branch information
adamdbradley committed Apr 4, 2014
1 parent 4ff6acb commit 3ee5ea7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
6 changes: 6 additions & 0 deletions js/ext/angular/test/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,14 @@ <h1>YELLOW {{slideBox.slideIndex}}</h1>
});

var mouseTimerId;
var mouseMoveCount = 0;
function onMouseMove(e) {
clearTimeout(mouseTimerId);
mouseTimerId = setTimeout(function(){
var el = document.getElementById('mousemove-notify');
el.style.display = 'block';
mouseMoveCount++;
el.innerText = 'Mouse Move! ' + mouseMoveCount;
clearTimeout(mouseTimerId);
mouseTimerId = setTimeout(function(){
el.style.display = 'none';
Expand All @@ -280,11 +283,14 @@ <h1>YELLOW {{slideBox.slideIndex}}</h1>
}

var touchTimerId;
var touchMoveCount = 0;
function onTouchMove(e) {
clearTimeout(touchTimerId);
touchTimerId = setTimeout(function(){
var el = document.getElementById('touchmove-notify');
el.style.display = 'block';
touchMoveCount++;
el.innerText = 'Touch Move! ' + touchMoveCount;
clearTimeout(touchTimerId);
touchTimerId = setTimeout(function(){
el.style.display = 'none';
Expand Down
8 changes: 4 additions & 4 deletions js/ext/angular/test/service/ionicTap.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ describe('Ionic Tap', function() {
it('Should setStart and hasScrolled true if >= touch tolerance', function() {
ionic.tap.setStart({ clientX: 100, clientY: 100 });

var s = ionic.tap.hasScrolled({ clientX: 105, clientY: 100 });
var s = ionic.tap.hasScrolled({ clientX: 111, clientY: 100 });
expect(s).toEqual(true);

s = ionic.tap.hasScrolled({ clientX: 95, clientY: 100 });
s = ionic.tap.hasScrolled({ clientX: 89, clientY: 100 });
expect(s).toEqual(true);

s = ionic.tap.hasScrolled({ clientX: 100, clientY: 103 });
s = ionic.tap.hasScrolled({ clientX: 100, clientY: 107 });
expect(s).toEqual(true);

s = ionic.tap.hasScrolled({ clientX: 100, clientY: 97 });
s = ionic.tap.hasScrolled({ clientX: 100, clientY: 93 });
expect(s).toEqual(true);

s = ionic.tap.hasScrolled({ clientX: 100, clientY: 200 });
Expand Down
15 changes: 14 additions & 1 deletion js/utils/activator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
var activeElements = {}; // elements that are currently active
var keyId = 0; // a counter for unique keys for the above ojects
var ACTIVATED_CLASS = 'activated';
var touchMoveClearTimer;

ionic.activator = {

start: function(e) {
clearTimeout(touchMoveClearTimer);

// when an element is touched/clicked, it climbs up a few
// parents to see if it is an .item or .button element
ionic.requestAnimationFrame(function(){
Expand Down Expand Up @@ -39,7 +42,9 @@
// add listeners to clear all queued/active elements onMove
if(e.type === 'touchstart') {
document.body.removeEventListener('mousedown', ionic.activator.start);
document.body.addEventListener('touchmove', clear, false);
touchMoveClearTimer = setTimeout(function(){
document.body.addEventListener('touchmove', onTouchMove, false);
}, 85);
setTimeout(activateElements, 85);
} else {
document.body.addEventListener('mousemove', clear, false);
Expand Down Expand Up @@ -73,12 +78,20 @@
}
}

function onTouchMove(e) {
if( ionic.tap.hasScrolled(e) ) {
clear();
}
}

function onEnd(e) {
// clear out any active/queued elements after XX milliseconds
setTimeout(clear, 200);
}

function clear() {
clearTimeout(touchMoveClearTimer);

// clear out any elements that are queued to be set to active
queueElements = {};

Expand Down
9 changes: 5 additions & 4 deletions js/utils/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
var REMOVE_PREVENT_DELAY = 380; // delay after a touchend/mouseup before removing the ghostclick prevent
var REMOVE_PREVENT_DELAY_GRADE_C = 800; // same as REMOVE_PREVENT_DELAY, but for grade c devices
var HIT_RADIUS = 15; // surrounding area of a click that if a ghostclick happens it would get ignored
var TOUCH_TOLERANCE_X = 4; // how much the X coordinates can be off between start/end, but still a click
var TOUCH_TOLERANCE_Y = 2; // how much the Y coordinates can be off between start/end, but still a click
var TOUCH_TOLERANCE_X = 10; // how much the X coordinates can be off between start/end, but still a click
var TOUCH_TOLERANCE_Y = 6; // how much the Y coordinates can be off between start/end, but still a click
var tapCoordinates = {}; // used to remember coordinates to ignore if they happen again quickly
var startCoordinates = {}; // used to remember where the coordinates of the start of a touch
var clickPreventTimerId;
Expand Down Expand Up @@ -110,11 +110,11 @@

} else if(ionic.tap.hasScrolled(e)) {
// this click's coordinates are different than its touchstart/mousedown, must have been scrolling
console.debug('preventGhostClick', 'hasScrolled, startCoordinates, x:' + startCoordinates.x + ' y:' + startCoordinates.y);
console.debug('preventGhostClick', 'hasScrolled');
}

var c = ionic.tap.getCoordinates(e);
return 'click at x:' + c.x + ', y:' + c.y;
return 'click(' + c.x + ',' + c.y + '), start(' + startCoordinates.x + ',' + startCoordinates.y + ')';
})());


Expand Down Expand Up @@ -153,6 +153,7 @@
return false;
}

// the allowed distance between touchstart/mousedown and
return (c.x > startCoordinates.x + TOUCH_TOLERANCE_X ||
c.x < startCoordinates.x - TOUCH_TOLERANCE_X ||
c.y > startCoordinates.y + TOUCH_TOLERANCE_Y ||
Expand Down

0 comments on commit 3ee5ea7

Please sign in to comment.