X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=event%2Fevent.js;h=1b3bd770f012f5e0a1c7bc0f43413ed449c20703;hb=a208222f3713a7f28499040d924d8c89b41c4350;hp=5511b351e2e4898c3d27fdf998c49b118fd384d2;hpb=ba7ebaf70be4cc91d0b4114f35e4afdf11151ae1;p=jquery.git diff --git a/event/event.js b/event/event.js index 5511b35..1b3bd77 100644 --- a/event/event.js +++ b/event/event.js @@ -1,122 +1,179 @@ -(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"]; +// We're overriding the old toggle function, so +// remember it for later +jQuery.prototype._toggle = jQuery.prototype.toggle; - 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]); - }); }; +/** + * Toggle between two function calls every other click. + */ +jQuery.prototype.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; - // 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]); - }); - }); + // Make sure that clicks stop + e.preventDefault(); + + // and execute the function + return this.last.apply( this, [e] ) || false; + }) : + + // Otherwise, execute the old toggle function + this._toggle(); }; -$.$$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] ); - } - $.$$ready = []; +/** + * Toggle between two function calls on mouse over/out. + */ +jQuery.prototype.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.fromElement || e.toElement || e.relatedTarget; + 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); }; -// 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('