1(function($) { 2 3 var defaults = { 4 'swipeTolerance': 40 5 }; 6 7 var touchStatus = function(target, touch) { 8 this.target = $(target); 9 this.touch = touch; 10 this.startX = this.currentX = touch.screenX; 11 this.startY = this.currentY = touch.screenY; 12 this.eventType = null; 13 } 14 touchStatus.options = {}; 15 touchStatus.latestTap = null; 16 17 touchStatus.prototype.move = function(touch) { 18 this.currentX = touch.screenX; 19 this.currentY = touch.screenY; 20 } 21 22 touchStatus.prototype.process = function() { 23 var offsetX = this.currentX - this.startX; 24 var offsetY = this.currentY - this.startY; 25 if(offsetX == 0 && offsetY == 0) { 26 this.checkForDoubleTap(); 27 } else if(Math.abs(offsetY) > touchStatus.options.swipeTolerance && Math.abs(offsetY) > Math.abs(offsetX)) { 28 this.eventType = offsetY > 0 ? 'swipedown' : 'swipeup'; 29 this.target.trigger('swipe', [this]) 30 } else if(Math.abs(offsetX) > touchStatus.options.swipeTolerance) { 31 this.eventType = offsetX > 0 ? 'swiperight' : 'swipeleft'; 32 this.target.trigger('swipe', [this]) 33 } 34 if(this.eventType) this.target.trigger(this.eventType, [this]) 35 this.target.trigger('touch', [this]) 36 } 37 38 touchStatus.prototype.checkForDoubleTap = function() { 39 if(touchStatus.latestTap) { 40 if((new Date() - touchStatus.latestTap) < 400) 41 this.eventType = 'doubletap' 42 } 43 if(!this.eventType) this.eventType = 'tap' 44 touchStatus.latestTap = new Date() 45 } 46 47 var swipeEvents = function(elements, options) { 48 touchStatus.options = $.extend(defaults, options); 49 elements.bind('touchstart', this.touchStart); 50 elements.bind('touchmove', this.touchMove); 51 elements.bind('touchcancel', this.touchCancel); 52 elements.bind('touchend', this.touchEnd); 53 } 54 55 swipeEvents.prototype.touchStart = function(evt) { 56 var target = this; 57 swipeEvents.eachTouch(evt, function(touch) { 58 swipeEvents.touches[touch.identifier] = new touchStatus(target, touch); 59 }) 60 } 61 62 swipeEvents.prototype.touchMove = function(evt) { 63 swipeEvents.eachTouch(evt, function(touch) { 64 var loc = swipeEvents.touches[touch.identifier] 65 if(loc) loc.move(touch) 66 }) 67 } 68 69 swipeEvents.prototype.touchCancel = function(evt) { 70 swipeEvents.eachTouch(evt, function(touch) { 71 swipeEvents.purge(touch, true) 72 }) 73 } 74 75 swipeEvents.prototype.touchEnd = function(evt) { 76 swipeEvents.eachTouch(evt, function(touch) { 77 swipeEvents.purge(touch) 78 }) 79 } 80 81 swipeEvents.touches = {} 82 swipeEvents.purge = function(touch, cancelled) { 83 if(!cancelled) { 84 var loc = swipeEvents.touches[touch.identifier] 85 if(loc) loc.process() 86 } 87 delete swipeEvents.touches[touch.identifier] 88 } 89 90 swipeEvents.eachTouch = function(evt, callback) { 91 var evt = evt.originalEvent; 92 var num = evt.changedTouches.length; 93 for(var i = 0; i < num; i++) { 94 callback(evt.changedTouches[i]) 95 } 96 } 97 98 // adds custom events: 99 // touch // all events 100 // swipe // only swipe* events 101 // swipeleft 102 // swiperight 103 // swipeup 104 // swipedown 105 // tap 106 // doubletap 107 $.fn.addSwipeEvents = function(options, callback) { 108 if (!callback && jQuery.isFunction(options)) { 109 callback = options; 110 options = null; 111 } 112 new swipeEvents(this, options); 113 if(callback) this.bind('touch', callback); 114 return this; 115 } 116})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.