Permalink
Browse files

adds comments to touch.js on async and logic changes

  • Loading branch information...
1 parent 7095373 commit 91dac48bfa2119d5e8dc48f23075bc13b08d3fc3 @hunterloftis committed Apr 30, 2012
Showing with 58 additions and 45 deletions.
  1. +58 −45 src/touch.js
View
@@ -41,55 +41,68 @@
$(document).ready(function(){
var now, delta
- $(document.body).bind('touchstart', function(e){
- now = Date.now()
- delta = now - (touch.last || now)
- touch.el = $(parentIfText(e.touches[0].target))
- touchTimeout && clearTimeout(touchTimeout)
- touch.x1 = e.touches[0].pageX
- touch.y1 = e.touches[0].pageY
- if (delta > 0 && delta <= 250) touch.isDoubleTap = true
- touch.last = now
- longTapTimeout = setTimeout(longTap, longTapDelay)
- }).bind('touchmove', function(e){
- cancelLongTap()
- touch.x2 = e.touches[0].pageX
- touch.y2 = e.touches[0].pageY
- }).bind('touchend', function(e){
- cancelLongTap()
-
- // swipe
- if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
- (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) {
- swipeTimeout = setTimeout(function() {
- touch.el.trigger('swipe') &&
- touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
- touch = {}
- }, 0);
-
- // normal tap
- } else if ('last' in touch) {
- tapTimeout = setTimeout(function() {
- var event = $.Event('tap');
- event.cancelTouch = cancelAll;
- touch.el.trigger(event)
-
- if (touch.isDoubleTap) {
- touch.el.trigger('doubleTap')
+ $(document.body)
+ .bind('touchstart', function(e){
+ now = Date.now()
+ delta = now - (touch.last || now)
+ touch.el = $(parentIfText(e.touches[0].target))
+ touchTimeout && clearTimeout(touchTimeout)
+ touch.x1 = e.touches[0].pageX
+ touch.y1 = e.touches[0].pageY
+ if (delta > 0 && delta <= 250) touch.isDoubleTap = true
+ touch.last = now
+ longTapTimeout = setTimeout(longTap, longTapDelay)
+ })
+ .bind('touchmove', function(e){
+ cancelLongTap()
+ touch.x2 = e.touches[0].pageX
+ touch.y2 = e.touches[0].pageY
+ })
+ .bind('touchend', function(e){
+ cancelLongTap()
+
+ // swipe
+ if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
+ (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) {
+ swipeTimeout = setTimeout(function() {
+ touch.el.trigger('swipe')
+ touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
touch = {}
- }
- else {
- touchTimeout = setTimeout(function(){
- touchTimeout = null
- touch.el.trigger('singleTap')
+ }, 0);
+
+ // normal tap
+ } else if ('last' in touch) {
+
+ // delay by one tick so we can cancel the 'tap' event if 'scroll' fires
+ // ('tap' fires before 'scroll')
+ tapTimeout = setTimeout(function() {
+
+ // trigger universal 'tap' with the option to cancelTouch()
+ // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
+ var event = $.Event('tap')
+ event.cancelTouch = cancelAll
+ touch.el.trigger(event)
+
+ // trigger double tap immediately
+ if (touch.isDoubleTap) {
+ touch.el.trigger('doubleTap')
touch = {}
- }, 250)
- }
+ }
+
+ // trigger single tap after 250ms of inactivity
+ else {
+ touchTimeout = setTimeout(function(){
+ touchTimeout = null
+ touch.el.trigger('singleTap')
+ touch = {}
+ }, 250)
+ }
- }, 0);
+ }, 0);
- }
- }).bind('touchcancel', cancelAll)
+ }
+ })
+ .bind('touchcancel', cancelAll)
$(window).bind('scroll', cancelAll)
})

0 comments on commit 91dac48

Please sign in to comment.