]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/jquery/jquery.hoverIntent.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / jquery / jquery.hoverIntent.js
1 /*******************************************************************************
2  jquery.mb.components
3  Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
4  email: info@pupunzi.com
5  site: http://pupunzi.com
6
7  Licences: MIT, GPL
8  http://www.opensource.org/licenses/mit-license.php
9  http://www.gnu.org/licenses/gpl.html
10  ******************************************************************************/
11
12 /**
13 * hoverIntent is similar to jQuery's built-in "hover" function except that
14 * instead of firing the onMouseOver event immediately, hoverIntent checks
15 * to see if the user's mouse has slowed down (beneath the sensitivity
16 * threshold) before firing the onMouseOver event.
17
18 * hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
19 * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
20
21 * hoverIntent is currently available for use in all personal or commercial 
22 * projects under both MIT and GPL licenses. This means that you can choose 
23 * the license that best suits your project, and use it accordingly.
24
25 * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
26 * $("ul li").hoverIntent( showNav , hideNav );
27
28 * // advanced usage receives configuration object only
29 * $("ul li").hoverIntent({
30 *       sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
31 *       interval: 100,   // number = milliseconds of polling interval
32 *       over: showNav,  // function = onMouseOver callback (required)
33 *       timeout: 0,   // number = milliseconds delay before onMouseOut function call
34 *       out: hideNav    // function = onMouseOut callback (required)
35 * });
36
37 * @param  f  onMouseOver function || An object with configuration options
38 * @param  g  onMouseOut function  || Nothing (use configuration options object)
39 * @author    Brian Cherne <brian@cherne.net>
40 */
41 (function($) {
42         $.fn.hoverIntent = function(f,g) {
43                 // default configuration options
44                 var cfg = {
45                         sensitivity: 7,
46                         interval: 100,
47                         timeout: 0
48                 };
49                 // override configuration options with user supplied object
50                 cfg = $.extend(cfg, g ? { over: f, out: g } : f );
51
52                 // instantiate variables
53                 // cX, cY = current X and Y position of mouse, updated by mousemove event
54                 // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
55                 var cX, cY, pX, pY;
56
57                 // A private function for getting mouse position
58                 var track = function(ev) {
59                         cX = ev.pageX;
60                         cY = ev.pageY;
61                 };
62
63                 // A private function for comparing current and previous mouse position
64                 var compare = function(ev,ob) {
65                         ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
66                         // compare mouse positions to see if they've crossed the threshold
67                         if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
68                                 $(ob).unbind("mousemove",track);
69                                 // set hoverIntent state to true (so mouseOut can be called)
70                                 ob.hoverIntent_s = 1;
71                                 return cfg.over.apply(ob,[ev]);
72                         } else {
73                                 // set previous coordinates for next time
74                                 pX = cX; pY = cY;
75                                 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
76                                 ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
77                         }
78                 };
79
80                 // A private function for delaying the mouseOut function
81                 var delay = function(ev,ob) {
82                         ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
83                         ob.hoverIntent_s = 0;
84                         return cfg.out.apply(ob,[ev]);
85                 };
86
87                 // A private function for handling mouse 'hovering'
88                 var handleHover = function(e) {
89                         // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
90                         var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
91                         while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
92                         if ( p == this ) { return false; }
93
94                         // copy objects to be passed into t (required for event object to be passed in IE)
95                         var ev = jQuery.extend({},e);
96                         var ob = this;
97
98                         // cancel hoverIntent timer if it exists
99                         if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
100
101                         // else e.type == "onmouseover"
102                         if (e.type == "mouseover") {
103                                 // set "previous" X and Y position based on initial entry point
104                                 pX = ev.pageX; pY = ev.pageY;
105                                 // update "current" X and Y position based on mousemove
106                                 $(ob).bind("mousemove",track);
107                                 // start polling interval (self-calling timeout) to compare mouse coordinates over time
108                                 if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
109
110                         // else e.type == "onmouseout"
111                         } else {
112                                 // unbind expensive mousemove event
113                                 $(ob).unbind("mousemove",track);
114                                 // if hoverIntent state is true, then call the mouseOut function after the specified delay
115                                 if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
116                         }
117                 };
118
119                 // bind the function to the two event listeners
120                 return this.mouseover(handleHover).mouseout(handleHover);
121         };
122 })(jQuery);