|
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 Y.log("DOM event listener associated with the " + sEventName + " Custom Event removed. Removing all " + sEventName + " listeners.", "info", "Event"); |
|
88 |
|
89 // Unsubscribe all listeners of the Custom Event fired |
|
90 // by this DOM event. |
|
91 Y.detachAll(sEventName); |
|
92 |
|
93 } |
|
94 |
|
95 }, domEventHandle.evt, "_delete"); |
|
96 |
|
97 |
|
98 listener = {}; |
|
99 listener.handle = domEventHandle; |
|
100 |
|
101 listeners[sEventName] = listener; |
|
102 |
|
103 } |
|
104 |
|
105 nListeners = listener.count; |
|
106 |
|
107 listener.count = nListeners ? (nListeners + 1) : 1; |
|
108 |
|
109 args[0] = sEventName; |
|
110 |
|
111 // Remove the element from the args |
|
112 args.splice(2, 1); |
|
113 |
|
114 // Subscribe to the custom event |
|
115 ceHandle = Y.on.apply(Y, args); |
|
116 |
|
117 // Hook into the detach method of the handle in order to clean up the |
|
118 // 'listeners' map and remove the associated DOM event handler |
|
119 // responsible for firing this Custom Event if all listener for this |
|
120 // event have been removed. |
|
121 |
|
122 Y.after(function () { |
|
123 |
|
124 listener.count = (listener.count - 1); |
|
125 |
|
126 if (listener.count === 0) { |
|
127 Y.log("No more listeners for the " + sEventName + " Custom Event. Removing its associated DOM event listener.", "info", "Event"); |
|
128 listener.handle.detach(); |
|
129 } |
|
130 |
|
131 }, ceHandle, "detach"); |
|
132 |
|
133 |
|
134 return ceHandle; |
|
135 |
|
136 } |
|
137 |
|
138 }; |
|
139 |
|
140 |
|
141 Event._fireMouseEnter = function (e, eventName) { |
|
142 |
|
143 var relatedTarget = e.relatedTarget, |
|
144 currentTarget = e.currentTarget; |
|
145 |
|
146 if (currentTarget !== relatedTarget && |
|
147 !currentTarget.contains(relatedTarget)) { |
|
148 |
|
149 Y.publish(eventName, { |
|
150 contextFn: function() { |
|
151 return currentTarget; |
|
152 } |
|
153 }); |
|
154 |
|
155 Y.fire(eventName, e); |
|
156 |
|
157 } |
|
158 |
|
159 }; |
|
160 |
|
161 |
|
162 /** |
|
163 * Sets up a "mouseenter" listener—a listener that is called the first time |
|
164 * the user's mouse enters the specified element(s). |
|
165 * |
|
166 * @event mouseenter |
|
167 * @param type {string} "mouseenter" |
|
168 * @param fn {function} The method the event invokes. |
|
169 * @param el {string|node} The element(s) to assign the listener to. |
|
170 * @param spec {string} Optional. String representing a selector that must |
|
171 * match the target of the event in order for the listener to be called. |
|
172 * @return {EventHandle} the detach handle |
|
173 * @for YUI |
|
174 */ |
|
175 plugins.mouseenter = eventConfig; |
|
176 |
|
177 /** |
|
178 * Sets up a "mouseleave" listener—a listener that is called the first time |
|
179 * the user's mouse leaves the specified element(s). |
|
180 * |
|
181 * @event mouseleave |
|
182 * @param type {string} "mouseleave" |
|
183 * @param fn {function} The method the event invokes. |
|
184 * @param el {string|node} The element(s) to assign the listener to. |
|
185 * @param spec {string} Optional. String representing a selector that must |
|
186 * match the target of the event in order for the listener to be called. |
|
187 * @return {EventHandle} the detach handle |
|
188 * @for YUI |
|
189 */ |
|
190 plugins.mouseleave = eventConfig; |
|
191 |
|
192 |
|
193 }, '3.0.0' ,{requires:['node-base']}); |