1 |
efrain |
1 |
YUI.add('yui2-element-delegate', function(Y) {
|
|
|
2 |
var YAHOO = Y.YUI2;
|
|
|
3 |
/*
|
|
|
4 |
Copyright (c) 2011, Yahoo! Inc. All rights reserved.
|
|
|
5 |
Code licensed under the BSD License:
|
|
|
6 |
http://developer.yahoo.com/yui/license.html
|
|
|
7 |
version: 2.9.0
|
|
|
8 |
*/
|
|
|
9 |
/**
|
|
|
10 |
* Augments the Element Utility with a <code>delegate</code> method that
|
|
|
11 |
* facilitates easy creation of delegated event listeners. (Note: Using CSS
|
|
|
12 |
* selectors as the filtering criteria for delegated event listeners requires
|
|
|
13 |
* inclusion of the Selector Utility.)
|
|
|
14 |
*
|
|
|
15 |
* @module element-delegate
|
|
|
16 |
* @title Element Event Delegation Module
|
|
|
17 |
* @namespace YAHOO.util
|
|
|
18 |
* @requires element, event-delegate
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
(function () {
|
|
|
22 |
|
|
|
23 |
var Event = YAHOO.util.Event,
|
|
|
24 |
delegates = [],
|
|
|
25 |
specialTypes = {
|
|
|
26 |
mouseenter: true,
|
|
|
27 |
mouseleave: true
|
|
|
28 |
};
|
|
|
29 |
|
|
|
30 |
YAHOO.lang.augmentObject(YAHOO.util.Element.prototype, {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Appends a delegated event listener. Delegated event listeners
|
|
|
34 |
* receive two arguments by default: the DOM event and the element
|
|
|
35 |
* specified by the filtering function or CSS selector.
|
|
|
36 |
* (Note: Using the delegate method requires the element-delegate
|
|
|
37 |
* module. Using CSS selectors as the filtering criteria for delegated
|
|
|
38 |
* event listeners requires inclusion of the Selector Utility.)
|
|
|
39 |
* @method delegate
|
|
|
40 |
* @param {String} type The name of the event to listen for
|
|
|
41 |
* @param {Function} fn The handler to call when the event fires
|
|
|
42 |
* @param {Function|string} filter Function or CSS selector used to
|
|
|
43 |
* determine for what element(s) the event listener should be called.
|
|
|
44 |
* When a function is specified, the function should return an
|
|
|
45 |
* HTML element. Using a CSS Selector requires the inclusion of the
|
|
|
46 |
* CSS Selector Utility.
|
|
|
47 |
* @param {Any} obj A variable to pass to the handler
|
|
|
48 |
* @param {Object} scope The object to use for the scope of the handler
|
|
|
49 |
* @return {boolean} Returns true if the delegated event listener
|
|
|
50 |
* was added successfully
|
|
|
51 |
* @for Element
|
|
|
52 |
*/
|
|
|
53 |
delegate: function (type, fn, filter, obj, overrideContext) {
|
|
|
54 |
|
|
|
55 |
if (YAHOO.lang.isString(filter) && !YAHOO.util.Selector) {
|
|
|
56 |
return false;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if (!Event._createDelegate) {
|
|
|
60 |
return false;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
var sType = Event._getType(type),
|
|
|
64 |
el = this.get("element"),
|
|
|
65 |
fnDelegate,
|
|
|
66 |
fnMouseDelegate,
|
|
|
67 |
|
|
|
68 |
fnWrapper = function (e) {
|
|
|
69 |
|
|
|
70 |
return fnDelegate.call(el, e);
|
|
|
71 |
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
if (specialTypes[type]) {
|
|
|
75 |
|
|
|
76 |
if (!Event._createMouseDelegate) {
|
|
|
77 |
return false;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
fnMouseDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
|
|
|
81 |
|
|
|
82 |
fnDelegate = Event._createDelegate(function (event, matchedEl, container) {
|
|
|
83 |
|
|
|
84 |
return fnMouseDelegate.call(matchedEl, event, container);
|
|
|
85 |
|
|
|
86 |
}, filter, obj, overrideContext);
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
else {
|
|
|
90 |
fnDelegate = Event._createDelegate(fn, filter, obj, overrideContext);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
delegates.push([el, sType, fn, fnWrapper]);
|
|
|
95 |
|
|
|
96 |
return this.on(sType, fnWrapper);
|
|
|
97 |
|
|
|
98 |
},
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Remove a delegated event listener
|
|
|
103 |
* @method removeDelegate
|
|
|
104 |
* @param {String} type The name of the event to listen for
|
|
|
105 |
* @param {Function} fn The function call when the event fires
|
|
|
106 |
* @return {boolean} Returns true if the unbind was successful, false
|
|
|
107 |
* otherwise.
|
|
|
108 |
* @for Element
|
|
|
109 |
*/
|
|
|
110 |
removeDelegate: function (type, fn) {
|
|
|
111 |
|
|
|
112 |
var sType = Event._getType(type),
|
|
|
113 |
index = Event._getCacheIndex(delegates, this.get("element"), sType, fn),
|
|
|
114 |
returnVal,
|
|
|
115 |
cacheItem;
|
|
|
116 |
|
|
|
117 |
if (index >= 0) {
|
|
|
118 |
cacheItem = delegates[index];
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (cacheItem) {
|
|
|
122 |
|
|
|
123 |
returnVal = this.removeListener(cacheItem[1], cacheItem[3]);
|
|
|
124 |
|
|
|
125 |
if (returnVal) {
|
|
|
126 |
delete delegates[index][2];
|
|
|
127 |
delete delegates[index][3];
|
|
|
128 |
delegates.splice(index, 1);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
return returnVal;
|
|
|
134 |
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
});
|
|
|
138 |
|
|
|
139 |
}());
|
|
|
140 |
YAHOO.register("element-delegate", YAHOO.util.Element, {version: "2.9.0", build: "2800"});
|
|
|
141 |
|
|
|
142 |
}, '2.9.0' ,{"requires": ["yui2-yahoo", "yui2-dom", "yui2-event", "yui2-element"]});
|