X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=sidebyside;f=event%2Fevent.js;h=9094e73ac041d2400870b3f9da490ad313c2746a;hb=82e0a5a810f09b601543d3406b65ac15bd22307e;hp=5511b351e2e4898c3d27fdf998c49b118fd384d2;hpb=ba7ebaf70be4cc91d0b4114f35e4afdf11151ae1;p=jquery.git diff --git a/event/event.js b/event/event.js index 5511b35..9094e73 100644 --- a/event/event.js +++ b/event/event.js @@ -1,122 +1,1557 @@ -(function(){ - var e = ["blur","focus","contextmenu","load","resize","scroll","unload", - "click","dblclick","mousedown","mouseup","mouseenter","mouseleave", - "mousemove","mouseover","mouseout","change","reset","select","submit", - "keydown","keypress","keyup","abort","error","ready"]; - - for ( var i = 0; i < e.length; i++ ) { - (function(){ - var o = e[i]; - $.fn[o] = function(f){ return this.bind(o, f); }; - $.fn["un"+o] = function(f){ return this.unbind(o, f); }; - $.fn["do"+o] = function(){ return this.trigger(o); }; - $.fn["one"+o] = function(f){ return this.bind(o, function(e){ - if ( this[o+f] !== null ) { return true; } - this[o+f]++; - return $.apply(this,f,[e]); - }); }; +jQuery.fn.extend({ + + // We're overriding the old toggle function, so + // remember it for later + //_toggle: jQuery.fn.toggle, + + /** + * Toggle between two function calls every other click. + * Whenever a matched element is clicked, the first specified function + * is fired, when clicked again, the second is fired. All subsequent + * clicks continue to rotate through the two functions. + * + * @example $("p").toggle(function(){ + * $(this).addClass("selected"); + * },function(){ + * $(this).removeClass("selected"); + * }); + * + * @name toggle + * @type jQuery + * @param Function even The function to execute on every even click. + * @param Function odd The function to execute on every odd click. + */ + toggle: function(a,b) { + // If two functions are passed in, we're + // toggling on a click + return a && b ? this.click(function(e){ + // Figure out which function to execute + this.last = this.last == a ? b : a; + + // Make sure that clicks stop + e.preventDefault(); + + // and execute the function + return this.last.apply( this, [e] ) || false; + }) : - // Deprecated - //$.fn["on"+o] = function(f){ return this.bind(o, f); }; - })(); - } -})(); - -$.fn.hover = function(f,g) { - // Check if mouse(over|out) are still within the same parent element - return this.each(function(){ - var obj = this; - $.event.add(this, "mouseover", function(e) { - var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget ); - while ( p && p != obj ) { p = p.parentNode; } - if ( p == obj ) { return false; } - return $.apply(obj,f,[e]); - }); - $.event.add(this, "mouseout", function(e) { - var p = ( e.toElement !== null ? e.toElement : e.relatedTarget ); - while ( p && p != obj ) { p = p.parentNode; } - if ( p == obj ) { return false; } - return $.apply(obj,g,[e]); - }); - }); -}; - -$.$$isReady = false; -$.$$ready = []; - -// Handle when the DOM is ready -$.ready = function() { - $.$$isReady = true; - if ( $.$$ready ) { - for ( var i = 0; i < $.$$ready.length; i++ ) { - $.apply( document, $.$$ready[i] ); + // Otherwise, execute the old toggle function + this._toggle(); + }, + + /** + * A method for simulating hovering (moving the mouse on, and off, + * an object). This is a custom method which provides an 'in' to a + * frequent task. + * + * Whenever the mouse cursor is moved over a matched + * element, the first specified function is fired. Whenever the mouse + * moves off of the element, the second specified function fires. + * Additionally, checks are in place to see if the mouse is still within + * the specified element itself (for example, an image inside of a div), + * and if it is, it will continue to 'hover', and not move out + * (a common error in using a mouseout event handler). + * + * @example $("p").hover(function(){ + * $(this).addClass("over"); + * },function(){ + * $(this).addClass("out"); + * }); + * + * @name hover + * @type jQuery + * @param Function over The function to fire whenever the mouse is moved over a matched element. + * @param Function out The function to fire whenever the mouse is moved off of a matched element. + */ + hover: function(f,g) { + + // A private function for haandling mouse 'hovering' + function handleHover(e) { + // Check if mouse(over|out) are still within the same parent element + var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; + + // Traverse up the tree + while ( p && p != this ) p = p.parentNode; + + // If we actually just moused on to a sub-element, ignore it + if ( p == this ) return false; + + // Execute the right function + return (e.type == "mouseover" ? f : g).apply(this, [e]); + } + + // Bind the function to the two event listeners + return this.mouseover(handleHover).mouseout(handleHover); + }, + + /** + * Bind a function to be executed whenever the DOM is ready to be + * traversed and manipulated. This is probably the most important + * function included in the event module, as it can greatly improve + * the response times of your web applications. + * + * In a nutshell, this is a solid replacement for using window.onload, + * and attaching a function to that. By using this method, your bound Function + * will be called the instant the DOM is ready to be read and manipulated, + * which is exactly what 99.99% of all Javascript code needs to run. + * + * Please ensure you have no code in your onload event handler, + * otherwise $(document).ready() may not fire. + * + * @example $(document).ready(function(){ Your code here... }); + * + * @name ready + * @type jQuery + * @param Function fn The function to be executed when the DOM is ready. + */ + ready: function(f) { + // If the DOM is already ready + if ( jQuery.isReady ) + // Execute the function immediately + f.apply( document ); + + // Otherwise, remember the function for later + else { + // Add the function to the wait list + jQuery.readyList.push( f ); } - $.$$ready = []; + + return this; } -}; - -// If Mozilla is used -if ( $.browser == "mozilla" ) { - // Use the handy event callback - document.addEventListener( "DOMContentLoaded", $.ready, null ); - -// If IE is used, use the excellent hack by Matthias Miller -// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited -} else if ( $.browser == "msie" ) { - - // Only works if you document.write() it - document.write('