68
67
//all key and mouse events bubble
69
68
Y.mix(bubbleEvents, mouseEvents);
70
Y.mix(bubbleEvents, keyEvents);
69
Y.mix(bubbleEvents, keyEvents);
73
72
* Note: Intentionally not for YUIDoc generation.
87
86
* default. The default is true.
88
87
* @param {Boolean} cancelable (Optional) Indicates if the event can be
89
88
* canceled using preventDefault(). DOM Level 3 specifies that all
90
* key events can be cancelled. The default
89
* key events can be cancelled. The default
92
91
* @param {Window} view (Optional) The view containing the target. This is
93
92
* typically the window object. The default is window.
99
98
* is pressed while the event is firing. The default is false.
100
99
* @param {Boolean} metaKey (Optional) Indicates if one of the META keys
101
100
* 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.
101
* @param {int} keyCode (Optional) The code for the key that is in use.
103
102
* The default is 0.
104
103
* @param {int} charCode (Optional) The Unicode code for the character
105
104
* associated with the key being used. The default is 0.
107
function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,
108
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
106
function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,
107
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
109
108
view /*:Window*/,
110
ctrlKey /*:Boolean*/, altKey /*:Boolean*/,
111
shiftKey /*:Boolean*/, metaKey /*:Boolean*/,
112
keyCode /*:int*/, charCode /*:int*/) /*:Void*/
109
ctrlKey /*:Boolean*/, altKey /*:Boolean*/,
110
shiftKey /*:Boolean*/, metaKey /*:Boolean*/,
111
keyCode /*:int*/, charCode /*:int*/) /*:Void*/
116
115
Y.error("simulateKeyEvent(): Invalid target.");
119
118
//check event type
120
119
if (isString(type)){
121
120
type = type.toLowerCase();
162
161
if (!isNumber(charCode)){
166
165
//try to create a mouse event
167
166
var customEvent /*:MouseEvent*/ = null;
169
168
//check for DOM-compliant browsers first
170
169
if (isFunction(doc.createEvent)){
174
173
//try to create key event
175
174
customEvent = doc.createEvent("KeyEvents");
178
177
* Interesting problem: Firefox implemented a non-standard
179
178
* version of initKeyEvent() based on DOM Level 2 specs.
185
184
// @TODO: Decipher between Firefox's implementation and a correct one.
186
185
customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,
187
altKey, shiftKey, metaKey, keyCode, charCode);
186
altKey, shiftKey, metaKey, keyCode, charCode);
189
188
} catch (ex /*:Error*/){
192
* If it got here, that means key events aren't officially supported.
191
* If it got here, that means key events aren't officially supported.
193
192
* Safari/WebKit is a real problem now. WebKit 522 won't let you
194
193
* set keyCode, charCode, or other properties if you use a
195
194
* UIEvent, so we first must try to create a generic event. The
219
218
customEvent.metaKey = metaKey;
220
219
customEvent.keyCode = keyCode;
221
220
customEvent.charCode = charCode;
228
227
target.dispatchEvent(customEvent);
230
229
} else if (isObject(doc.createEventObject)){ //IE
232
231
//create an IE event object
233
232
customEvent = doc.createEventObject();
235
234
//assign available properties
236
235
customEvent.bubbles = bubbles;
237
236
customEvent.cancelable = cancelable;
240
239
customEvent.altKey = altKey;
241
240
customEvent.shiftKey = shiftKey;
242
241
customEvent.metaKey = metaKey;
245
244
* IE doesn't support charCode explicitly. CharCode should
246
245
* take precedence over any keyCode value for accurate
247
246
* representation.
249
248
customEvent.keyCode = (charCode > 0) ? charCode : keyCode;
252
target.fireEvent("on" + type, customEvent);
251
target.fireEvent("on" + type, customEvent);
255
254
Y.error("simulateKeyEvent(): No event simulation framework present.");
274
273
* default. The default is true.
275
274
* @param {Boolean} cancelable (Optional) Indicates if the event can be
276
275
* 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
276
* mouse events except mousemove can be cancelled. The default
277
* is true for all events except mousemove, for which the default
280
279
* @param {Window} view (Optional) The view containing the target. This is
281
280
* typically the window object. The default is window.
307
306
* events, this is the element that the mouse has moved from. This
308
307
* 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*/,
309
function simulateMouseEvent(target /*:HTMLElement*/, type /*:String*/,
310
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
311
view /*:Window*/, detail /*:int*/,
312
screenX /*:int*/, screenY /*:int*/,
313
clientX /*:int*/, clientY /*:int*/,
314
ctrlKey /*:Boolean*/, altKey /*:Boolean*/,
315
shiftKey /*:Boolean*/, metaKey /*:Boolean*/,
317
316
button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/
322
321
Y.error("simulateMouseEvent(): Invalid target.");
325
324
//check event type
326
325
if (isString(type)){
327
326
type = type.toLowerCase();
329
328
//make sure it's a supported mouse event
330
329
if (!mouseEvents[type]){
331
330
Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");
374
373
if (!isNumber(button)){
377
relatedTarget = relatedTarget || null;
378
379
//try to create a mouse event
379
380
var customEvent /*:MouseEvent*/ = null;
381
382
//check for DOM-compliant browsers first
382
383
if (isFunction(doc.createEvent)){
384
385
customEvent = doc.createEvent("MouseEvents");
386
387
//Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()
387
388
if (customEvent.initMouseEvent){
388
389
customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,
389
screenX, screenY, clientX, clientY,
390
ctrlKey, altKey, shiftKey, metaKey,
390
screenX, screenY, clientX, clientY,
391
ctrlKey, altKey, shiftKey, metaKey,
391
392
button, relatedTarget);
392
393
} else { //Safari
394
395
//the closest thing available in Safari 2.x is UIEvents
395
396
customEvent = doc.createEvent("UIEvents");
396
397
customEvent.initEvent(type, bubbles, cancelable);
424
425
customEvent.fromElement = relatedTarget;
429
430
target.dispatchEvent(customEvent);
431
432
} else if (isObject(doc.createEventObject)){ //IE
433
434
//create an IE event object
434
435
customEvent = doc.createEventObject();
436
437
//assign available properties
437
438
customEvent.bubbles = bubbles;
438
439
customEvent.cancelable = cancelable;
495
496
* default. The default is true.
496
497
* @param {Boolean} cancelable (Optional) Indicates if the event can be
497
498
* canceled using preventDefault(). DOM Level 2 specifies that all
498
* mouse events except mousemove can be cancelled. The default
499
* is true for all events except mousemove, for which the default
499
* mouse events except mousemove can be cancelled. The default
500
* is true for all events except mousemove, for which the default
501
502
* @param {Window} view (Optional) The view containing the target. This is
502
503
* typically the window object. The default is window.
503
504
* @param {int} detail (Optional) The number of times the mouse button has
504
505
* been used. The default value is 1.
506
function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,
507
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
507
function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,
508
bubbles /*:Boolean*/, cancelable /*:Boolean*/,
508
509
view /*:Window*/, detail /*:int*/) /*:Void*/
513
514
Y.error("simulateUIEvent(): Invalid target.");
516
517
//check event type
517
518
if (isString(type)){
518
519
type = type.toLowerCase();
520
521
//make sure it's a supported mouse event
521
522
if (!uiEvents[type]){
522
523
Y.error("simulateUIEvent(): Event type '" + type + "' not supported.");
542
543
if (!isNumber(detail)){
543
544
detail = 1; //usually not used but defaulted to this
546
547
//check for DOM-compliant browsers first
547
548
if (isFunction(doc.createEvent)){
549
550
//just a generic UI Event object is needed
550
551
customEvent = doc.createEvent("UIEvents");
551
552
customEvent.initUIEvent(type, bubbles, cancelable, view, detail);
554
555
target.dispatchEvent(customEvent);
556
557
} else if (isObject(doc.createEventObject)){ //IE
558
559
//create an IE event object
559
560
customEvent = doc.createEventObject();
561
562
//assign available properties
562
563
customEvent.bubbles = bubbles;
563
564
customEvent.cancelable = cancelable;
578
579
* @param {String} type The type of event to simulate (i.e., "click").
579
580
* @param {Object} options (Optional) Extra options to copy onto the event object.
581
583
* @method simulate
584
586
Y.Event.simulate = function(target, type, options){
586
588
options = options || {};
588
590
if (mouseEvents[type]){
589
591
simulateMouseEvent(target, type, options.bubbles,
590
options.cancelable, options.view, options.detail, options.screenX,
592
options.cancelable, options.view, options.detail, options.screenX,
591
593
options.screenY, options.clientX, options.clientY, options.ctrlKey,
592
options.altKey, options.shiftKey, options.metaKey, options.button,
593
options.relatedTarget);
594
options.altKey, options.shiftKey, options.metaKey, options.button,
595
options.relatedTarget);
594
596
} else if (keyEvents[type]){
595
597
simulateKeyEvent(target, type, options.bubbles,
596
598
options.cancelable, options.view, options.ctrlKey,
597
options.altKey, options.shiftKey, options.metaKey,
598
options.keyCode, options.charCode);
599
options.altKey, options.shiftKey, options.metaKey,
600
options.keyCode, options.charCode);
599
601
} else if (uiEvents[type]){
600
602
simulateUIEvent(target, type, options.bubbles,
601
options.cancelable, options.view, options.detail);
603
options.cancelable, options.view, options.detail);
603
605
Y.error("simulate(): Event '" + type + "' can't be simulated.");