1
+ /*
2
+
3
+ jQuery Bridge for jQTouch
4
+ (adds events which Zepto includes by default)
5
+
6
+ Created by David Kaneda <http://www.davidkaneda.com>
7
+ Maintained by Jonathan Stark <http://jonathanstark.com/>
8
+ Sponsored by Sencha Labs <http://www.sencha.com/>
9
+
10
+ Documentation and issue tracking on GitHub <http://wiki.github.com/senchalabs/jQTouch/>
11
+
12
+ (c) 2009-2011 by jQTouch project members.
13
+ See LICENSE.txt for license.
14
+
15
+ */
16
+
17
+ ( function ( $ ) {
18
+ var SUPPORT_TOUCH = ( ! ! window . Touch ) ,
19
+ START_EVENT = SUPPORT_TOUCH ? 'touchstart' : 'mousedown' ,
20
+ MOVE_EVENT = SUPPORT_TOUCH ? 'touchmove' : 'mousemove' ,
21
+ END_EVENT = SUPPORT_TOUCH ? 'touchend' : 'mouseup' ,
22
+ CANCEL_EVENT = SUPPORT_TOUCH ? 'touchcancel' : 'mouseout' , // mouseout on document
23
+ lastTime = 0 ,
24
+ tapReady = true ,
25
+ jQTSettings = {
26
+ useFastTouch : true , // experimental
27
+ debug : true ,
28
+ moveThreshold : 10 ,
29
+ hoverDelay : 50 ,
30
+ pressDelay : 750
31
+ } ;
32
+
33
+ function warn ( message ) {
34
+ if ( window . console !== undefined ) {
35
+ console . log ( message ) ;
36
+ }
37
+ }
38
+
39
+ function touchStartHandler ( e ) {
40
+
41
+ if ( ! tapReady ) {
42
+ warn ( 'TouchStart handler aborted because tap is not ready' ) ;
43
+ e . preventDefault ( ) ;
44
+ return false ;
45
+ }
46
+
47
+ var $el = $ ( e . target ) ;
48
+
49
+ // Error check
50
+ if ( ! $el . length ) {
51
+ warn ( 'Could not find target of touchstart event.' ) ;
52
+ return ;
53
+ }
54
+
55
+ var startTime = new Date ( ) . getTime ( ) ,
56
+ hoverTimeout = null ,
57
+ pressTimeout = null ,
58
+ touch ,
59
+ startX ,
60
+ startY ,
61
+ deltaX = 0 ,
62
+ deltaY = 0 ,
63
+ deltaT = 0 ;
64
+
65
+ touch = SUPPORT_TOUCH ? event . changedTouches [ 0 ] : event ;
66
+ startX = touch . pageX ;
67
+ startY = touch . pageY ;
68
+
69
+ // Prep the element
70
+ bindEvents ( $el ) ;
71
+
72
+ hoverTimeout = setTimeout ( function ( ) {
73
+ $el . makeActive ( ) ;
74
+ } , jQTSettings . hoverDelay ) ;
75
+
76
+ pressTimeout = setTimeout ( function ( ) {
77
+ unbindEvents ( $el ) ;
78
+ $el . unselect ( ) ;
79
+ clearTimeout ( hoverTimeout ) ;
80
+ $el . trigger ( 'press' ) ;
81
+ } , jQTSettings . pressDelay ) ;
82
+
83
+ // Private touch functions
84
+ function touchCancelHandler ( e ) {
85
+ clearTimeout ( hoverTimeout ) ;
86
+ $el . unselect ( ) ;
87
+ unbindEvents ( $el ) ;
88
+ }
89
+
90
+ function touchEndHandler ( e ) {
91
+ // updateChanges();
92
+ unbindEvents ( $el ) ;
93
+ clearTimeout ( hoverTimeout ) ;
94
+ clearTimeout ( pressTimeout ) ;
95
+ if ( Math . abs ( deltaX ) < jQTSettings . moveThreshold && Math . abs ( deltaY ) < jQTSettings . moveThreshold && deltaT < jQTSettings . pressDelay ) {
96
+ // e.preventDefault();
97
+ // e.stopImmediatePropagation();
98
+ if ( SUPPORT_TOUCH && jQTSettings . useFastTouch ) {
99
+ $el . trigger ( 'tap' , e ) ;
100
+ }
101
+ } else {
102
+ $el . unselect ( ) ;
103
+ }
104
+ }
105
+
106
+ function touchMoveHandler ( e ) {
107
+ updateChanges ( ) ;
108
+ var absX = Math . abs ( deltaX ) ;
109
+ var absY = Math . abs ( deltaY ) ;
110
+ var direction ;
111
+ if ( absX > absY && ( absX > 30 ) && deltaT < 1000 ) {
112
+ if ( deltaX < 0 ) {
113
+ direction = 'left' ;
114
+ } else {
115
+ direction = 'right' ;
116
+ }
117
+ unbindEvents ( $el ) ;
118
+ $el . trigger ( 'swipe' , { direction :direction , deltaX :deltaX , deltaY : deltaY } ) ;
119
+ }
120
+ $el . unselect ( ) ;
121
+ clearTimeout ( hoverTimeout ) ;
122
+ if ( absX > jQTSettings . moveThreshold || absY > jQTSettings . moveThreshold ) {
123
+ clearTimeout ( pressTimeout ) ;
124
+ }
125
+ }
126
+
127
+ function updateChanges ( ) {
128
+ var firstFinger = SUPPORT_TOUCH ? event . changedTouches [ 0 ] : event ;
129
+ deltaX = firstFinger . pageX - startX ;
130
+ deltaY = firstFinger . pageY - startY ;
131
+ deltaT = new Date ( ) . getTime ( ) - startTime ;
132
+ }
133
+
134
+ function bindEvents ( $el ) {
135
+ $el . bind ( MOVE_EVENT , touchMoveHandler ) . bind ( END_EVENT , touchEndHandler ) ;
136
+ if ( SUPPORT_TOUCH ) {
137
+ $el . bind ( CANCEL_EVENT , touchCancelHandler ) ;
138
+ } else {
139
+ $ ( document ) . bind ( 'mouseout' , touchCancelHandler ) ;
140
+ }
141
+ }
142
+
143
+ function unbindEvents ( $el ) {
144
+ if ( ! $el ) return ;
145
+
146
+ $el . unbind ( MOVE_EVENT , touchMoveHandler ) . unbind ( END_EVENT , touchEndHandler ) ;
147
+ if ( SUPPORT_TOUCH ) {
148
+ $el . unbind ( CANCEL_EVENT , touchCancelHandler ) ;
149
+ } else {
150
+ $ ( document ) . unbind ( 'mouseout' , touchCancelHandler ) ;
151
+ }
152
+ }
153
+ } // End touch handler
154
+
155
+ $ . jQTouch = function ( options ) {
156
+
157
+ // take in options
158
+ for ( var i in options ) {
159
+ jQTSettings [ i ] = options [ i ] ;
160
+ }
161
+
162
+ $ ( document ) . bind ( 'ready' , function ( ) {
163
+ $ ( '#jqt' ) . bind ( START_EVENT , touchStartHandler ) ;
164
+ } ) ;
165
+
166
+ $ . fn . press = function ( fn ) {
167
+ if ( $ . isFunction ( fn ) ) {
168
+ return $ ( this ) . live ( 'press' , fn ) ;
169
+ } else {
170
+ return $ ( this ) . trigger ( 'press' ) ;
171
+ }
172
+ } ;
173
+ $ . fn . swipe = function ( fn ) {
174
+ if ( $ . isFunction ( fn ) ) {
175
+ return $ ( this ) . live ( 'swipe' , fn ) ;
176
+ } else {
177
+ return $ ( this ) . trigger ( 'swipe' ) ;
178
+ }
179
+ } ;
180
+ $ . fn . tap = function ( fn ) {
181
+ if ( $ . isFunction ( fn ) ) {
182
+ return $ ( this ) . live ( 'tap' , fn ) ;
183
+ } else {
184
+ return $ ( this ) . trigger ( 'tap' ) ;
185
+ }
186
+ } ;
187
+
188
+ options . framework = $ ;
189
+
190
+ var core = jQTouchCore ( options ) ;
191
+
192
+ return core ;
193
+ } ;
194
+
195
+ // Extensions directly manipulate the jQTouch object, before it's initialized.
196
+ $ . jQTouch . addExtension = function ( extension ) {
197
+ jQTouchCore . prototype . extensions . push ( extension ) ;
198
+ } ;
199
+
200
+ } ) ( jQuery ) ;
0 commit comments