~bjornt/lazr-js/prefetch-yui-3.5

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/event-simulate/event-simulate.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-01-14 23:32:29 UTC
  • mfrom: (197.1.7 yui-3.3.0)
  • Revision ID: launchpad@pqm.canonical.com-20110114233229-r6i4cazdiiw18o7p
Upgrade to YUI 3.3.0 [r=mars]

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
3
Code licensed under the BSD License:
4
4
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
 
5
version: 3.3.0
 
6
build: 3167
7
7
*/
8
8
YUI.add('event-simulate', function(Y) {
9
9
 
23
23
    isObject    = L.isObject,
24
24
    isNumber    = L.isNumber,
25
25
    doc         = Y.config.doc,
26
 
    
 
26
 
27
27
    //mouse events supported
28
28
    mouseEvents = {
29
29
        click:      1,
34
34
        mouseup:    1,
35
35
        mousemove:  1
36
36
    },
37
 
    
 
37
 
38
38
    //key events supported
39
39
    keyEvents   = {
40
40
        keydown:    1,
41
41
        keyup:      1,
42
42
        keypress:   1
43
43
    },
44
 
    
 
44
 
45
45
    //HTML events supported
46
46
    uiEvents  = {
47
47
        blur:       1,
50
50
        resize:     1,
51
51
        scroll:     1,
52
52
        select:     1
 
53
    },
53
54
 
54
 
    },
55
 
    
56
55
    //events that bubble by default
57
56
    bubbleEvents = {
58
57
        scroll:     1,
64
63
        error:      1,
65
64
        abort:      1
66
65
    };
67
 
    
 
66
 
68
67
//all key and mouse events bubble
69
68
Y.mix(bubbleEvents, mouseEvents);
70
 
Y.mix(bubbleEvents, keyEvents);    
 
69
Y.mix(bubbleEvents, keyEvents);
71
70
 
72
71
/*
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
91
90
 *      is true.
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.
106
105
 */
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*/
113
112
{
114
 
    //check target    
 
113
    //check target
115
114
    if (!target){
116
115
        Y.error("simulateKeyEvent(): Invalid target.");
117
116
    }
118
 
    
 
117
 
119
118
    //check event type
120
119
    if (isString(type)){
121
120
        type = type.toLowerCase();
133
132
    } else {
134
133
        Y.error("simulateKeyEvent(): Event type must be a string.");
135
134
    }
136
 
    
 
135
 
137
136
    //setup default values
138
137
    if (!isBoolean(bubbles)){
139
138
        bubbles = true; //all key events bubble
160
159
        keyCode = 0;
161
160
    }
162
161
    if (!isNumber(charCode)){
163
 
        charCode = 0; 
 
162
        charCode = 0;
164
163
    }
165
164
 
166
165
    //try to create a mouse event
167
166
    var customEvent /*:MouseEvent*/ = null;
168
 
        
 
167
 
169
168
    //check for DOM-compliant browsers first
170
169
    if (isFunction(doc.createEvent)){
171
 
    
 
170
 
172
171
        try {
173
 
            
 
172
 
174
173
            //try to create key event
175
174
            customEvent = doc.createEvent("KeyEvents");
176
 
            
 
175
 
177
176
            /*
178
177
             * Interesting problem: Firefox implemented a non-standard
179
178
             * version of initKeyEvent() based on DOM Level 2 specs.
184
183
             */
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);       
188
 
            
 
186
                altKey, shiftKey, metaKey, keyCode, charCode);
 
187
 
189
188
        } catch (ex /*:Error*/){
190
189
 
191
190
            /*
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;
222
 
      
223
 
            }          
224
 
         
 
221
 
 
222
            }
 
223
 
225
224
        }
226
 
        
 
225
 
227
226
        //fire the event
228
227
        target.dispatchEvent(customEvent);
229
228
 
230
229
    } else if (isObject(doc.createEventObject)){ //IE
231
 
    
 
230
 
232
231
        //create an IE event object
233
232
        customEvent = doc.createEventObject();
234
 
        
 
233
 
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;
243
 
        
 
242
 
244
243
        /*
245
244
         * IE doesn't support charCode explicitly. CharCode should
246
245
         * take precedence over any keyCode value for accurate
247
246
         * representation.
248
247
         */
249
248
        customEvent.keyCode = (charCode > 0) ? charCode : keyCode;
250
 
        
 
249
 
251
250
        //fire the event
252
 
        target.fireEvent("on" + type, customEvent);  
253
 
                
 
251
        target.fireEvent("on" + type, customEvent);
 
252
 
254
253
    } else {
255
254
        Y.error("simulateKeyEvent(): No event simulation framework present.");
256
255
    }
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
279
278
 *      is false.
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.
309
308
 */
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*/
318
317
{
319
 
    
320
 
    //check target   
 
318
 
 
319
    //check target
321
320
    if (!target){
322
321
        Y.error("simulateMouseEvent(): Invalid target.");
323
322
    }
324
 
    
 
323
 
325
324
    //check event type
326
325
    if (isString(type)){
327
326
        type = type.toLowerCase();
328
 
        
 
327
 
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.");
333
332
    } else {
334
333
        Y.error("simulateMouseEvent(): Event type must be a string.");
335
334
    }
336
 
    
 
335
 
337
336
    //setup default values
338
337
    if (!isBoolean(bubbles)){
339
338
        bubbles = true; //all mouse events bubble
348
347
        detail = 1;  //number of mouse clicks must be at least one
349
348
    }
350
349
    if (!isNumber(screenX)){
351
 
        screenX = 0; 
 
350
        screenX = 0;
352
351
    }
353
352
    if (!isNumber(screenY)){
354
 
        screenY = 0; 
 
353
        screenY = 0;
355
354
    }
356
355
    if (!isNumber(clientX)){
357
 
        clientX = 0; 
 
356
        clientX = 0;
358
357
    }
359
358
    if (!isNumber(clientY)){
360
 
        clientY = 0; 
 
359
        clientY = 0;
361
360
    }
362
361
    if (!isBoolean(ctrlKey)){
363
362
        ctrlKey = false;
372
371
        metaKey = false;
373
372
    }
374
373
    if (!isNumber(button)){
375
 
        button = 0; 
 
374
        button = 0;
376
375
    }
377
376
 
 
377
    relatedTarget = relatedTarget || null;
 
378
 
378
379
    //try to create a mouse event
379
380
    var customEvent /*:MouseEvent*/ = null;
380
 
        
 
381
 
381
382
    //check for DOM-compliant browsers first
382
383
    if (isFunction(doc.createEvent)){
383
 
    
 
384
 
384
385
        customEvent = doc.createEvent("MouseEvents");
385
 
    
 
386
 
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
393
 
        
 
394
 
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);
407
408
            customEvent.button = button;
408
409
            customEvent.relatedTarget = relatedTarget;
409
410
        }
410
 
        
 
411
 
411
412
        /*
412
413
         * Check to see if relatedTarget has been assigned. Firefox
413
414
         * versions less than 2.0 don't allow it to be assigned via
424
425
                customEvent.fromElement = relatedTarget;
425
426
            }
426
427
        }
427
 
        
 
428
 
428
429
        //fire the event
429
430
        target.dispatchEvent(customEvent);
430
431
 
431
432
    } else if (isObject(doc.createEventObject)){ //IE
432
 
    
 
433
 
433
434
        //create an IE event object
434
435
        customEvent = doc.createEventObject();
435
 
        
 
436
 
436
437
        //assign available properties
437
438
        customEvent.bubbles = bubbles;
438
439
        customEvent.cancelable = cancelable;
459
460
                //leave as is
460
461
                break;
461
462
            default:
462
 
                customEvent.button = 0;                    
463
 
        }    
 
463
                customEvent.button = 0;
 
464
        }
464
465
 
465
466
        /*
466
467
         * Have to use relatedTarget because IE won't allow assignment
468
469
         * YAHOO.util.customEvent.getRelatedTarget() functional.
469
470
         */
470
471
        customEvent.relatedTarget = relatedTarget;
471
 
        
 
472
 
472
473
        //fire the event
473
474
        target.fireEvent("on" + type, customEvent);
474
 
                
 
475
 
475
476
    } else {
476
477
        Y.error("simulateMouseEvent(): No event simulation framework present.");
477
478
    }
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
500
501
 *      is false.
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.
505
506
 */
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*/
509
510
{
510
 
    
511
 
    //check target   
 
511
 
 
512
    //check target
512
513
    if (!target){
513
514
        Y.error("simulateUIEvent(): Invalid target.");
514
515
    }
515
 
    
 
516
 
516
517
    //check event type
517
518
    if (isString(type)){
518
519
        type = type.toLowerCase();
519
 
        
 
520
 
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.");
524
525
    } else {
525
526
        Y.error("simulateUIEvent(): Event type must be a string.");
526
527
    }
527
 
    
 
528
 
528
529
    //try to create a mouse event
529
 
    var customEvent = null;    
530
 
    
531
 
    
 
530
    var customEvent = null;
 
531
 
 
532
 
532
533
    //setup default values
533
534
    if (!isBoolean(bubbles)){
534
535
        bubbles = (type in bubbleEvents);  //not all events bubble
542
543
    if (!isNumber(detail)){
543
544
        detail = 1;  //usually not used but defaulted to this
544
545
    }
545
 
        
 
546
 
546
547
    //check for DOM-compliant browsers first
547
548
    if (isFunction(doc.createEvent)){
548
 
    
 
549
 
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);
552
 
        
 
553
 
553
554
        //fire the event
554
555
        target.dispatchEvent(customEvent);
555
556
 
556
557
    } else if (isObject(doc.createEventObject)){ //IE
557
 
    
 
558
 
558
559
        //create an IE event object
559
560
        customEvent = doc.createEventObject();
560
 
        
 
561
 
561
562
        //assign available properties
562
563
        customEvent.bubbles = bubbles;
563
564
        customEvent.cancelable = cancelable;
566
567
 
567
568
        //fire the event
568
569
        target.fireEvent("on" + type, customEvent);
569
 
                
 
570
 
570
571
    } else {
571
572
        Y.error("simulateUIEvent(): No event simulation framework present.");
572
573
    }
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.
580
581
 * @return {void}
 
582
 * @for Event
581
583
 * @method simulate
582
584
 * @static
583
585
 */
584
586
Y.Event.simulate = function(target, type, options){
585
587
 
586
588
    options = options || {};
587
 
    
 
589
 
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);
602
604
     } else {
603
605
        Y.error("simulate(): Event '" + type + "' can't be simulated.");
604
606
    }
609
611
 
610
612
 
611
613
 
612
 
}, '3.2.0' ,{requires:['event-base']});
 
614
}, '3.3.0' ,{requires:['event-base']});