API Docs for: 3.8.0
Show:

File: node/js/node-event.js

  1. /**
  2.  * @module node
  3.  * @submodule node-base
  4.  */

  5. var Y_Node = Y.Node;
  6. var Y_NodeList = Y.NodeList;
  7. /**
  8.  * List of events that route to DOM events
  9.  * @static
  10.  * @property DOM_EVENTS
  11.  * @for Node
  12.  */

  13. Y_Node.DOM_EVENTS = {
  14.     abort: 1,
  15.     beforeunload: 1,
  16.     blur: 1,
  17.     change: 1,
  18.     click: 1,
  19.     close: 1,
  20.     command: 1,
  21.     contextmenu: 1,
  22.     dblclick: 1,
  23.     DOMMouseScroll: 1,
  24.     drag: 1,
  25.     dragstart: 1,
  26.     dragenter: 1,
  27.     dragover: 1,
  28.     dragleave: 1,
  29.     dragend: 1,
  30.     drop: 1,
  31.     error: 1,
  32.     focus: 1,
  33.     key: 1,
  34.     keydown: 1,
  35.     keypress: 1,
  36.     keyup: 1,
  37.     load: 1,
  38.     message: 1,
  39.     mousedown: 1,
  40.     mouseenter: 1,
  41.     mouseleave: 1,
  42.     mousemove: 1,
  43.     mousemultiwheel: 1,
  44.     mouseout: 1,
  45.     mouseover: 1,
  46.     mouseup: 1,
  47.     mousewheel: 1,
  48.     orientationchange: 1,
  49.     reset: 1,
  50.     resize: 1,
  51.     select: 1,
  52.     selectstart: 1,
  53.     submit: 1,
  54.     scroll: 1,
  55.     textInput: 1,
  56.     unload: 1
  57. };

  58. // Add custom event adaptors to this list.  This will make it so
  59. // that delegate, key, available, contentready, etc all will
  60. // be available through Node.on
  61. Y.mix(Y_Node.DOM_EVENTS, Y.Env.evt.plugins);

  62. Y.augment(Y_Node, Y.EventTarget);

  63. Y.mix(Y_Node.prototype, {
  64.     /**
  65.      * Removes event listeners from the node and (optionally) its subtree
  66.      * @method purge
  67.      * @param {Boolean} recurse (optional) Whether or not to remove listeners from the
  68.      * node's subtree
  69.      * @param {String} type (optional) Only remove listeners of the specified type
  70.      * @chainable
  71.      *
  72.      */
  73.     purge: function(recurse, type) {
  74.         Y.Event.purgeElement(this._node, recurse, type);
  75.         return this;
  76.     }

  77. });

  78. Y.mix(Y.NodeList.prototype, {
  79.     _prepEvtArgs: function(type, fn, context) {
  80.         // map to Y.on/after signature (type, fn, nodes, context, arg1, arg2, etc)
  81.         var args = Y.Array(arguments, 0, true);

  82.         if (args.length < 2) { // type only (event hash) just add nodes
  83.             args[2] = this._nodes;
  84.         } else {
  85.             args.splice(2, 0, this._nodes);
  86.         }

  87.         args[3] = context || this; // default to NodeList instance as context

  88.         return args;
  89.     },

  90.     /**
  91.     Subscribe a callback function for each `Node` in the collection to execute
  92.     in response to a DOM event.

  93.     NOTE: Generally, the `on()` method should be avoided on `NodeLists`, in
  94.     favor of using event delegation from a parent Node.  See the Event user
  95.     guide for details.

  96.     Most DOM events are associated with a preventable default behavior, such as
  97.     link clicks navigating to a new page.  Callbacks are passed a
  98.     `DOMEventFacade` object as their first argument (usually called `e`) that
  99.     can be used to prevent this default behavior with `e.preventDefault()`. See
  100.     the `DOMEventFacade` API for all available properties and methods on the
  101.     object.

  102.     By default, the `this` object will be the `NodeList` that the subscription
  103.     came from, <em>not the `Node` that received the event</em>.  Use
  104.     `e.currentTarget` to refer to the `Node`.

  105.     Returning `false` from a callback is supported as an alternative to calling
  106.     `e.preventDefault(); e.stopPropagation();`.  However, it is recommended to
  107.     use the event methods.

  108.     @example

  109.         Y.all(".sku").on("keydown", function (e) {
  110.             if (e.keyCode === 13) {
  111.                 e.preventDefault();

  112.                 // Use e.currentTarget to refer to the individual Node
  113.                 var item = Y.MyApp.searchInventory( e.currentTarget.get('value') );
  114.                 // etc ...
  115.             }
  116.         });

  117.     @method on
  118.     @param {String} type The name of the event
  119.     @param {Function} fn The callback to execute in response to the event
  120.     @param {Object} [context] Override `this` object in callback
  121.     @param {Any} [arg*] 0..n additional arguments to supply to the subscriber
  122.     @return {EventHandle} A subscription handle capable of detaching that
  123.                           subscription
  124.     @for NodeList
  125.     **/
  126.     on: function(type, fn, context) {
  127.         return Y.on.apply(Y, this._prepEvtArgs.apply(this, arguments));
  128.     },

  129.     /**
  130.      * Applies an one-time event listener to each Node bound to the NodeList.
  131.      * @method once
  132.      * @param {String} type The event being listened for
  133.      * @param {Function} fn The handler to call when the event fires
  134.      * @param {Object} context The context to call the handler with.
  135.      * Default is the NodeList instance.
  136.      * @return {EventHandle} A subscription handle capable of detaching that
  137.      *                    subscription
  138.      * @for NodeList
  139.      */
  140.     once: function(type, fn, context) {
  141.         return Y.once.apply(Y, this._prepEvtArgs.apply(this, arguments));
  142.     },

  143.     /**
  144.      * Applies an event listener to each Node bound to the NodeList.
  145.      * The handler is called only after all on() handlers are called
  146.      * and the event is not prevented.
  147.      * @method after
  148.      * @param {String} type The event being listened for
  149.      * @param {Function} fn The handler to call when the event fires
  150.      * @param {Object} context The context to call the handler with.
  151.      * Default is the NodeList instance.
  152.      * @return {EventHandle} A subscription handle capable of detaching that
  153.      *                    subscription
  154.      * @for NodeList
  155.      */
  156.     after: function(type, fn, context) {
  157.         return Y.after.apply(Y, this._prepEvtArgs.apply(this, arguments));
  158.     },

  159.     /**
  160.      * Applies an one-time event listener to each Node bound to the NodeList
  161.      * that will be called only after all on() handlers are called and the
  162.      * event is not prevented.
  163.      *
  164.      * @method onceAfter
  165.      * @param {String} type The event being listened for
  166.      * @param {Function} fn The handler to call when the event fires
  167.      * @param {Object} context The context to call the handler with.
  168.      * Default is the NodeList instance.
  169.      * @return {EventHandle} A subscription handle capable of detaching that
  170.      *                    subscription
  171.      * @for NodeList
  172.      */
  173.     onceAfter: function(type, fn, context) {
  174.         return Y.onceAfter.apply(Y, this._prepEvtArgs.apply(this, arguments));
  175.     }
  176. });

  177. Y_NodeList.importMethod(Y.Node.prototype, [
  178.     /**
  179.       * Called on each Node instance
  180.       * @method detach
  181.       * @see Node.detach
  182.       * @for NodeList
  183.       */
  184.     'detach',

  185.     /** Called on each Node instance
  186.       * @method detachAll
  187.       * @see Node.detachAll
  188.       * @for NodeList
  189.       */
  190.     'detachAll'
  191. ]);

  192. /**
  193. Subscribe a callback function to execute in response to a DOM event or custom
  194. event.

  195. Most DOM events are associated with a preventable default behavior such as
  196. link clicks navigating to a new page.  Callbacks are passed a `DOMEventFacade`
  197. object as their first argument (usually called `e`) that can be used to
  198. prevent this default behavior with `e.preventDefault()`. See the
  199. `DOMEventFacade` API for all available properties and methods on the object.

  200. If the event name passed as the first parameter is not a whitelisted DOM event,
  201. it will be treated as a custom event subscriptions, allowing
  202. `node.fire('customEventName')` later in the code.  Refer to the Event user guide
  203. for the full DOM event whitelist.

  204. By default, the `this` object in the callback will refer to the subscribed
  205. `Node`.

  206. Returning `false` from a callback is supported as an alternative to calling
  207. `e.preventDefault(); e.stopPropagation();`.  However, it is recommended to use
  208. the event methods.

  209. @example

  210.     Y.one("#my-form").on("submit", function (e) {
  211.         e.preventDefault();

  212.         // proceed with ajax form submission instead...
  213.     });

  214. @method on
  215. @param {String} type The name of the event
  216. @param {Function} fn The callback to execute in response to the event
  217. @param {Object} [context] Override `this` object in callback
  218. @param {Any} [arg*] 0..n additional arguments to supply to the subscriber
  219. @return {EventHandle} A subscription handle capable of detaching that
  220.                       subscription
  221. @for Node
  222. **/


  223.