2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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, and any object
112 // the user passed when subscribing
116 // Add the element and delegation container as arguments
117 // when delegating mouseenter and mouseleave
120 args.splice(1, 0, el, container);
123 return fn.apply(context, args);
131 addListener: function (el, type, fn, obj, overrideContext) {
136 if (specialTypes[type]) {
138 fnDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
140 fnDelegate.mouseDelegate = true;
142 delegates.push([el, type, fn, fnDelegate]);
144 // addListener will translate the value of type
145 returnVal = addListener.call(Event, el, type, fnDelegate);
149 returnVal = addListener.apply(Event, arguments);
156 removeListener: function (el, type, fn) {
160 if (specialTypes[type]) {
161 returnVal = remove.apply(Event, arguments);
164 returnVal = removeListener.apply(Event, arguments);
171 getListeners: function (el, type) {
173 // If the user specified the type as mouseover or mouseout,
174 // need to filter out those used by mouseenter and mouseleave.
175 // If the user specified the type as mouseenter or mouseleave,
176 // need to filter out the true mouseover and mouseout listeners.
180 bMouseOverOrOut = (type === "mouseover" || type === "mouseout"),
185 if (type && (bMouseOverOrOut || specialTypes[type])) {
187 elListeners = getListeners.call(Event, el, this._getType(type));
191 for (i=elListeners.length-1; i>-1; i--) {
194 bMouseDelegate = l.fn.mouseDelegate;
196 if ((specialTypes[type] && bMouseDelegate) || (bMouseOverOrOut && !bMouseDelegate)) {
206 listeners = getListeners.apply(Event, arguments);
209 return (listeners && listeners.length) ? listeners : null;
215 Event.on = Event.addListener;
218 YAHOO.register("event-mouseenter", YAHOO.util.Event, {version: "2.9.0", build: "2800"});