API Docs for: 3.8.0
Show:

File: event/js/event-facade-dom.js

  1. /**
  2.  * Custom event engine, DOM event listener abstraction layer, synthetic DOM
  3.  * events.
  4.  * @module event
  5.  * @submodule event-base
  6.  */

  7. /**
  8.  * Wraps a DOM event, properties requiring browser abstraction are
  9.  * fixed here.  Provids a security layer when required.
  10.  * @class DOMEventFacade
  11.  * @param ev {Event} the DOM event
  12.  * @param currentTarget {HTMLElement} the element the listener was attached to
  13.  * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
  14.  */

  15.     var ua = Y.UA,

  16.     EMPTY = {},

  17.     /**
  18.      * webkit key remapping required for Safari < 3.1
  19.      * @property webkitKeymap
  20.      * @private
  21.      */
  22.     webkitKeymap = {
  23.         63232: 38, // up
  24.         63233: 40, // down
  25.         63234: 37, // left
  26.         63235: 39, // right
  27.         63276: 33, // page up
  28.         63277: 34, // page down
  29.         25:     9, // SHIFT-TAB (Safari provides a different key code in
  30.                    // this case, even though the shiftKey modifier is set)
  31.         63272: 46, // delete
  32.         63273: 36, // home
  33.         63275: 35  // end
  34.     },

  35.     /**
  36.      * Returns a wrapped node.  Intended to be used on event targets,
  37.      * so it will return the node's parent if the target is a text
  38.      * node.
  39.      *
  40.      * If accessing a property of the node throws an error, this is
  41.      * probably the anonymous div wrapper Gecko adds inside text
  42.      * nodes.  This likely will only occur when attempting to access
  43.      * the relatedTarget.  In this case, we now return null because
  44.      * the anonymous div is completely useless and we do not know
  45.      * what the related target was because we can't even get to
  46.      * the element's parent node.
  47.      *
  48.      * @method resolve
  49.      * @private
  50.      */
  51.     resolve = function(n) {
  52.         if (!n) {
  53.             return n;
  54.         }
  55.         try {
  56.             if (n && 3 == n.nodeType) {
  57.                 n = n.parentNode;
  58.             }
  59.         } catch(e) {
  60.             return null;
  61.         }

  62.         return Y.one(n);
  63.     },

  64.     DOMEventFacade = function(ev, currentTarget, wrapper) {
  65.         this._event = ev;
  66.         this._currentTarget = currentTarget;
  67.         this._wrapper = wrapper || EMPTY;

  68.         // if not lazy init
  69.         this.init();
  70.     };

  71. Y.extend(DOMEventFacade, Object, {

  72.     init: function() {

  73.         var e = this._event,
  74.             overrides = this._wrapper.overrides,
  75.             x = e.pageX,
  76.             y = e.pageY,
  77.             c,
  78.             currentTarget = this._currentTarget;

  79.         this.altKey   = e.altKey;
  80.         this.ctrlKey  = e.ctrlKey;
  81.         this.metaKey  = e.metaKey;
  82.         this.shiftKey = e.shiftKey;
  83.         this.type     = (overrides && overrides.type) || e.type;
  84.         this.clientX  = e.clientX;
  85.         this.clientY  = e.clientY;

  86.         this.pageX = x;
  87.         this.pageY = y;

  88.         // charCode is unknown in keyup, keydown. keyCode is unknown in keypress.
  89.         // FF 3.6 - 8+? pass 0 for keyCode in keypress events.
  90.         // Webkit, FF 3.6-8+?, and IE9+? pass 0 for charCode in keydown, keyup.
  91.         // Webkit and IE9+? duplicate charCode in keyCode.
  92.         // Opera never sets charCode, always keyCode (though with the charCode).
  93.         // IE6-8 don't set charCode or which.
  94.         // All browsers other than IE6-8 set which=keyCode in keydown, keyup, and
  95.         // which=charCode in keypress.
  96.         //
  97.         // Moral of the story: (e.which || e.keyCode) will always return the
  98.         // known code for that key event phase. e.keyCode is often different in
  99.         // keypress from keydown and keyup.
  100.         c = e.keyCode || e.charCode;

  101.         if (ua.webkit && (c in webkitKeymap)) {
  102.             c = webkitKeymap[c];
  103.         }

  104.         this.keyCode = c;
  105.         this.charCode = c;
  106.         // Fill in e.which for IE - implementers should always use this over
  107.         // e.keyCode or e.charCode.
  108.         this.which = e.which || e.charCode || c;
  109.         // this.button = e.button;
  110.         this.button = this.which;

  111.         this.target = resolve(e.target);
  112.         this.currentTarget = resolve(currentTarget);
  113.         this.relatedTarget = resolve(e.relatedTarget);

  114.         if (e.type == "mousewheel" || e.type == "DOMMouseScroll") {
  115.             this.wheelDelta = (e.detail) ? (e.detail * -1) : Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
  116.         }

  117.         if (this._touch) {
  118.             this._touch(e, currentTarget, this._wrapper);
  119.         }
  120.     },

  121.     stopPropagation: function() {
  122.         this._event.stopPropagation();
  123.         this._wrapper.stopped = 1;
  124.         this.stopped = 1;
  125.     },

  126.     stopImmediatePropagation: function() {
  127.         var e = this._event;
  128.         if (e.stopImmediatePropagation) {
  129.             e.stopImmediatePropagation();
  130.         } else {
  131.             this.stopPropagation();
  132.         }
  133.         this._wrapper.stopped = 2;
  134.         this.stopped = 2;
  135.     },

  136.     preventDefault: function(returnValue) {
  137.         var e = this._event;
  138.         e.preventDefault();
  139.         e.returnValue = returnValue || false;
  140.         this._wrapper.prevented = 1;
  141.         this.prevented = 1;
  142.     },

  143.     halt: function(immediate) {
  144.         if (immediate) {
  145.             this.stopImmediatePropagation();
  146.         } else {
  147.             this.stopPropagation();
  148.         }

  149.         this.preventDefault();
  150.     }

  151. });

  152. DOMEventFacade.resolve = resolve;
  153. Y.DOM2EventFacade = DOMEventFacade;
  154. Y.DOMEventFacade = DOMEventFacade;

  155.     /**
  156.      * The native event
  157.      * @property _event
  158.      * @type {Native DOM Event}
  159.      * @private
  160.      */

  161.     /**
  162.     The name of the event (e.g. "click")

  163.     @property type
  164.     @type {String}
  165.     **/

  166.     /**
  167.     `true` if the "alt" or "option" key is pressed.

  168.     @property altKey
  169.     @type {Boolean}
  170.     **/

  171.     /**
  172.     `true` if the shift key is pressed.

  173.     @property shiftKey
  174.     @type {Boolean}
  175.     **/

  176.     /**
  177.     `true` if the "Windows" key on a Windows keyboard, "command" key on an
  178.     Apple keyboard, or "meta" key on other keyboards is pressed.

  179.     @property metaKey
  180.     @type {Boolean}
  181.     **/

  182.     /**
  183.     `true` if the "Ctrl" or "control" key is pressed.

  184.     @property ctrlKey
  185.     @type {Boolean}
  186.     **/

  187.     /**
  188.      * The X location of the event on the page (including scroll)
  189.      * @property pageX
  190.      * @type {Number}
  191.      */

  192.     /**
  193.      * The Y location of the event on the page (including scroll)
  194.      * @property pageY
  195.      * @type {Number}
  196.      */

  197.     /**
  198.      * The X location of the event in the viewport
  199.      * @property clientX
  200.      * @type {Number}
  201.      */

  202.     /**
  203.      * The Y location of the event in the viewport
  204.      * @property clientY
  205.      * @type {Number}
  206.      */

  207.     /**
  208.      * The keyCode for key events.  Uses charCode if keyCode is not available
  209.      * @property keyCode
  210.      * @type {Number}
  211.      */

  212.     /**
  213.      * The charCode for key events.  Same as keyCode
  214.      * @property charCode
  215.      * @type {Number}
  216.      */

  217.     /**
  218.      * The button that was pushed. 1 for left click, 2 for middle click, 3 for
  219.      * right click.  This is only reliably populated on `mouseup` events.
  220.      * @property button
  221.      * @type {Number}
  222.      */

  223.     /**
  224.      * The button that was pushed.  Same as button.
  225.      * @property which
  226.      * @type {Number}
  227.      */

  228.     /**
  229.      * Node reference for the targeted element
  230.      * @property target
  231.      * @type {Node}
  232.      */

  233.     /**
  234.      * Node reference for the element that the listener was attached to.
  235.      * @property currentTarget
  236.      * @type {Node}
  237.      */

  238.     /**
  239.      * Node reference to the relatedTarget
  240.      * @property relatedTarget
  241.      * @type {Node}
  242.      */

  243.     /**
  244.      * Number representing the direction and velocity of the movement of the mousewheel.
  245.      * Negative is down, the higher the number, the faster.  Applies to the mousewheel event.
  246.      * @property wheelDelta
  247.      * @type {Number}
  248.      */

  249.     /**
  250.      * Stops the propagation to the next bubble target
  251.      * @method stopPropagation
  252.      */

  253.     /**
  254.      * Stops the propagation to the next bubble target and
  255.      * prevents any additional listeners from being exectued
  256.      * on the current target.
  257.      * @method stopImmediatePropagation
  258.      */

  259.     /**
  260.      * Prevents the event's default behavior
  261.      * @method preventDefault
  262.      * @param returnValue {string} sets the returnValue of the event to this value
  263.      * (rather than the default false value).  This can be used to add a customized
  264.      * confirmation query to the beforeunload event).
  265.      */

  266.     /**
  267.      * Stops the event propagation and prevents the default
  268.      * event behavior.
  269.      * @method halt
  270.      * @param immediate {boolean} if true additional listeners
  271.      * on the current target will not be executed
  272.      */

  273.