3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('node-event-delegate', function(Y) {
10 * Functionality to make the node a delegated event container
12 * @submodule node-event-delegate
16 * <p>Sets up a delegation listener for an event occurring inside the Node.
17 * The delegated event will be verified against a supplied selector or
18 * filtering function to test if the event references at least one node that
19 * should trigger the subscription callback.</p>
21 * <p>Selector string filters will trigger the callback if the event originated
22 * from a node that matches it or is contained in a node that matches it.
23 * Function filters are called for each Node up the parent axis to the
24 * subscribing container node, and receive at each level the Node and the event
25 * object. The function should return true (or a truthy value) if that Node
26 * should trigger the subscription callback. Note, it is possible for filters
27 * to match multiple Nodes for a single event. In this case, the delegate
28 * callback will be executed for each matching Node.</p>
30 * <p>For each matching Node, the callback will be executed with its 'this'
31 * object set to the Node matched by the filter (unless a specific context was
32 * provided during subscription), and the provided event's
33 * <code>currentTarget</code> will also be set to the matching Node. The
34 * containing Node from which the subscription was originally made can be
35 * referenced as <code>e.container</code>.
38 * @param type {String} the event type to delegate
39 * @param fn {Function} the callback function to execute. This function
40 * will be provided the event object for the delegated event.
41 * @param spec {String|Function} a selector that must match the target of the
42 * event or a function to test target and its parents for a match
43 * @param context {Object} optional argument that specifies what 'this' refers to.
44 * @param args* {any} 0..n additional arguments to pass on to the callback function.
45 * These arguments will be added after the event object.
46 * @return {EventHandle} the detach handle
49 Y.Node.prototype.delegate = function(type) {
51 var args = Y.Array(arguments, 0, true),
52 index = (Y.Lang.isObject(type) && !Y.Lang.isArray(type)) ? 1 : 2;
54 args.splice(index, 0, this._node);
56 return Y.delegate.apply(Y, args);
60 }, '3.5.1' ,{requires:['node-base', 'event-delegate']});