Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 02f332b

Browse files
author
Alexander Schmitz
committed
Swipe Event: made it possible to hook to the start and stop objects and swipe event handler to allow custom implementations and extensions. Fixes #2328 - Scrolling up and down causes swipe event
1 parent 9f7cbfc commit 02f332b

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

js/events/touch.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,43 @@ 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 ) {
141170
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-
},
171+
event.originalEvent.touches[ 0 ] : event,
172+
start = $.event.special.swipe.start( event ),
148173
stop;
149174

150175
function moveHandler( event ) {
@@ -153,13 +178,7 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
153178
return;
154179
}
155180

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-
};
181+
stop = $.event.special.swipe.start( event );
163182

164183
// prevent scrolling
165184
if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) {
@@ -172,13 +191,7 @@ define( [ "jquery", "../jquery.mobile.vmouse", "../jquery.mobile.support.touch"
172191
$this.unbind( touchMoveEvent, moveHandler );
173192

174193
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-
}
194+
$.event.special.swipe.handleSwipe( start, stop );
182195
}
183196
start = stop = undefined;
184197
});

0 commit comments

Comments
 (0)