2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 * Augments the Event Utility with a <code>delegate</code> method that
9 * facilitates easy creation of delegated event listeners. (Note: Using CSS
10 * selectors as the filtering criteria for delegated event listeners requires
11 * inclusion of the Selector Utility.)
13 * @module event-delegate
14 * @title Event Utility Event Delegation Module
15 * @namespace YAHOO.util
21 var Event = YAHOO.util.Event,
26 getMatch = function(el, selector, container) {
30 if (!el || el === container) {
34 returnVal = YAHOO.util.Selector.test(el, selector) ? el: getMatch(el.parentNode, selector, container);
42 Lang.augmentObject(Event, {
45 * Creates a delegate function used to call event listeners specified
46 * via the <code>YAHOO.util.Event.delegate</code> method.
48 * @method _createDelegate
50 * @param {Function} fn The method (event listener) to call.
51 * @param {Function|string} filter Function or CSS selector used to
52 * determine for what element(s) the event listener should be called.
53 * @param {Object} obj An arbitrary object that will be
54 * passed as a parameter to the listener.
55 * @param {Boolean|object} overrideContext If true, the value of the
56 * obj parameter becomes the execution context
57 * of the listener. If an object, this object
58 * becomes the execution context.
59 * @return {Function} Function that will call the event listener
60 * specified by the <code>YAHOO.util.Event.delegate</code> method.
65 _createDelegate: function (fn, filter, obj, overrideContext) {
67 return function (event) {
70 target = Event.getTarget(event),
73 // The user might have specified the document object
74 // as the delegation container, in which case it is not
75 // nessary to scope the provided CSS selector(s) to the
76 // delegation container
77 bDocument = (container.nodeType === 9),
85 if (Lang.isFunction(filter)) {
86 matchedEl = filter(target);
88 else if (Lang.isString(filter)) {
95 sID = Event.generateId(container);
98 // Scope all selectors to the container
99 sIDSelector = ("#" + sID + " ");
100 selector = (sIDSelector + filter).replace(/,/gi, ("," + sIDSelector));
105 if (YAHOO.util.Selector.test(target, selector)) {
108 else if (YAHOO.util.Selector.test(target, ((selector.replace(/,/gi, " *,")) + " *"))) {
110 // The target is a descendant of an element matching
111 // the selector, so crawl up to find the ancestor that
112 // matches the selector
114 matchedEl = getMatch(target, selector, container);
123 // The default context for delegated listeners is the
124 // element that matched the filter.
128 if (overrideContext) {
129 if (overrideContext === true) {
132 context = overrideContext;
136 // Call the listener passing in the container and the
137 // element that matched the filter in case the user
140 return fn.call(context, event, matchedEl, container, obj);
150 * Appends a delegated event listener. Delegated event listeners
151 * receive three arguments by default: the DOM event, the element
152 * specified by the filtering function or CSS selector, and the
153 * container element (the element to which the event listener is
154 * bound). (Note: Using the delegate method requires the event-delegate
155 * module. Using CSS selectors as the filtering criteria for delegated
156 * event listeners requires inclusion of the Selector Utility.)
160 * @param {String|HTMLElement|Array|NodeList} container An id, an element
161 * reference, or a collection of ids and/or elements to assign the
163 * @param {String} type The type of event listener to append
164 * @param {Function} fn The method the event invokes
165 * @param {Function|string} filter Function or CSS selector used to
166 * determine for what element(s) the event listener should be called.
167 * When a function is specified, the function should return an
168 * HTML element. Using a CSS Selector requires the inclusion of the
169 * CSS Selector Utility.
170 * @param {Object} obj An arbitrary object that will be
171 * passed as a parameter to the listener
172 * @param {Boolean|object} overrideContext If true, the value of the obj parameter becomes
173 * the execution context of the listener. If an
174 * object, this object becomes the execution
176 * @return {Boolean} Returns true if the action was successful or defered,
177 * false if one or more of the elements
178 * could not have the listener attached,
179 * or if the operation throws an exception.
183 delegate: function (container, type, fn, filter, obj, overrideContext) {
190 if (Lang.isString(filter) && !YAHOO.util.Selector) {
195 if (type == "mouseenter" || type == "mouseleave") {
197 if (!Event._createMouseDelegate) {
201 // Look up the real event--either mouseover or mouseout
202 sType = Event._getType(type);
204 fnMouseDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
206 fnDelegate = Event._createDelegate(function (event, matchedEl, container) {
208 return fnMouseDelegate.call(matchedEl, event, container);
210 }, filter, obj, overrideContext);
215 fnDelegate = Event._createDelegate(fn, filter, obj, overrideContext);
219 delegates.push([container, sType, fn, fnDelegate]);
221 return Event.on(container, sType, fnDelegate);
227 * Removes a delegated event listener.
229 * @method removeDelegate
231 * @param {String|HTMLElement|Array|NodeList} container An id, an element
232 * reference, or a collection of ids and/or elements to remove
234 * @param {String} type The type of event to remove.
235 * @param {Function} fn The method the event invokes. If fn is
236 * undefined, then all event listeners for the type of event are
238 * @return {boolean} Returns true if the unbind was successful, false
243 removeDelegate: function (container, type, fn) {
250 // Look up the real event--either mouseover or mouseout
251 if (type == "mouseenter" || type == "mouseleave") {
252 sType = Event._getType(type);
255 index = Event._getCacheIndex(delegates, container, sType, fn);
258 cacheItem = delegates[index];
262 if (container && cacheItem) {
264 returnVal = Event.removeListener(cacheItem[0], cacheItem[1], cacheItem[3]);
267 delete delegates[index][2];
268 delete delegates[index][3];
269 delegates.splice(index, 1);
281 YAHOO.register("event-delegate", YAHOO.util.Event, {version: "2.9.0", build: "2800"});