[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/event-simulate/ -> event-simulate.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('event-simulate', function (Y, NAME) {
   9  
  10  (function() {
  11  /**
  12   * Simulate user interaction by generating native DOM events.
  13   *
  14   * @module event-simulate
  15   * @requires event
  16   */
  17  
  18  //shortcuts
  19  var L   = Y.Lang,
  20      win = Y.config.win,
  21      isFunction  = L.isFunction,
  22      isString    = L.isString,
  23      isBoolean   = L.isBoolean,
  24      isObject    = L.isObject,
  25      isNumber    = L.isNumber,
  26  
  27      //mouse events supported
  28      mouseEvents = {
  29          click:      1,
  30          dblclick:   1,
  31          mouseover:  1,
  32          mouseout:   1,
  33          mousedown:  1,
  34          mouseup:    1,
  35          mousemove:  1,
  36          contextmenu:1
  37      },
  38  
  39      pointerEvents = (win && win.PointerEvent) ? {
  40          pointerover:  1,
  41          pointerout:   1,
  42          pointerdown:  1,
  43          pointerup:    1,
  44          pointermove:  1
  45      } : {
  46          MSPointerOver:  1,
  47          MSPointerOut:   1,
  48          MSPointerDown:  1,
  49          MSPointerUp:    1,
  50          MSPointerMove:  1
  51      },
  52  
  53      //key events supported
  54      keyEvents   = {
  55          keydown:    1,
  56          keyup:      1,
  57          keypress:   1
  58      },
  59  
  60      //HTML events supported
  61      uiEvents  = {
  62          submit:     1,
  63          blur:       1,
  64          change:     1,
  65          focus:      1,
  66          resize:     1,
  67          scroll:     1,
  68          select:     1
  69      },
  70  
  71      //events that bubble by default
  72      bubbleEvents = {
  73          scroll:     1,
  74          resize:     1,
  75          reset:      1,
  76          submit:     1,
  77          change:     1,
  78          select:     1,
  79          error:      1,
  80          abort:      1
  81      },
  82  
  83      //touch events supported
  84      touchEvents = {
  85          touchstart: 1,
  86          touchmove: 1,
  87          touchend: 1,
  88          touchcancel: 1
  89      },
  90  
  91      gestureEvents = {
  92          gesturestart: 1,
  93          gesturechange: 1,
  94          gestureend: 1
  95      };
  96  
  97  //all key, mouse and touch events bubble
  98  Y.mix(bubbleEvents, mouseEvents);
  99  Y.mix(bubbleEvents, keyEvents);
 100  Y.mix(bubbleEvents, touchEvents);
 101  
 102  /*
 103   * Note: Intentionally not for YUIDoc generation.
 104   * Simulates a key event using the given event information to populate
 105   * the generated event object. This method does browser-equalizing
 106   * calculations to account for differences in the DOM and IE event models
 107   * as well as different browser quirks. Note: keydown causes Safari 2.x to
 108   * crash.
 109   * @method simulateKeyEvent
 110   * @private
 111   * @static
 112   * @param {HTMLElement} target The target of the given event.
 113   * @param {String} type The type of event to fire. This can be any one of
 114   *      the following: keyup, keydown, and keypress.
 115   * @param {Boolean} [bubbles=true] Indicates if the event can be
 116   *      bubbled up. DOM Level 3 specifies that all key events bubble by
 117   *      default.
 118   * @param {Boolean} [cancelable=true] Indicates if the event can be
 119   *      canceled using preventDefault(). DOM Level 3 specifies that all
 120   *      key events can be cancelled.
 121   * @param {Window} [view=window] The view containing the target. This is
 122   *      typically the window object.
 123   * @param {Boolean} [ctrlKey=false] Indicates if one of the CTRL keys
 124   *      is pressed while the event is firing.
 125   * @param {Boolean} [altKey=false] Indicates if one of the ALT keys
 126   *      is pressed while the event is firing.
 127   * @param {Boolean} [shiftKey=false] Indicates if one of the SHIFT keys
 128   *      is pressed while the event is firing.
 129   * @param {Boolean} [metaKey=false] Indicates if one of the META keys
 130   *      is pressed while the event is firing.
 131   * @param {Number} [keyCode=0] The code for the key that is in use.
 132   * @param {Number} [charCode=0] The Unicode code for the character
 133   *      associated with the key being used.
 134   */
 135  function simulateKeyEvent(target /*:HTMLElement*/, type /*:String*/,
 136                               bubbles /*:Boolean*/,  cancelable /*:Boolean*/,
 137                               view /*:Window*/,
 138                               ctrlKey /*:Boolean*/,    altKey /*:Boolean*/,
 139                               shiftKey /*:Boolean*/,   metaKey /*:Boolean*/,
 140                               keyCode /*:int*/,        charCode /*:int*/) /*:Void*/
 141  {
 142      //check target
 143      if (!target){
 144          Y.error("simulateKeyEvent(): Invalid target.");
 145      }
 146  
 147      //check event type
 148      if (isString(type)){
 149          type = type.toLowerCase();
 150          switch(type){
 151              case "textevent": //DOM Level 3
 152                  type = "keypress";
 153                  break;
 154              case "keyup":
 155              case "keydown":
 156              case "keypress":
 157                  break;
 158              default:
 159                  Y.error("simulateKeyEvent(): Event type '" + type + "' not supported.");
 160          }
 161      } else {
 162          Y.error("simulateKeyEvent(): Event type must be a string.");
 163      }
 164  
 165      //setup default values
 166      if (!isBoolean(bubbles)){
 167          bubbles = true; //all key events bubble
 168      }
 169      if (!isBoolean(cancelable)){
 170          cancelable = true; //all key events can be cancelled
 171      }
 172      if (!isObject(view)){
 173          view = Y.config.win; //view is typically window
 174      }
 175      if (!isBoolean(ctrlKey)){
 176          ctrlKey = false;
 177      }
 178      if (!isBoolean(altKey)){
 179          altKey = false;
 180      }
 181      if (!isBoolean(shiftKey)){
 182          shiftKey = false;
 183      }
 184      if (!isBoolean(metaKey)){
 185          metaKey = false;
 186      }
 187      if (!isNumber(keyCode)){
 188          keyCode = 0;
 189      }
 190      if (!isNumber(charCode)){
 191          charCode = 0;
 192      }
 193  
 194      //try to create a mouse event
 195      var customEvent /*:MouseEvent*/ = null;
 196  
 197      //check for DOM-compliant browsers first
 198      if (isFunction(Y.config.doc.createEvent)){
 199  
 200          try {
 201  
 202              //try to create key event
 203              customEvent = Y.config.doc.createEvent("KeyEvents");
 204  
 205              /*
 206               * Interesting problem: Firefox implemented a non-standard
 207               * version of initKeyEvent() based on DOM Level 2 specs.
 208               * Key event was removed from DOM Level 2 and re-introduced
 209               * in DOM Level 3 with a different interface. Firefox is the
 210               * only browser with any implementation of Key Events, so for
 211               * now, assume it's Firefox if the above line doesn't error.
 212               */
 213              // @TODO: Decipher between Firefox's implementation and a correct one.
 214              customEvent.initKeyEvent(type, bubbles, cancelable, view, ctrlKey,
 215                  altKey, shiftKey, metaKey, keyCode, charCode);
 216  
 217          } catch (ex /*:Error*/){
 218  
 219              /*
 220               * If it got here, that means key events aren't officially supported.
 221               * Safari/WebKit is a real problem now. WebKit 522 won't let you
 222               * set keyCode, charCode, or other properties if you use a
 223               * UIEvent, so we first must try to create a generic event. The
 224               * fun part is that this will throw an error on Safari 2.x. The
 225               * end result is that we need another try...catch statement just to
 226               * deal with this mess.
 227               */
 228              try {
 229  
 230                  //try to create generic event - will fail in Safari 2.x
 231                  customEvent = Y.config.doc.createEvent("Events");
 232  
 233              } catch (uierror /*:Error*/){
 234  
 235                  //the above failed, so create a UIEvent for Safari 2.x
 236                  customEvent = Y.config.doc.createEvent("UIEvents");
 237  
 238              } finally {
 239  
 240                  customEvent.initEvent(type, bubbles, cancelable);
 241  
 242                  //initialize
 243                  customEvent.view = view;
 244                  customEvent.altKey = altKey;
 245                  customEvent.ctrlKey = ctrlKey;
 246                  customEvent.shiftKey = shiftKey;
 247                  customEvent.metaKey = metaKey;
 248                  customEvent.keyCode = keyCode;
 249                  customEvent.charCode = charCode;
 250  
 251              }
 252  
 253          }
 254  
 255          //fire the event
 256          target.dispatchEvent(customEvent);
 257  
 258      } else if (isObject(Y.config.doc.createEventObject)){ //IE
 259  
 260          //create an IE event object
 261          customEvent = Y.config.doc.createEventObject();
 262  
 263          //assign available properties
 264          customEvent.bubbles = bubbles;
 265          customEvent.cancelable = cancelable;
 266          customEvent.view = view;
 267          customEvent.ctrlKey = ctrlKey;
 268          customEvent.altKey = altKey;
 269          customEvent.shiftKey = shiftKey;
 270          customEvent.metaKey = metaKey;
 271  
 272          /*
 273           * IE doesn't support charCode explicitly. CharCode should
 274           * take precedence over any keyCode value for accurate
 275           * representation.
 276           */
 277          customEvent.keyCode = (charCode > 0) ? charCode : keyCode;
 278  
 279          //fire the event
 280          target.fireEvent("on" + type, customEvent);
 281  
 282      } else {
 283          Y.error("simulateKeyEvent(): No event simulation framework present.");
 284      }
 285  }
 286  
 287  /*
 288   * Note: Intentionally not for YUIDoc generation.
 289   * Simulates a mouse event using the given event information to populate
 290   * the generated event object. This method does browser-equalizing
 291   * calculations to account for differences in the DOM and IE event models
 292   * as well as different browser quirks.
 293   * @method simulateMouseEvent
 294   * @private
 295   * @static
 296   * @param {HTMLElement} target The target of the given event.
 297   * @param {String} type The type of event to fire. This can be any one of
 298   *      the following: click, dblclick, mousedown, mouseup, mouseout,
 299   *      mouseover, and mousemove.
 300   * @param {Boolean} bubbles (Optional) Indicates if the event can be
 301   *      bubbled up. DOM Level 2 specifies that all mouse events bubble by
 302   *      default. The default is true.
 303   * @param {Boolean} cancelable (Optional) Indicates if the event can be
 304   *      canceled using preventDefault(). DOM Level 2 specifies that all
 305   *      mouse events except mousemove can be cancelled. The default
 306   *      is true for all events except mousemove, for which the default
 307   *      is false.
 308   * @param {Window} view (Optional) The view containing the target. This is
 309   *      typically the window object. The default is window.
 310   * @param {Number} detail (Optional) The number of times the mouse button has
 311   *      been used. The default value is 1.
 312   * @param {Number} screenX (Optional) The x-coordinate on the screen at which
 313   *      point the event occured. The default is 0.
 314   * @param {Number} screenY (Optional) The y-coordinate on the screen at which
 315   *      point the event occured. The default is 0.
 316   * @param {Number} clientX (Optional) The x-coordinate on the client at which
 317   *      point the event occured. The default is 0.
 318   * @param {Number} clientY (Optional) The y-coordinate on the client at which
 319   *      point the event occured. The default is 0.
 320   * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
 321   *      is pressed while the event is firing. The default is false.
 322   * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
 323   *      is pressed while the event is firing. The default is false.
 324   * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
 325   *      is pressed while the event is firing. The default is false.
 326   * @param {Boolean} metaKey (Optional) Indicates if one of the META keys
 327   *      is pressed while the event is firing. The default is false.
 328   * @param {Number} button (Optional) The button being pressed while the event
 329   *      is executing. The value should be 0 for the primary mouse button
 330   *      (typically the left button), 1 for the terciary mouse button
 331   *      (typically the middle button), and 2 for the secondary mouse button
 332   *      (typically the right button). The default is 0.
 333   * @param {HTMLElement} relatedTarget (Optional) For mouseout events,
 334   *      this is the element that the mouse has moved to. For mouseover
 335   *      events, this is the element that the mouse has moved from. This
 336   *      argument is ignored for all other events. The default is null.
 337   */
 338  function simulateMouseEvent(target /*:HTMLElement*/, type /*:String*/,
 339                                 bubbles /*:Boolean*/,  cancelable /*:Boolean*/,
 340                                 view /*:Window*/,        detail /*:int*/,
 341                                 screenX /*:int*/,        screenY /*:int*/,
 342                                 clientX /*:int*/,        clientY /*:int*/,
 343                                 ctrlKey /*:Boolean*/,    altKey /*:Boolean*/,
 344                                 shiftKey /*:Boolean*/,   metaKey /*:Boolean*/,
 345                                 button /*:int*/,         relatedTarget /*:HTMLElement*/) /*:Void*/
 346  {
 347      //check target
 348      if (!target){
 349          Y.error("simulateMouseEvent(): Invalid target.");
 350      }
 351  
 352  
 353      if (isString(type)){
 354  
 355          //make sure it's a supported mouse event or an msPointerEvent.
 356          if (!mouseEvents[type.toLowerCase()] && !pointerEvents[type]){
 357              Y.error("simulateMouseEvent(): Event type '" + type + "' not supported.");
 358          }
 359      }
 360      else {
 361          Y.error("simulateMouseEvent(): Event type must be a string.");
 362      }
 363  
 364      //setup default values
 365      if (!isBoolean(bubbles)){
 366          bubbles = true; //all mouse events bubble
 367      }
 368      if (!isBoolean(cancelable)){
 369          cancelable = (type !== "mousemove"); //mousemove is the only one that can't be cancelled
 370      }
 371      if (!isObject(view)){
 372          view = Y.config.win; //view is typically window
 373      }
 374      if (!isNumber(detail)){
 375          detail = 1;  //number of mouse clicks must be at least one
 376      }
 377      if (!isNumber(screenX)){
 378          screenX = 0;
 379      }
 380      if (!isNumber(screenY)){
 381          screenY = 0;
 382      }
 383      if (!isNumber(clientX)){
 384          clientX = 0;
 385      }
 386      if (!isNumber(clientY)){
 387          clientY = 0;
 388      }
 389      if (!isBoolean(ctrlKey)){
 390          ctrlKey = false;
 391      }
 392      if (!isBoolean(altKey)){
 393          altKey = false;
 394      }
 395      if (!isBoolean(shiftKey)){
 396          shiftKey = false;
 397      }
 398      if (!isBoolean(metaKey)){
 399          metaKey = false;
 400      }
 401      if (!isNumber(button)){
 402          button = 0;
 403      }
 404  
 405      relatedTarget = relatedTarget || null;
 406  
 407      //try to create a mouse event
 408      var customEvent /*:MouseEvent*/ = null;
 409  
 410      //check for DOM-compliant browsers first
 411      if (isFunction(Y.config.doc.createEvent)){
 412  
 413          customEvent = Y.config.doc.createEvent("MouseEvents");
 414  
 415          //Safari 2.x (WebKit 418) still doesn't implement initMouseEvent()
 416          if (customEvent.initMouseEvent){
 417              customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,
 418                                   screenX, screenY, clientX, clientY,
 419                                   ctrlKey, altKey, shiftKey, metaKey,
 420                                   button, relatedTarget);
 421          } else { //Safari
 422  
 423              //the closest thing available in Safari 2.x is UIEvents
 424              customEvent = Y.config.doc.createEvent("UIEvents");
 425              customEvent.initEvent(type, bubbles, cancelable);
 426              customEvent.view = view;
 427              customEvent.detail = detail;
 428              customEvent.screenX = screenX;
 429              customEvent.screenY = screenY;
 430              customEvent.clientX = clientX;
 431              customEvent.clientY = clientY;
 432              customEvent.ctrlKey = ctrlKey;
 433              customEvent.altKey = altKey;
 434              customEvent.metaKey = metaKey;
 435              customEvent.shiftKey = shiftKey;
 436              customEvent.button = button;
 437              customEvent.relatedTarget = relatedTarget;
 438          }
 439  
 440          /*
 441           * Check to see if relatedTarget has been assigned. Firefox
 442           * versions less than 2.0 don't allow it to be assigned via
 443           * initMouseEvent() and the property is readonly after event
 444           * creation, so in order to keep YAHOO.util.getRelatedTarget()
 445           * working, assign to the IE proprietary toElement property
 446           * for mouseout event and fromElement property for mouseover
 447           * event.
 448           */
 449          if (relatedTarget && !customEvent.relatedTarget){
 450              if (type === "mouseout"){
 451                  customEvent.toElement = relatedTarget;
 452              } else if (type === "mouseover"){
 453                  customEvent.fromElement = relatedTarget;
 454              }
 455          }
 456  
 457          //fire the event
 458          target.dispatchEvent(customEvent);
 459  
 460      } else if (isObject(Y.config.doc.createEventObject)){ //IE
 461  
 462          //create an IE event object
 463          customEvent = Y.config.doc.createEventObject();
 464  
 465          //assign available properties
 466          customEvent.bubbles = bubbles;
 467          customEvent.cancelable = cancelable;
 468          customEvent.view = view;
 469          customEvent.detail = detail;
 470          customEvent.screenX = screenX;
 471          customEvent.screenY = screenY;
 472          customEvent.clientX = clientX;
 473          customEvent.clientY = clientY;
 474          customEvent.ctrlKey = ctrlKey;
 475          customEvent.altKey = altKey;
 476          customEvent.metaKey = metaKey;
 477          customEvent.shiftKey = shiftKey;
 478  
 479          //fix button property for IE's wacky implementation
 480          switch(button){
 481              case 0:
 482                  customEvent.button = 1;
 483                  break;
 484              case 1:
 485                  customEvent.button = 4;
 486                  break;
 487              case 2:
 488                  //leave as is
 489                  break;
 490              default:
 491                  customEvent.button = 0;
 492          }
 493  
 494          /*
 495           * Have to use relatedTarget because IE won't allow assignment
 496           * to toElement or fromElement on generic events. This keeps
 497           * YAHOO.util.customEvent.getRelatedTarget() functional.
 498           */
 499          customEvent.relatedTarget = relatedTarget;
 500  
 501          //fire the event
 502          target.fireEvent("on" + type, customEvent);
 503  
 504      } else {
 505          Y.error("simulateMouseEvent(): No event simulation framework present.");
 506      }
 507  }
 508  
 509  /*
 510   * Note: Intentionally not for YUIDoc generation.
 511   * Simulates a UI event using the given event information to populate
 512   * the generated event object. This method does browser-equalizing
 513   * calculations to account for differences in the DOM and IE event models
 514   * as well as different browser quirks.
 515   * @method simulateHTMLEvent
 516   * @private
 517   * @static
 518   * @param {HTMLElement} target The target of the given event.
 519   * @param {String} type The type of event to fire. This can be any one of
 520   *      the following: click, dblclick, mousedown, mouseup, mouseout,
 521   *      mouseover, and mousemove.
 522   * @param {Boolean} bubbles (Optional) Indicates if the event can be
 523   *      bubbled up. DOM Level 2 specifies that all mouse events bubble by
 524   *      default. The default is true.
 525   * @param {Boolean} cancelable (Optional) Indicates if the event can be
 526   *      canceled using preventDefault(). DOM Level 2 specifies that all
 527   *      mouse events except mousemove can be cancelled. The default
 528   *      is true for all events except mousemove, for which the default
 529   *      is false.
 530   * @param {Window} view (Optional) The view containing the target. This is
 531   *      typically the window object. The default is window.
 532   * @param {Number} detail (Optional) The number of times the mouse button has
 533   *      been used. The default value is 1.
 534   */
 535  function simulateUIEvent(target /*:HTMLElement*/, type /*:String*/,
 536                                 bubbles /*:Boolean*/,  cancelable /*:Boolean*/,
 537                                 view /*:Window*/,        detail /*:int*/) /*:Void*/
 538  {
 539  
 540      //check target
 541      if (!target){
 542          Y.error("simulateUIEvent(): Invalid target.");
 543      }
 544  
 545      //check event type
 546      if (isString(type)){
 547          type = type.toLowerCase();
 548  
 549          //make sure it's a supported mouse event
 550          if (!uiEvents[type]){
 551              Y.error("simulateUIEvent(): Event type '" + type + "' not supported.");
 552          }
 553      } else {
 554          Y.error("simulateUIEvent(): Event type must be a string.");
 555      }
 556  
 557      //try to create a mouse event
 558      var customEvent = null;
 559  
 560  
 561      //setup default values
 562      if (!isBoolean(bubbles)){
 563          bubbles = (type in bubbleEvents);  //not all events bubble
 564      }
 565      if (!isBoolean(cancelable)){
 566          cancelable = (type === "submit"); //submit is the only one that can be cancelled
 567      }
 568      if (!isObject(view)){
 569          view = Y.config.win; //view is typically window
 570      }
 571      if (!isNumber(detail)){
 572          detail = 1;  //usually not used but defaulted to this
 573      }
 574  
 575      //check for DOM-compliant browsers first
 576      if (isFunction(Y.config.doc.createEvent)){
 577  
 578          //just a generic UI Event object is needed
 579          customEvent = Y.config.doc.createEvent("UIEvents");
 580          customEvent.initUIEvent(type, bubbles, cancelable, view, detail);
 581  
 582          //fire the event
 583          target.dispatchEvent(customEvent);
 584  
 585      } else if (isObject(Y.config.doc.createEventObject)){ //IE
 586  
 587          //create an IE event object
 588          customEvent = Y.config.doc.createEventObject();
 589  
 590          //assign available properties
 591          customEvent.bubbles = bubbles;
 592          customEvent.cancelable = cancelable;
 593          customEvent.view = view;
 594          customEvent.detail = detail;
 595  
 596          //fire the event
 597          target.fireEvent("on" + type, customEvent);
 598  
 599      } else {
 600          Y.error("simulateUIEvent(): No event simulation framework present.");
 601      }
 602  }
 603  
 604  /*
 605   * (iOS only) This is for creating native DOM gesture events which only iOS
 606   * v2.0+ is supporting.
 607   *
 608   * @method simulateGestureEvent
 609   * @private
 610   * @param {HTMLElement} target The target of the given event.
 611   * @param {String} type The type of event to fire. This can be any one of
 612   *      the following: touchstart, touchmove, touchend, touchcancel.
 613   * @param {Boolean} bubbles (Optional) Indicates if the event can be
 614   *      bubbled up. DOM Level 2 specifies that all mouse events bubble by
 615   *      default. The default is true.
 616   * @param {Boolean} cancelable (Optional) Indicates if the event can be
 617   *      canceled using preventDefault(). DOM Level 2 specifies that all
 618   *      touch events except touchcancel can be cancelled. The default
 619   *      is true for all events except touchcancel, for which the default
 620   *      is false.
 621   * @param {Window} view (Optional) The view containing the target. This is
 622   *      typically the window object. The default is window.
 623   * @param {Number} detail (Optional) Specifies some detail information about
 624   *      the event depending on the type of event.
 625   * @param {Number} screenX (Optional) The x-coordinate on the screen at which
 626   *      point the event occured. The default is 0.
 627   * @param {Number} screenY (Optional) The y-coordinate on the screen at which
 628   *      point the event occured. The default is 0.
 629   * @param {Number} clientX (Optional) The x-coordinate on the client at which
 630   *      point the event occured. The default is 0.
 631   * @param {Number} clientY (Optional) The y-coordinate on the client at which
 632   *      point the event occured. The default is 0.
 633   * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
 634   *      is pressed while the event is firing. The default is false.
 635   * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
 636   *      is pressed while the event is firing. The default is false.
 637   * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
 638   *      is pressed while the event is firing. The default is false.
 639   * @param {Boolean} metaKey (Optional) Indicates if one of the META keys
 640   *      is pressed while the event is firing. The default is false.
 641   * @param {Number} scale (iOS v2+ only) The distance between two fingers
 642   *      since the start of an event as a multiplier of the initial distance.
 643   *      The default value is 1.0.
 644   * @param {Number} rotation (iOS v2+ only) The delta rotation since the start
 645   *      of an event, in degrees, where clockwise is positive and
 646   *      counter-clockwise is negative. The default value is 0.0.
 647   */
 648  function simulateGestureEvent(target, type,
 649      bubbles,            // boolean
 650      cancelable,         // boolean
 651      view,               // DOMWindow
 652      detail,             // long
 653      screenX, screenY,   // long
 654      clientX, clientY,   // long
 655      ctrlKey, altKey, shiftKey, metaKey, // boolean
 656      scale,              // float
 657      rotation            // float
 658  ) {
 659      var customEvent;
 660  
 661      if(!Y.UA.ios || Y.UA.ios<2.0) {
 662          Y.error("simulateGestureEvent(): Native gesture DOM eventframe is not available in this platform.");
 663      }
 664  
 665      // check taget
 666      if (!target){
 667          Y.error("simulateGestureEvent(): Invalid target.");
 668      }
 669  
 670      //check event type
 671      if (Y.Lang.isString(type)) {
 672          type = type.toLowerCase();
 673  
 674          //make sure it's a supported touch event
 675          if (!gestureEvents[type]){
 676              Y.error("simulateTouchEvent(): Event type '" + type + "' not supported.");
 677          }
 678      } else {
 679          Y.error("simulateGestureEvent(): Event type must be a string.");
 680      }
 681  
 682      // setup default values
 683      if (!Y.Lang.isBoolean(bubbles)) { bubbles = true; } // bubble by default
 684      if (!Y.Lang.isBoolean(cancelable)) { cancelable = true; }
 685      if (!Y.Lang.isObject(view))     { view = Y.config.win; }
 686      if (!Y.Lang.isNumber(detail))   { detail = 2; }     // usually not used.
 687      if (!Y.Lang.isNumber(screenX))  { screenX = 0; }
 688      if (!Y.Lang.isNumber(screenY))  { screenY = 0; }
 689      if (!Y.Lang.isNumber(clientX))  { clientX = 0; }
 690      if (!Y.Lang.isNumber(clientY))  { clientY = 0; }
 691      if (!Y.Lang.isBoolean(ctrlKey)) { ctrlKey = false; }
 692      if (!Y.Lang.isBoolean(altKey))  { altKey = false; }
 693      if (!Y.Lang.isBoolean(shiftKey)){ shiftKey = false; }
 694      if (!Y.Lang.isBoolean(metaKey)) { metaKey = false; }
 695  
 696      if (!Y.Lang.isNumber(scale)){ scale = 1.0; }
 697      if (!Y.Lang.isNumber(rotation)){ rotation = 0.0; }
 698  
 699      customEvent = Y.config.doc.createEvent("GestureEvent");
 700  
 701      customEvent.initGestureEvent(type, bubbles, cancelable, view, detail,
 702          screenX, screenY, clientX, clientY,
 703          ctrlKey, altKey, shiftKey, metaKey,
 704          target, scale, rotation);
 705  
 706      target.dispatchEvent(customEvent);
 707  }
 708  
 709  
 710  /*
 711   * @method simulateTouchEvent
 712   * @private
 713   * @param {HTMLElement} target The target of the given event.
 714   * @param {String} type The type of event to fire. This can be any one of
 715   *      the following: touchstart, touchmove, touchend, touchcancel.
 716   * @param {Boolean} bubbles (Optional) Indicates if the event can be
 717   *      bubbled up. DOM Level 2 specifies that all mouse events bubble by
 718   *      default. The default is true.
 719   * @param {Boolean} cancelable (Optional) Indicates if the event can be
 720   *      canceled using preventDefault(). DOM Level 2 specifies that all
 721   *      touch events except touchcancel can be cancelled. The default
 722   *      is true for all events except touchcancel, for which the default
 723   *      is false.
 724   * @param {Window} view (Optional) The view containing the target. This is
 725   *      typically the window object. The default is window.
 726   * @param {Number} detail (Optional) Specifies some detail information about
 727   *      the event depending on the type of event.
 728   * @param {Number} screenX (Optional) The x-coordinate on the screen at which
 729   *      point the event occured. The default is 0.
 730   * @param {Number} screenY (Optional) The y-coordinate on the screen at which
 731   *      point the event occured. The default is 0.
 732   * @param {Number} clientX (Optional) The x-coordinate on the client at which
 733   *      point the event occured. The default is 0.
 734   * @param {Number} clientY (Optional) The y-coordinate on the client at which
 735   *      point the event occured. The default is 0.
 736   * @param {Boolean} ctrlKey (Optional) Indicates if one of the CTRL keys
 737   *      is pressed while the event is firing. The default is false.
 738   * @param {Boolean} altKey (Optional) Indicates if one of the ALT keys
 739   *      is pressed while the event is firing. The default is false.
 740   * @param {Boolean} shiftKey (Optional) Indicates if one of the SHIFT keys
 741   *      is pressed while the event is firing. The default is false.
 742   * @param {Boolean} metaKey (Optional) Indicates if one of the META keys
 743   *      is pressed while the event is firing. The default is false.
 744   * @param {TouchList} touches A collection of Touch objects representing
 745   *      all touches associated with this event.
 746   * @param {TouchList} targetTouches A collection of Touch objects
 747   *      representing all touches associated with this target.
 748   * @param {TouchList} changedTouches A collection of Touch objects
 749   *      representing all touches that changed in this event.
 750   * @param {Number} scale (iOS v2+ only) The distance between two fingers
 751   *      since the start of an event as a multiplier of the initial distance.
 752   *      The default value is 1.0.
 753   * @param {Number} rotation (iOS v2+ only) The delta rotation since the start
 754   *      of an event, in degrees, where clockwise is positive and
 755   *      counter-clockwise is negative. The default value is 0.0.
 756   */
 757  function simulateTouchEvent(target, type,
 758      bubbles,            // boolean
 759      cancelable,         // boolean
 760      view,               // DOMWindow
 761      detail,             // long
 762      screenX, screenY,   // long
 763      clientX, clientY,   // long
 764      ctrlKey, altKey, shiftKey, metaKey, // boolean
 765      touches,            // TouchList
 766      targetTouches,      // TouchList
 767      changedTouches,     // TouchList
 768      scale,              // float
 769      rotation            // float
 770  ) {
 771  
 772      var customEvent;
 773  
 774      // check taget
 775      if (!target){
 776          Y.error("simulateTouchEvent(): Invalid target.");
 777      }
 778  
 779      //check event type
 780      if (Y.Lang.isString(type)) {
 781          type = type.toLowerCase();
 782  
 783          //make sure it's a supported touch event
 784          if (!touchEvents[type]){
 785              Y.error("simulateTouchEvent(): Event type '" + type + "' not supported.");
 786          }
 787      } else {
 788          Y.error("simulateTouchEvent(): Event type must be a string.");
 789      }
 790  
 791      // note that the caller is responsible to pass appropriate touch objects.
 792      // check touch objects
 793      // Android(even 4.0) doesn't define TouchList yet
 794      /*if(type === 'touchstart' || type === 'touchmove') {
 795          if(!touches instanceof TouchList) {
 796              Y.error('simulateTouchEvent(): Invalid touches. It must be a TouchList');
 797          } else {
 798              if(touches.length === 0) {
 799                  Y.error('simulateTouchEvent(): No touch object found.');
 800              }
 801          }
 802      } else if(type === 'touchend') {
 803          if(!changedTouches instanceof TouchList) {
 804              Y.error('simulateTouchEvent(): Invalid touches. It must be a TouchList');
 805          } else {
 806              if(changedTouches.length === 0) {
 807                  Y.error('simulateTouchEvent(): No touch object found.');
 808              }
 809          }
 810      }*/
 811  
 812      if(type === 'touchstart' || type === 'touchmove') {
 813          if(touches.length === 0) {
 814              Y.error('simulateTouchEvent(): No touch object in touches');
 815          }
 816      } else if(type === 'touchend') {
 817          if(changedTouches.length === 0) {
 818              Y.error('simulateTouchEvent(): No touch object in changedTouches');
 819          }
 820      }
 821  
 822      // setup default values
 823      if (!Y.Lang.isBoolean(bubbles)) { bubbles = true; } // bubble by default.
 824      if (!Y.Lang.isBoolean(cancelable)) {
 825          cancelable = (type !== "touchcancel"); // touchcancel is not cancelled
 826      }
 827      if (!Y.Lang.isObject(view))     { view = Y.config.win; }
 828      if (!Y.Lang.isNumber(detail))   { detail = 1; } // usually not used. defaulted to # of touch objects.
 829      if (!Y.Lang.isNumber(screenX))  { screenX = 0; }
 830      if (!Y.Lang.isNumber(screenY))  { screenY = 0; }
 831      if (!Y.Lang.isNumber(clientX))  { clientX = 0; }
 832      if (!Y.Lang.isNumber(clientY))  { clientY = 0; }
 833      if (!Y.Lang.isBoolean(ctrlKey)) { ctrlKey = false; }
 834      if (!Y.Lang.isBoolean(altKey))  { altKey = false; }
 835      if (!Y.Lang.isBoolean(shiftKey)){ shiftKey = false; }
 836      if (!Y.Lang.isBoolean(metaKey)) { metaKey = false; }
 837      if (!Y.Lang.isNumber(scale))    { scale = 1.0; }
 838      if (!Y.Lang.isNumber(rotation)) { rotation = 0.0; }
 839  
 840  
 841      //check for DOM-compliant browsers first
 842      if (Y.Lang.isFunction(Y.config.doc.createEvent)) {
 843          if (Y.UA.android) {
 844              /*
 845                  * Couldn't find android start version that supports touch event.
 846                  * Assumed supported(btw APIs broken till icecream sandwitch)
 847                  * from the beginning.
 848              */
 849              if(Y.UA.android < 4.0) {
 850                  /*
 851                      * Touch APIs are broken in androids older than 4.0. We will use
 852                      * simulated touch apis for these versions.
 853                      * App developer still can listen for touch events. This events
 854                      * will be dispatched with touch event types.
 855                      *
 856                      * (Note) Used target for the relatedTarget. Need to verify if
 857                      * it has a side effect.
 858                  */
 859                  customEvent = Y.config.doc.createEvent("MouseEvents");
 860                  customEvent.initMouseEvent(type, bubbles, cancelable, view, detail,
 861                      screenX, screenY, clientX, clientY,
 862                      ctrlKey, altKey, shiftKey, metaKey,
 863                      0, target);
 864  
 865                  customEvent.touches = touches;
 866                  customEvent.targetTouches = targetTouches;
 867                  customEvent.changedTouches = changedTouches;
 868              } else {
 869                  customEvent = Y.config.doc.createEvent("TouchEvent");
 870  
 871                  // Andoroid isn't compliant W3C initTouchEvent method signature.
 872                  customEvent.initTouchEvent(touches, targetTouches, changedTouches,
 873                      type, view,
 874                      screenX, screenY, clientX, clientY,
 875                      ctrlKey, altKey, shiftKey, metaKey);
 876              }
 877          } else if (Y.UA.ios) {
 878              if(Y.UA.ios >= 2.0) {
 879                  customEvent = Y.config.doc.createEvent("TouchEvent");
 880  
 881                  // Available iOS 2.0 and later
 882                  customEvent.initTouchEvent(type, bubbles, cancelable, view, detail,
 883                      screenX, screenY, clientX, clientY,
 884                      ctrlKey, altKey, shiftKey, metaKey,
 885                      touches, targetTouches, changedTouches,
 886                      scale, rotation);
 887              } else {
 888                  Y.error('simulateTouchEvent(): No touch event simulation framework present for iOS, '+Y.UA.ios+'.');
 889              }
 890          } else {
 891              Y.error('simulateTouchEvent(): Not supported agent yet, '+Y.UA.userAgent);
 892          }
 893  
 894          //fire the event
 895          target.dispatchEvent(customEvent);
 896      //} else if (Y.Lang.isObject(doc.createEventObject)){ // Windows Mobile/IE, support later
 897      } else {
 898          Y.error('simulateTouchEvent(): No event simulation framework present.');
 899      }
 900  }
 901  
 902  /**
 903   * Simulates the event or gesture with the given name on a target.
 904   * @param {HTMLElement} target The DOM element that's the target of the event.
 905   * @param {String} type The type of event or name of the supported gesture to simulate
 906   *      (i.e., "click", "doubletap", "flick").
 907   * @param {Object} options (Optional) Extra options to copy onto the event object.
 908   *      For gestures, options are used to refine the gesture behavior.
 909   * @for Event
 910   * @method simulate
 911   * @static
 912   */
 913  Y.Event.simulate = function(target, type, options){
 914  
 915      options = options || {};
 916  
 917      if (mouseEvents[type] || pointerEvents[type]){
 918          simulateMouseEvent(target, type, options.bubbles,
 919              options.cancelable, options.view, options.detail, options.screenX,
 920              options.screenY, options.clientX, options.clientY, options.ctrlKey,
 921              options.altKey, options.shiftKey, options.metaKey, options.button,
 922              options.relatedTarget);
 923      } else if (keyEvents[type]){
 924          simulateKeyEvent(target, type, options.bubbles,
 925              options.cancelable, options.view, options.ctrlKey,
 926              options.altKey, options.shiftKey, options.metaKey,
 927              options.keyCode, options.charCode);
 928      } else if (uiEvents[type]){
 929          simulateUIEvent(target, type, options.bubbles,
 930              options.cancelable, options.view, options.detail);
 931  
 932      // touch low-level event simulation
 933      } else if (touchEvents[type]) {
 934          if((Y.config.win && ("ontouchstart" in Y.config.win)) && !(Y.UA.phantomjs) && !(Y.UA.chrome && Y.UA.chrome < 6)) {
 935              simulateTouchEvent(target, type,
 936                  options.bubbles, options.cancelable, options.view, options.detail,
 937                  options.screenX, options.screenY, options.clientX, options.clientY,
 938                  options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
 939                  options.touches, options.targetTouches, options.changedTouches,
 940                  options.scale, options.rotation);
 941          } else {
 942              Y.error("simulate(): Event '" + type + "' can't be simulated. Use gesture-simulate module instead.");
 943          }
 944  
 945      // ios gesture low-level event simulation (iOS v2+ only)
 946      } else if(Y.UA.ios && Y.UA.ios >= 2.0 && gestureEvents[type]) {
 947          simulateGestureEvent(target, type,
 948              options.bubbles, options.cancelable, options.view, options.detail,
 949              options.screenX, options.screenY, options.clientX, options.clientY,
 950              options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
 951              options.scale, options.rotation);
 952  
 953      // anything else
 954      } else {
 955          Y.error("simulate(): Event '" + type + "' can't be simulated.");
 956      }
 957  };
 958  
 959  
 960  })();
 961  
 962  
 963  
 964  }, '3.17.2', {"requires": ["event-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1