OSDN Git Service

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