2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * Augments the Event Utility with support for the mouseenter and mouseleave
9 * events: A mouseenter event fires the first time the mouse enters an
10 * element; a mouseleave event first the first time the mouse leaves an
13 * @module event-mouseenter
14 * @title Event Utility mouseenter and mouseout Module
15 * @namespace YAHOO.util
21 var Event = YAHOO.util.Event,
24 addListener = Event.addListener,
25 removeListener = Event.removeListener,
26 getListeners = Event.getListeners,
31 mouseenter: "mouseover",
32 mouseleave: "mouseout"
35 remove = function(el, type, fn) {
37 var index = Event._getCacheIndex(delegates, el, type, fn),
42 cacheItem = delegates[index];
45 if (el && cacheItem) {
47 // removeListener will translate the value of type
48 returnVal = removeListener.call(Event, cacheItem[0], type, cacheItem[3]);
51 delete delegates[index][2];
52 delete delegates[index][3];
53 delegates.splice(index, 1);
63 Lang.augmentObject(Event._specialTypes, specialTypes);
65 Lang.augmentObject(Event, {
68 * Creates a delegate function used to call mouseover and mouseleave
69 * event listeners specified via the
70 * <code>YAHOO.util.Event.addListener</code>
71 * or <code>YAHOO.util.Event.on</code> method.
73 * @method _createMouseDelegate
75 * @param {Function} fn The method (event listener) to call
76 * @param {Object} obj An arbitrary object that will be
77 * passed as a parameter to the listener
78 * @param {Boolean|object} overrideContext If true, the value of the
79 * obj parameter becomes the execution context
80 * of the listener. If an object, this object
81 * becomes the execution context.
82 * @return {Function} Function that will call the event listener
83 * specified by either the <code>YAHOO.util.Event.addListener</code>
84 * or <code>YAHOO.util.Event.on</code> method.
89 _createMouseDelegate: function (fn, obj, overrideContext) {
91 return function (event, container) {
94 relatedTarget = Event.getRelatedTarget(event),
98 if (el != relatedTarget && !YAHOO.util.Dom.isAncestor(el, relatedTarget)) {
102 if (overrideContext) {
103 if (overrideContext === true) {
106 context = overrideContext;
110 // The default args passed back to a mouseenter or
111 // mouseleave listener are: the event, the element
112 // to which the listener is bound, and any object the
113 // user passed when subscribing
115 args = [event, el, obj];
117 // Add the delegation container as an argument when
118 // delegating mouseenter and mouseleave
121 args.splice(2, 0, container);
124 return fn.apply(context, args);
132 addListener: function (el, type, fn, obj, overrideContext) {
137 if (specialTypes[type]) {
139 fnDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
141 fnDelegate.mouseDelegate = true;
143 delegates.push([el, type, fn, fnDelegate]);
145 // addListener will translate the value of type
146 returnVal = addListener.call(Event, el, type, fnDelegate);
150 returnVal = addListener.apply(Event, arguments);
157 removeListener: function (el, type, fn) {
161 if (specialTypes[type]) {
162 returnVal = remove.apply(Event, arguments);
165 returnVal = removeListener.apply(Event, arguments);
172 getListeners: function (el, type) {
174 // If the user specified the type as mouseover or mouseout,
175 // need to filter out those used by mouseenter and mouseleave.
176 // If the user specified the type as mouseenter or mouseleave,
177 // need to filter out the true mouseover and mouseout listeners.
181 bMouseOverOrOut = (type === "mouseover" || type === "mouseout"),
186 if (type && (bMouseOverOrOut || specialTypes[type])) {
188 elListeners = getListeners.call(Event, el, this._getType(type));
192 for (i=elListeners.length-1; i>-1; i--) {
195 bMouseDelegate = l.fn.mouseDelegate;
197 if ((specialTypes[type] && bMouseDelegate) || (bMouseOverOrOut && !bMouseDelegate)) {
207 listeners = getListeners.apply(Event, arguments);
210 return (listeners && listeners.length) ? listeners : null;
216 Event.on = Event.addListener;
219 YAHOO.register("event-mouseenter", YAHOO.util.Event, {version: "2.8.0r4", build: "2449"});