1(function($) { 2 var touchStatus = function(target, touch) { 3 this.target = $(target); 4 this.touch = touch; 5 this.startX = this.currentX = touch.screenX; 6 this.startY = this.currentY = touch.screenY; 7 this.eventType = null; 8 } 9 touchStatus.latestTap = null; 10 11 touchStatus.prototype.move = function(touch) { 12 this.currentX = touch.screenX; 13 this.currentY = touch.screenY; 14 } 15 16 touchStatus.prototype.process = function() { 17 var offsetX = this.currentX - this.startX; 18 var offsetY = this.currentY - this.startY; 19 if(offsetX == 0 && offsetY == 0) { 20 this.checkForDoubleTap() 21 } else if(Math.abs(offsetY) > Math.abs(offsetX)) { 22 this.eventType = offsetY > 0 ? 'swipedown' : 'swipeup'; 23 this.target.trigger('swipe', [this]) 24 } else { 25 this.eventType = offsetX > 0 ? 'swiperight' : 'swipeleft'; 26 this.target.trigger('swipe', [this]) 27 } 28 this.target.trigger(this.eventType, [this]) 29 this.target.trigger('touch', [this]) 30 } 31 32 touchStatus.prototype.checkForDoubleTap = function() { 33 if(touchStatus.latestTap) { 34 if((new Date() - touchStatus.latestTap) < 400) 35 this.eventType = 'doubletap' 36 } 37 if(!this.eventType) this.eventType = 'tap' 38 touchStatus.latestTap = new Date() 39 } 40 41 var swipeEvents = function(elements) { 42 elements.bind('touchstart', this.touchStart); 43 elements.bind('touchmove', this.touchMove); 44 elements.bind('touchcancel', this.touchCancel); 45 elements.bind('touchend', this.touchEnd); 46 } 47 48 swipeEvents.prototype.touchStart = function(evt) { 49 var target = this; 50 swipeEvents.eachTouch(evt, function(touch) { 51 swipeEvents.touches[touch.identifier] = new touchStatus(target, touch); 52 }) 53 } 54 55 swipeEvents.prototype.touchMove = function(evt) { 56 swipeEvents.eachTouch(evt, function(touch) { 57 var loc = swipeEvents.touches[touch.identifier] 58 if(loc) loc.move(touch) 59 }) 60 } 61 62 swipeEvents.prototype.touchCancel = function(evt) { 63 swipeEvents.eachTouch(evt, function(touch) { 64 swipeEvents.purge(touch, true) 65 }) 66 } 67 68 swipeEvents.prototype.touchEnd = function(evt) { 69 swipeEvents.eachTouch(evt, function(touch) { 70 swipeEvents.purge(touch) 71 }) 72 } 73 74 swipeEvents.touches = {} 75 swipeEvents.purge = function(touch, cancelled) { 76 if(!cancelled) { 77 var loc = swipeEvents.touches[touch.identifier] 78 if(loc) loc.process() 79 } 80 delete swipeEvents.touches[touch.identifier] 81 } 82 83 swipeEvents.eachTouch = function(evt, callback) { 84 var evt = evt.originalEvent; 85 var num = evt.changedTouches.length; 86 for(var i = 0; i < num; i++) { 87 callback(evt.changedTouches[i]) 88 } 89 } 90 91 // adds custom events: 92 // touch // all events 93 // swipe // only swipe* events 94 // swipeleft 95 // swiperight 96 // swipeup 97 // swipedown 98 // tap 99 // doubletap 100 $.fn.addSwipeEvents = function(callback) { 101 new swipeEvents(this); 102 if(callback) this.bind('touch', callback) 103 return this; 104 } 105})(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.