|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0 |
|
|
6 |
build: 1549 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('event-mouseenter', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Adds support for mouseenter/mouseleave events |
|
|
12 |
* @module event |
|
|
13 |
* @submodule event-mouseenter |
|
|
14 |
*/ |
|
|
15 |
var Event = Y.Event, |
|
|
16 |
Lang = Y.Lang, |
|
|
17 |
|
|
|
18 |
plugins = Y.Env.evt.plugins, |
|
|
19 |
|
|
|
20 |
listeners = {}, |
|
|
21 |
|
|
|
22 |
eventConfig = { |
|
|
23 |
|
|
|
24 |
on: function(type, fn, el) { |
|
|
25 |
|
|
|
26 |
var args = Y.Array(arguments, 0, true), |
|
|
27 |
element = el, |
|
|
28 |
availHandle; |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
if (Lang.isString(el)) { |
|
|
32 |
|
|
|
33 |
// Need to use Y.all because if el is a string it could be a |
|
|
34 |
// selector that returns a NodeList |
|
|
35 |
|
|
|
36 |
element = Y.all(el); |
|
|
37 |
|
|
|
38 |
if (element.size() === 0) { // Not found, check using onAvailable |
|
|
39 |
|
|
|
40 |
availHandle = Event.onAvailable(el, function() { |
|
|
41 |
|
|
|
42 |
availHandle.handle = Y.on.apply(Y, args); |
|
|
43 |
|
|
|
44 |
}, Event, true, false); |
|
|
45 |
|
|
|
46 |
return availHandle; |
|
|
47 |
|
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
var sDOMEvent = (type === "mouseenter") ? "mouseover" : "mouseout", |
|
|
54 |
|
|
|
55 |
// The name of the custom event |
|
|
56 |
sEventName = type + ":" + Y.stamp(element) + sDOMEvent, |
|
|
57 |
|
|
|
58 |
listener = listeners[sEventName], |
|
|
59 |
|
|
|
60 |
domEventHandle, |
|
|
61 |
|
|
|
62 |
ceHandle, |
|
|
63 |
|
|
|
64 |
nListeners; |
|
|
65 |
|
|
|
66 |
|
|
|
67 |
// Bind an actual DOM event listener that will call the |
|
|
68 |
// the custom event |
|
|
69 |
if (!listener) { |
|
|
70 |
|
|
|
71 |
domEventHandle = Y.on(sDOMEvent, Y.rbind(Event._fireMouseEnter, Y, sEventName), element); |
|
|
72 |
|
|
|
73 |
// Hook into the _delete method for the Custom Event wrapper of this |
|
|
74 |
// DOM Event in order to clean up the 'listeners' map and unsubscribe |
|
|
75 |
// the associated Custom Event listeners fired by this DOM event |
|
|
76 |
// listener if/when the user calls "purgeElement" OR removes all |
|
|
77 |
// listeners of the Custom Event. |
|
|
78 |
|
|
|
79 |
Y.after(function (sub) { |
|
|
80 |
|
|
|
81 |
if (domEventHandle.sub == sub) { |
|
|
82 |
|
|
|
83 |
// Delete this event from the map of known mouseenter |
|
|
84 |
// and mouseleave listeners |
|
|
85 |
delete listeners[sEventName]; |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
// Unsubscribe all listeners of the Custom Event fired |
|
|
89 |
// by this DOM event. |
|
|
90 |
Y.detachAll(sEventName); |
|
|
91 |
|
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
}, domEventHandle.evt, "_delete"); |
|
|
95 |
|
|
|
96 |
|
|
|
97 |
listener = {}; |
|
|
98 |
listener.handle = domEventHandle; |
|
|
99 |
|
|
|
100 |
listeners[sEventName] = listener; |
|
|
101 |
|
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
nListeners = listener.count; |
|
|
105 |
|
|
|
106 |
listener.count = nListeners ? (nListeners + 1) : 1; |
|
|
107 |
|
|
|
108 |
args[0] = sEventName; |
|
|
109 |
|
|
|
110 |
// Remove the element from the args |
|
|
111 |
args.splice(2, 1); |
|
|
112 |
|
|
|
113 |
// Subscribe to the custom event |
|
|
114 |
ceHandle = Y.on.apply(Y, args); |
|
|
115 |
|
|
|
116 |
// Hook into the detach method of the handle in order to clean up the |
|
|
117 |
// 'listeners' map and remove the associated DOM event handler |
|
|
118 |
// responsible for firing this Custom Event if all listener for this |
|
|
119 |
// event have been removed. |
|
|
120 |
|
|
|
121 |
Y.after(function () { |
|
|
122 |
|
|
|
123 |
listener.count = (listener.count - 1); |
|
|
124 |
|
|
|
125 |
if (listener.count === 0) { |
|
|
126 |
listener.handle.detach(); |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
}, ceHandle, "detach"); |
|
|
130 |
|
|
|
131 |
|
|
|
132 |
return ceHandle; |
|
|
133 |
|
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
}; |
|
|
137 |
|
|
|
138 |
|
|
|
139 |
Event._fireMouseEnter = function (e, eventName) { |
|
|
140 |
|
|
|
141 |
var relatedTarget = e.relatedTarget, |
|
|
142 |
currentTarget = e.currentTarget; |
|
|
143 |
|
|
|
144 |
if (currentTarget !== relatedTarget && |
|
|
145 |
!currentTarget.contains(relatedTarget)) { |
|
|
146 |
|
|
|
147 |
Y.publish(eventName, { |
|
|
148 |
contextFn: function() { |
|
|
149 |
return currentTarget; |
|
|
150 |
} |
|
|
151 |
}); |
|
|
152 |
|
|
|
153 |
Y.fire(eventName, e); |
|
|
154 |
|
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
}; |
|
|
158 |
|
|
|
159 |
|
|
|
160 |
/** |
|
|
161 |
* Sets up a "mouseenter" listener—a listener that is called the first time |
|
|
162 |
* the user's mouse enters the specified element(s). |
|
|
163 |
* |
|
|
164 |
* @event mouseenter |
|
|
165 |
* @param type {string} "mouseenter" |
|
|
166 |
* @param fn {function} The method the event invokes. |
|
|
167 |
* @param el {string|node} The element(s) to assign the listener to. |
|
|
168 |
* @param spec {string} Optional. String representing a selector that must |
|
|
169 |
* match the target of the event in order for the listener to be called. |
|
|
170 |
* @return {EventHandle} the detach handle |
|
|
171 |
* @for YUI |
|
|
172 |
*/ |
|
|
173 |
plugins.mouseenter = eventConfig; |
|
|
174 |
|
|
|
175 |
/** |
|
|
176 |
* Sets up a "mouseleave" listener—a listener that is called the first time |
|
|
177 |
* the user's mouse leaves the specified element(s). |
|
|
178 |
* |
|
|
179 |
* @event mouseleave |
|
|
180 |
* @param type {string} "mouseleave" |
|
|
181 |
* @param fn {function} The method the event invokes. |
|
|
182 |
* @param el {string|node} The element(s) to assign the listener to. |
|
|
183 |
* @param spec {string} Optional. String representing a selector that must |
|
|
184 |
* match the target of the event in order for the listener to be called. |
|
|
185 |
* @return {EventHandle} the detach handle |
|
|
186 |
* @for YUI |
|
|
187 |
*/ |
|
|
188 |
plugins.mouseleave = eventConfig; |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
}, '3.0.0' ,{requires:['node-base']}); |