]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui/build/event-mouseenter/event-mouseenter.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / yui / build / event-mouseenter / event-mouseenter.js
1 /*
2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 2.9.0
6 */
7 /**
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 
11  * element.
12  * 
13  * @module event-mouseenter
14  * @title Event Utility mouseenter and mouseout Module
15  * @namespace YAHOO.util
16  * @requires event
17  */
18
19 (function () {
20
21     var Event = YAHOO.util.Event,
22         Lang = YAHOO.lang,
23
24         addListener = Event.addListener,
25         removeListener = Event.removeListener,
26         getListeners = Event.getListeners,
27
28         delegates = [],
29         
30         specialTypes = {
31             mouseenter: "mouseover",
32             mouseleave: "mouseout"
33         }, 
34
35         remove = function(el, type, fn) {
36
37             var index = Event._getCacheIndex(delegates, el, type, fn),
38                 cacheItem,
39                 returnVal;
40
41             if (index >= 0) {
42                 cacheItem = delegates[index];
43             }
44
45             if (el && cacheItem) {
46
47                 //    removeListener will translate the value of type                
48                 returnVal = removeListener.call(Event, cacheItem[0], type, cacheItem[3]);
49         
50                 if (returnVal) {
51                     delete delegates[index][2];
52                     delete delegates[index][3];
53                     delegates.splice(index, 1);
54                 }
55         
56             }
57
58             return returnVal;
59
60         };        
61
62
63     Lang.augmentObject(Event._specialTypes, specialTypes); 
64
65     Lang.augmentObject(Event, {
66
67         /**
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.
72          *
73          * @method _createMouseDelegate
74          *
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.
85          * @private
86          * @static
87          * @for Event
88          */
89         _createMouseDelegate: function (fn, obj, overrideContext) {
90
91             return function (event, container) {
92
93                 var el = this,
94                     relatedTarget = Event.getRelatedTarget(event),
95                     context,
96                     args;
97
98                 if (el != relatedTarget && !YAHOO.util.Dom.isAncestor(el, relatedTarget)) {
99
100                     context = el;
101
102                     if (overrideContext) {
103                         if (overrideContext === true) {
104                             context = obj;
105                         } else {
106                             context = overrideContext;
107                         }
108                     }
109
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
113
114                     args = [event, obj];
115
116                     // Add the element and delegation container as arguments
117                     // when delegating mouseenter and mouseleave
118
119                     if (container) {
120                         args.splice(1, 0, el, container);
121                     }
122
123                     return fn.apply(context, args);
124
125                 }
126
127             };
128
129         },
130         
131         addListener: function (el, type, fn, obj, overrideContext) {
132
133             var fnDelegate,
134                 returnVal;
135
136             if (specialTypes[type]) {
137
138                 fnDelegate = Event._createMouseDelegate(fn, obj, overrideContext);
139                 
140                 fnDelegate.mouseDelegate = true;
141
142                 delegates.push([el, type, fn, fnDelegate]);
143
144                 //    addListener will translate the value of type
145                 returnVal = addListener.call(Event, el, type, fnDelegate);
146
147             }
148             else {
149                 returnVal = addListener.apply(Event, arguments);
150             }
151
152             return returnVal;
153
154         },
155         
156         removeListener: function (el, type, fn) {
157
158             var returnVal;
159
160             if (specialTypes[type]) {
161                 returnVal = remove.apply(Event, arguments);
162             }
163             else {
164                 returnVal = removeListener.apply(Event, arguments);
165             }
166
167             return returnVal;
168
169         },
170         
171         getListeners: function (el, type) {
172
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.
177
178             var listeners = [],
179                 elListeners,
180                 bMouseOverOrOut = (type === "mouseover" || type === "mouseout"),
181                 bMouseDelegate,
182                 i,
183                 l;
184             
185             if (type && (bMouseOverOrOut || specialTypes[type])) {
186                 
187                 elListeners = getListeners.call(Event, el, this._getType(type));
188
189                 if (elListeners) {
190
191                     for (i=elListeners.length-1; i>-1; i--) {
192
193                         l = elListeners[i];
194                         bMouseDelegate = l.fn.mouseDelegate;
195
196                         if ((specialTypes[type] && bMouseDelegate) || (bMouseOverOrOut && !bMouseDelegate)) {
197                             listeners.push(l);
198                         }
199
200                     }
201
202                 }
203                 
204             }
205             else {
206                 listeners = getListeners.apply(Event, arguments);
207             }
208
209             return (listeners && listeners.length) ? listeners : null;
210             
211         }
212         
213     }, true);
214     
215     Event.on = Event.addListener;
216
217 }());
218 YAHOO.register("event-mouseenter", YAHOO.util.Event, {version: "2.9.0", build: "2800"});