mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-20 07:50:27 +00:00
197 lines
4.7 KiB
JavaScript
197 lines
4.7 KiB
JavaScript
// add new event shortcuts
|
|
$.each( "touchstart touchmove touchend orientationchange tap taphold swipe swipeleft swiperight scrollstart scrollstop".split( " " ), function( i, name ) {
|
|
$.fn[ name ] = function( fn ) {
|
|
return fn ? this.bind( name, fn ) : this.trigger( name );
|
|
};
|
|
$.attrFn[ name ] = true;
|
|
});
|
|
|
|
var supportTouch = $.support.touch,
|
|
scrollEvent = "touchmove scroll",
|
|
touchStartEvent = supportTouch ? "touchstart" : "mousedown",
|
|
touchStopEvent = supportTouch ? "touchend" : "mouseup",
|
|
touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
|
|
|
|
// also handles scrollstop
|
|
$.event.special.scrollstart = {
|
|
enabled: true,
|
|
|
|
setup: function() {
|
|
var thisObject = this,
|
|
$this = $( thisObject ),
|
|
scrolling,
|
|
timer;
|
|
|
|
function trigger( event, state ) {
|
|
scrolling = state;
|
|
var originalType = event.type;
|
|
event.type = scrolling ? "scrollstart" : "scrollstop";
|
|
$.event.handle.call( thisObject, event );
|
|
event.type = originalType;
|
|
}
|
|
|
|
// iPhone triggers scroll after a small delay; use touchmove instead
|
|
$this.bind( scrollEvent, function( event ) {
|
|
if ( !$.event.special.scrollstart.enabled ) {
|
|
return;
|
|
}
|
|
|
|
if ( !scrolling ) {
|
|
trigger( event, true );
|
|
}
|
|
|
|
clearTimeout( timer );
|
|
timer = setTimeout(function() {
|
|
trigger( event, false );
|
|
}, 50 );
|
|
});
|
|
}
|
|
};
|
|
|
|
// also handles taphold
|
|
$.event.special.tap = {
|
|
setup: function() {
|
|
var thisObject = this,
|
|
$this = $( thisObject );
|
|
|
|
$this
|
|
.bind( touchStartEvent, function( event ) {
|
|
if ( event.which && event.which !== 1 ) {
|
|
return;
|
|
}
|
|
|
|
var held = false,
|
|
moved = false,
|
|
touching = true,
|
|
originalType,
|
|
timer;
|
|
|
|
function moveHandler() {
|
|
moved = true;
|
|
}
|
|
|
|
timer = setTimeout(function() {
|
|
if ( touching && !moved ) {
|
|
held = true;
|
|
originalType = event.type;
|
|
event.type = "taphold";
|
|
$.event.handle.call( thisObject, event );
|
|
event.type = originalType;
|
|
}
|
|
}, 300 );
|
|
|
|
$this
|
|
.one( touchMoveEvent, moveHandler)
|
|
.one( touchStopEvent, function( event ) {
|
|
$this.unbind( touchMoveEvent, moveHandler );
|
|
clearTimeout( timer );
|
|
touching = false;
|
|
|
|
if ( !held && !moved ) {
|
|
originalType = event.type;
|
|
event.type = "tap";
|
|
$.event.handle.call( thisObject, event );
|
|
event.type = originalType;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
// also handles swipeleft, swiperight
|
|
$.event.special.swipe = {
|
|
setup: function() {
|
|
var thisObject = this,
|
|
$this = $( thisObject );
|
|
|
|
$this
|
|
.bind( touchStartEvent, function( event ) {
|
|
var data = event.originalEvent.touches ?
|
|
event.originalEvent.touches[ 0 ] :
|
|
event,
|
|
start = {
|
|
time: (new Date).getTime(),
|
|
coords: [ data.pageX, data.pageY ],
|
|
origin: $( event.target )
|
|
},
|
|
stop;
|
|
|
|
function moveHandler( event ) {
|
|
if ( !start ) {
|
|
return;
|
|
}
|
|
|
|
var data = event.originalEvent.touches ?
|
|
event.originalEvent.touches[ 0 ] :
|
|
event;
|
|
stop = {
|
|
time: (new Date).getTime(),
|
|
coords: [ data.pageX, data.pageY ]
|
|
};
|
|
|
|
// prevent scrolling
|
|
if ( Math.abs( start.coords[0] - stop.coords[0] ) > 10 ) {
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
|
|
$this
|
|
.bind( touchMoveEvent, moveHandler )
|
|
.one( touchStopEvent, function( event ) {
|
|
$this.unbind( touchMoveEvent, moveHandler );
|
|
if ( start && stop ) {
|
|
if ( stop.time - start.time < 1000 &&
|
|
Math.abs( start.coords[0] - stop.coords[0]) > 30 &&
|
|
Math.abs( start.coords[1] - stop.coords[1]) < 20 ) {
|
|
start.origin
|
|
.trigger( "swipe" )
|
|
.trigger( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" );
|
|
}
|
|
}
|
|
start = stop = undefined;
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
$.event.special.orientationchange = {
|
|
orientation: function( elem ) {
|
|
return elem.width() / elem.height() < 1.1 ? "portrait" : "landscape";
|
|
},
|
|
|
|
setup: function() {
|
|
var thisObject = this,
|
|
$this = $( thisObject ),
|
|
orientation = $.event.special.orientationchange.orientation( $this );
|
|
|
|
function handler() {
|
|
var newOrientation = $.event.special.orientationchange.orientation( $this );
|
|
|
|
if ( orientation !== newOrientation ) {
|
|
$.event.handle.call( thisObject, "orientationchange", {
|
|
orientation: newOrientation
|
|
} );
|
|
orientation = newOrientation;
|
|
}
|
|
}
|
|
|
|
if ( $.support.orientation ) {
|
|
thisObject.addEventListener( "orientationchange", handler, false );
|
|
} else {
|
|
$this.bind( "resize", handler );
|
|
}
|
|
}
|
|
};
|
|
|
|
$.each({
|
|
scrollstop: "scrollstart",
|
|
taphold: "tap",
|
|
swipeleft: "swipe",
|
|
swiperight: "swipe"
|
|
}, function( event, sourceEvent ) {
|
|
$.event.special[ event ] = {
|
|
setup: function() {
|
|
$( this ).bind( sourceEvent, $.noop );
|
|
}
|
|
};
|
|
});
|