OSDN Git Service

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