File indexing completed on 2024-12-29 05:26:12
0001 (function() { 0002 0003 var event = jQuery.event, 0004 0005 //helper that finds handlers by type and calls back a function, this is basically handle 0006 // events - the events object 0007 // types - an array of event types to look for 0008 // callback(type, handlerFunc, selector) - a callback 0009 // selector - an optional selector to filter with, if there, matches by selector 0010 // if null, matches anything, otherwise, matches with no selector 0011 findHelper = function( events, types, callback, selector ) { 0012 var t, type, typeHandlers, all, h, handle, 0013 namespaces, namespace, 0014 match; 0015 for ( t = 0; t < types.length; t++ ) { 0016 type = types[t]; 0017 all = type.indexOf(".") < 0; 0018 if (!all ) { 0019 namespaces = type.split("."); 0020 type = namespaces.shift(); 0021 namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join("\\.(?:.*\\.)?") + "(\\.|$)"); 0022 } 0023 typeHandlers = (events[type] || []).slice(0); 0024 0025 for ( h = 0; h < typeHandlers.length; h++ ) { 0026 handle = typeHandlers[h]; 0027 0028 match = (all || namespace.test(handle.namespace)); 0029 0030 if(match){ 0031 if(selector){ 0032 if (handle.selector === selector ) { 0033 callback(type, handle.origHandler || handle.handler); 0034 } 0035 } else if (selector === null){ 0036 callback(type, handle.origHandler || handle.handler, handle.selector); 0037 } 0038 else if (!handle.selector ) { 0039 callback(type, handle.origHandler || handle.handler); 0040 0041 } 0042 } 0043 0044 0045 } 0046 } 0047 }; 0048 0049 /** 0050 * Finds event handlers of a given type on an element. 0051 * @param {HTMLElement} el 0052 * @param {Array} types an array of event names 0053 * @param {String} [selector] optional selector 0054 * @return {Array} an array of event handlers 0055 */ 0056 event.find = function( el, types, selector ) { 0057 var events = ( $._data(el) || {} ).events, 0058 handlers = [], 0059 t, liver, live; 0060 0061 if (!events ) { 0062 return handlers; 0063 } 0064 findHelper(events, types, function( type, handler ) { 0065 handlers.push(handler); 0066 }, selector); 0067 return handlers; 0068 }; 0069 /** 0070 * Finds all events. Group by selector. 0071 * @param {HTMLElement} el the element 0072 * @param {Array} types event types 0073 */ 0074 event.findBySelector = function( el, types ) { 0075 var events = $._data(el).events, 0076 selectors = {}, 0077 //adds a handler for a given selector and event 0078 add = function( selector, event, handler ) { 0079 var select = selectors[selector] || (selectors[selector] = {}), 0080 events = select[event] || (select[event] = []); 0081 events.push(handler); 0082 }; 0083 0084 if (!events ) { 0085 return selectors; 0086 } 0087 //first check live: 0088 /*$.each(events.live || [], function( i, live ) { 0089 if ( $.inArray(live.origType, types) !== -1 ) { 0090 add(live.selector, live.origType, live.origHandler || live.handler); 0091 } 0092 });*/ 0093 //then check straight binds 0094 findHelper(events, types, function( type, handler, selector ) { 0095 add(selector || "", type, handler); 0096 }, null); 0097 0098 return selectors; 0099 }; 0100 event.supportTouch = "ontouchend" in document; 0101 0102 $.fn.respondsTo = function( events ) { 0103 if (!this.length ) { 0104 return false; 0105 } else { 0106 //add default ? 0107 return event.find(this[0], $.isArray(events) ? events : [events]).length > 0; 0108 } 0109 }; 0110 $.fn.triggerHandled = function( event, data ) { 0111 event = (typeof event == "string" ? $.Event(event) : event); 0112 this.trigger(event, data); 0113 return event.handled; 0114 }; 0115 /** 0116 * Only attaches one event handler for all types ... 0117 * @param {Array} types llist of types that will delegate here 0118 * @param {Object} startingEvent the first event to start listening to 0119 * @param {Object} onFirst a function to call 0120 */ 0121 event.setupHelper = function( types, startingEvent, onFirst ) { 0122 if (!onFirst ) { 0123 onFirst = startingEvent; 0124 startingEvent = null; 0125 } 0126 var add = function( handleObj ) { 0127 0128 var bySelector, selector = handleObj.selector || ""; 0129 if ( selector ) { 0130 bySelector = event.find(this, types, selector); 0131 if (!bySelector.length ) { 0132 $(this).delegate(selector, startingEvent, onFirst); 0133 } 0134 } 0135 else { 0136 //var bySelector = event.find(this, types, selector); 0137 if (!event.find(this, types, selector).length ) { 0138 event.add(this, startingEvent, onFirst, { 0139 selector: selector, 0140 delegate: this 0141 }); 0142 } 0143 0144 } 0145 0146 }, 0147 remove = function( handleObj ) { 0148 var bySelector, selector = handleObj.selector || ""; 0149 if ( selector ) { 0150 bySelector = event.find(this, types, selector); 0151 if (!bySelector.length ) { 0152 $(this).undelegate(selector, startingEvent, onFirst); 0153 } 0154 } 0155 else { 0156 if (!event.find(this, types, selector).length ) { 0157 event.remove(this, startingEvent, onFirst, { 0158 selector: selector, 0159 delegate: this 0160 }); 0161 } 0162 } 0163 }; 0164 $.each(types, function() { 0165 event.special[this] = { 0166 add: add, 0167 remove: remove, 0168 setup: function() {}, 0169 teardown: function() {} 0170 }; 0171 }); 0172 }; 0173 })(jQuery); 0174 (function($){ 0175 var isPhantom = /Phantom/.test(navigator.userAgent), 0176 supportTouch = !isPhantom && "ontouchend" in document, 0177 scrollEvent = "touchmove scroll", 0178 // Use touch events or map it to mouse events 0179 touchStartEvent = supportTouch ? "touchstart" : "mousedown", 0180 touchStopEvent = supportTouch ? "touchend" : "mouseup", 0181 touchMoveEvent = supportTouch ? "touchmove" : "mousemove", 0182 data = function(event){ 0183 var d = event.originalEvent.touches ? 0184 event.originalEvent.touches[ 0 ] : 0185 event; 0186 return { 0187 time: (new Date).getTime(), 0188 coords: [ d.pageX, d.pageY ], 0189 origin: $( event.target ) 0190 }; 0191 }; 0192 0193 /** 0194 * @add jQuery.event.swipe 0195 */ 0196 var swipe = $.event.swipe = { 0197 /** 0198 * @attribute delay 0199 * Delay is the upper limit of time the swipe motion can take in milliseconds. This defaults to 500. 0200 * 0201 * A user must perform the swipe motion in this much time. 0202 */ 0203 delay : 500, 0204 /** 0205 * @attribute max 0206 * The maximum distance the pointer must travel in pixels. The default is 75 pixels. 0207 */ 0208 max : 75, 0209 /** 0210 * @attribute min 0211 * The minimum distance the pointer must travel in pixels. The default is 30 pixels. 0212 */ 0213 min : 30 0214 }; 0215 0216 $.event.setupHelper( [ 0217 0218 /** 0219 * @hide 0220 * @attribute swipe 0221 */ 0222 "swipe", 0223 /** 0224 * @hide 0225 * @attribute swipeleft 0226 */ 0227 'swipeleft', 0228 /** 0229 * @hide 0230 * @attribute swiperight 0231 */ 0232 'swiperight', 0233 /** 0234 * @hide 0235 * @attribute swipeup 0236 */ 0237 'swipeup', 0238 /** 0239 * @hide 0240 * @attribute swipedown 0241 */ 0242 'swipedown'], touchStartEvent, function(ev){ 0243 var 0244 // update with data when the event was started 0245 start = data(ev), 0246 stop, 0247 delegate = ev.delegateTarget || ev.currentTarget, 0248 selector = ev.handleObj.selector, 0249 entered = this; 0250 0251 function moveHandler(event){ 0252 if ( !start ) { 0253 return; 0254 } 0255 // update stop with the data from the current event 0256 stop = data(event); 0257 0258 // prevent scrolling 0259 if ( Math.abs( start.coords[0] - stop.coords[0] ) > 10 ) { 0260 event.preventDefault(); 0261 } 0262 }; 0263 0264 // Attach to the touch move events 0265 $(document.documentElement).bind(touchMoveEvent, moveHandler) 0266 .one(touchStopEvent, function(event){ 0267 $(this).unbind( touchMoveEvent, moveHandler); 0268 // if start and stop contain data figure out if we have a swipe event 0269 if ( start && stop ) { 0270 // calculate the distance between start and stop data 0271 var deltaX = Math.abs(start.coords[0] - stop.coords[0]), 0272 deltaY = Math.abs(start.coords[1] - stop.coords[1]), 0273 distance = Math.sqrt(deltaX*deltaX+deltaY*deltaY); 0274 0275 // check if the delay and distance are matched 0276 if ( stop.time - start.time < swipe.delay && distance >= swipe.min ) { 0277 var events = ['swipe']; 0278 // check if we moved horizontally 0279 if( deltaX >= swipe.min && deltaY < swipe.min) { 0280 // based on the x coordinate check if we moved left or right 0281 events.push( start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight" ); 0282 } else 0283 // check if we moved vertically 0284 if(deltaY >= swipe.min && deltaX < swipe.min){ 0285 // based on the y coordinate check if we moved up or down 0286 events.push( start.coords[1] < stop.coords[1] ? "swipedown" : "swipeup" ); 0287 } 0288 0289 // trigger swipe events on this guy 0290 $.each($.event.find(delegate, events, selector), function(){ 0291 this.call(entered, ev, {start : start, end: stop}) 0292 }) 0293 0294 } 0295 } 0296 // reset start and stop 0297 start = stop = undefined; 0298 }) 0299 }); 0300 0301 })(jQuery)