/usr/share/javascript/yui/event-delegate/event-delegate.js is in libjs-yui 2.9.0.dfsg.0.1-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /*
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.html
version: 2.9.0
*/
/**
* Augments the Event Utility with a <code>delegate</code> method that
* facilitates easy creation of delegated event listeners. (Note: Using CSS
* selectors as the filtering criteria for delegated event listeners requires
* inclusion of the Selector Utility.)
*
* @module event-delegate
* @title Event Utility Event Delegation Module
* @namespace YAHOO.util
* @requires event
*/
(function () {
var Event = YAHOO.util.Event,
Lang = YAHOO.lang,
delegates = [],
getMatch = function(el, selector, container) {
var returnVal;
if (!el || el === container) {
returnVal = false;
}
else {
returnVal = YAHOO.util.Selector.test(el, selector) ? el: getMatch(el.parentNode, selector, container);
}
return returnVal;
};
Lang.augmentObject(Event, {
/**
* Creates a delegate function used to call event listeners specified
* via the <code>YAHOO.util.Event.delegate</code> method.
*
* @method _createDelegate
*
* @param {Function} fn The method (event listener) to call.
* @param {Function|string} filter Function or CSS selector used to
* determine for what element(s) the event listener should be called.
* @param {Object} obj An arbitrary object that will be
* passed as a parameter to the listener.
* @param {Boolean|object} overrideContext If true, the value of the
* obj parameter becomes the execution context
* of the listener. If an object, this object
* becomes the execution context.
* @return {Function} Function that will call the event listener
* specified by the <code>YAHOO.util.Event.delegate</code> method.
* @private
* @for Event
* @static
*/
_createDelegate: function (fn, filter, obj, overrideContext) {
return function (event) {
var container = this,
target = Event.getTarget(event),
selector = filter,
// The user might have specified the document object
// as the delegation container, in which case it is not
// nessary to scope the provided CSS selector(s) to the
// delegation container
bDocument = (container.nodeType === 9),
matchedEl,
context,
sID,
sIDSelector;
if (Lang.isFunction(filter)) {
matchedEl = filter(target);
}
else if (Lang.isString(filter)) {
if (!bDocument) {
sID = container.id;
if (!sID) {
sID = Event.generateId(container);
}
// Scope all selectors to the container
sIDSelector = ("#" + sID + " ");
selector = (sIDSelector + filter).replace(/,/gi, ("," + sIDSelector));
}
if (YAHOO.util.Selector.test(target, selector)) {
matchedEl = target;
}
else if (YAHOO.util.Selector.test(target, ((selector.replace(/,/gi, " *,")) + " *"))) {
// The target is a descendant of an element matching
// the selector, so crawl up to find the ancestor that
// matches the selector
matchedEl = getMatch(target, selector, container);
}
}
if (matchedEl) {
// The default context for delegated listeners is the
// element that matched the filter.
context = matchedEl;
if (overrideContext) {
if (overrideContext === true) {
context = obj;
} else {
context = overrideContext;
}
}
// Call the listener passing in the container and the
// element that matched the filter in case the user
// needs those.
return fn.call(context, event, matchedEl, container, obj);
}
};
},
/**
* Appends a delegated event listener. Delegated event listeners
* receive three arguments by default: the DOM event, the element
* specified by the filtering function or CSS selector, and the
* container element (the element to which the event listener is
* bound). (Note: Using the delegate method requires the event-delegate
* module. Using CSS selectors as the filtering criteria for delegated
* event listeners requires inclusion of the Selector Utility.)
*
* @method delegate
*
* @param {String|HTMLElement|Array|NodeList} container An id, an element
* reference, or a collection of ids and/or elements to assign the
* listener to.
* @param {String} type The type of event listener to append
* @param {Function} fn The method the event invokes
* @param {Function|string} filter Function or CSS selector used to
* determine for what element(s) the event listener should be called.
* When a function is specified, the function should return an
* HTML element. Using a CSS Selector requires the inclusion of the
* CSS Selector Utility.
* @param {Object} obj An arbitrary object that will be
* passed as a parameter to the listener
* @param {Boolean|object} overrideContext If true, the value of the obj parameter becomes
* the execution context of the listener. If an
* object, this object becomes the execution
* context.
* @return {Boolean} Returns true if the action was successful or defered,
* false if one or more of the elements
* could not have the listener attached,
* or if the operation throws an exception.
* @static
* @for Event
*/
delegate: function (container, type, fn, filter, obj, overrideContext) {
var sType = type,
fnMouseDelegate,
fnDelegate;
if (Lang.isString(filter) && !YAHOO.util.Selector) {
return false;
}
if (type == "mouseenter" || type == "mouseleave") {
if (!Event._createMouseDelegate) {
return false;
}
// Look up the real event--either mouseover or mouseout
sType = Event._getType(type);
fnMouseDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
fnDelegate = Event._createDelegate(function (event, matchedEl, container) {
return fnMouseDelegate.call(matchedEl, event, container);
}, filter, obj, overrideContext);
}
else {
fnDelegate = Event._createDelegate(fn, filter, obj, overrideContext);
}
delegates.push([container, sType, fn, fnDelegate]);
return Event.on(container, sType, fnDelegate);
},
/**
* Removes a delegated event listener.
*
* @method removeDelegate
*
* @param {String|HTMLElement|Array|NodeList} container An id, an element
* reference, or a collection of ids and/or elements to remove
* the listener from.
* @param {String} type The type of event to remove.
* @param {Function} fn The method the event invokes. If fn is
* undefined, then all event listeners for the type of event are
* removed.
* @return {boolean} Returns true if the unbind was successful, false
* otherwise.
* @static
* @for Event
*/
removeDelegate: function (container, type, fn) {
var sType = type,
returnVal = false,
index,
cacheItem;
// Look up the real event--either mouseover or mouseout
if (type == "mouseenter" || type == "mouseleave") {
sType = Event._getType(type);
}
index = Event._getCacheIndex(delegates, container, sType, fn);
if (index >= 0) {
cacheItem = delegates[index];
}
if (container && cacheItem) {
returnVal = Event.removeListener(cacheItem[0], cacheItem[1], cacheItem[3]);
if (returnVal) {
delete delegates[index][2];
delete delegates[index][3];
delegates.splice(index, 1);
}
}
return returnVal;
}
});
}());
YAHOO.register("event-delegate", YAHOO.util.Event, {version: "2.9.0", build: "2800"});
|