Reworked tap/taphold code so that it makes use of vmousecancel to detect when the user has scrolled the page. Also refactored some of the event handler calling code into a function. I'm still not sure why the handler is called directly versus using the jQuery trigger() method which would allow bubbling.

This commit is contained in:
Kin Blas 2011-03-09 14:14:53 -08:00
parent 8dd766dbce
commit d2594ec585

View file

@ -20,6 +20,14 @@ var supportTouch = $.support.touch,
touchStopEvent = supportTouch ? "touchend" : "mouseup",
touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
function triggerCustomEvent(obj, eventType, event)
{
var originalType = event.type;
event.type = eventType;
$.event.handle.call( obj, event );
event.type = originalType;
}
// also handles scrollstop
$.event.special.scrollstart = {
enabled: true,
@ -32,10 +40,7 @@ $.event.special.scrollstart = {
function trigger( event, state ) {
scrolling = state;
var originalType = event.type;
event.type = scrolling ? "scrollstart" : "scrollstop";
$.event.handle.call( thisObject, event );
event.type = originalType;
triggerCustomEvent( thisObject, scrolling ? "scrollstart" : "scrollstop", event );
}
// iPhone triggers scroll after a small delay; use touchmove instead
@ -68,57 +73,35 @@ $.event.special.tap = {
return false;
}
var moved = false,
touching = true,
var touching = true,
origTarget = event.target,
origEvent = event.originalEvent,
origPos = [ event.pageX, event.pageY ],
originalType,
timer;
function moveHandler( event ) {
if( event.type == "scroll" ){
moved = true;
return;
}
var newPageXY = event;
if ((Math.abs(origPos[0] - newPageXY.pageX) > 10) ||
(Math.abs(origPos[1] - newPageXY.pageY) > 10)) {
moved = true;
}
function clearTapHandlers() {
touching = false;
clearTimeout(timer);
$(this).unbind("vmouseclick", clickHandler).unbind("vmousecancel", clearTapHandlers);
}
function clickHandler(event) {
clearTapHandlers();
/* ONLY trigger a 'tap' event if the start target is
* the same as the stop target.
*/
if ( !origTarget == event.target ) {
triggerCustomEvent( thisObject, "tap", event );
}
}
$this.bind("vmousecancel", clearTapHandlers).bind("vclick", clickHandler);
timer = setTimeout(function() {
if ( touching && !moved ) {
originalType = event.type;
event.type = "taphold";
$.event.handle.call( thisObject, event );
event.type = originalType;
if ( touching ) {
triggerCustomEvent( thisObject, "taphold", event );
}
}, 750 );
//scroll now cancels tap
$(window).one("scroll", moveHandler);
$this
.bind( "vmousemove", moveHandler )
.one( "vmouseup", function( event ) {
$this.unbind( "vmousemove", moveHandler );
$(window).unbind("scroll", moveHandler);
clearTimeout( timer );
touching = false;
/* ONLY trigger a 'tap' event if the start target is
* the same as the stop target.
*/
if ( !moved && ( origTarget == event.target ) ) {
originalType = event.type;
event.type = "tap";
$.event.handle.call( thisObject, event );
event.type = originalType;
}
});
});
}
};
@ -170,6 +153,7 @@ $.event.special.swipe = {
Math.abs( start.coords[1] - stop.coords[1]) < 75 ) {
start.origin
.trigger( "swipe" )
.trigger( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" );
}
}