[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 YUI.add('yui2-event-simulate', function(Y) { 2 var YAHOO = Y.YUI2; 3 /* 4 Copyright (c) 2011, Yahoo! Inc. All rights reserved. 5 Code licensed under the BSD License: 6 http://developer.yahoo.com/yui/license.html 7 version: 2.9.0 8 */ 9 10 /** 11 * DOM event simulation utility 12 * @module event-simulate 13 * @namespace YAHOO.util 14 * @requires yahoo,dom,event 15 */ 16 17 /** 18 * The UserAction object provides functions that simulate events occurring in 19 * the browser. Since these are simulated events, they do not behave exactly 20 * as regular, user-initiated events do, but can be used to test simple 21 * user interactions safely. 22 * 23 * @namespace YAHOO.util 24 * @class UserAction 25 * @static 26 */ 27 YAHOO.util.UserAction = { 28 29 //-------------------------------------------------------------------------- 30 // Generic event methods 31 //-------------------------------------------------------------------------- 32 33 /** 34 * Simulates a key event using the given event information to populate 35 * the generated event object. This method does browser-equalizing 36 * calculations to account for differences in the DOM and IE event models 37 * as well as different browser quirks. Note: keydown causes Safari 2.x to 38 * crash. 39 * @method simulateKeyEvent 40 * @private 41 * @static 42 * @param {HTMLElement} target The target of the given event. 43 * @param {String} type The type of event to fire. This can be any one of 44 * the following: keyup, keydown, and keypress. 45 * @param {Boolean} bubbles (Optional) Indicates if the event can be 46 * bubbled up. DOM Level 3 specifies that all key events bubble by 47 * default. The default is true. 48 * @param {Boolean} cancelable (Optional) Indicates if the event can be 49 * canceled using preventDefault(). DOM Level 3 specifies that all 50 * key events can be cancelled. The default 51 * is true. 52 * @param {Window} view (Optional) The view containing the target. This is 53 * typically the window object. The default is window. 54 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys 55 * is pressed while the event is firing. The default is false. 56 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys 57 * is pressed while the event is firing. The default is false. 58 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys 59 * is pressed while the event is firing. The default is false. 60 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys 61 * is pressed while the event is firing. The default is false. 62 * @param {int} keyCode (Optional) The code for the key that is in use. 63 * The default is 0. 64 * @param {int} charCode (Optional) The Unicode code for the character 65 * associated with the key being used. The default is 0. 66 */ 67 simulateKeyEvent : function (target /*:HTMLElement*/, type /*:String*/, 68 bubbles /*:Boolean*/, cancelable /*:Boolean*/, 69 view /*:Window*/, 70 ctrlKey /*:Boolean*/, altKey /*:Boolean*/, 71 shiftKey /*:Boolean*/, metaKey /*:Boolean*/, 72 keyCode /*:int*/, charCode /*:int*/) /*:Void*/ 73 { 74 //check target 75 target = YAHOO.util.Dom.get(target); 76 if (!target){ 77 throw new Error("simulateKeyEvent(): Invalid target."); 78 } 79 80 //check event type 81 if (YAHOO.lang.isString(type)){ 82 type = type.toLowerCase(); 83 switch(type){ 84 case "keyup": 85 case "keydown": 86 case "keypress": 87 break; 88 case "textevent": //DOM Level 3 89 type = "keypress"; 90 break; 91 // @TODO was the fallthrough intentional, if so throw error 92 default: 93 throw new Error("simulateKeyEvent(): Event type '" + type + "' not supported."); 94 } 95 } else { 96 throw new Error("simulateKeyEvent(): Event type must be a string."); 97 } 98 99 //setup default values 100 if (!YAHOO.lang.isBoolean(bubbles)){ 101 bubbles = true; //all key events bubble 102 } 103 if (!YAHOO.lang.isBoolean(cancelable)){ 104 cancelable = true; //all key events can be cancelled 105 } 106 if (!YAHOO.lang.isObject(view)){ 107 view = window; //view is typically window 108 } 109 if (!YAHOO.lang.isBoolean(ctrlKey)){ 110 ctrlKey = false; 111 } 112 if (!YAHOO.lang.isBoolean(altKey)){ 113 altKey = false; 114 } 115 if (!YAHOO.lang.isBoolean(shiftKey)){ 116 shiftKey = false; 117 } 118 if (!YAHOO.lang.isBoolean(metaKey)){ 119 metaKey = false; 120 } 121 if (!YAHOO.lang.isNumber(keyCode)){ 122 keyCode = 0; 123 } 124 if (!YAHOO.lang.isNumber(charCode)){ 125 charCode = 0; 126 } 127 128 //try to create a mouse event 129 var customEvent /*:MouseEvent*/ = null; 130 131 //check for DOM-compliant browsers first 132 if (YAHOO.lang.isFunction(document.createEvent)){ 133 134 try { 135 136 //try to create key event 137 customEvent = document.createEvent("KeyEvents"); 138 139 /* 140 * Interesting problem: Firefox implemented a non-standard 141 * version of initKeyEvent() based on DOM Level 2 specs. 142 * Key event was removed from DOM Level 2 and re-introduced 143 * in DOM Level 3 with a different interface. Firefox is the 144 * only browser with any implementation of Key Events, so for 145 * now, assume it's Firefox if the above line doesn't error. 146 */ 147 //TODO: Decipher between Firefox's implementation and a correct one. 148 customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey, 149 altKey, shiftKey, metaKey, keyCode, charCode); 150 151 } catch (ex /*:Error*/){ 152 153 /* 154 * If it got here, that means key events aren't officially supported. 155 * Safari/WebKit is a real problem now. WebKit 522 won't let you 156 * set keyCode, charCode, or other properties if you use a 157 * UIEvent, so we first must try to create a generic event. The 158 * fun part is that this will throw an error on Safari 2.x. The 159 * end result is that we need another try...catch statement just to 160 * deal with this mess. 161 */ 162 try { 163 164 //try to create generic event - will fail in Safari 2.x 165 customEvent = document.createEvent("Events"); 166 167 } catch (uierror /*:Error*/){ 168 169 //the above failed, so create a UIEvent for Safari 2.x 170 customEvent = document.createEvent("UIEvents"); 171 172 } finally { 173 174 customEvent.initEvent(type, bubbles, cancelable); 175 176 //initialize 177 customEvent.view = view; 178 customEvent.altKey = altKey; 179 customEvent.ctrlKey = ctrlKey; 180 customEvent.shiftKey = shiftKey; 181 customEvent.metaKey = metaKey; 182 customEvent.keyCode = keyCode; 183 customEvent.charCode = charCode; 184 185 } 186 187 } 188 189 //fire the event 190 target.dispatchEvent(customEvent); 191 192 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE 193 194 //create an IE event object 195 customEvent = document.createEventObject(); 196 197 //assign available properties 198 customEvent.bubbles = bubbles; 199 customEvent.cancelable = cancelable; 200 customEvent.view = view; 201 customEvent.ctrlKey = ctrlKey; 202 customEvent.altKey = altKey; 203 customEvent.shiftKey = shiftKey; 204 customEvent.metaKey = metaKey; 205 206 /* 207 * IE doesn't support charCode explicitly. CharCode should 208 * take precedence over any keyCode value for accurate 209 * representation. 210 */ 211 customEvent.keyCode = (charCode > 0) ? charCode : keyCode; 212 213 //fire the event 214 target.fireEvent("on" + type, customEvent); 215 216 } else { 217 throw new Error("simulateKeyEvent(): No event simulation framework present."); 218 } 219 }, 220 221 /** 222 * Simulates a mouse event using the given event information to populate 223 * the generated event object. This method does browser-equalizing 224 * calculations to account for differences in the DOM and IE event models 225 * as well as different browser quirks. 226 * @method simulateMouseEvent 227 * @private 228 * @static 229 * @param {HTMLElement} target The target of the given event. 230 * @param {String} type The type of event to fire. This can be any one of 231 * the following: click, dblclick, mousedown, mouseup, mouseout, 232 * mouseover, and mousemove. 233 * @param {Boolean} bubbles (Optional) Indicates if the event can be 234 * bubbled up. DOM Level 2 specifies that all mouse events bubble by 235 * default. The default is true. 236 * @param {Boolean} cancelable (Optional) Indicates if the event can be 237 * canceled using preventDefault(). DOM Level 2 specifies that all 238 * mouse events except mousemove can be cancelled. The default 239 * is true for all events except mousemove, for which the default 240 * is false. 241 * @param {Window} view (Optional) The view containing the target. This is 242 * typically the window object. The default is window. 243 * @param {int} detail (Optional) The number of times the mouse button has 244 * been used. The default value is 1. 245 * @param {int} screenX (Optional) The x-coordinate on the screen at which 246 * point the event occured. The default is 0. 247 * @param {int} screenY (Optional) The y-coordinate on the screen at which 248 * point the event occured. The default is 0. 249 * @param {int} clientX (Optional) The x-coordinate on the client at which 250 * point the event occured. The default is 0. 251 * @param {int} clientY (Optional) The y-coordinate on the client at which 252 * point the event occured. The default is 0. 253 * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys 254 * is pressed while the event is firing. The default is false. 255 * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys 256 * is pressed while the event is firing. The default is false. 257 * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys 258 * is pressed while the event is firing. The default is false. 259 * @param {Boolean} metaKey (Optional) Indicates if one of the META keys 260 * is pressed while the event is firing. The default is false. 261 * @param {int} button (Optional) The button being pressed while the event 262 * is executing. The value should be 0 for the primary mouse button 263 * (typically the left button), 1 for the terciary mouse button 264 * (typically the middle button), and 2 for the secondary mouse button 265 * (typically the right button). The default is 0. 266 * @param {HTMLElement} relatedTarget (Optional) For mouseout events, 267 * this is the element that the mouse has moved to. For mouseover 268 * events, this is the element that the mouse has moved from. This 269 * argument is ignored for all other events. The default is null. 270 */ 271 simulateMouseEvent : function (target /*:HTMLElement*/, type /*:String*/, 272 bubbles /*:Boolean*/, cancelable /*:Boolean*/, 273 view /*:Window*/, detail /*:int*/, 274 screenX /*:int*/, screenY /*:int*/, 275 clientX /*:int*/, clientY /*:int*/, 276 ctrlKey /*:Boolean*/, altKey /*:Boolean*/, 277 shiftKey /*:Boolean*/, metaKey /*:Boolean*/, 278 button /*:int*/, relatedTarget /*:HTMLElement*/) /*:Void*/ 279 { 280 281 //check target 282 target = YAHOO.util.Dom.get(target); 283 if (!target){ 284 throw new Error("simulateMouseEvent(): Invalid target."); 285 } 286 287 relatedTarget = relatedTarget || null; 288 289 //check event type 290 if (YAHOO.lang.isString(type)){ 291 type = type.toLowerCase(); 292 switch(type){ 293 case "mouseover": 294 case "mouseout": 295 case "mousedown": 296 case "mouseup": 297 case "click": 298 case "dblclick": 299 case "mousemove": 300 break; 301 default: 302 throw new Error("simulateMouseEvent(): Event type '" + type + "' not supported."); 303 } 304 } else { 305 throw new Error("simulateMouseEvent(): Event type must be a string."); 306 } 307 308 //setup default values 309 if (!YAHOO.lang.isBoolean(bubbles)){ 310 bubbles = true; //all mouse events bubble 311 } 312 if (!YAHOO.lang.isBoolean(cancelable)){ 313 cancelable = (type != "mousemove"); //mousemove is the only one that can't be cancelled 314 } 315 if (!YAHOO.lang.isObject(view)){ 316 view = window; //view is typically window 317 } 318 if (!YAHOO.lang.isNumber(detail)){ 319 detail = 1; //number of mouse clicks must be at least one 320 } 321 if (!YAHOO.lang.isNumber(screenX)){ 322 screenX = 0; 323 } 324 if (!YAHOO.lang.isNumber(screenY)){ 325 screenY = 0; 326 } 327 if (!YAHOO.lang.isNumber(clientX)){ 328 clientX = 0; 329 } 330 if (!YAHOO.lang.isNumber(clientY)){ 331 clientY = 0; 332 } 333 if (!YAHOO.lang.isBoolean(ctrlKey)){ 334 ctrlKey = false; 335 } 336 if (!YAHOO.lang.isBoolean(altKey)){ 337 altKey = false; 338 } 339 if (!YAHOO.lang.isBoolean(shiftKey)){ 340 shiftKey = false; 341 } 342 if (!YAHOO.lang.isBoolean(metaKey)){ 343 metaKey = false; 344 } 345 if (!YAHOO.lang.isNumber(button)){ 346 button = 0; 347 } 348 349 //try to create a mouse event 350 var customEvent /*:MouseEvent*/ = null; 351 352 //check for DOM-compliant browsers first 353 if (YAHOO.lang.isFunction(document.createEvent)){ 354 355 customEvent = document.createEvent("MouseEvents"); 356 357 //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent() 358 if (customEvent.initMouseEvent){ 359 customEvent.initMouseEvent(type, bubbles, cancelable, view, detail, 360 screenX, screenY, clientX, clientY, 361 ctrlKey, altKey, shiftKey, metaKey, 362 button, relatedTarget); 363 } else { //Safari 364 365 //the closest thing available in Safari 2.x is UIEvents 366 customEvent = document.createEvent("UIEvents"); 367 customEvent.initEvent(type, bubbles, cancelable); 368 customEvent.view = view; 369 customEvent.detail = detail; 370 customEvent.screenX = screenX; 371 customEvent.screenY = screenY; 372 customEvent.clientX = clientX; 373 customEvent.clientY = clientY; 374 customEvent.ctrlKey = ctrlKey; 375 customEvent.altKey = altKey; 376 customEvent.metaKey = metaKey; 377 customEvent.shiftKey = shiftKey; 378 customEvent.button = button; 379 customEvent.relatedTarget = relatedTarget; 380 } 381 382 /* 383 * Check to see if relatedTarget has been assigned. Firefox 384 * versions less than 2.0 don't allow it to be assigned via 385 * initMouseEvent() and the property is readonly after event 386 * creation, so in order to keep YAHOO.util.getRelatedTarget() 387 * working, assign to the IE proprietary toElement property 388 * for mouseout event and fromElement property for mouseover 389 * event. 390 */ 391 if (relatedTarget && !customEvent.relatedTarget){ 392 if (type == "mouseout"){ 393 customEvent.toElement = relatedTarget; 394 } else if (type == "mouseover"){ 395 customEvent.fromElement = relatedTarget; 396 } 397 } 398 399 //fire the event 400 target.dispatchEvent(customEvent); 401 402 } else if (YAHOO.lang.isObject(document.createEventObject)){ //IE 403 404 //create an IE event object 405 customEvent = document.createEventObject(); 406 407 //assign available properties 408 customEvent.bubbles = bubbles; 409 customEvent.cancelable = cancelable; 410 customEvent.view = view; 411 customEvent.detail = detail; 412 customEvent.screenX = screenX; 413 customEvent.screenY = screenY; 414 customEvent.clientX = clientX; 415 customEvent.clientY = clientY; 416 customEvent.ctrlKey = ctrlKey; 417 customEvent.altKey = altKey; 418 customEvent.metaKey = metaKey; 419 customEvent.shiftKey = shiftKey; 420 421 //fix button property for IE's wacky implementation 422 switch(button){ 423 case 0: 424 customEvent.button = 1; 425 break; 426 case 1: 427 customEvent.button = 4; 428 break; 429 case 2: 430 //leave as is 431 break; 432 default: 433 customEvent.button = 0; 434 } 435 436 /* 437 * Have to use relatedTarget because IE won't allow assignment 438 * to toElement or fromElement on generic events. This keeps 439 * YAHOO.util.customEvent.getRelatedTarget() functional. 440 */ 441 customEvent.relatedTarget = relatedTarget; 442 443 //fire the event 444 target.fireEvent("on" + type, customEvent); 445 446 } else { 447 throw new Error("simulateMouseEvent(): No event simulation framework present."); 448 } 449 }, 450 451 //-------------------------------------------------------------------------- 452 // Mouse events 453 //-------------------------------------------------------------------------- 454 455 /** 456 * Simulates a mouse event on a particular element. 457 * @param {HTMLElement} target The element to click on. 458 * @param {String} type The type of event to fire. This can be any one of 459 * the following: click, dblclick, mousedown, mouseup, mouseout, 460 * mouseover, and mousemove. 461 * @param {Object} options Additional event options (use DOM standard names). 462 * @method mouseEvent 463 * @static 464 */ 465 fireMouseEvent : function (target /*:HTMLElement*/, type /*:String*/, 466 options /*:Object*/) /*:Void*/ 467 { 468 options = options || {}; 469 this.simulateMouseEvent(target, type, options.bubbles, 470 options.cancelable, options.view, options.detail, options.screenX, 471 options.screenY, options.clientX, options.clientY, options.ctrlKey, 472 options.altKey, options.shiftKey, options.metaKey, options.button, 473 options.relatedTarget); 474 }, 475 476 /** 477 * Simulates a click on a particular element. 478 * @param {HTMLElement} target The element to click on. 479 * @param {Object} options Additional event options (use DOM standard names). 480 * @method click 481 * @static 482 */ 483 click : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ { 484 this.fireMouseEvent(target, "click", options); 485 }, 486 487 /** 488 * Simulates a double click on a particular element. 489 * @param {HTMLElement} target The element to double click on. 490 * @param {Object} options Additional event options (use DOM standard names). 491 * @method dblclick 492 * @static 493 */ 494 dblclick : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ { 495 this.fireMouseEvent( target, "dblclick", options); 496 }, 497 498 /** 499 * Simulates a mousedown on a particular element. 500 * @param {HTMLElement} target The element to act on. 501 * @param {Object} options Additional event options (use DOM standard names). 502 * @method mousedown 503 * @static 504 */ 505 mousedown : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 506 this.fireMouseEvent(target, "mousedown", options); 507 }, 508 509 /** 510 * Simulates a mousemove on a particular element. 511 * @param {HTMLElement} target The element to act on. 512 * @param {Object} options Additional event options (use DOM standard names). 513 * @method mousemove 514 * @static 515 */ 516 mousemove : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 517 this.fireMouseEvent(target, "mousemove", options); 518 }, 519 520 /** 521 * Simulates a mouseout event on a particular element. Use "relatedTarget" 522 * on the options object to specify where the mouse moved to. 523 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so 524 * toElement is assigned in its place. IE doesn't allow toElement to be 525 * be assigned, so relatedTarget is assigned in its place. Both of these 526 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly 527 * in both browsers. 528 * @param {HTMLElement} target The element to act on. 529 * @param {Object} options Additional event options (use DOM standard names). 530 * @method mouseout 531 * @static 532 */ 533 mouseout : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 534 this.fireMouseEvent(target, "mouseout", options); 535 }, 536 537 /** 538 * Simulates a mouseover event on a particular element. Use "relatedTarget" 539 * on the options object to specify where the mouse moved from. 540 * Quirks: Firefox less than 2.0 doesn't set relatedTarget properly, so 541 * fromElement is assigned in its place. IE doesn't allow fromElement to be 542 * be assigned, so relatedTarget is assigned in its place. Both of these 543 * concessions allow YAHOO.util.Event.getRelatedTarget() to work correctly 544 * in both browsers. 545 * @param {HTMLElement} target The element to act on. 546 * @param {Object} options Additional event options (use DOM standard names). 547 * @method mouseover 548 * @static 549 */ 550 mouseover : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 551 this.fireMouseEvent(target, "mouseover", options); 552 }, 553 554 /** 555 * Simulates a mouseup on a particular element. 556 * @param {HTMLElement} target The element to act on. 557 * @param {Object} options Additional event options (use DOM standard names). 558 * @method mouseup 559 * @static 560 */ 561 mouseup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 562 this.fireMouseEvent(target, "mouseup", options); 563 }, 564 565 //-------------------------------------------------------------------------- 566 // Key events 567 //-------------------------------------------------------------------------- 568 569 /** 570 * Fires an event that normally would be fired by the keyboard (keyup, 571 * keydown, keypress). Make sure to specify either keyCode or charCode as 572 * an option. 573 * @private 574 * @param {String} type The type of event ("keyup", "keydown" or "keypress"). 575 * @param {HTMLElement} target The target of the event. 576 * @param {Object} options Options for the event. Either keyCode or charCode 577 * are required. 578 * @method fireKeyEvent 579 * @static 580 */ 581 fireKeyEvent : function (type /*:String*/, target /*:HTMLElement*/, 582 options /*:Object*/) /*:Void*/ 583 { 584 options = options || {}; 585 this.simulateKeyEvent(target, type, options.bubbles, 586 options.cancelable, options.view, options.ctrlKey, 587 options.altKey, options.shiftKey, options.metaKey, 588 options.keyCode, options.charCode); 589 }, 590 591 /** 592 * Simulates a keydown event on a particular element. 593 * @param {HTMLElement} target The element to act on. 594 * @param {Object} options Additional event options (use DOM standard names). 595 * @method keydown 596 * @static 597 */ 598 keydown : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ { 599 this.fireKeyEvent("keydown", target, options); 600 }, 601 602 /** 603 * Simulates a keypress on a particular element. 604 * @param {HTMLElement} target The element to act on. 605 * @param {Object} options Additional event options (use DOM standard names). 606 * @method keypress 607 * @static 608 */ 609 keypress : function (target /*:HTMLElement*/, options /*:Object*/) /*:Void*/ { 610 this.fireKeyEvent("keypress", target, options); 611 }, 612 613 /** 614 * Simulates a keyup event on a particular element. 615 * @param {HTMLElement} target The element to act on. 616 * @param {Object} options Additional event options (use DOM standard names). 617 * @method keyup 618 * @static 619 */ 620 keyup : function (target /*:HTMLElement*/, options /*Object*/) /*:Void*/ { 621 this.fireKeyEvent("keyup", target, options); 622 } 623 624 625 }; 626 YAHOO.register("event-simulate", YAHOO.util.UserAction, {version: "2.9.0", build: "2800"}); 627 628 }, '2.9.0' ,{"requires": ["yui2-yahoo", "yui2-event"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |