r79026 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79025‎ | r79026 | r79027 >
Date:20:41, 26 December 2010
Author:krinkle
Status:ok
Tags:
Comment:
adding hoverIntent plugin
Modified paths:
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.hoverIntent.js (added) (history)

Diff [purge]

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

Status & tagging log