[TemporaryStorage] add files required for SDK build
[samples/web/TemporaryStorage.git] / tizen-web-ui-fw / latest / js / src / events / touch.js
1
2 (function( $, window, undefined ) {
3         // add new event shortcuts
4         $.each( ( "touchstart touchmove touchend " +
5                 "tap taphold " +
6                 "swipe swipeleft swiperight " +
7                 "scrollstart scrollstop" ).split( " " ), function( i, name ) {
8
9                 $.fn[ name ] = function( fn ) {
10                         return fn ? this.bind( name, fn ) : this.trigger( name );
11                 };
12
13                 // jQuery < 1.8
14                 if ( $.attrFn ) {
15                         $.attrFn[ name ] = true;
16                 }
17         });
18
19         var supportTouch = $.mobile.support.touch,
20                 scrollEvent = "touchmove scroll",
21                 touchStartEvent = supportTouch ? "touchstart" : "mousedown",
22                 touchStopEvent = supportTouch ? "touchend" : "mouseup",
23                 touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
24
25         function triggerCustomEvent( obj, eventType, event ) {
26                 var originalType = event.type;
27                 event.type = eventType;
28                 // event.liveFired is already set by basic events, e.g. vclick, which is fired already.
29                 // To fire this custom event, event.liveFired must be cleared.
30                 event.liveFired = undefined;
31
32                 $.event.handle.call( obj, event );
33                 event.type = originalType;
34         }
35
36         // also handles scrollstop
37         $.event.special.scrollstart = {
38
39                 enabled: true,
40
41                 setup: function() {
42
43                         var thisObject = this,
44                                 $this = $( thisObject ),
45                                 scrolling,
46                                 timer;
47
48                         function trigger( event, state ) {
49                                 scrolling = state;
50                                 triggerCustomEvent( thisObject, scrolling ? "scrollstart" : "scrollstop", event );
51                         }
52
53                         // iPhone triggers scroll after a small delay; use touchmove instead
54                         $this.bind( scrollEvent, function( event ) {
55
56                                 if ( !$.event.special.scrollstart.enabled ) {
57                                         return;
58                                 }
59
60                                 if ( !scrolling ) {
61                                         trigger( event, true );
62                                 }
63
64                                 clearTimeout( timer );
65                                 timer = setTimeout( function() {
66                                         trigger( event, false );
67                                 }, 50 );
68                         });
69                 }
70         };
71
72         // also handles taphold
73         $.event.special.tap = {
74                 tapholdThreshold: 750,
75
76                 setup: function() {
77                         var thisObject = this,
78                                 $this = $( thisObject );
79
80                         $this.bind( "vmousedown", function( event ) {
81
82                                 if ( event.which && event.which !== 1 ) {
83                                         return false;
84                                 }
85
86                                 var origTarget = event.target,
87                                         origEvent = event.originalEvent,
88                                         timer;
89
90                                 function clearTapTimer() {
91                                         clearTimeout( timer );
92                                 }
93
94                                 function clearTapHandlers() {
95                                         clearTapTimer();
96
97                                         $this.unbind( "vclick", clickHandler )
98                                                 .unbind( "vmouseup", clearTapTimer );
99                                         $.mobile.$document.unbind( "vmousecancel", clearTapHandlers );
100                                 }
101
102                                 function clickHandler( event ) {
103                                         clearTapHandlers();
104
105                                         // ONLY trigger a 'tap' event if the start target is
106                                         // the same as the stop target.
107                                         if ( origTarget === event.target ) {
108                                                 triggerCustomEvent( thisObject, "tap", event );
109                                         }
110                                 }
111
112                                 $this.bind( "vmouseup", clearTapTimer )
113                                         .bind( "vclick", clickHandler );
114                                 $.mobile.$document.bind( "vmousecancel", clearTapHandlers );
115
116                                 timer = setTimeout( function() {
117                                         triggerCustomEvent( thisObject, "taphold", $.Event( "taphold", { target: origTarget } ) );
118                                 }, $.event.special.tap.tapholdThreshold );
119                         });
120                 }
121         };
122
123         // also handles swipeleft, swiperight
124         $.event.special.swipe = {
125                 scrollSupressionThreshold: 30, // More than this horizontal displacement, and we will suppress scrolling.
126
127                 durationThreshold: 1000, // More time than this, and it isn't a swipe.
128
129                 horizontalDistanceThreshold: 30,  // Swipe horizontal displacement must be more than this.
130
131                 verticalDistanceThreshold: 75,  // Swipe vertical displacement must be less than this.
132
133                 setup: function() {
134                         var thisObject = this,
135                                 $this = $( thisObject );
136
137                         $this.bind( touchStartEvent, function( event ) {
138                                 var data = event.originalEvent.touches ?
139                                                 event.originalEvent.touches[ 0 ] : event,
140                                         start = {
141                                                 time: ( new Date() ).getTime(),
142                                                 coords: [ data.pageX, data.pageY ],
143                                                 origin: $( event.target )
144                                         },
145                                         stop;
146
147                                 function moveHandler( event ) {
148
149                                         if ( !start ) {
150                                                 return;
151                                         }
152
153                                         var data = event.originalEvent.touches ?
154                                                 event.originalEvent.touches[ 0 ] : event;
155
156                                         stop = {
157                                                 time: ( new Date() ).getTime(),
158                                                 coords: [ data.pageX, data.pageY ]
159                                         };
160
161                                         // prevent scrolling
162                                         if ( Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.scrollSupressionThreshold ) {
163                                                 event.preventDefault();
164                                         }
165                                 }
166
167                                 $this.bind( touchMoveEvent, moveHandler )
168                                         .one( touchStopEvent, function( event ) {
169                                                 $this.unbind( touchMoveEvent, moveHandler );
170
171                                                 if ( start && stop ) {
172                                                         if ( stop.time - start.time < $.event.special.swipe.durationThreshold &&
173                                                                 Math.abs( start.coords[ 0 ] - stop.coords[ 0 ] ) > $.event.special.swipe.horizontalDistanceThreshold &&
174                                                                 Math.abs( start.coords[ 1 ] - stop.coords[ 1 ] ) < $.event.special.swipe.verticalDistanceThreshold ) {
175
176                                                                 start.origin.trigger( "swipe" )
177                                                                         .trigger( start.coords[0] > stop.coords[ 0 ] ? "swipeleft" : "swiperight" );
178                                                         }
179                                                 }
180                                                 start = stop = undefined;
181                                         });
182                         });
183                 }
184         };
185         $.each({
186                 scrollstop: "scrollstart",
187                 taphold: "tap",
188                 swipeleft: "swipe",
189                 swiperight: "swipe"
190         }, function( event, sourceEvent ) {
191
192                 $.event.special[ event ] = {
193                         setup: function() {
194                                 $( this ).bind( sourceEvent, $.noop );
195                         }
196                 };
197         });
198
199 })( jQuery, this );