2 (function( $, window, undefined ) {
3 // add new event shortcuts
4 $.each( ( "touchstart touchmove touchend " +
6 "swipe swipeleft swiperight " +
7 "scrollstart scrollstop" ).split( " " ), function( i, name ) {
9 $.fn[ name ] = function( fn ) {
10 return fn ? this.bind( name, fn ) : this.trigger( name );
15 $.attrFn[ name ] = true;
19 var supportTouch = $.mobile.support.touch,
20 scrollEvent = "touchmove scroll",
21 touchStartEvent = supportTouch ? "touchstart" : "mousedown",
22 touchStopEvent = supportTouch ? "touchend" : "mouseup",
23 touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
25 function triggerCustomEvent( obj, eventType, event ) {
26 var originalType = event.type;
27 event.type = eventType;
28 // event.liveFired is already set by basic events, e.g. vclick, which is fired already.
29 // To fire this custom event, event.liveFired must be cleared.
30 event.liveFired = undefined;
32 $.event.handle.call( obj, event );
33 event.type = originalType;
36 // also handles scrollstop
37 $.event.special.scrollstart = {
43 var thisObject = this,
44 $this = $( thisObject ),
48 function trigger( event, state ) {
50 triggerCustomEvent( thisObject, scrolling ? "scrollstart" : "scrollstop", event );
53 // iPhone triggers scroll after a small delay; use touchmove instead
54 $this.bind( scrollEvent, function( event ) {
56 if ( !$.event.special.scrollstart.enabled ) {
61 trigger( event, true );
64 clearTimeout( timer );
65 timer = setTimeout( function() {
66 trigger( event, false );
72 // also handles taphold
73 $.event.special.tap = {
74 tapholdThreshold: 750,
77 var thisObject = this,
78 $this = $( thisObject );
80 $this.bind( "vmousedown", function( event ) {
82 if ( event.which && event.which !== 1 ) {
86 var origTarget = event.target,
87 origEvent = event.originalEvent,
90 function clearTapTimer() {
91 clearTimeout( timer );
94 function clearTapHandlers() {
97 $this.unbind( "vclick", clickHandler )
98 .unbind( "vmouseup", clearTapTimer );
99 $.mobile.$document.unbind( "vmousecancel", clearTapHandlers );
102 function clickHandler( event ) {
105 // ONLY trigger a 'tap' event if the start target is
106 // the same as the stop target.
107 if ( origTarget === event.target ) {
108 triggerCustomEvent( thisObject, "tap", event );
112 $this.bind( "vmouseup", clearTapTimer )
113 .bind( "vclick", clickHandler );
114 $.mobile.$document.bind( "vmousecancel", clearTapHandlers );
116 timer = setTimeout( function() {
117 triggerCustomEvent( thisObject, "taphold", $.Event( "taphold", { target: origTarget } ) );
118 }, $.event.special.tap.tapholdThreshold );
123 // also handles swipeleft, swiperight
124 $.event.special.swipe = {
125 scrollSupressionThreshold: 30, // More than this horizontal displacement, and we will suppress scrolling.
127 durationThreshold: 1000, // More time than this, and it isn't a swipe.
129 horizontalDistanceThreshold: 30, // Swipe horizontal displacement must be more than this.
131 verticalDistanceThreshold: 75, // Swipe vertical displacement must be less than this.
134 var thisObject = this,
135 $this = $( thisObject );
137 $this.bind( touchStartEvent, function( event ) {
138 var data = event.originalEvent.touches ?
139 event.originalEvent.touches[ 0 ] : event,
141 time: ( new Date() ).getTime(),
142 coords: [ data.pageX, data.pageY ],
143 origin: $( event.target )
147 function moveHandler( event ) {
153 var data = event.originalEvent.touches ?
154 event.originalEvent.touches[ 0 ] : event;
157 time: ( new Date() ).getTime(),
158 coords: [ data.pageX, data.pageY ]
162 if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) {
163 event.preventDefault();
167 $this.bind( touchMoveEvent, moveHandler )
168 .one( touchStopEvent, function( event ) {
169 $this.unbind( touchMoveEvent, moveHandler );
171 if ( start && stop ) {
172 if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
173 Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
174 Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
176 start.origin.trigger( "swipe" )
177 .trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
180 start = stop = undefined;
186 scrollstop: "scrollstart",
190 }, function( event, sourceEvent ) {
192 $.event.special[ event ] = {
194 $( this ).bind( sourceEvent, $.noop );