X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent.js;h=02ed088c2e5431f892935e5ed0ca8b0d07af0b4a;hb=1d7b7b94ef1c0c10cd5b2a976be40ccacbc49581;hp=0ccbf5d31e913122b456fad3612649d670d2294e;hpb=859aa6c9df064ef7ec9d8cc33af59d867fac0b9a;p=jquery.git diff --git a/src/event.js b/src/event.js index 0ccbf5d..02ed088 100644 --- a/src/event.js +++ b/src/event.js @@ -134,7 +134,7 @@ jQuery.event = { // remove generic event handler if no more handlers exist for ( ret in events[type] ) break; if ( !ret ) { - if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(this, elem) === false ) { + if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem) === false ) { if (elem.removeEventListener) elem.removeEventListener(type, jQuery.data(elem, "handle"), false); else if (elem.detachEvent) @@ -196,7 +196,7 @@ jQuery.event = { data.shift(); // Handle triggering of extra function - if ( extra ) { + if ( extra && jQuery.isFunction( extra ) ) { // call the extra function and tack the current return value on the end for possible inspection var ret = extra.apply( elem, data.concat( val ) ); // if anything is returned, give it precedence and have it overwrite the previous value @@ -323,96 +323,55 @@ jQuery.event = { special: { ready: { setup: function() { - var handler = jQuery.event.special.ready.handler; - - // Mozilla, Opera and webkit nightlies currently support this event - if ( document.addEventListener ) - // Use the handy event callback - document.addEventListener( "DOMContentLoaded", handler, false ); - - // If Safari or IE is used - // Continually check to see if the document is ready - if ((jQuery.browser.msie && window == top) || jQuery.browser.safari ) (function(){ - try { - // If IE is used, use the trick by Diego Perini - // http://javascript.nwbox.com/IEContentLoaded/ - if ( jQuery.browser.msie || document.readyState != "loaded" && document.readyState != "complete" ) - document.documentElement.doScroll("left"); - } catch( error ) { - setTimeout( arguments.callee, 0 ); - return; - } - - // and execute any waiting functions - handler(); - })(); - - // A fallback to window.onload, that will always work - jQuery.event.add( window, "load", handler ); + // Make sure the ready event is setup + bindReady(); + return; }, - teardown: function() {return;}, - - handler: function() { - // Make sure that the DOM is not already loaded - if ( !jQuery.isReady ) { - // Remember that the DOM is ready - jQuery.isReady = true; - jQuery(document).triggerHandler("ready"); - jQuery(document).unbind("ready"); - } - } + teardown: function() { return; } }, mouseenter: { setup: function() { - if (jQuery.browser.msie) return false; - jQuery(this).bind('mouseover', jQuery.event.special.mouseenter.handler); + if ( jQuery.browser.msie ) return false; + jQuery(this).bind("mouseover", jQuery.event.special.mouseenter.handler); return true; }, teardown: function() { - if (jQuery.browser.msie) return false; - jQuery(this).unbind('mouseover', jQuery.event.special.mouseenter.handler); + if ( jQuery.browser.msie ) return false; + jQuery(this).unbind("mouseover", jQuery.event.special.mouseenter.handler); return true; }, handler: function(event) { - var args = Array.prototype.slice.call( arguments, 1 ); // If we actually just moused on to a sub-element, ignore it if ( withinElement(event, this) ) return true; // Execute the right handlers by setting the event type to mouseenter - event.type = 'mouseenter'; - // Include the event object as the first argument - args.unshift(event); - var val = jQuery.event.handle.apply(this, args); - return val; + arguments[0].type = "mouseenter"; + return jQuery.event.handle.apply(this, arguments); } }, mouseleave: { setup: function() { - if (jQuery.browser.msie) return false; - jQuery(this).bind('mouseout', jQuery.event.special.mouseleave.handler); + if ( jQuery.browser.msie ) return false; + jQuery(this).bind("mouseout", jQuery.event.special.mouseleave.handler); return true; }, teardown: function() { - if (jQuery.browser.msie) return false; - jQuery(this).unbind('mouseout', jQuery.event.special.mouseleave.handler); + if ( jQuery.browser.msie ) return false; + jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.handler); return true; }, handler: function(event) { - var args = Array.prototype.slice.call( arguments, 1 ); // If we actually just moused on to a sub-element, ignore it if ( withinElement(event, this) ) return true; // Execute the right handlers by setting the event type to mouseleave - event.type = 'mouseleave'; - // Include the event object as the first argument - args.unshift(event); - var val = jQuery.event.handle.apply(this, args); - return val; + arguments[0].type = "mouseleave"; + return jQuery.event.handle.apply(this, arguments); } } } @@ -470,14 +429,85 @@ jQuery.fn.extend({ hover: function(fnOver, fnOut) { return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut); + }, + + ready: function(fn) { + // Attach the listeners + bindReady(); + + // If the DOM is already ready + if ( jQuery.isReady ) + // Execute the function immediately + fn.call( document, jQuery ); + + // Otherwise, remember the function for later + else + // Add the function to the wait list + jQuery.readyList.push( function() { return fn.call(this, jQuery); } ); + + return this; } }); jQuery.extend({ - isReady: false + isReady: false, + readyList: [], + // Handle when the DOM is ready + ready: function() { + // Make sure that the DOM is not already loaded + if ( !jQuery.isReady ) { + // Remember that the DOM is ready + jQuery.isReady = true; + + // If there are functions bound, to execute + if ( jQuery.readyList ) { + // Execute all of them + jQuery.each( jQuery.readyList, function(){ + this.apply( document ); + }); + + // Reset the list of functions + jQuery.readyList = null; + } + + // Trigger any bound ready events + $(document).triggerHandler("ready"); + } + } }); -jQuery.each( ("blur,focus,load,ready,resize,scroll,unload,click,dblclick," + +var readyBound = false; + +function bindReady(){ + if ( readyBound ) return; + readyBound = true; + + // Mozilla, Opera and webkit nightlies currently support this event + if ( document.addEventListener ) + // Use the handy event callback + document.addEventListener( "DOMContentLoaded", jQuery.ready, false ); + + // If Safari or IE is used + // Continually check to see if the document is ready + if (jQuery.browser.msie || jQuery.browser.safari ) (function(){ + try { + // If IE is used, use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + if ( jQuery.browser.msie || document.readyState != "loaded" && document.readyState != "complete" ) + document.documentElement.doScroll("left"); + } catch( error ) { + return setTimeout( arguments.callee, 0 ); + } + + // and execute any waiting functions + jQuery.ready(); + })(); + + // A fallback to window.onload, that will always work + jQuery.event.add( window, "load", jQuery.ready ); +} + +jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + "submit,keydown,keypress,keyup,error").split(","), function(i, name){