|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Simulating DOM Events</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Simulating DOM Events</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p> |
|
|
32 |
When creating automated tests for your application or modules, you need |
|
|
33 |
to be able to mock user events. The DOM supports creating native events |
|
|
34 |
that behave essentially the same as user generated events, though |
|
|
35 |
without the associated browser default behaviors (e.g. following a link |
|
|
36 |
on click). |
|
|
37 |
</p> |
|
|
38 |
<p> |
|
|
39 |
The <code>event-simulate</code> module adds the <code>Y.Event.simulate</code> method for |
|
|
40 |
working with raw DOM nodes, but for most cases, the |
|
|
41 |
<code>node-event-simulate</code> module is the right choice, since it allows you |
|
|
42 |
to call the <code>simulate</code> method directly from the <code>Node</code>. |
|
|
43 |
</p> |
|
|
44 |
</div> |
|
|
45 |
|
|
|
46 |
<h2 id="simulating-mouse-events">Simulating Mouse Events</h2> |
|
|
47 |
|
|
|
48 |
<p> |
|
|
49 |
There are seven mouse events that can be simulated: |
|
|
50 |
</p> |
|
|
51 |
|
|
|
52 |
<ul> |
|
|
53 |
<li><code>click</code></li> |
|
|
54 |
<li><code>dblclick</code></li> |
|
|
55 |
<li><code>mousedown</code></li> |
|
|
56 |
<li><code>mouseup</code></li> |
|
|
57 |
<li><code>mouseover</code></li> |
|
|
58 |
<li><code>mouseout</code></li> |
|
|
59 |
<li><code>mousemove</code></li> |
|
|
60 |
</ul> |
|
|
61 |
|
|
|
62 |
<p> |
|
|
63 |
Each event is fired by calling <code>simulate()</code> and passing in two |
|
|
64 |
arguments: the type of event to fire and an optional object specifying |
|
|
65 |
additional information for the event. To simulate a click on the document's |
|
|
66 |
body element, for example, the following code can be used: |
|
|
67 |
</p> |
|
|
68 |
|
|
|
69 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
70 |
|
|
|
71 |
Y.one("body").simulate("click"); |
|
|
72 |
});</pre> |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
<p> |
|
|
76 |
This code simulates a click with all of the default properties on the |
|
|
77 |
<code>event</code> object. To specify additional information, such as the |
|
|
78 |
Shift key being down, the second argument must be used and the exact DOM |
|
|
79 |
name for the event property specified (there is browser-normalizing logic |
|
|
80 |
that translates these into browser-specific properties when necessary): |
|
|
81 |
</p> |
|
|
82 |
|
|
|
83 |
<pre class="code prettyprint">Y.one("body").simulate("click", { shiftKey: true });</pre> |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
<p> |
|
|
87 |
In this updated example, a click event is fired on the document's body |
|
|
88 |
while simulating that the Shift key is down. |
|
|
89 |
</p> |
|
|
90 |
|
|
|
91 |
<p> |
|
|
92 |
The extra properties to specify vary depending on the event being simulated |
|
|
93 |
and are limited to this list: |
|
|
94 |
</p> |
|
|
95 |
|
|
|
96 |
<dl> |
|
|
97 |
<dt><code>detail</code></dt> |
|
|
98 |
<dd> |
|
|
99 |
Indicates the number of times a button was clicked (DOM-compliant |
|
|
100 |
browsers only). |
|
|
101 |
</dd> |
|
|
102 |
|
|
|
103 |
<dt><code>screenX</code>, <code>screenY</code></dt> |
|
|
104 |
<dd> |
|
|
105 |
Coordinates of the mouse event in relation to the entire screen |
|
|
106 |
(DOM-compliant browsers only). |
|
|
107 |
</dd> |
|
|
108 |
|
|
|
109 |
<dt><code>clientX</code>, <code>clientY</code></dt> |
|
|
110 |
<dd> |
|
|
111 |
Coordinates of the mouse event in relation to the browser client |
|
|
112 |
area. |
|
|
113 |
</dd> |
|
|
114 |
|
|
|
115 |
<dt><code>ctrlKey</code>, <code>altKey</code>, <code>shiftKey</code>, <code>metaKey</code></dt> |
|
|
116 |
<dd> |
|
|
117 |
The state of the Ctrl, Alt, Shift, and Meta keys, respectively |
|
|
118 |
(true for down, false for up). |
|
|
119 |
</dd> |
|
|
120 |
|
|
|
121 |
<dt><code>button</code></dt> |
|
|
122 |
<dd> |
|
|
123 |
The button being used for the event, 0 for left (default), 1 for |
|
|
124 |
right, 2 for center. |
|
|
125 |
</dd> |
|
|
126 |
|
|
|
127 |
<dt><code>relatedTarget</code></dt> |
|
|
128 |
<dd> |
|
|
129 |
the element the mouse moved from (during a <code>mouseover</code> event) or to |
|
|
130 |
(during a <code>mouseout</code> event). |
|
|
131 |
</dd> |
|
|
132 |
</dl> |
|
|
133 |
|
|
|
134 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
135 |
|
|
|
136 |
var node = Y.one("#myDiv"); |
|
|
137 |
|
|
|
138 |
//simulate a click Alt key down |
|
|
139 |
node.simulate("click", { altKey: true}); |
|
|
140 |
|
|
|
141 |
//simulate a double click with Ctrl key down |
|
|
142 |
node.simulate("dblclick", { ctrlKey: true }); |
|
|
143 |
|
|
|
144 |
//simulate a mouse over |
|
|
145 |
node.simulate("mouseover", { relatedTarget: document.body }); |
|
|
146 |
|
|
|
147 |
//simulate a mouse out |
|
|
148 |
node.simulate("mouseout", { relatedTarget: document.body }); |
|
|
149 |
|
|
|
150 |
//simulate a mouse down at point (100,100) in the client area |
|
|
151 |
node.simulate("mousedown", { clientX: 100, clientY: 100 }); |
|
|
152 |
|
|
|
153 |
//simulate a mouse up at point (100,100) in the client area |
|
|
154 |
node.simulate("mouseup", { clientX: 100, clientY: 100 }); |
|
|
155 |
|
|
|
156 |
//simulate a mouse move at point (200, 200) in the client area |
|
|
157 |
node.simulate("mousemove", { clientX: 200, clientY: 200 }); |
|
|
158 |
});</pre> |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
<h2 id="simulating-key-events">Simulating Key Events</h2> |
|
|
162 |
|
|
|
163 |
<p>There are three key event simulations available:</p> |
|
|
164 |
|
|
|
165 |
<ul> |
|
|
166 |
<li><code>keyup</code></li> |
|
|
167 |
<li><code>keydown</code></li> |
|
|
168 |
<li><code>keypress</code></li> |
|
|
169 |
</ul> |
|
|
170 |
|
|
|
171 |
<p> |
|
|
172 |
As with the mouse events, key events are simulated using |
|
|
173 |
<code>simulate()</code>. For <code>keyup</code> and <code>keydown</code>, |
|
|
174 |
the <code>keyCode</code> property must be specified; for |
|
|
175 |
<code>keypress</code>, the <code>charCode</code> property must be included. |
|
|
176 |
In many cases, <code>keyCode</code> and <code>charCode</code> may be the |
|
|
177 |
same value to represent the same key (97, for instance, represents the |
|
|
178 |
"A" key as well as being the ASCII code for the letter |
|
|
179 |
"a"). For example: |
|
|
180 |
</p> |
|
|
181 |
|
|
|
182 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
183 |
|
|
|
184 |
var node = Y.one("#myDiv"); |
|
|
185 |
|
|
|
186 |
//simulate a keydown on the A key |
|
|
187 |
node.simulate("keydown", { keyCode: 97 }); |
|
|
188 |
|
|
|
189 |
//simulate a keyup on the A key |
|
|
190 |
node.simulate("keyup", { keyCode: 97 }); |
|
|
191 |
|
|
|
192 |
//simulate typing "a" |
|
|
193 |
node.simulate("keypress", { charCode: 97 }); |
|
|
194 |
});</pre> |
|
|
195 |
|
|
|
196 |
|
|
|
197 |
<p> |
|
|
198 |
Key events also support the <code>ctrlKey</code>, <code>altKey</code>, |
|
|
199 |
<code>shiftKey</code>, and <code>metaKey</code> event properties. |
|
|
200 |
</p> |
|
|
201 |
<p> |
|
|
202 |
<strong>Note:</strong> Due to differences in browser implementations, key |
|
|
203 |
events may not be simulated in the same manner across all browsers. For |
|
|
204 |
instance, when simulating a keypress event on a textbox, only Firefox will |
|
|
205 |
update the textbox with the new character of the key that was simulated to |
|
|
206 |
be pressed. For other browsers, the events are still registered and all |
|
|
207 |
event handlers are called, however, the textbox display and |
|
|
208 |
<code>value</code> property are not updated. These differences should go |
|
|
209 |
away as browser support for simulated events improves in the future. |
|
|
210 |
</p> |
|
|
211 |
|
|
|
212 |
<h2 id="simulating-ui-events">Simulating UI Events</h2> |
|
|
213 |
|
|
|
214 |
<p>There are several UI event simulations available:</p> |
|
|
215 |
|
|
|
216 |
<ul> |
|
|
217 |
<li><code>blur</code></li> |
|
|
218 |
<li><code>change</code></li> |
|
|
219 |
<li><code>focus</code></li> |
|
|
220 |
<li><code>resize</code></li> |
|
|
221 |
<li><code>scroll</code></li> |
|
|
222 |
<li><code>select</code></li> |
|
|
223 |
</ul> |
|
|
224 |
|
|
|
225 |
<p> |
|
|
226 |
As with the other events, UI events are simulated using |
|
|
227 |
<code>simulate()</code>. There are no properties that are required to |
|
|
228 |
simulate UI events as these events don't carry extra information. Some |
|
|
229 |
examples: |
|
|
230 |
</p> |
|
|
231 |
|
|
|
232 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
233 |
|
|
|
234 |
var node = Y.one("#myInput"); |
|
|
235 |
|
|
|
236 |
//simulate a change event |
|
|
237 |
node.simulate("change"); |
|
|
238 |
|
|
|
239 |
//simulate a select event |
|
|
240 |
node.simulate("select"); |
|
|
241 |
});</pre> |
|
|
242 |
|
|
|
243 |
|
|
|
244 |
<h2 id="simulating-touch-gestures">Simulating Touch Gestures</h2> |
|
|
245 |
<p> |
|
|
246 |
There are several high level gesture simulations primarily targeted for |
|
|
247 |
smartphones, tablets, and other touch-enabled devices. Single touch gestures |
|
|
248 |
such as <code>tap</code> and <code>flick</code> are simulated using Mouse Events on desktop or mobile |
|
|
249 |
devices where creating Touch Events are not supported. All gesture simulations |
|
|
250 |
are done by calling the <code>simulateGesture()</code> method against a Node instance. The |
|
|
251 |
method is asynchronous by default so an optional callback function can be |
|
|
252 |
passed. |
|
|
253 |
</p> |
|
|
254 |
|
|
|
255 |
<dl> |
|
|
256 |
<dt><code>tap</code></dt> |
|
|
257 |
<dd> |
|
|
258 |
Single finger gesture to simulate a tap. Default is to simulate |
|
|
259 |
one tap but it can be configured to simulate any number of taps. |
|
|
260 |
</dd> |
|
|
261 |
|
|
|
262 |
<dt><code>doubletap</code></dt> |
|
|
263 |
<dd> |
|
|
264 |
Single finger gesture to simulate double taps. |
|
|
265 |
</dd> |
|
|
266 |
|
|
|
267 |
<dt><code>press</code></dt> |
|
|
268 |
<dd> |
|
|
269 |
Single finger gesture to simulate a long press. |
|
|
270 |
</dd> |
|
|
271 |
|
|
|
272 |
<dt><code>move</code></dt> |
|
|
273 |
<dd> |
|
|
274 |
Single finger gesture to simulate a move. It simulates moving |
|
|
275 |
one finger straight in any direction. |
|
|
276 |
</dd> |
|
|
277 |
|
|
|
278 |
<dt><code>flick</code></dt> |
|
|
279 |
<dd> |
|
|
280 |
Single finger gesture to simulate the flick gesture. It simulates |
|
|
281 |
moving one finger with a certain velocity along either an X or Y |
|
|
282 |
axis. |
|
|
283 |
</dd> |
|
|
284 |
|
|
|
285 |
<dt><code>pinch</code></dt> |
|
|
286 |
<dd> |
|
|
287 |
Two finger gesture to simulate pinch and spread gestures. |
|
|
288 |
</dd> |
|
|
289 |
|
|
|
290 |
<dt><code>rotate</code></dt> |
|
|
291 |
<dd> |
|
|
292 |
Two finger gesture to simulate a rotate. |
|
|
293 |
</dd> |
|
|
294 |
</dl> |
|
|
295 |
|
|
|
296 |
<p> |
|
|
297 |
Gesture can be simulated by calling <code>simulateGesture()</code> and |
|
|
298 |
passing the following arguments: the name of the gesture to simulate, an |
|
|
299 |
optional object to define gesture properties, and an optional callback function. |
|
|
300 |
The available properties vary per gesture. |
|
|
301 |
</p> |
|
|
302 |
|
|
|
303 |
<p>If the location of the finger is not given, the center of the node element is |
|
|
304 |
used by default. This default behavior can be overridden by passing coordinates |
|
|
305 |
into the optional object. The coordinate values are relative to the top/left |
|
|
306 |
corner of the node element. |
|
|
307 |
</p> |
|
|
308 |
|
|
|
309 |
<h3 id="single-touch-gestures-tap-double-tab-and-press">Single Touch Gestures: Tap, Double Tab and Press</h3> |
|
|
310 |
|
|
|
311 |
<p> |
|
|
312 |
The following code simulates tap, double tap and press gestures: |
|
|
313 |
</p> |
|
|
314 |
|
|
|
315 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
316 |
|
|
|
317 |
var node = Y.one("#myElement"); |
|
|
318 |
|
|
|
319 |
//simulate tap gesture |
|
|
320 |
node.simulateGesture("tap"); |
|
|
321 |
|
|
|
322 |
//simulate double-tap gesture |
|
|
323 |
node.simulateGesture("doubletap"); |
|
|
324 |
|
|
|
325 |
//simulate press gesture |
|
|
326 |
node.simulateGesture("press", { |
|
|
327 |
hold: 3000 // press and hold for 3000ms |
|
|
328 |
}); |
|
|
329 |
|
|
|
330 |
// simulate tap with options and callback |
|
|
331 |
node.simulateGesture("tap", { |
|
|
332 |
point: [30, 30], // tap (30, 30) relative to the top/left of the node |
|
|
333 |
hold: 3000, // hold for 3sec in a tap |
|
|
334 |
times: 2, // tap 2 times |
|
|
335 |
delay: 500 // delay time before next tap starts |
|
|
336 |
}, function() { |
|
|
337 |
Y.log("I was called."); |
|
|
338 |
}); |
|
|
339 |
});</pre> |
|
|
340 |
|
|
|
341 |
<h4 id="valid-options">Valid Options</h4> |
|
|
342 |
|
|
|
343 |
<p> |
|
|
344 |
Optional properties for the <code>tap</code> gesture: |
|
|
345 |
</p> |
|
|
346 |
|
|
|
347 |
<dl> |
|
|
348 |
<dt><code>point</code></dt> |
|
|
349 |
<dd> |
|
|
350 |
Indicates the [x,y] coordinates where the tap should be |
|
|
351 |
simulated. Default is the center of the node element. |
|
|
352 |
</dd> |
|
|
353 |
<dt><code>hold</code></dt> |
|
|
354 |
<dd> |
|
|
355 |
The hold time in milliseconds. This is the time between |
|
|
356 |
<code>touchstart</code> and <code>touchend</code> event generation. |
|
|
357 |
</dd> |
|
|
358 |
<dt><code>times</code></dt> |
|
|
359 |
<dd> |
|
|
360 |
Indicates the number of taps. Default is 1. |
|
|
361 |
</dd> |
|
|
362 |
<dt><code>delay</code></dt> |
|
|
363 |
<dd> |
|
|
364 |
The number of milliseconds before the next tap simulation |
|
|
365 |
happens. This is valid only when <code>times</code> is more than 1. |
|
|
366 |
</dd> |
|
|
367 |
</dl> |
|
|
368 |
|
|
|
369 |
<p> |
|
|
370 |
Optional properties for the <code>doubletap</code> gesture: |
|
|
371 |
</p> |
|
|
372 |
|
|
|
373 |
<dl> |
|
|
374 |
<dt><code>point</code></dt> |
|
|
375 |
<dd> |
|
|
376 |
Indicates the [x, y] coordinates where the doubletap should be |
|
|
377 |
simulated. Default is the center of the node element. |
|
|
378 |
</dd> |
|
|
379 |
</dl> |
|
|
380 |
|
|
|
381 |
<p> |
|
|
382 |
The <code>press</code> gesture is essentially a single tap with the <code>hold</code> property |
|
|
383 |
defined. Optional properties for the <code>press</code> gesture: |
|
|
384 |
</p> |
|
|
385 |
|
|
|
386 |
<dl> |
|
|
387 |
<dt><code>point</code></dt> |
|
|
388 |
<dd> |
|
|
389 |
Indicates the [x, y] coordinates where the tap should be |
|
|
390 |
simulated. Default is the center of the node element. |
|
|
391 |
</dd> |
|
|
392 |
<dt><code>hold</code></dt> |
|
|
393 |
<dd> |
|
|
394 |
The hold time in milliseconds. This is the time between |
|
|
395 |
<code>touchstart</code> and <code>touchend</code> event generation. |
|
|
396 |
</dd> |
|
|
397 |
</dl> |
|
|
398 |
|
|
|
399 |
<h3 id="single-touch-gestures-move-and-flick">Single Touch Gestures: Move and Flick</h3> |
|
|
400 |
|
|
|
401 |
<p> |
|
|
402 |
The following code can be used To simulate various gestures of moving one |
|
|
403 |
finger: |
|
|
404 |
</p> |
|
|
405 |
|
|
|
406 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
407 |
|
|
|
408 |
var node = Y.one("#myElement"); |
|
|
409 |
|
|
|
410 |
//simulate moving a finger 200 pixels along the x-axis |
|
|
411 |
//to the right for one second (default) |
|
|
412 |
node.simulateGesture("move"); |
|
|
413 |
|
|
|
414 |
//simulate moving a finger from the center of the node |
|
|
415 |
//to a point 70 pixels to the right and 50 pixels down over 2000ms |
|
|
416 |
node.simulateGesture("move", { |
|
|
417 |
path: { |
|
|
418 |
xdist: 70, |
|
|
419 |
ydist: -50 |
|
|
420 |
} , |
|
|
421 |
duration: 2000 |
|
|
422 |
}); |
|
|
423 |
|
|
|
424 |
//simulate a flick to the right (default) |
|
|
425 |
node.simulateGesture("flick"); |
|
|
426 |
|
|
|
427 |
//simulate a flick down 100 pixels over 50ms |
|
|
428 |
node.simulateGesture("flick", { |
|
|
429 |
axis: y |
|
|
430 |
distance: -100 |
|
|
431 |
duration: 50 |
|
|
432 |
}); |
|
|
433 |
|
|
|
434 |
});</pre> |
|
|
435 |
|
|
|
436 |
|
|
|
437 |
<h4 id="valid-options2">Valid Options</h4> |
|
|
438 |
|
|
|
439 |
<p> |
|
|
440 |
Optional properties for the <code>move</code> gesture: |
|
|
441 |
</p> |
|
|
442 |
|
|
|
443 |
<dl> |
|
|
444 |
<dt><code>path</code></dt> |
|
|
445 |
<dd> |
|
|
446 |
Indicates the path of the finger movement. It's an object with three |
|
|
447 |
optional properties: <code>point</code>, <code>xdist</code> and <code>ydist</code>. The <code>point</code> is |
|
|
448 |
the start point and defaults to the center of the node element. |
|
|
449 |
<code>xdist</code> and <code>ydist</code> indicate the distance moved in pixels along the |
|
|
450 |
x- and y-axis. A negative distance value indicates moving to left |
|
|
451 |
for <code>xdist</code> and down for <code>ydist</code>. |
|
|
452 |
</dd> |
|
|
453 |
<dt><code>duration</code></dt> |
|
|
454 |
<dd> |
|
|
455 |
The duration of the gesture in milliseconds. |
|
|
456 |
</dd> |
|
|
457 |
</dl> |
|
|
458 |
|
|
|
459 |
<p> |
|
|
460 |
Optional properties for the <code>flick</code> gesture: |
|
|
461 |
</p> |
|
|
462 |
|
|
|
463 |
<dl> |
|
|
464 |
<dt><code>point</code></dt> |
|
|
465 |
<dd> |
|
|
466 |
Indicates the [x, y] coordinates where the flick should be |
|
|
467 |
simulated. Default is the center of the node element. |
|
|
468 |
</dd> |
|
|
469 |
<dt><code>axis</code></dt> |
|
|
470 |
<dd> |
|
|
471 |
Valid values are either "x" or "y". Indicates moving axis. The flick |
|
|
472 |
moves in only 4 directions (left, right, up and down). |
|
|
473 |
</dd> |
|
|
474 |
<dt><code>distance</code></dt> |
|
|
475 |
<dd> |
|
|
476 |
Distance to move (in pixels). |
|
|
477 |
</dd> |
|
|
478 |
<dt><code>duration</code></dt> |
|
|
479 |
<dd> |
|
|
480 |
The duration of the gesture in milliseconds. This value may be |
|
|
481 |
adjusted if it is below the minimum velocity to be a flick gesture. |
|
|
482 |
</dd> |
|
|
483 |
</dl> |
|
|
484 |
|
|
|
485 |
<h3 id="two-finger-gestures-pinch-and-rotate">Two Finger Gestures: Pinch and Rotate</h3> |
|
|
486 |
|
|
|
487 |
<p> |
|
|
488 |
The <code>pinch</code> gesture is used to simulate the pinching and spreading of two |
|
|
489 |
fingers. During a pinch simulation, rotation is also possible. Essentially |
|
|
490 |
<code>pinch</code> and <code>rotate</code> simulations share the same base implementation to allow |
|
|
491 |
both pinching and rotation at the same time. The only difference is <code>pinch</code> |
|
|
492 |
requires <code>start</code> and <code>end</code> option properties while <code>rotate</code> requires <code>rotation</code> |
|
|
493 |
option property. |
|
|
494 |
</p> |
|
|
495 |
|
|
|
496 |
<p> |
|
|
497 |
The <code>pinch</code> and <code>rotate</code> gestures can be described as placing 2 fingers along a |
|
|
498 |
circle. Pinching and spreading can be described by start and end circles while |
|
|
499 |
rotation occurs on a single circle. If the radius of the start circle is greater |
|
|
500 |
than the end circle, the gesture becomes a pinch, otherwise it is a spread spread. |
|
|
501 |
</p> |
|
|
502 |
|
|
|
503 |
<p> |
|
|
504 |
The following code can be used to simulate two finger gestures: |
|
|
505 |
</p> |
|
|
506 |
|
|
|
507 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
508 |
|
|
|
509 |
var node = Y.one("#myElement"); |
|
|
510 |
|
|
|
511 |
//simulate a pinch: "r1" and "r2" are required |
|
|
512 |
node.simulateGesture("pinch", { |
|
|
513 |
r1: 100, // start circle radius at the center of the node |
|
|
514 |
r2: 50 // end circle radius at the center of the node |
|
|
515 |
}); |
|
|
516 |
|
|
|
517 |
//simulate a spread: same as "pinch" gesture but r2>r1 |
|
|
518 |
node.simulateGesture("pinch", { |
|
|
519 |
r1: 50, |
|
|
520 |
r2: 100 |
|
|
521 |
}); |
|
|
522 |
|
|
|
523 |
//simulate rotating a node 75 degrees counter-clockwise |
|
|
524 |
node.simulateGesture("rotate", { |
|
|
525 |
rotation: -75 |
|
|
526 |
}); |
|
|
527 |
|
|
|
528 |
//simulate a pinch and a rotation at the same time. |
|
|
529 |
//fingers start on a circle of radius 100 px, placed at top/bottom |
|
|
530 |
//fingers end on a circle of radius 50px, placed at right/left |
|
|
531 |
node.simulateGesture("pinch", { |
|
|
532 |
r1: 100, |
|
|
533 |
r2: 50, |
|
|
534 |
start: 0 |
|
|
535 |
rotation: 90 |
|
|
536 |
}); |
|
|
537 |
});</pre> |
|
|
538 |
|
|
|
539 |
<h4 id="valid-options3">Valid Options</h4> |
|
|
540 |
|
|
|
541 |
<p> |
|
|
542 |
The optional properties available for <code>pinch</code> and <code>rotate</code> gestures are the |
|
|
543 |
same: |
|
|
544 |
</p> |
|
|
545 |
|
|
|
546 |
<dl> |
|
|
547 |
<dt><code>center</code></dt> |
|
|
548 |
<dd> |
|
|
549 |
The center of the circle where the two fingers are placed. Default |
|
|
550 |
is the center of the node element. |
|
|
551 |
</dd> |
|
|
552 |
<dt><code>r1</code></dt> |
|
|
553 |
<dd> |
|
|
554 |
Required for <code>pinch</code> gestures but optional for <code>rotate</code>. Pixel radius |
|
|
555 |
of the start circle. If omitted in <code>rotate</code> gestures, default is |
|
|
556 |
a fourth of the node element width or height, whichever is smaller. |
|
|
557 |
</dd> |
|
|
558 |
<dt><code>r2</code></dt> |
|
|
559 |
<dd> |
|
|
560 |
Required for <code>pinch</code> gestures but optional for <code>rotate</code> gestures. |
|
|
561 |
Pixel radius of the end circle. If omitted in <code>rotate</code> gestures, |
|
|
562 |
default is a fourth of the node element width or height, whichever |
|
|
563 |
is smaller. |
|
|
564 |
</dd> |
|
|
565 |
<dt><code>duration</code></dt> |
|
|
566 |
<dd> |
|
|
567 |
The duration of the gesture in milliseconds. |
|
|
568 |
</dd> |
|
|
569 |
<dt><code>start</code></dt> |
|
|
570 |
<dd> |
|
|
571 |
Start degree of the first finger for the rotation gesture. Default |
|
|
572 |
is 0 (i.e., 12:00 on a clock). |
|
|
573 |
</dd> |
|
|
574 |
<dt><code>rotation</code></dt> |
|
|
575 |
<dd> |
|
|
576 |
Degrees to rotate from the start degree. Negative value means |
|
|
577 |
rotation of counter-clockwise direction. |
|
|
578 |
</dd> |
|
|
579 |
</dl> |
|
|
580 |
|
|
|
581 |
<h3 id="gesture-simulation-on-ios">Gesture Simulation on iOS</h3> |
|
|
582 |
<p> |
|
|
583 |
If the gesture simulation is called in iOS, it generates not only touch events |
|
|
584 |
but also <a href="http://developer.apple.com/library/safari/#documentation/UserExperience/Reference/GestureEventClassReference/GestureEvent/GestureEvent.html"> |
|
|
585 |
iOS specific gesture events</a>: <code>gesturestart</code>, <code>gesturechange</code> |
|
|
586 |
and <code>gestureend</code>. |
|
|
587 |
</p> |
|
|
588 |
|
|
|
589 |
<h3 id="customizing-default-gesture-properties">Customizing Default Gesture Properties</h3> |
|
|
590 |
|
|
|
591 |
<p> |
|
|
592 |
You can define custom default behaviors for gesture simulations by modifying the |
|
|
593 |
following <code>Y.GestureSimulation.defaults</code> object properties: |
|
|
594 |
</p> |
|
|
595 |
|
|
|
596 |
<ul> |
|
|
597 |
<li>HOLD_TAP</li> |
|
|
598 |
<li>DELAY_TAP</li> |
|
|
599 |
<li>HOLD_PRESS</li> |
|
|
600 |
<li>MIN_HOLD_PRESS</li> |
|
|
601 |
<li>MAX_HOLD_PRESS</li> |
|
|
602 |
<li>DISTANCE_MOVE</li> |
|
|
603 |
<li>DURATION_MOVE</li> |
|
|
604 |
<li>MAX_DURATION_MOVE</li> |
|
|
605 |
<li>MIN_VELOCITY_FLICK</li> |
|
|
606 |
<li>DISTANCE_FLICK</li> |
|
|
607 |
<li>DURATION_FLICK</li> |
|
|
608 |
<li>MAX_DURATION_FLICK</li> |
|
|
609 |
<li>DURATION_PINCH</li> |
|
|
610 |
</ul> |
|
|
611 |
|
|
|
612 |
<p> |
|
|
613 |
And an example: |
|
|
614 |
</p> |
|
|
615 |
|
|
|
616 |
<pre class="code prettyprint">YUI().use('node-event-simulate', function(Y) { |
|
|
617 |
|
|
|
618 |
var node = Y.one("#myElement"); |
|
|
619 |
|
|
|
620 |
Y.GestureSimulation.defaults = Y.merge(Y.GestureSimulation.defaults, { |
|
|
621 |
HOLD_TAP: 3000 |
|
|
622 |
}); |
|
|
623 |
|
|
|
624 |
//now touchend event will be generated after 3 sec from the touchstart |
|
|
625 |
//event generation. |
|
|
626 |
node.simulateGesture("tap"); |
|
|
627 |
});</pre> |
|
|
628 |
|
|
|
629 |
|
|
|
630 |
<h2 id="caveats-and-coming-soons">Caveats and Coming Soons</h2> |
|
|
631 |
|
|
|
632 |
<h3 id="faking">Don't use simulation in user facing code</h3> |
|
|
633 |
|
|
|
634 |
<p> |
|
|
635 |
Event simulation is for automated testing. Your application should respond |
|
|
636 |
to real user events. For reasons |
|
|
637 |
<a href="#only-what-you-ask-for">mentioned below</a>, it can be easy to get |
|
|
638 |
your application into a confused runtime state when some callbacks have |
|
|
639 |
been executed but not others. |
|
|
640 |
</p> |
|
|
641 |
|
|
|
642 |
<p> |
|
|
643 |
Typically, event simulation is sought to trigger certain callbacks. If a |
|
|
644 |
function needs to respond to user action or be called programmatically, it |
|
|
645 |
should be written accordingly and called directly in the latter case. |
|
|
646 |
Often a better solution is to extract the core logic from the event handler |
|
|
647 |
into a separate function and call that method from the event handler and |
|
|
648 |
from the other part of the application that was going to use simulation. |
|
|
649 |
</p> |
|
|
650 |
|
|
|
651 |
<p> |
|
|
652 |
In some cases, simulation is wanted because there may be any number of |
|
|
653 |
subscriptions on a node, and all applicable callbacks should be triggered. |
|
|
654 |
If this is the case, investigate using <a |
|
|
655 |
href="../event-custom/index.html">custom events</a>, instead. |
|
|
656 |
</p> |
|
|
657 |
|
|
|
658 |
<p> |
|
|
659 |
The bottom line is, reliance on event simulation in production code is a |
|
|
660 |
warning sign that the architecture is not scaling. The affected code |
|
|
661 |
should be refactored before it becomes a larger problem. |
|
|
662 |
</p> |
|
|
663 |
|
|
|
664 |
<h3 id="only-what-you-ask-for">Only what you ask for</h3> |
|
|
665 |
|
|
|
666 |
<p> |
|
|
667 |
In many cases, events happen in groups (<code>mousedown</code>, <code>mouseup</code>, <code>click</code>, or |
|
|
668 |
<code>keydown</code>, <code>keyup</code>, <code>keypress</code>). If you simulate an event that is |
|
|
669 |
typically part of a group or is often followed by other events, <em>the |
|
|
670 |
other events will NOT be generated</em> for free. |
|
|
671 |
</p> |
|
|
672 |
|
|
|
673 |
<p> |
|
|
674 |
For example, if you simulate a <code>click</code> event on a submit button, you only |
|
|
675 |
simulate the <code>click</code> event. The preceding <code>mousedown</code> and <code>mouseup</code>, as |
|
|
676 |
well as the subsequently expected 'submit' are neither simulated or fired |
|
|
677 |
natively. |
|
|
678 |
</p> |
|
|
679 |
|
|
|
680 |
<h3 id="no-synthetic-event-simulation-yet">No synthetic event simulation yet</h3> |
|
|
681 |
|
|
|
682 |
<p> |
|
|
683 |
The <a href="synths.html">Synthetic event system</a> doesn't yet support |
|
|
684 |
defining simulation. In most cases, though, synthetic events are triggered |
|
|
685 |
by other DOM events that can be simulated, so it's often possible to |
|
|
686 |
trigger them by simulating the underlying events. But that ignores the |
|
|
687 |
point that synthetic events are supposed to mask that abstraction for your |
|
|
688 |
benefit. |
|
|
689 |
</p> |
|
|
690 |
|
|
|
691 |
<p> |
|
|
692 |
Support for synthetic event simulation is on the roadmap. |
|
|
693 |
</p> |
|
|
694 |
</div> |
|
|
695 |
</div> |
|
|
696 |
</div> |
|
|
697 |
|
|
|
698 |
<div class="yui3-u-1-4"> |
|
|
699 |
<div class="sidebar"> |
|
|
700 |
|
|
|
701 |
<div id="toc" class="sidebox"> |
|
|
702 |
<div class="hd"> |
|
|
703 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
704 |
</div> |
|
|
705 |
|
|
|
706 |
<div class="bd"> |
|
|
707 |
<ul class="toc"> |
|
|
708 |
<li> |
|
|
709 |
<a href="#simulating-mouse-events">Simulating Mouse Events</a> |
|
|
710 |
</li> |
|
|
711 |
<li> |
|
|
712 |
<a href="#simulating-key-events">Simulating Key Events</a> |
|
|
713 |
</li> |
|
|
714 |
<li> |
|
|
715 |
<a href="#simulating-ui-events">Simulating UI Events</a> |
|
|
716 |
</li> |
|
|
717 |
<li> |
|
|
718 |
<a href="#simulating-touch-gestures">Simulating Touch Gestures</a> |
|
|
719 |
<ul class="toc"> |
|
|
720 |
<li> |
|
|
721 |
<a href="#single-touch-gestures-tap-double-tab-and-press">Single Touch Gestures: Tap, Double Tab and Press</a> |
|
|
722 |
<ul class="toc"> |
|
|
723 |
<li> |
|
|
724 |
<a href="#valid-options">Valid Options</a> |
|
|
725 |
</li> |
|
|
726 |
</ul> |
|
|
727 |
</li> |
|
|
728 |
<li> |
|
|
729 |
<a href="#single-touch-gestures-move-and-flick">Single Touch Gestures: Move and Flick</a> |
|
|
730 |
<ul class="toc"> |
|
|
731 |
<li> |
|
|
732 |
<a href="#valid-options2">Valid Options</a> |
|
|
733 |
</li> |
|
|
734 |
</ul> |
|
|
735 |
</li> |
|
|
736 |
<li> |
|
|
737 |
<a href="#two-finger-gestures-pinch-and-rotate">Two Finger Gestures: Pinch and Rotate</a> |
|
|
738 |
<ul class="toc"> |
|
|
739 |
<li> |
|
|
740 |
<a href="#valid-options3">Valid Options</a> |
|
|
741 |
</li> |
|
|
742 |
</ul> |
|
|
743 |
</li> |
|
|
744 |
<li> |
|
|
745 |
<a href="#gesture-simulation-on-ios">Gesture Simulation on iOS</a> |
|
|
746 |
</li> |
|
|
747 |
<li> |
|
|
748 |
<a href="#customizing-default-gesture-properties">Customizing Default Gesture Properties</a> |
|
|
749 |
</li> |
|
|
750 |
</ul> |
|
|
751 |
</li> |
|
|
752 |
<li> |
|
|
753 |
<a href="#caveats-and-coming-soons">Caveats and Coming Soons</a> |
|
|
754 |
<ul class="toc"> |
|
|
755 |
<li> |
|
|
756 |
<a href="#faking">Don't use simulation in user facing code</a> |
|
|
757 |
</li> |
|
|
758 |
<li> |
|
|
759 |
<a href="#only-what-you-ask-for">Only what you ask for</a> |
|
|
760 |
</li> |
|
|
761 |
<li> |
|
|
762 |
<a href="#no-synthetic-event-simulation-yet">No synthetic event simulation yet</a> |
|
|
763 |
</li> |
|
|
764 |
</ul> |
|
|
765 |
</li> |
|
|
766 |
</ul> |
|
|
767 |
</div> |
|
|
768 |
</div> |
|
|
769 |
|
|
|
770 |
|
|
|
771 |
|
|
|
772 |
<div class="sidebox"> |
|
|
773 |
<div class="hd"> |
|
|
774 |
<h2 class="no-toc">Examples</h2> |
|
|
775 |
</div> |
|
|
776 |
|
|
|
777 |
<div class="bd"> |
|
|
778 |
<ul class="examples"> |
|
|
779 |
|
|
|
780 |
|
|
|
781 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
782 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
783 |
</li> |
|
|
784 |
|
|
|
785 |
|
|
|
786 |
|
|
|
787 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
788 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
789 |
</li> |
|
|
790 |
|
|
|
791 |
|
|
|
792 |
|
|
|
793 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
794 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
795 |
</li> |
|
|
796 |
|
|
|
797 |
|
|
|
798 |
|
|
|
799 |
|
|
|
800 |
|
|
|
801 |
|
|
|
802 |
|
|
|
803 |
|
|
|
804 |
|
|
|
805 |
|
|
|
806 |
|
|
|
807 |
|
|
|
808 |
</ul> |
|
|
809 |
</div> |
|
|
810 |
</div> |
|
|
811 |
|
|
|
812 |
|
|
|
813 |
|
|
|
814 |
<div class="sidebox"> |
|
|
815 |
<div class="hd"> |
|
|
816 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
817 |
</div> |
|
|
818 |
|
|
|
819 |
<div class="bd"> |
|
|
820 |
<ul class="examples"> |
|
|
821 |
|
|
|
822 |
|
|
|
823 |
|
|
|
824 |
|
|
|
825 |
|
|
|
826 |
|
|
|
827 |
|
|
|
828 |
|
|
|
829 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
830 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
831 |
</li> |
|
|
832 |
|
|
|
833 |
|
|
|
834 |
|
|
|
835 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
836 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
837 |
</li> |
|
|
838 |
|
|
|
839 |
|
|
|
840 |
|
|
|
841 |
<li data-description="Example Photo Browser application."> |
|
|
842 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
843 |
</li> |
|
|
844 |
|
|
|
845 |
|
|
|
846 |
|
|
|
847 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
848 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
849 |
</li> |
|
|
850 |
|
|
|
851 |
|
|
|
852 |
|
|
|
853 |
<li data-description="Use IO to request data over HTTP."> |
|
|
854 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
855 |
</li> |
|
|
856 |
|
|
|
857 |
|
|
|
858 |
</ul> |
|
|
859 |
</div> |
|
|
860 |
</div> |
|
|
861 |
|
|
|
862 |
</div> |
|
|
863 |
</div> |
|
|
864 |
</div> |
|
|
865 |
</div> |
|
|
866 |
|
|
|
867 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
868 |
<script>prettyPrint();</script> |
|
|
869 |
|
|
|
870 |
<script> |
|
|
871 |
YUI.Env.Tests = { |
|
|
872 |
examples: [], |
|
|
873 |
project: '../assets', |
|
|
874 |
assets: '../assets/event', |
|
|
875 |
name: 'event', |
|
|
876 |
title: 'Simulating DOM Events', |
|
|
877 |
newWindow: '', |
|
|
878 |
auto: false |
|
|
879 |
}; |
|
|
880 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
881 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
882 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
883 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
884 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
885 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
886 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
887 |
YUI.Env.Tests.examples.push('get'); |
|
|
888 |
|
|
|
889 |
</script> |
|
|
890 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
891 |
|
|
|
892 |
|
|
|
893 |
|
|
|
894 |
</body> |
|
|
895 |
</html> |