OSDN Git Service

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