Skip to content

Commit bf0d0c6

Browse files
committed
Merge pull request jquery-archive#5348 from jquery/swipe
Swipe: merge swipe branch
2 parents 65f72e1 + a924ea9 commit bf0d0c6

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

js/events/touch.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,33 +133,49 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
133133

134134
verticalDistanceThreshold: 75, // Swipe vertical displacement must be less than this.
135135

136+
start: function( event ) {
137+
var data = event.originalEvent.touches ?
138+
event.originalEvent.touches[ 0 ] : event;
139+
return {
140+
time: ( new Date() ).getTime(),
141+
coords: [ data.pageX, data.pageY ],
142+
origin: $( event.target )
143+
};
144+
},
145+
146+
stop: function( event ) {
147+
var data = event.originalEvent.touches ?
148+
event.originalEvent.touches[ 0 ] : event;
149+
return {
150+
time: ( new Date() ).getTime(),
151+
coords: [ data.pageX, data.pageY ]
152+
};
153+
},
154+
155+
handleSwipe: function( start, stop ) {
156+
if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
157+
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
158+
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
159+
160+
start.origin.trigger( "swipe" )
161+
.trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
162+
}
163+
},
164+
136165
setup: function() {
137166
var thisObject = this,
138167
$this = $( thisObject );
139168

140169
$this.bind( touchStartEvent, function( event ) {
141-
var data = event.originalEvent.touches ?
142-
event.originalEvent.touches[ 0 ] : event,
143-
start = {
144-
time: ( new Date() ).getTime(),
145-
coords: [ data.pageX, data.pageY ],
146-
origin: $( event.target )
147-
},
170+
var start = $.event.special.swipe.start( event ),
148171
stop;
149172

150173
function moveHandler( event ) {
151-
152174
if ( !start ) {
153175
return;
154176
}
155177

156-
var data = event.originalEvent.touches ?
157-
event.originalEvent.touches[ 0 ] : event;
158-
159-
stop = {
160-
time: ( new Date() ).getTime(),
161-
coords: [ data.pageX, data.pageY ]
162-
};
178+
stop = $.event.special.swipe.stop( event );
163179

164180
// prevent scrolling
165181
if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) {
@@ -168,17 +184,11 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
168184
}
169185

170186
$this.bind( touchMoveEvent, moveHandler )
171-
.one( touchStopEvent, function( event ) {
187+
.one( touchStopEvent, function() {
172188
$this.unbind( touchMoveEvent, moveHandler );
173189

174190
if ( start && stop ) {
175-
if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
176-
Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
177-
Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
178-
179-
start.origin.trigger( "swipe" )
180-
.trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
181-
}
191+
$.event.special.swipe.handleSwipe( start, stop );
182192
}
183193
start = stop = undefined;
184194
});

0 commit comments

Comments
 (0)