1
- /*
2
- * jquery.simulate - simulate browser mouse and keyboard events
3
- * http ://jqueryui .com
1
+ /*!
2
+ * jQuery Simulate v0.0.1 - simulate browser mouse and keyboard events
3
+ * https ://github .com/jquery/jquery-simulate
4
4
*
5
5
* Copyright 2012 jQuery Foundation and other contributors
6
6
* Released under the MIT license.
7
7
* http://jquery.org/license
8
+ *
9
+ * Date: Sun Dec 9 12:15:33 2012 -0500
8
10
*/
9
11
10
- ; ( function ( $ ) {
12
+ ; ( function ( $ , undefined ) {
11
13
12
14
var rkeyEvent = / ^ k e y / ,
13
15
rmouseEvent = / ^ (?: m o u s e | c o n t e x t m e n u ) | c l i c k / ;
@@ -31,7 +33,42 @@ $.simulate = function( elem, type, options ) {
31
33
}
32
34
} ;
33
35
36
+ $ . extend ( $ . simulate , {
37
+
38
+ keyCode : {
39
+ BACKSPACE : 8 ,
40
+ COMMA : 188 ,
41
+ DELETE : 46 ,
42
+ DOWN : 40 ,
43
+ END : 35 ,
44
+ ENTER : 13 ,
45
+ ESCAPE : 27 ,
46
+ HOME : 36 ,
47
+ LEFT : 37 ,
48
+ NUMPAD_ADD : 107 ,
49
+ NUMPAD_DECIMAL : 110 ,
50
+ NUMPAD_DIVIDE : 111 ,
51
+ NUMPAD_ENTER : 108 ,
52
+ NUMPAD_MULTIPLY : 106 ,
53
+ NUMPAD_SUBTRACT : 109 ,
54
+ PAGE_DOWN : 34 ,
55
+ PAGE_UP : 33 ,
56
+ PERIOD : 190 ,
57
+ RIGHT : 39 ,
58
+ SPACE : 32 ,
59
+ TAB : 9 ,
60
+ UP : 38
61
+ } ,
62
+
63
+ buttonCode : {
64
+ LEFT : 0 ,
65
+ MIDDLE : 1 ,
66
+ RIGHT : 2
67
+ }
68
+ } ) ;
69
+
34
70
$ . extend ( $ . simulate . prototype , {
71
+
35
72
simulateEvent : function ( elem , type , options ) {
36
73
var event = this . createEvent ( type , options ) ;
37
74
this . dispatchEvent ( elem , type , event , options ) ;
@@ -56,7 +93,6 @@ $.extend( $.simulate.prototype, {
56
93
detail : 0 ,
57
94
screenX : 0 ,
58
95
screenY : 0 ,
59
- // TODO: default clientX/Y to a position within the target element
60
96
clientX : 1 ,
61
97
clientY : 1 ,
62
98
ctrlKey : false ,
@@ -101,8 +137,14 @@ $.extend( $.simulate.prototype, {
101
137
} else if ( document . createEventObject ) {
102
138
event = document . createEventObject ( ) ;
103
139
$ . extend ( event , options ) ;
104
- // TODO: what is this mapping for?
105
- event . button = { 0 :1 , 1 :4 , 2 :2 } [ event . button ] || event . button ;
140
+ // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
141
+ // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
142
+ // so we actually need to map the standard back to oldIE
143
+ event . button = {
144
+ 0 : 1 ,
145
+ 1 : 4 ,
146
+ 2 : 2
147
+ } [ event . button ] || event . button ;
106
148
}
107
149
108
150
return event ;
@@ -128,7 +170,10 @@ $.extend( $.simulate.prototype, {
128
170
event . initKeyEvent ( type , options . bubbles , options . cancelable , options . view ,
129
171
options . ctrlKey , options . altKey , options . shiftKey , options . metaKey ,
130
172
options . keyCode , options . charCode ) ;
131
- // TODO: what is this supporting?
173
+ // initKeyEvent throws an exception in WebKit
174
+ // see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
175
+ // and also https://bugs.webkit.org/show_bug.cgi?id=13368
176
+ // fall back to a generic event until we decide to implement initKeyboardEvent
132
177
} catch ( err ) {
133
178
event = document . createEvent ( "Events" ) ;
134
179
event . initEvent ( type , options . bubbles , options . cancelable ) ;
@@ -147,17 +192,14 @@ $.extend( $.simulate.prototype, {
147
192
$ . extend ( event , options ) ;
148
193
}
149
194
150
- // TODO: can we hook into core's logic?
151
- if ( $ . ui . ie || ( ( { } ) . toString . call ( window . opera ) === "[object Opera]" ) ) {
152
- // TODO: is charCode ever <0 ? Can we just use charCode || keyCode?
195
+ if ( ! ! / m s i e [ \w . ] + / . exec ( navigator . userAgent . toLowerCase ( ) ) || ( ( { } ) . toString . call ( window . opera ) === "[object Opera]" ) ) {
153
196
event . keyCode = ( options . charCode > 0 ) ? options . charCode : options . keyCode ;
154
197
event . charCode = undefined ;
155
198
}
156
199
157
200
return event ;
158
201
} ,
159
202
160
- // TODO: does this need type? Can't we just check event.type?
161
203
dispatchEvent : function ( elem , type , event ) {
162
204
if ( elem . dispatchEvent ) {
163
205
elem . dispatchEvent ( event ) ;
@@ -237,20 +279,31 @@ function findCenter( elem ) {
237
279
238
280
$ . extend ( $ . simulate . prototype , {
239
281
simulateDrag : function ( ) {
240
- var target = this . target ,
282
+ var i = 0 ,
283
+ target = this . target ,
241
284
options = this . options ,
242
285
center = findCenter ( target ) ,
243
286
x = Math . floor ( center . x ) ,
244
287
y = Math . floor ( center . y ) ,
245
288
dx = options . dx || 0 ,
246
289
dy = options . dy || 0 ,
290
+ moves = options . moves || 3 ,
247
291
coord = { clientX : x , clientY : y } ;
292
+
248
293
this . simulateEvent ( target , "mousedown" , coord ) ;
249
- coord = { clientX : x + 1 , clientY : y + 1 } ;
250
- this . simulateEvent ( document , "mousemove" , coord ) ;
251
- coord = { clientX : x + dx , clientY : y + dy } ;
252
- this . simulateEvent ( document , "mousemove" , coord ) ;
253
- this . simulateEvent ( document , "mousemove" , coord ) ;
294
+
295
+ for ( ; i < moves ; i ++ ) {
296
+ x += dx / moves ;
297
+ y += dy / moves ;
298
+
299
+ coord = {
300
+ clientX : Math . round ( x ) ,
301
+ clientY : Math . round ( y )
302
+ } ;
303
+
304
+ this . simulateEvent ( document , "mousemove" , coord ) ;
305
+ }
306
+
254
307
this . simulateEvent ( target , "mouseup" , coord ) ;
255
308
this . simulateEvent ( target , "click" , coord ) ;
256
309
}
0 commit comments