Skip to content

Commit

Permalink
🪲 better press handling (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Oct 5, 2016
1 parent 0fe6e40 commit 00167b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/jquery.finger.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
start = {},
move = {},
motion,
cancel,
safeguard,
timeout,
prevEl,
Expand Down Expand Up @@ -71,7 +70,7 @@
function startHandler(event) {
if (event.which > 1)
return;

var timeStamp = event.timeStamp || +new Date();

if (safeguard == timeStamp) return;
Expand All @@ -85,10 +84,8 @@
move.orientation = null;
move.end = false;
motion = false;
cancel = false;
timeout = setTimeout(function() {
cancel = true;
trigger(event, 'press');
trigger(event, 'press', true);
}, Finger.pressDuration);

$.event.add(rootEl, moveEvent + '.' + namespace, moveHandler);
Expand Down Expand Up @@ -152,12 +149,14 @@
clearTimeout(timeout);

// tap-like events
// triggered only if targets match
if (!motion && !cancel && event.target === start.target) {
var doubleTap = prevEl === event.target && timeStamp - prevTime < Finger.doubleTapInterval;
evtName = doubleTap ? 'doubletap' : 'tap';
prevEl = doubleTap ? null : start.target;
prevTime = timeStamp;
if (!motion) {
// triggered only if targets match
if (event.target === start.target) {
var doubleTap = prevEl === event.target && timeStamp - prevTime < Finger.doubleTapInterval;
evtName = doubleTap ? 'doubletap' : 'tap';
prevEl = doubleTap ? null : start.target;
prevTime = timeStamp;
}
}
// motion events
else {
Expand All @@ -168,7 +167,8 @@
evtName = 'drag';
}

trigger(event, evtName, true);
if (evtName)
trigger(event, evtName, true);
}

// initial binding
Expand Down
19 changes: 16 additions & 3 deletions test/jquery.finger_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
this.pointer = VirtualPointer(this);

// adjusting time values for testing purposes
this.pointer.DOUBLETAP_DURATION = $.Finger.doubleTapInterval = 25;
this.pointer.DOUBLETAP_DURATION = $.Finger.doubleTapInterval = 50;
this.pointer.PRESS_DURATION = $.Finger.pressDuration = 25;
this.pointer.FLICK_DURATION = $.Finger.flickDuration = 25;
});
Expand All @@ -24,7 +24,7 @@
this.$elems = null;

// wait a enough time between tests in order to not trigger double tap events
setTimeout(done, $.Finger.doubleTapInterval);
setTimeout(done, $.Finger.pressDuration * 3);
});

describe('tap event', function() {
Expand Down Expand Up @@ -395,9 +395,22 @@
this.pointer.drag(0, 300, function() {
y.should.be.greaterThan(200);
end.should.be.truthy;
$('#fixtures').css('padding', 0)
done();
});
});

it('should not fire when press event is fired (#49)', function(done) {
var handler1 = sinon.spy();
var handler2 = sinon.spy();
this.$elems.on('drag', handler1);
this.$elems.on('press', handler2);
this.pointer.press(function() {
handler1.should.not.have.been.called;
handler2.should.have.been.calledOnce;
done();
});
});
});

xdescribe('flick event', function() {
Expand Down Expand Up @@ -442,4 +455,4 @@
});
});

}(jQuery));
}(jQuery));

0 comments on commit 00167b6

Please sign in to comment.