OSDN Git Service

version 0.6.20, bugfix & add opacity0.gif
[pettanr/clientJs.git] / 0.6.x / js / ui / 05_XUI_Gesture.js
1 /* original:\r
2  *  Hammer.JS - v1.0.5 - 2013-04-07\r
3  *  http://eightmedia.github.com/hammer.js\r
4  *  Jorik Tangelder <j.tangelder@gmail.com>, MIT license\r
5  **/\r
6 \r
7 ( function( Math, window, document, undefined ){\r
8         \r
9         var ELEENT_LIST = [],\r
10                 HAMMER_LIST = [],\r
11                 POINTERS    = [],\r
12                 ABS = new Function( 'v', 'return v<0?-v:v' );\r
13         \r
14         X.UI.Gesture = Hammer;\r
15         \r
16         function Hammer( uinodeRoot, uinode, type ){\r
17                 this.uinode  = uinode;\r
18                 this.enabled = true;\r
19                 \r
20                 Hammer.startup && Hammer.startup( uinodeRoot );\r
21 \r
22                 this.options = Hammer.defaults;\r
23 \r
24                 // start detection on touchstart\r
25                 Utils.addEvents( uinode, Hammer.EVENT_TYPES_START, this );\r
26                 \r
27                 this.listen( type );\r
28         };\r
29         \r
30         Hammer.defaults = {};\r
31         \r
32         Hammer.prototype.handleEvent = function( e ){\r
33                 //var sourceEventType = e.type.toLowerCase();\r
34 \r
35                 var type       = IdToGestureID[ e.type ],\r
36                         gestures   = Detection.gestures,\r
37                         numTouches = 0,// count the total touches on the screen\r
38                         pointerType, i, l, touches, ret, active, gesture, startEv,\r
39                         deltaTime, deltaX, deltaY, velocity;\r
40 \r
41                 if( !type ) return;\r
42                 \r
43                 if( e.pointerType ){\r
44                         type |= POINTER;\r
45                         switch( e.pointerType ){\r
46                                 case 'touch' :\r
47                                 case e.MSPOINTER_TYPE_TOUCH :\r
48                                         type |= TOUCH; break;\r
49                                 case 'pen' :\r
50                                 case e.MSPOINTER_TYPE_PEN :\r
51                                         type |= PEN; break;\r
52                                 case 'mouse' :\r
53                                 case e.MSPOINTER_TYPE_MOUSE :\r
54                                         type |= MOUSE; break;\r
55                                 default :\r
56                                         return;\r
57                         };\r
58                 } else\r
59                 if( e.touches ){\r
60                         type |= TOUCH;\r
61                 } else {\r
62                         type |= MOUSE;\r
63                 };\r
64                 \r
65                 // onmouseup, but when touchend has been fired we do nothing.\r
66                 // this is for touchdevices which also fire a mouseup on touchend\r
67                 if( type & MOUSE && touch_triggered ){\r
68                         return X.Callback.STOP_NOW | X.Callback.STOP_PROPAGATION | X.Callback.PREVENT_DEFAULT;\r
69                 }\r
70                 // mousebutton must be down or a touch event\r
71                 else if (\r
72                         type & TOUCH || //sourceEventType.match(/touch/) || // touch events are always on screen\r
73                         ( type & POINTER && type & START ) || //sourceEventType.match(/pointerdown/) || // pointerevents touch\r
74                         ( type & MOUSE   && e.which === 1 ) //(sourceEventType.match(/mouse/) && e.which === 1) // mouse is pressed\r
75                 ){\r
76                         enable_detect = true;\r
77                 };\r
78 \r
79                 // we are in a touch event, set the touch triggered bool to true,\r
80                 // this for the conflicts that may occur on ios and android\r
81                 type & ( TOUCH | POINTER ) && ( touch_triggered = true );\r
82                 //if (sourceEventType.match(/touch|pointer/)) { touch_triggered = true;}\r
83 \r
84                 // when touch has been triggered in this detection session\r
85                 // and we are now handling a mouse event, we stop that to prevent conflicts\r
86                 if( enable_detect ){\r
87                         // update pointerevent\r
88                         if( Hammer.HAS_POINTEREVENTS ){ //eventType !== Hammer.EVENT_END ){\r
89                                 POINTERS[ e.identifier = e.pointerId ] = type & END ? null : e;\r
90                                 touches = [];\r
91                                 // we can use forEach since pointerEvents only is in IE10\r
92                                 for( i = 0, l = POINTERS.length; i < l; ++i ){\r
93                                         POINTERS[ i ] && ( touches[ touches.length ] = POINTERS[ i ] );\r
94                                 };\r
95                                 numTouches = touches.length;\r
96                         } else\r
97                         // touch\r
98                         if ( type & TOUCH ){ //sourceEventType.match(/touch/)) {\r
99                                 touches    = Hammer.DO_TOUCHES_FIX && type & END ? [] : e.touches;\r
100                                 numTouches = touches.length;\r
101                         } else\r
102                         // mouse\r
103                         if( !touch_triggered ){\r
104                                 numTouches = ( type & END ) ? 0 : 1;\r
105                                 touches    = numTouches === 0 ? [] : [{\r
106                                         identifier : 1,\r
107                                         pageX      : e.pageX,\r
108                                         pageY      : e.pageY,\r
109                                         target     : e.target\r
110                                 }];\r
111                         };\r
112 \r
113                         // if we are in a end event, but when we remove one touch and\r
114                         // we still have enough, set eventType to move\r
115                         if( 0 < numTouches && type & END ){ // eventType === Hammer.EVENT_END ){\r
116                                 type = type & POINTER_TYPE_MASK | MOVE;\r
117                                 //eventType = Hammer.EVENT_MOVE;\r
118                         } else if( !numTouches ){\r
119                         // no touches, force the end event\r
120                                 type = type & POINTER_TYPE_MASK | END;\r
121                                 //eventType = Hammer.EVENT_END;\r
122                         };\r
123 \r
124                         // because touchend has no touches, and we often want to use these in our gestures,\r
125                         // we send the last move event as our eventData in touchend\r
126                         ( !numTouches && last_move_event !== null ) ?\r
127                                 ( e = last_move_event ):\r
128                                 ( last_move_event = e ); // store the last move event\r
129 \r
130                         e = {\r
131                                 center      : Utils.getCenter( touches ),\r
132                                 timeStamp   : Date.now ? Date.now() : +new Date,\r
133                                 target      : e.target,\r
134                                 touches     : touches,\r
135                                 eventType   : type & EVENT_TYPE_MASK,\r
136                                 pointerType : type & POINTER_TYPE_MASK\r
137                         };\r
138 \r
139                         if( type & START ){\r
140                                 if( !this.enabled ) return;\r
141                                 // already busy with a Hammer.gesture detection on an element\r
142                                 if( Detection.current ) return;\r
143                                 Detection.current = {\r
144                                         hammer     : this, // reference to HammerInstance we're working for\r
145                                         startEvent : Utils.extend( {}, e ), // start eventData for distances, timing etc\r
146                                         lastEvent  : false, // last eventData\r
147                                         name       : '' // current gesture we're in/detected, can be 'tap', 'hold' etc\r
148                                 };\r
149                                 Detection.stopped = false;\r
150                                 hammer = this;\r
151                                 active = hammer.activeGesture;\r
152                         } else\r
153                         if( !Detection.current || Detection.stopped ){\r
154                                 return;\r
155                         } else {\r
156                                 hammer = Detection.current.hammer;\r
157                                 active = hammer.activeGesture;\r
158                         };\r
159                         \r
160                         // ----------------------------------------------------------------------------------------------------------------\r
161                         // ret = Detection.detect( e );\r
162 \r
163                         // ----------------------------------------------------------------------------------------------------------------\r
164                         // extend event data with calculations about scale, distance etc\r
165                         // e = Detection.extendEventData( e );\r
166                         startEv = Detection.current.startEvent;\r
167                         center  = e.center;\r
168 \r
169                         // if the touches change, set the new touches over the startEvent touches\r
170                         // this because touchevents don't have all the touches on touchstart, or the\r
171                         // user must place his fingers at the EXACT same time on the screen, which is not realistic\r
172                         // but, sometimes it happens that both fingers are touching at the EXACT same time\r
173                         if( startEv && ( numTouches !== startEv.touches.length || touches === startEv.touches ) ){\r
174                                 // extend 1 level deep to get the touchlist with the touch objects\r
175                                 startEv.touches.length = i = 0;\r
176                                 for( ; i < numTouches; ++i ){\r
177                                         startEv.touches[ startEv.touches.length ] = Utils.extend( {}, touches[ i ] );\r
178                                 };\r
179                         };\r
180 \r
181                         deltaTime = e.timeStamp  - startEv.timeStamp;\r
182                         deltaX    = center.pageX - startEv.center.pageX;\r
183                         deltaY    = center.pageY - startEv.center.pageY;\r
184                         velocity  = Utils.getVelocity( deltaTime, deltaX, deltaY );\r
185 \r
186                         Utils.extend( e, {\r
187                                 deltaTime  : deltaTime,\r
188 \r
189                                 deltaX     : deltaX,\r
190                                 deltaY     : deltaY,\r
191 \r
192                                 velocityX  : velocity.x,\r
193                                 velocityY  : velocity.y,\r
194 \r
195                                 distance   : Utils.getDistance( startEv.center, center ),\r
196                                 angle      : Utils.getAngle( startEv.center, center ),\r
197                                 direction  : Utils.getDirection( startEv.center, center ),\r
198 \r
199                                 scale      : Utils.getScale( startEv.touches, touches ),\r
200                                 rotation   : Utils.getRotation( startEv.touches, touches ),\r
201 \r
202                                 startEvent : startEv\r
203                         });\r
204 \r
205                         // store as previous event event\r
206                         Detection.current.lastEvent = e;\r
207                         \r
208                         // call Hammer.gesture handlers\r
209                         for( i = 0, l = gestures.length; i < l; ++i ){\r
210                                 gesture = gestures[ i ];\r
211                                 if( Detection.stopped ) break;\r
212                                 //if( active[ gesture.name ] ) console.log( gesture.name );\r
213                                 // only when the instance options have enabled this gesture\r
214                                 active[ gesture.name ] &&\r
215                                         // if a handler returns false, we stop with the detection\r
216                                         ( ret |= ( gesture.handler.call( gesture, e, hammer ) || X.Callback.NONE ) );\r
217                         };\r
218 \r
219                         // endevent, but not the last touch, so dont stop\r
220                         type & END && numTouches === 0 && Detection.stopDetect();\r
221                         \r
222                         // ----------------------------------------------------------------------------------------------------------------\r
223                         // trigger the handler\r
224                         //handler.call( context, HamEvent.collectEventData( element, eventType, e ) );\r
225 \r
226                         // remove pointerevent from list\r
227                         if( Hammer.HAS_POINTEREVENTS && type & END ){ // eventType === Hammer.EVENT_END ){\r
228                                 numTouches = 0;\r
229                         };\r
230                 };\r
231 \r
232                 //debug(sourceEventType +" "+ eventType);\r
233 \r
234                 // on the end we reset everything\r
235                 if( numTouches === 0 ){\r
236                         last_move_event = null;\r
237                         enable_detect   = false;\r
238                         touch_triggered = false;\r
239                         POINTERS.length = 0;\r
240                 };\r
241                 \r
242                 return ret;\r
243         };\r
244         \r
245         Hammer.startup = function( uinodeRoot ){\r
246                 // find what eventtypes we add listeners to\r
247                 /**\r
248                  * we have different events for each device/browser\r
249                  * determine what we need and set them in the Hammer.EVENT_TYPES constant\r
250                  */\r
251                 // determine the eventtype we want to set\r
252                 // for non pointer events browsers and mixed browsers,\r
253                 // like chrome on windows8 touch laptop         \r
254                 var types, name;\r
255 \r
256                 // Register all gestures inside Gestures\r
257                 for( name in Gestures ){\r
258                         //Gestures.hasOwnProperty( name ) && \r
259                         Detection.register( Gestures[ name ] );\r
260                 };\r
261 \r
262                 if( navigator.pointerEnabled || navigator.msPointerEnabled ){\r
263                         Hammer.EVENT_TYPES_START = [ X.UI.Event._POINTER_DOWN ];\r
264                         types = [ X.UI.Event._POINTER_MOVE, X.UI.Event._POINTER_UP, X.UI.Event._POINTER_CANCEL ];\r
265                 } else\r
266                 if( window.ontouchstart !== void 0 ){\r
267                         Hammer.EVENT_TYPES_START = [ X.UI.Event._TOUCH_START ];\r
268                         types = [ X.UI.Event._TOUCH_MOVE, X.UI.Event._MOUSE_MOVE, X.UI.Event._TOUCH_END, X.UI.Event._TOUCH_CANCEL ];\r
269                 } else {\r
270                         Hammer.EVENT_TYPES_START = [ X.UI.Event._MOUSE_DOWN ];\r
271                         types = [ X.UI.Event._MOUSE_MOVE, X.UI.Event._MOUSE_UP, X.UI.Event._MOUSE_CANCEL ];\r
272                 };\r
273 \r
274                 // Add touch events on the document\r
275                 Utils.addEvents( uinodeRoot, types, Hammer.prototype.handleEvent );\r
276 \r
277                 // Hammer is ready...!\r
278                 delete Hammer.startup;\r
279         };\r
280         \r
281         Hammer.prototype.trigger = function( type, gesture ){\r
282                 if( !this.types[ type ] ) return;\r
283                 var e = Utils.extend( {}, gesture );\r
284                 e.type = type;\r
285                 console.log( 'trigger : ' + type );\r
286                 return this.uinode.dispatch( e );\r
287         };\r
288         \r
289         Hammer.prototype.listen = function( type ){\r
290                 var gestures = Detection.gestures,\r
291                         i = gestures.length, g;\r
292                 for( ; i; ){\r
293                         g = gestures[ --i ];\r
294                         if( g.startID <= type && type <= g.endID ){\r
295                                 if( !this.activeGesture ) this.activeGesture = {};\r
296                                 if( !this.types ) this.types = {};\r
297                                 this.activeGesture[ g.name ] = this.types[ type ] = 1;\r
298                                 return;\r
299                         };\r
300                 };\r
301         };\r
302         \r
303         Hammer.prototype.unlisten = function( type ){\r
304                 var gestures = Detection.gestures,\r
305                         i = gestures.length, g;\r
306                 if( !this.activeGesture ) return;\r
307                 for( ; i; ){\r
308                         g = gestures[ --i ];\r
309                         if( g.startID <= type && type <= g.endID ){\r
310                                 if( this.activeGesture[ g.name ] ){\r
311                                         if( this.types[ type ] ) delete this.types[ type ];\r
312                                         for( i = g.startID; i <= g.endID; ++i ){\r
313                                                 if( this.types[ i ] ) return;\r
314                                         };\r
315                                         delete this.activeGesture[ g.name ];\r
316                                 };\r
317                                 return;\r
318                         };\r
319                 };\r
320         };\r
321         \r
322         /*\r
323          *  "Android version < 2.2" return ev.touches.length === 1 when touchend, others return ev.touches.length === 0\r
324          */\r
325         Hammer.DO_TOUCHES_FIX = Hammer.HAS_TOUCHEVENTS && ( function( ua, i ){\r
326                                 if( ( i = ua.indexOf('android') ) === -1 ) return false;\r
327                                 return ( parseFloat( ua.substr( i + 8 ) ) || 0 ) < 2.2;\r
328                         })( navigator.userAgent.toLowerCase() );\r
329         \r
330         // detect touchevents\r
331         Hammer.HAS_POINTEREVENTS = navigator.pointerEnabled || navigator.msPointerEnabled;\r
332 \r
333         // eventtypes per touchevent (start, move, end)\r
334         // are filled by HamEvent.determineEventTypes on setup\r
335         Hammer.EVENT_TYPES_START = null;\r
336 \r
337         // direction defines\r
338         Hammer.DIRECTION_DOWN  = 'down';\r
339         Hammer.DIRECTION_LEFT  = 'left';\r
340         Hammer.DIRECTION_UP    = 'up';\r
341         Hammer.DIRECTION_RIGHT = 'right';\r
342 \r
343         // plugins namespace\r
344         Hammer.plugins = {};\r
345 \r
346         var POINTER     = 1,\r
347                 TOUCH       = 2,\r
348                 PEN         = 8, //4,\r
349                 MOUSE       = 8,\r
350                 START       = 16,\r
351                 MOVE        = 32,\r
352                 END         = 64,\r
353                 CANCEL      = 128,\r
354                 EVENT_TYPE_MASK   = START | MOVE | END,\r
355                 POINTER_TYPE_MASK = POINTER | TOUCH | MOUSE | PEN,\r
356                 IdToGestureID = {};\r
357         IdToGestureID[ X.UI.Event._POINTER_DOWN   ] = START;\r
358         IdToGestureID[ X.UI.Event._POINTER_MOVE   ] = MOVE;\r
359         IdToGestureID[ X.UI.Event._POINTER_UP     ] = END;\r
360         IdToGestureID[ X.UI.Event._POINTER_CANCEL ] = END;\r
361         \r
362         IdToGestureID[ X.UI.Event._TOUCH_START  ] = START;\r
363         IdToGestureID[ X.UI.Event._TOUCH_MOVE   ] = MOVE;\r
364         IdToGestureID[ X.UI.Event._TOUCH_END    ] = END;\r
365         IdToGestureID[ X.UI.Event._TOUCH_CANCEL ] = END;\r
366         \r
367         IdToGestureID[ X.UI.Event._MOUSE_DOWN   ] = START;\r
368         IdToGestureID[ X.UI.Event._MOUSE_MOVE   ] = MOVE;\r
369         IdToGestureID[ X.UI.Event._MOUSE_UP     ] = END;\r
370         IdToGestureID[ X.UI.Event._MOUSE_CANCEL ] = END;\r
371         \r
372         Utils = {\r
373                 \r
374                 /**\r
375                  * touch events with mouse fallback\r
376                  * @param   {HTMLElement}   element\r
377                  * @param   {String}        eventType        like Hammer.EVENT_MOVE\r
378                  * @param   {Function}      handler\r
379                  */\r
380                 addEvents : function( uinode, types, context ){\r
381                         for( var i = 0; i < types.length; ++i ){\r
382                                 uinode.listen( types[ i ], context );\r
383                         };\r
384                 },\r
385                 \r
386                 /**\r
387                  * extend method,\r
388                  * also used for cloning when dest is an empty object\r
389                  * @param   {Object}    dest\r
390                  * @param   {Object}    src\r
391                  * @parm        {Boolean}       merge           do a merge\r
392                  * @returns {Object}    dest\r
393                  */\r
394                 extend : function extend( dest, src, merge ){\r
395                         for( var key in src ){\r
396                                 if( dest[ key ] !== undefined && merge ) continue;\r
397                                 dest[ key ] = src[ key ];\r
398                         };\r
399                         return dest;\r
400                 },\r
401 \r
402                 /**\r
403                  * find if a node is in the given parent\r
404                  * used for event delegation tricks\r
405                  * @param   {HTMLElement}   node\r
406                  * @param   {HTMLElement}   parent\r
407                  * @returns {boolean}       has_parent\r
408                  */\r
409                 hasParent : function( node, parent ){\r
410                         while( node && node.tagName ){ /* tagName for ie */\r
411                                 if( node === parent ) return true;\r
412                                 node = node.parentNode;\r
413                         };\r
414                         return false;\r
415                 },\r
416 \r
417                 /**\r
418                  * get the center of all the touches\r
419                  * @param   {Array}     touches\r
420                  * @returns {Object}    center\r
421                  */\r
422                 getCenter : function getCenter(touches) {\r
423                         var i = 0,\r
424                                 l = touches.length,\r
425                                 valuesX, valuesY;\r
426                         switch( l ){\r
427                                 case 0 :\r
428                                         return {};\r
429                                 case 1 :\r
430                                         return {\r
431                                                 pageX : touches[ 0 ].pageX,\r
432                                                 pageY : touches[ 0 ].pageY\r
433                                         };\r
434                                 case 2 :\r
435                                         return {\r
436                                                 pageX : ( touches[ 0 ].pageX + touches[ 1 ].pageX ) / 2,\r
437                                                 pageY : ( touches[ 0 ].pageY + touches[ 1 ].pageY ) / 2\r
438                                         };\r
439                         };\r
440                         valuesX = [];\r
441                         valuesY = [];\r
442                         for( ; i < l; ++i ){\r
443                                 valuesX[ valuesX.length ] = touches[ i ].pageX;\r
444                                 valuesY[ valuesY.length ] = touches[ i ].pageY;\r
445                         };\r
446                         return {\r
447                                 pageX : ( ( Math.min.apply( null, valuesX ) + Math.max.apply( null, valuesX ) ) / 2 ),\r
448                                 pageY : ( ( Math.min.apply( null, valuesY ) + Math.max.apply( null, valuesY ) ) / 2 )\r
449                         };\r
450                 },\r
451 \r
452                 /**\r
453                  * calculate the velocity between two points\r
454                  * @param   {Number}    deltaTime\r
455                  * @param   {Number}    deltaX\r
456                  * @param   {Number}    deltaY\r
457                  * @returns {Object}    velocity\r
458                  */\r
459                 getVelocity : function getVelocity( deltaTime, deltaX, deltaY ) {\r
460                         return {\r
461                                 x : ABS( deltaX / deltaTime ) || 0,\r
462                                 y : ABS( deltaY / deltaTime ) || 0\r
463                         };\r
464                 },\r
465 \r
466                 /**\r
467                  * calculate the angle between two coordinates\r
468                  * @param   {Touch}     touch1\r
469                  * @param   {Touch}     touch2\r
470                  * @returns {Number}    angle\r
471                  */\r
472                 getAngle : function getAngle(touch1, touch2) {\r
473                         var y = touch2.pageY - touch1.pageY,\r
474                                 x = touch2.pageX - touch1.pageX;\r
475                         return Math.atan2( y, x ) * 180 / Math.PI;\r
476                 },\r
477 \r
478                 /**\r
479                  * angle to direction define\r
480                  * @param   {Touch}     touch1\r
481                  * @param   {Touch}     touch2\r
482                  * @returns {String}    direction constant, like Hammer.DIRECTION_LEFT\r
483                  */\r
484                 getDirection : function getDirection( touch1, touch2 ){\r
485                         var x = touch1.pageX - touch2.pageX,\r
486                                 y = touch1.pageY - touch2.pageY;\r
487                         return ABS( y ) <= ABS( x ) ?\r
488                                 ( x > 0 ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT ) :\r
489                                 ( y > 0 ? Hammer.DIRECTION_UP   : Hammer.DIRECTION_DOWN );\r
490                 },\r
491 \r
492                 /**\r
493                  * calculate the distance between two touches\r
494                  * @param   {Touch}     touch1\r
495                  * @param   {Touch}     touch2\r
496                  * @returns {Number}    distance\r
497                  */\r
498                 getDistance : function getDistance( touch1, touch2 ){\r
499                         var x = touch2.pageX - touch1.pageX,\r
500                                 y = touch2.pageY - touch1.pageY;\r
501                         return Math.sqrt( ( x * x ) + ( y * y ) );\r
502                 },\r
503 \r
504                 /**\r
505                  * calculate the scale factor between two touchLists (fingers)\r
506                  * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out\r
507                  * @param   {Array}     start\r
508                  * @param   {Array}     end\r
509                  * @returns {Number}    scale\r
510                  */\r
511                 getScale : function getScale( start, end ){\r
512                         // need two fingers...\r
513                         return ( 2 <= start.length && 2 <= end.length ) ?\r
514                                 Utils.getDistance( end[ 0 ], end[ 1 ] ) / Utils.getDistance( start[ 0 ], start[ 1 ] ) :\r
515                                 1;\r
516                 },\r
517 \r
518                 /**\r
519                  * calculate the rotation degrees between two touchLists (fingers)\r
520                  * @param   {Array}     start\r
521                  * @param   {Array}     end\r
522                  * @returns {Number}    rotation\r
523                  */\r
524                 getRotation : function getRotation( start, end ){\r
525                         // need two fingers\r
526                         return ( 2 <= start.length && 2 <= end.length ) ?\r
527                                 Utils.getAngle( end[ 1 ], end[ 0 ] ) - Utils.getAngle( start[ 1 ], start[ 0 ] ) :\r
528                                 0;\r
529                 },\r
530 \r
531                 /**\r
532                  * boolean if the direction is vertical\r
533                  * @param    {String}    direction\r
534                  * @returns  {Boolean}   is_vertical\r
535                  */\r
536                 isVertical : function isVertical( direction ){\r
537                         return direction === Hammer.DIRECTION_UP || direction === Hammer.DIRECTION_DOWN;\r
538                 }\r
539         };\r
540         \r
541         /**\r
542          * this holds the last move event,\r
543          * used to fix empty touchend issue\r
544          * see the onTouch event for an explanation\r
545          * @type {Object}\r
546          */\r
547         var last_move_event = null;\r
548 \r
549         /**\r
550          * when the mouse is hold down, this is true\r
551          * @type {Boolean}\r
552          */\r
553         var enable_detect = false;\r
554 \r
555         /**\r
556          * when touch events have been fired, this is true\r
557          * @type {Boolean}\r
558          */\r
559         var touch_triggered = false;\r
560         \r
561         Detection = {\r
562                 // contains all registred Gestures in the correct order\r
563                 gestures : [],\r
564 \r
565                 // data of the current Hammer.gesture detection session\r
566                 current : null,\r
567 \r
568                 // the previous Hammer.gesture session data\r
569                 // is a full clone of the previous gesture.current object\r
570                 previous : null,\r
571 \r
572                 // when this becomes true, no gestures are fired\r
573                 stopped : false,\r
574 \r
575                 /**\r
576                  * clear the Hammer.gesture vars\r
577                  * this is called on endDetect, but can also be used when a final Hammer.gesture has been detected\r
578                  * to stop other Gestures from being fired\r
579                  */\r
580                 stopDetect : function stopDetect() {\r
581                         // clone current data to the store as the previous gesture\r
582                         // used for the double tap gesture, since this is an other gesture detect session\r
583                         Detection.previous = Utils.extend( {}, Detection.current );\r
584 \r
585                         // reset the current\r
586                         Detection.current = null;\r
587 \r
588                         // stopped!\r
589                         Detection.stopped = true;\r
590                 },\r
591 \r
592                 /**\r
593                  * register new gesture\r
594                  * @param   {Object}    gesture object, see gestures.js for documentation\r
595                  * @returns {Array}     gestures\r
596                  */\r
597                 register : function( gesture ){\r
598                         // add an enable gesture options if there is no given\r
599                         var options = gesture.defaults || {},\r
600                                 list    = Detection.gestures,\r
601                                 _index, i = 0, l = list.length, index;\r
602                         if( options[ gesture.name ] === undefined ) options[ gesture.name ] = true;\r
603 \r
604                         // extend Hammer default options with the Hammer.gesture options\r
605                         Utils.extend( Hammer.defaults, options, true );\r
606 \r
607                         // set its index\r
608                         gesture.index = gesture.index || 1000;\r
609 \r
610                         // add Hammer.gesture to the list\r
611                         //Detection.gestures.push( gesture );\r
612 \r
613                         // sort the list by index\r
614                         //Detection.gestures.sort( function( a, b ){\r
615                         //      return\r
616                         //              a.index < b.index ? -1 :\r
617                         //              a.index > b.index ? 1 : 0;\r
618                         //});\r
619                         if( l === 0 ){\r
620                                 list[ 0 ] = gesture;\r
621                                 return;\r
622                         };\r
623                         _index = gesture.index;\r
624                         for( i = 0; i < l; ++i ){\r
625                                 index = list[ i ].index;\r
626                                 if( i === 0 && _index < index ){\r
627                                         list.unshift( gesture );\r
628                                         return;\r
629                                 } else\r
630                                 if( i === l - 1 ){\r
631                                         list[ l ] = gesture;\r
632                                         return;\r
633                                 } else\r
634                                 if( index <= _index && _index < list[ i + 1 ].index ){\r
635                                         list.splice( i, 0, gesture );\r
636                                         return;\r
637                                 };\r
638                         };\r
639                 }\r
640         };\r
641 \r
642         var Gestures = Gestures || {};\r
643 \r
644         /**\r
645          * Custom gestures\r
646          * ==============================\r
647          *\r
648          * Gesture object\r
649          * --------------------\r
650          * The object structure of a gesture:\r
651          *\r
652          * { name: 'mygesture',\r
653          *   index: 1337,\r
654          *   defaults: {\r
655          *     mygesture_option: true\r
656          *   }\r
657          *   handler: function(type, e, inst) {\r
658          *     // trigger gesture event\r
659          *     inst.trigger(this.name, e );\r
660          *   }\r
661          * }\r
662 \r
663          * @param   {String}    name\r
664          * this should be the name of the gesture, lowercase\r
665          * it is also being used to disable/enable the gesture per instance config.\r
666          *\r
667          * @param   {Number}    [index=1000]\r
668          * the index of the gesture, where it is going to be in the stack of gestures detection\r
669          * like when you build an gesture that depends on the drag gesture, it is a good\r
670          * idea to place it after the index of the drag gesture.\r
671          *\r
672          * @param   {Object}    [defaults={}]\r
673          * the default settings of the gesture. these are added to the instance settings,\r
674          * and can be overruled per instance. you can also add the name of the gesture,\r
675          * but this is also added by default (and set to true).\r
676          *\r
677          * @param   {Function}  handler\r
678          * this handles the gesture detection of your custom gesture and receives the\r
679          * following arguments:\r
680          *\r
681          *      @param  {Object}    eventData\r
682          *      event data containing the following properties:\r
683          *          timeStamp   {Number}        time the event occurred\r
684          *          target      {HTMLElement}   target element\r
685          *          touches     {Array}         touches (fingers, pointers, mouse) on the screen\r
686          *          pointerType {String}        kind of pointer that was used. matches Hammer.POINTER_MOUSE|TOUCH\r
687          *          center      {Object}        center position of the touches. contains pageX and pageY\r
688          *          deltaTime   {Number}        the total time of the touches in the screen\r
689          *          deltaX      {Number}        the delta on x axis we haved moved\r
690          *          deltaY      {Number}        the delta on y axis we haved moved\r
691          *          velocityX   {Number}        the velocity on the x\r
692          *          velocityY   {Number}        the velocity on y\r
693          *          angle       {Number}        the angle we are moving\r
694          *          direction   {String}        the direction we are moving. matches Hammer.DIRECTION_UP|DOWN|LEFT|RIGHT\r
695          *          distance    {Number}        the distance we haved moved\r
696          *          scale       {Number}        scaling of the touches, needs 2 touches\r
697          *          rotation    {Number}        rotation of the touches, needs 2 touches *\r
698          *          eventType   {String}        matches Hammer.EVENT_START|MOVE|END\r
699          *          srcEvent    {Object}        the source event, like TouchStart or MouseDown *\r
700          *          startEvent  {Object}        contains the same properties as above,\r
701          *                                      but from the first touch. this is used to calculate\r
702          *                                      distances, deltaTime, scaling etc\r
703          *\r
704          *      @param  {Hammer.Instance}    inst\r
705          *      the instance we are doing the detection for. you can get the options from\r
706          *      the inst.options object and trigger the gesture event by calling inst.trigger\r
707          *\r
708          *\r
709          * Handle gestures\r
710          * --------------------\r
711          * inside the handler you can get/set Detection.current. This is the current\r
712          * detection session. It has the following properties\r
713          *      @param  {String}    name\r
714          *      contains the name of the gesture we have detected. it has not a real function,\r
715          *      only to check in other gestures if something is detected.\r
716          *      like in the drag gesture we set it to 'drag' and in the swipe gesture we can\r
717          *      check if the current gesture is 'drag' by accessing Detection.current.name\r
718          *\r
719          *      @readonly\r
720          *      @param  {Hammer.Instance}    inst\r
721          *      the instance we do the detection for\r
722          *\r
723          *      @readonly\r
724          *      @param  {Object}    startEvent\r
725          *      contains the properties of the first gesture detection in this session.\r
726          *      Used for calculations about timing, distance, etc.\r
727          *\r
728          *      @readonly\r
729          *      @param  {Object}    lastEvent\r
730          *      contains all the properties of the last gesture detect in this session.\r
731          *\r
732          * after the gesture detection session has been completed (user has released the screen)\r
733          * the Detection.current object is copied into Detection.previous,\r
734          * this is usefull for gestures like doubletap, where you need to know if the\r
735          * previous gesture was a tap\r
736          *\r
737          * options that have been set by the instance can be received by calling inst.options\r
738          *\r
739          * You can trigger a gesture event by calling inst.trigger("mygesture", event).\r
740          * The first param is the name of your gesture, the second the event argument\r
741          *\r
742          *\r
743          * Register gestures\r
744          * --------------------\r
745          * When an gesture is added to the Gestures object, it is auto registered\r
746          * at the setup of the first Hammer instance. You can also call Detection.register\r
747          * manually and pass your gesture object as a param\r
748          *\r
749          */\r
750 \r
751         /**\r
752          * Hold\r
753          * Touch stays at the same place for x time\r
754          * @events  hold holdend\r
755          */\r
756         Gestures.Hold = {\r
757                 name  : 'hold',\r
758                 index : 10,\r
759                 startID : X.UI.Event.HOLD,\r
760                 endID   : X.UI.Event.HOLD_END,\r
761                 defaults : {\r
762                         hold_timeout   : 500,\r
763                         hold_threshold : 1\r
764                 },\r
765                 timerID : null,\r
766                 holding : false,\r
767                 handler : function holdGesture( e, hammer ){\r
768                         switch( e.eventType ){\r
769                                 case START :\r
770                                         // clear any running timers\r
771                                         this.timerID && X.Timer.remove( this.timerID );\r
772 \r
773                                         // set the gesture so we can check in the timeout if it still is\r
774                                         Detection.current.name = this.name;\r
775                                         Gestures.Hold.holding = false;\r
776                                         \r
777                                         // set timer and if after the timeout it still is hold,\r
778                                         // we trigger the hold event\r
779                                         this.timerID = X.Timer.add( hammer.options.hold_timeout, 1, Gestures.Hold._onTimer, [ e, hammer ] );\r
780                                         return;\r
781 \r
782                                 // when you move or end we clear the timer\r
783                                 case MOVE :\r
784                                         if( e.distance <= hammer.options.hold_threshold ) return;\r
785                                 case END :\r
786                                         this.timerID && X.Timer.remove( this.timerID );\r
787                                         if( Gestures.Hold.holding === true ){\r
788                                                 Gestures.Hold.holding = false;\r
789                                                 return hammer.trigger( X.UI.Event.HOLD_END, e );\r
790                                         };\r
791                                         break;\r
792                         };\r
793                 },\r
794                 _onTimer : function( e, hammer ){\r
795                         if( Detection.current.name === 'hold' ){\r
796                                 hammer.trigger( X.UI.Event.HOLD, e );\r
797                                 Gestures.Hold.holding = true;\r
798                         };\r
799                 }\r
800         };\r
801 \r
802         /**\r
803          * Tap/DoubleTap\r
804          * Quick touch at a place or double at the same place\r
805          * @events  tap, doubletap\r
806          */\r
807         Gestures.Tap = {\r
808                 name     : 'tap',\r
809                 index    : 100,\r
810                 startID  : X.UI.Event.TAP,\r
811                 endID    : X.UI.Event.DOUBLE_TAP,\r
812                 defaults : {\r
813                         tap_max_touchtime  : 250,\r
814                         tap_max_distance   : 10,\r
815                         tap_always         : true,\r
816                         doubletap_distance : 20,\r
817                         doubletap_interval : 300\r
818                 },\r
819                 handler : function tapGesture( e, hammer ){\r
820                         // previous gesture, for the double tap since these are two different gesture detections\r
821                         var prev = Detection.previous;\r
822                         if( e.eventType === END ){\r
823                                 // when the touchtime is higher then the max touch time\r
824                                 // or when the moving distance is too much\r
825                                 if( hammer.options.tap_max_touchtime < e.deltaTime || hammer.options.tap_max_distance < e.distance ) return;\r
826 \r
827                                 // check if double tap\r
828                                 if( prev && prev.name === 'tap' && ( e.timeStamp - prev.lastEvent.timeStamp ) < hammer.options.doubletap_interval && e.distance < hammer.options.doubletap_distance ){\r
829                                         return hammer.trigger( X.UI.Event.DOUBLE_TAP, e );\r
830                                 } else\r
831                                 // do a single tap\r
832                                 if( hammer.options.tap_always ){\r
833                                         Detection.current.name = 'tap';\r
834                                         return hammer.trigger(  X.UI.Event.TAP, e );\r
835                                 };\r
836                         };\r
837                 }\r
838         };\r
839 \r
840         /**\r
841          * Swipe\r
842          * triggers swipe events when the end velocity is above the threshold\r
843          * @events  swipe, swipeleft, swiperight, swipeup, swipedown\r
844          */\r
845         Gestures.Swipe = {\r
846                 name : 'swipe',\r
847                 index : 40,\r
848                 startID  : X.UI.Event.SWIP,\r
849                 endID    : X.UI.Event.SWIP_DOWN,\r
850                 defaults : {\r
851                         // set 0 for unlimited, but this can conflict with transform\r
852                         swipe_max_touches : 1,\r
853                         swipe_velocity : 0.7\r
854                 },\r
855                 handler : function swipeGesture(e, hammer) {\r
856                         if( e.eventType === END ){\r
857                                 // max touches\r
858                                 if( 0 < hammer.options.swipe_max_touches && hammer.options.swipe_max_touches < e.touches.length ) return;\r
859 \r
860                                 // when the distance we moved is too small we skip this gesture\r
861                                 // or we can be already in dragging\r
862                                 if( hammer.options.swipe_velocity < e.velocityX || hammer.options.swipe_velocity < e.velocityY ){\r
863                                         // trigger swipe events\r
864                                         hammer.trigger( X.UI.Event.SWIP, e );\r
865                                         hammer.trigger(\r
866                                                 e.direction === Hammer.DIRECTION_UP ?\r
867                                                         X.UI.Event.SWIP_UP :\r
868                                                 e.direction === Hammer.DIRECTION_DOWN ?\r
869                                                         X.UI.Event.SWIP_DOWN :\r
870                                                 e.direction === Hammer.DIRECTION_LEFT ?\r
871                                                         X.UI.Event.SWIP_LEFT :\r
872                                                         X.UI.Event.SWIP_RIGHT,\r
873                                                 e\r
874                                         );\r
875                                 };\r
876                         };\r
877                 }\r
878         };\r
879 \r
880         /**\r
881          * Drag\r
882          * Move with x fingers (default 1) around on the page. Blocking the scrolling when\r
883          * moving left and right is a good practice. When all the drag events are blocking\r
884          * you disable scrolling on that area.\r
885          * @events  drag, dragstart, dragend, drapleft, dragright, dragup, dragdown\r
886          */\r
887         Gestures.Drag = {\r
888                 name : 'drag',\r
889                 index : 50,\r
890                 startID  : X.UI.Event.DRAG,\r
891                 endID    : X.UI.Event.DRAG_DOWN,\r
892                 defaults : {\r
893                         drag_min_distance : 10,\r
894                         // set 0 for unlimited, but this can conflict with transform\r
895                         drag_max_touches : 1,\r
896                         // prevent default browser behavior when dragging occurs\r
897                         // be careful with it, it makes the element a blocking element\r
898                         // when you are using the drag gesture, it is a good practice to set this true\r
899                         drag_block_horizontal : false,\r
900                         drag_block_vertical : false,\r
901                         // drag_lock_to_axis keeps the drag gesture on the axis that it started on,\r
902                         // It disallows vertical directions if the initial direction was horizontal, and vice versa.\r
903                         drag_lock_to_axis : false,\r
904                         // drag lock only kicks in when distance > drag_lock_min_distance\r
905                         // This way, locking occurs only when the distance has become large enough to reliably determine the direction\r
906                         drag_lock_min_distance : 25\r
907                 },\r
908                 triggered : false,\r
909                 handler : function dragGesture( e, hammer ){\r
910                         var last_direction;\r
911                         // current gesture isnt drag, but dragged is true\r
912                         // this means an other gesture is busy. now call dragend\r
913                         if( Detection.current.name !== this.name && this.triggered ){\r
914                                 hammer.trigger( X.UI.Event.DRAG_END, e );\r
915                                 this.triggered = false;\r
916                                 return;\r
917                         };\r
918 \r
919                         // max touches\r
920                         if( 0 < hammer.options.drag_max_touches && hammer.options.drag_max_touches < e.touches.length ) return;\r
921 \r
922                         switch( e.eventType ){\r
923                                 case START:\r
924                                         this.triggered = false;\r
925                                         break;\r
926 \r
927                                 case MOVE :\r
928                                         // when the distance we moved is too small we skip this gesture\r
929                                         // or we can be already in dragging\r
930                                         if( e.distance < hammer.options.drag_min_distance && Detection.current.name !== this.name ) return;\r
931 \r
932                                         // we are dragging!\r
933                                         Detection.current.name = this.name;\r
934 \r
935                                         // lock drag to axis?\r
936                                         if( Detection.current.lastEvent.drag_locked_to_axis || ( hammer.options.drag_lock_to_axis && hammer.options.drag_lock_min_distance <= e.distance ) ){\r
937                                                 e.drag_locked_to_axis = true;\r
938                                         };\r
939                                         last_direction = Detection.current.lastEvent.direction;\r
940                                         if( e.drag_locked_to_axis && last_direction !== e.direction ){\r
941                                                 // keep direction on the axis that the drag gesture started on\r
942                                                 e.direction = Utils.isVertical( last_direction ) ?\r
943                                                         ( e.deltaY < 0 ? Hammer.DIRECTION_UP   : Hammer.DIRECTION_DOWN ) :\r
944                                                         ( e.deltaX < 0 ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT );\r
945                                         };\r
946 \r
947                                         // first time, trigger dragstart event\r
948                                         if( !this.triggered ){\r
949                                                 hammer.trigger( X.UI.Event.DRAG_START, e );\r
950                                                 this.triggered = true;\r
951                                         };\r
952 \r
953                                         // trigger normal event\r
954                                         hammer.trigger( X.UI.Event.DRAG, e );\r
955 \r
956                                         // direction event, like dragdown\r
957                                         hammer.trigger(\r
958                                                 e.direction === Hammer.DIRECTION_UP ?\r
959                                                         X.UI.Event.DRAG_UP :\r
960                                                 e.direction === Hammer.DIRECTION_DOWN ?\r
961                                                         X.UI.Event.DRAG_DOWN :\r
962                                                 e.direction === Hammer.DIRECTION_LEFT ?\r
963                                                         X.UI.Event.DRAG_LEFT :\r
964                                                         X.UI.Event.DRAG_RIGHT,\r
965                                                 e\r
966                                         );\r
967 \r
968                                         // block the browser events\r
969                                         (\r
970                                                 ( hammer.options.drag_block_vertical   &&  Utils.isVertical( e.direction ) ) ||\r
971                                                 ( hammer.options.drag_block_horizontal && !Utils.isVertical( e.direction ) )\r
972                                         ) && e.preventDefault();\r
973                                         break;\r
974 \r
975                                 case END:\r
976                                         // trigger dragend\r
977                                         this.triggered && hammer.trigger( X.UI.Event.DRAG_END, e );\r
978                                         this.triggered = false;\r
979                                         break;\r
980                         }\r
981                 }\r
982         };\r
983 \r
984         /**\r
985          * Transform\r
986          * User want to scale or rotate with 2 fingers\r
987          * @events  transform, transformstart, transformend, pinch, pinchin, pinchout, rotate\r
988          */\r
989         Gestures.Transform = {\r
990                 name : 'transform',\r
991                 index : 45,\r
992                 startID  : X.UI.Event.TRANSFORM,\r
993                 endID    : X.UI.Event.ROTATE,\r
994                 defaults : {\r
995                         // factor, no scale is 1, zoomin is to 0 and zoomout until higher then 1\r
996                         transform_min_scale : 0.01,\r
997                         // rotation in degrees\r
998                         transform_min_rotation : 1,\r
999                         // prevent default browser behavior when two touches are on the screen\r
1000                         // but it makes the element a blocking element\r
1001                         // when you are using the transform gesture, it is a good practice to set this true\r
1002                         transform_always_block : false\r
1003                 },\r
1004                 triggered : false,\r
1005                 handler : function transformGesture( e, hammer ){\r
1006                         // current gesture isnt drag, but dragged is true\r
1007                         // this means an other gesture is busy. now call dragend\r
1008                         if( Detection.current.name !== this.name && this.triggered ){\r
1009                                 hammer.trigger( X.UI.Event.TRANSFORM_END, e );\r
1010                                 this.triggered = false;\r
1011                                 return;\r
1012                         };\r
1013 \r
1014                         // atleast multitouch\r
1015                         if( e.touches.length < 2 ) return;\r
1016 \r
1017                         // prevent default when two fingers are on the screen\r
1018                         hammer.options.transform_always_block && e.preventDefault();\r
1019 \r
1020                         switch(e.eventType) {\r
1021                                 case START:\r
1022                                         this.triggered = false;\r
1023                                         break;\r
1024 \r
1025                                 case MOVE:\r
1026                                         var scale_threshold    = ABS( 1 - e.scale ),\r
1027                                                 rotation_threshold = ABS( e.rotation );\r
1028 \r
1029                                         // when the distance we moved is too small we skip this gesture\r
1030                                         // or we can be already in dragging\r
1031                                         if( scale_threshold < hammer.options.transform_min_scale && rotation_threshold < hammer.options.transform_min_rotation ) return;\r
1032 \r
1033                                         // we are transforming!\r
1034                                         Detection.current.name = this.name;\r
1035 \r
1036                                         // first time, trigger dragstart event\r
1037                                         if( !this.triggered ){\r
1038                                                 hammer.trigger( X.UI.Event.TRANSFORM_START, e );\r
1039                                                 this.triggered = true;\r
1040                                         };\r
1041 \r
1042                                         hammer.trigger( X.UI.Event.TRANSFORM, e );\r
1043                                         // basic transform event\r
1044 \r
1045                                         // trigger rotate event\r
1046                                         hammer.options.transform_min_rotation < rotation_threshold && hammer.trigger( X.UI.Event.ROTATE, e );\r
1047 \r
1048                                         // trigger pinch event\r
1049                                         if( scale_threshold > hammer.options.transform_min_scale ){\r
1050                                                 hammer.trigger( X.UI.Event.PINCH, e );\r
1051                                                 hammer.trigger( e.scale < 1 ? X.UI.Event.PINCH_IN : X.UI.Event.PINCH_OUT, e );\r
1052                                         };\r
1053                                         break;\r
1054 \r
1055                                 case END:\r
1056                                         // trigger dragend\r
1057                                         this.triggered && hammer.trigger( X.UI.Event.TRANSFORM_END, e );\r
1058                                         this.triggered = false;\r
1059                                         break;\r
1060                         };\r
1061                 }\r
1062         };\r
1063 \r
1064         /**\r
1065          * Touch\r
1066          * Called as first, tells the user has touched the screen\r
1067          * @events  touch\r
1068          */\r
1069         Gestures.Touch = {\r
1070                 name : 'touch',\r
1071                 index : -Infinity,\r
1072                 defaults : {\r
1073                         // call preventDefault at touchstart, and makes the element blocking by\r
1074                         // disabling the scrolling of the page, but it improves gestures like\r
1075                         // transforming and dragging.\r
1076                         // be careful with using this, it can be very annoying for users to be stuck\r
1077                         // on the page\r
1078                         prevent_default : false,\r
1079 \r
1080                         // disable mouse events, so only touch (or pen!) input triggers events\r
1081                         prevent_mouseevents : false\r
1082                 },\r
1083                 handler : function touchGesture( e, hammer ){\r
1084                         if( hammer.options.prevent_mouseevents && e.pointerType === MOUSE ){\r
1085                                 Detection.stopDetect();\r
1086                                 return;\r
1087                         };\r
1088 \r
1089                         hammer.options.prevent_default && e.preventDefault();\r
1090 \r
1091                         e.eventType === START && hammer.trigger( this.name, e );\r
1092                 }\r
1093         };\r
1094 \r
1095         /**\r
1096          * Release\r
1097          * Called as last, tells the user has released the screen\r
1098          * @events  release\r
1099          */\r
1100         Gestures.Release = {\r
1101                 name : 'release',\r
1102                 index : Infinity,\r
1103                 handler : function releaseGesture( e, hammer ){\r
1104                         e.eventType === END && hammer.trigger( this.name, e );\r
1105                 }\r
1106         };\r
1107         \r
1108 })( Math, window, document );