3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('event-simulate', function(Y) {
11
* Simulate user interaction by generating native DOM events.
13
* @module event-simulate
20
isFunction = L.isFunction,
21
isString = L.isString,
22
isBoolean = L.isBoolean,
23
isObject = L.isObject,
24
isNumber = L.isNumber,
27
//mouse events supported
39
//key events supported
46
//HTML events supported
56
//events that bubble by default
68
//all key and mouse events bubble
69
Y.mix(bubbleEvents, mouseEvents);
70
Y.mix(bubbleEvents, keyEvents);
73
* Note: Intentionally not for YUIDoc generation.
74
* Simulates a key event using the given event information to populate
75
* the generated event object. This method does browser-equalizing
76
* calculations to account for differences in the DOM and IE event models
77
* as well as different browser quirks. Note: keydown causes Safari 2.x to
79
* @method simulateKeyEvent
82
* @param {HTMLElement} target The target of the given event.
83
* @param {String} type The type of event to fire. This can be any one of
84
* the following: keyup, keydown, and keypress.
85
* @param {Boolean} bubbles (Optional) Indicates if the event can be
86
* bubbled up. DOM Level 3 specifies that all key events bubble by
87
* default. The default is true.
88
* @param {Boolean} cancelable (Optional) Indicates if the event can be
89
* canceled using preventDefault(). DOM Level 3 specifies that all
90
* key events can be cancelled. The default
92
* @param {Window} view (Optional) The view containing the target. This is
93
* typically the window object. The default is window.
94
* @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
95
* is pressed while the event is firing. The default is false.
96
* @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
97
* is pressed while the event is firing. The default is false.
98
* @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
99
* is pressed while the event is firing. The default is false.
100
* @param {Boolean} metaKey (Optional) Indicates if one of the META keys
101
* is pressed while the event is firing. The default is false.
102
* @param {int} keyCode (Optional) The code for the key that is in use.
104
* @param {int} charCode (Optional) The Unicode code for the character
105
* associated with the key being used. The default is 0.
107
function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,
108
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
110
ctrlKey /*:Boolean*/, altKey /*:Boolean*/,
111
shiftKey /*:Boolean*/, metaKey /*:Boolean*/,
112
keyCode /*:int*/, charCode /*:int*/) /*:Void*/
116
Y.error("simulateKeyEvent(): Invalid target.");
121
type = type.toLowerCase();
123
case "textevent": //DOM Level 3
131
Y.error("simulateKeyEvent(): Event type '" + type + "' not supported.");
134
Y.error("simulateKeyEvent(): Event type must be a string.");
137
//setup default values
138
if (!isBoolean(bubbles)){
139
bubbles = true; //all key events bubble
141
if (!isBoolean(cancelable)){
142
cancelable = true; //all key events can be cancelled
144
if (!isObject(view)){
145
view = Y.config.win; //view is typically window
147
if (!isBoolean(ctrlKey)){
150
if (!isBoolean(altKey)){
153
if (!isBoolean(shiftKey)){
156
if (!isBoolean(metaKey)){
159
if (!isNumber(keyCode)){
162
if (!isNumber(charCode)){
166
//try to create a mouse event
167
var customEvent /*:MouseEvent*/ = null;
169
//check for DOM-compliant browsers first
170
if (isFunction(doc.createEvent)){
174
//try to create key event
175
customEvent = doc.createEvent("KeyEvents");
178
* Interesting problem: Firefox implemented a non-standard
179
* version of initKeyEvent() based on DOM Level 2 specs.
180
* Key event was removed from DOM Level 2 and re-introduced
181
* in DOM Level 3 with a different interface. Firefox is the
182
* only browser with any implementation of Key Events, so for
183
* now, assume it's Firefox if the above line doesn't error.
185
// @TODO: Decipher between Firefox's implementation and a correct one.
186
customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,
187
altKey, shiftKey, metaKey, keyCode, charCode);
189
} catch (ex /*:Error*/){
192
* If it got here, that means key events aren't officially supported.
193
* Safari/WebKit is a real problem now. WebKit 522 won't let you
194
* set keyCode, charCode, or other properties if you use a
195
* UIEvent, so we first must try to create a generic event. The
196
* fun part is that this will throw an error on Safari 2.x. The
197
* end result is that we need another try...catch statement just to
198
* deal with this mess.
202
//try to create generic event - will fail in Safari 2.x
203
customEvent = doc.createEvent("Events");
205
} catch (uierror /*:Error*/){
207
//the above failed, so create a UIEvent for Safari 2.x
208
customEvent = doc.createEvent("UIEvents");
212
customEvent.initEvent(type, bubbles, cancelable);
215
customEvent.view = view;
216
customEvent.altKey = altKey;
217
customEvent.ctrlKey = ctrlKey;
218
customEvent.shiftKey = shiftKey;
219
customEvent.metaKey = metaKey;
220
customEvent.keyCode = keyCode;
221
customEvent.charCode = charCode;
228
target.dispatchEvent(customEvent);
230
} else if (isObject(doc.createEventObject)){ //IE
232
//create an IE event object
233
customEvent = doc.createEventObject();
235
//assign available properties
236
customEvent.bubbles = bubbles;
237
customEvent.cancelable = cancelable;
238
customEvent.view = view;
239
customEvent.ctrlKey = ctrlKey;
240
customEvent.altKey = altKey;
241
customEvent.shiftKey = shiftKey;
242
customEvent.metaKey = metaKey;
245
* IE doesn't support charCode explicitly. CharCode should
246
* take precedence over any keyCode value for accurate
249
customEvent.keyCode = (charCode > 0) ? charCode : keyCode;
252
target.fireEvent("on" + type, customEvent);
255
Y.error("simulateKeyEvent(): No event simulation framework present.");
260
* Note: Intentionally not for YUIDoc generation.
261
* Simulates a mouse event using the given event information to populate
262
* the generated event object. This method does browser-equalizing
263
* calculations to account for differences in the DOM and IE event models
264
* as well as different browser quirks.
265
* @method simulateMouseEvent
268
* @param {HTMLElement} target The target of the given event.
269
* @param {String} type The type of event to fire. This can be any one of
270
* the following: click, dblclick, mousedown, mouseup, mouseout,
271
* mouseover, and mousemove.
272
* @param {Boolean} bubbles (Optional) Indicates if the event can be
273
* bubbled up. DOM Level 2 specifies that all mouse events bubble by
274
* default. The default is true.
275
* @param {Boolean} cancelable (Optional) Indicates if the event can be
276
* canceled using preventDefault(). DOM Level 2 specifies that all
277
* mouse events except mousemove can be cancelled. The default
278
* is true for all events except mousemove, for which the default
280
* @param {Window} view (Optional) The view containing the target. This is
281
* typically the window object. The default is window.
282
* @param {int} detail (Optional) The number of times the mouse button has
283
* been used. The default value is 1.
284
* @param {int} screenX (Optional) The x-coordinate on the screen at which
285
* point the event occured. The default is 0.
286
* @param {int} screenY (Optional) The y-coordinate on the screen at which
287
* point the event occured. The default is 0.
288
* @param {int} clientX (Optional) The x-coordinate on the client at which
289
* point the event occured. The default is 0.
290
* @param {int} clientY (Optional) The y-coordinate on the client at which
291
* point the event occured. The default is 0.
292
* @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
293
* is pressed while the event is firing. The default is false.
294
* @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
295
* is pressed while the event is firing. The default is false.
296
* @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
297
* is pressed while the event is firing. The default is false.
298
* @param {Boolean} metaKey (Optional) Indicates if one of the META keys
299
* is pressed while the event is firing. The default is false.
300
* @param {int} button (Optional) The button being pressed while the event
301
* is executing. The value should be 0 for the primary mouse button
302
* (typically the left button), 1 for the terciary mouse button
303
* (typically the middle button), and 2 for the secondary mouse button
304
* (typically the right button). The default is 0.
305
* @param {HTMLElement} relatedTarget (Optional) For mouseout events,
306
* this is the element that the mouse has moved to. For mouseover
307
* events, this is the element that the mouse has moved from. This
308
* argument is ignored for all other events. The default is null.
310
function simulateMouseEvent(target /*:HTMLElement*/, type /*:String*/,
311
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
312
view /*:Window*/, detail /*:int*/,
313
screenX /*:int*/, screenY /*:int*/,
314
clientX /*:int*/, clientY /*:int*/,
315
ctrlKey /*:Boolean*/, altKey /*:Boolean*/,
316
shiftKey /*:Boolean*/, metaKey /*:Boolean*/,
317
button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/
322
Y.error("simulateMouseEvent(): Invalid target.");
327
type = type.toLowerCase();
329
//make sure it's a supported mouse event
330
if (!mouseEvents[type]){
331
Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");
334
Y.error("simulateMouseEvent(): Event type must be a string.");
337
//setup default values
338
if (!isBoolean(bubbles)){
339
bubbles = true; //all mouse events bubble
341
if (!isBoolean(cancelable)){
342
cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled
344
if (!isObject(view)){
345
view = Y.config.win; //view is typically window
347
if (!isNumber(detail)){
348
detail = 1; //number of mouse clicks must be at least one
350
if (!isNumber(screenX)){
353
if (!isNumber(screenY)){
356
if (!isNumber(clientX)){
359
if (!isNumber(clientY)){
362
if (!isBoolean(ctrlKey)){
365
if (!isBoolean(altKey)){
368
if (!isBoolean(shiftKey)){
371
if (!isBoolean(metaKey)){
374
if (!isNumber(button)){
378
relatedTarget = relatedTarget || null;
380
//try to create a mouse event
381
var customEvent /*:MouseEvent*/ = null;
383
//check for DOM-compliant browsers first
384
if (isFunction(doc.createEvent)){
386
customEvent = doc.createEvent("MouseEvents");
388
//Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()
389
if (customEvent.initMouseEvent){
390
customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,
391
screenX, screenY, clientX, clientY,
392
ctrlKey, altKey, shiftKey, metaKey,
393
button, relatedTarget);
396
//the closest thing available in Safari 2.x is UIEvents
397
customEvent = doc.createEvent("UIEvents");
398
customEvent.initEvent(type, bubbles, cancelable);
399
customEvent.view = view;
400
customEvent.detail = detail;
401
customEvent.screenX = screenX;
402
customEvent.screenY = screenY;
403
customEvent.clientX = clientX;
404
customEvent.clientY = clientY;
405
customEvent.ctrlKey = ctrlKey;
406
customEvent.altKey = altKey;
407
customEvent.metaKey = metaKey;
408
customEvent.shiftKey = shiftKey;
409
customEvent.button = button;
410
customEvent.relatedTarget = relatedTarget;
414
* Check to see if relatedTarget has been assigned. Firefox
415
* versions less than 2.0 don't allow it to be assigned via
416
* initMouseEvent() and the property is readonly after event
417
* creation, so in order to keep YAHOO.util.getRelatedTarget()
418
* working, assign to the IE proprietary toElement property
419
* for mouseout event and fromElement property for mouseover
422
if (relatedTarget && !customEvent.relatedTarget){
423
if (type == "mouseout"){
424
customEvent.toElement = relatedTarget;
425
} else if (type == "mouseover"){
426
customEvent.fromElement = relatedTarget;
431
target.dispatchEvent(customEvent);
433
} else if (isObject(doc.createEventObject)){ //IE
435
//create an IE event object
436
customEvent = doc.createEventObject();
438
//assign available properties
439
customEvent.bubbles = bubbles;
440
customEvent.cancelable = cancelable;
441
customEvent.view = view;
442
customEvent.detail = detail;
443
customEvent.screenX = screenX;
444
customEvent.screenY = screenY;
445
customEvent.clientX = clientX;
446
customEvent.clientY = clientY;
447
customEvent.ctrlKey = ctrlKey;
448
customEvent.altKey = altKey;
449
customEvent.metaKey = metaKey;
450
customEvent.shiftKey = shiftKey;
452
//fix button property for IE's wacky implementation
455
customEvent.button = 1;
458
customEvent.button = 4;
464
customEvent.button = 0;
468
* Have to use relatedTarget because IE won't allow assignment
469
* to toElement or fromElement on generic events. This keeps
470
* YAHOO.util.customEvent.getRelatedTarget() functional.
472
customEvent.relatedTarget = relatedTarget;
475
target.fireEvent("on" + type, customEvent);
478
Y.error("simulateMouseEvent(): No event simulation framework present.");
483
* Note: Intentionally not for YUIDoc generation.
484
* Simulates a UI event using the given event information to populate
485
* the generated event object. This method does browser-equalizing
486
* calculations to account for differences in the DOM and IE event models
487
* as well as different browser quirks.
488
* @method simulateHTMLEvent
491
* @param {HTMLElement} target The target of the given event.
492
* @param {String} type The type of event to fire. This can be any one of
493
* the following: click, dblclick, mousedown, mouseup, mouseout,
494
* mouseover, and mousemove.
495
* @param {Boolean} bubbles (Optional) Indicates if the event can be
496
* bubbled up. DOM Level 2 specifies that all mouse events bubble by
497
* default. The default is true.
498
* @param {Boolean} cancelable (Optional) Indicates if the event can be
499
* canceled using preventDefault(). DOM Level 2 specifies that all
500
* mouse events except mousemove can be cancelled. The default
501
* is true for all events except mousemove, for which the default
503
* @param {Window} view (Optional) The view containing the target. This is
504
* typically the window object. The default is window.
505
* @param {int} detail (Optional) The number of times the mouse button has
506
* been used. The default value is 1.
508
function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,
509
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
510
view /*:Window*/, detail /*:int*/) /*:Void*/
515
Y.error("simulateUIEvent(): Invalid target.");
520
type = type.toLowerCase();
522
//make sure it's a supported mouse event
523
if (!uiEvents[type]){
524
Y.error("simulateUIEvent(): Event type '" + type + "' not supported.");
527
Y.error("simulateUIEvent(): Event type must be a string.");
530
//try to create a mouse event
531
var customEvent = null;
534
//setup default values
535
if (!isBoolean(bubbles)){
536
bubbles = (type in bubbleEvents); //not all events bubble
538
if (!isBoolean(cancelable)){
539
cancelable = (type == "submit"); //submit is the only one that can be cancelled
541
if (!isObject(view)){
542
view = Y.config.win; //view is typically window
544
if (!isNumber(detail)){
545
detail = 1; //usually not used but defaulted to this
548
//check for DOM-compliant browsers first
549
if (isFunction(doc.createEvent)){
551
//just a generic UI Event object is needed
552
customEvent = doc.createEvent("UIEvents");
553
customEvent.initUIEvent(type, bubbles, cancelable, view, detail);
556
target.dispatchEvent(customEvent);
558
} else if (isObject(doc.createEventObject)){ //IE
560
//create an IE event object
561
customEvent = doc.createEventObject();
563
//assign available properties
564
customEvent.bubbles = bubbles;
565
customEvent.cancelable = cancelable;
566
customEvent.view = view;
567
customEvent.detail = detail;
570
target.fireEvent("on" + type, customEvent);
573
Y.error("simulateUIEvent(): No event simulation framework present.");
578
* Simulates the event with the given name on a target.
579
* @param {HTMLElement} target The DOM element that's the target of the event.
580
* @param {String} type The type of event to simulate (i.e., "click").
581
* @param {Object} options (Optional) Extra options to copy onto the event object.
587
Y.Event.simulate = function(target, type, options){
589
options = options || {};
591
if (mouseEvents[type]){
592
simulateMouseEvent(target, type, options.bubbles,
593
options.cancelable, options.view, options.detail, options.screenX,
594
options.screenY, options.clientX, options.clientY, options.ctrlKey,
595
options.altKey, options.shiftKey, options.metaKey, options.button,
596
options.relatedTarget);
597
} else if (keyEvents[type]){
598
simulateKeyEvent(target, type, options.bubbles,
599
options.cancelable, options.view, options.ctrlKey,
600
options.altKey, options.shiftKey, options.metaKey,
601
options.keyCode, options.charCode);
602
} else if (uiEvents[type]){
603
simulateUIEvent(target, type, options.bubbles,
604
options.cancelable, options.view, options.detail);
606
Y.error("simulate(): Event '" + type + "' can't be simulated.");
615
}, '3.5.1' ,{requires:['event-base']});