OSDN Git Service

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