X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=event%2Fevent.js;h=553c568b3d402df80720f8095c062eb9b7898fb0;hb=9e37da96d12c0f35c766a4724c5d565d0a50ab42;hp=4bef51404d61a7f4a961e3a045609796cdddb953;hpb=0fd3648d45be72bcfe76b95715b38dd5904b47da;p=jquery.git diff --git a/event/event.js b/event/event.js index 4bef514..553c568 100644 --- a/event/event.js +++ b/event/event.js @@ -1,149 +1,154 @@ -// We're overriding the old toggle function, so -// remember it for later -jQuery.prototype._toggle = jQuery.prototype.toggle; +jQuery.fn.extend({ -/** - * 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; - - // Make sure that clicks don't pass through - e.preventDefault(); - - // and execute the function - return jQuery.apply( this, this.last, [e] ) || false; - }) : + // We're overriding the old toggle function, so + // remember it for later + _toggle: jQuery.fn.toggle, - // Otherwise, execute the old toggle function - this._toggle(); -}; - -/** - * 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; + /** + * Toggle between two function calls every other 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; + }) : - // If we actually just moused on to a sub-element, ignore it - if ( p == this ) return false; + // Otherwise, execute the old toggle function + this._toggle(); + }, + + /** + * Toggle between two function calls on mouse over/out. + */ + hover: function(f,g) { - // Execute the right function - return (e.type == "mouseover" ? f : g).apply(this,[e]); - } + // 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; - // Bind the function to the two event listeners - return this.mouseover(handleHover).mouseout(handleHover); -}; - -/** - * Bind a function to fire when the DOM is ready. - */ -jQuery.prototype.ready = function(f) { - // If the DOM is already ready - if ( jQuery.isReady ) - // Execute the function immediately - jQuery.apply( document, f ); + // 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]); + } - // Otherwise, remember the function for later - else { - // Add the function to the wait list - jQuery.readyList.push( f ); + // Bind the function to the two event listeners + return this.mouseover(handleHover).mouseout(handleHover); + }, + + /** + * Bind a function to fire 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 ); + } + + return this; } +}); - return this; -}; +jQuery.extend({ + /* + * All the code that makes DOM Ready work nicely. + */ + 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 + for ( var i = 0; i < jQuery.readyList.length; i++ ) + jQuery.readyList[i].apply( document ); + + // Reset the list of functions + jQuery.readyList = null; + } + } + } +}); -(function(){ +new function(){ /* * Bind a number of event-handling functions, dynamically */ - 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").split(","); + var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," + + "mousedown,mouseup,mousemove,mouseover,mouseout,change,reset,select," + + "submit,keydown,keypress,keyup,error").split(","); // Go through all the event names, but make sure that // it is enclosed properly - for ( var i = 0; i < e.length; i++ ) {(function(){ + for ( var i = 0; i < e.length; i++ ) new function(){ var o = e[i]; // Handle event binding - jQuery.prototype[o] = function(f){ return this.bind(o, f); }; + jQuery.fn[o] = function(f){ + return f ? this.bind(o, f) : this.trigger(o); + }; // Handle event unbinding - jQuery.prototype["un"+o] = function(f){ return this.unbind(o, f); }; - - // Handle event triggering - jQuery.prototype["do"+o] = function(){ return this.trigger(o); }; + jQuery.fn["un"+o] = function(f){ return this.unbind(o, f); }; // Finally, handle events that only fire once - jQuery.prototype["one"+o] = function(f){ + jQuery.fn["one"+o] = function(f){ // Attach the event listener - return this.bind(o, function(e){ - // TODO: Remove the event listener, instead of this hack - - // If this function has already been executed, stop - if ( this[o+f] !== null ) - return true; - - // Otherwise, mark as having been executed - this[o+f]++; + return this.each(function(){ + + var count = 0; + + // Add the event + jQuery.event.add( this, o, function(e){ + // If this function has already been executed, stop + if ( count++ ) return; - // And execute the bound function - return jQuery.apply(this,f,[e]); + // And execute the bound function + return f.apply(this, [e]); + }); }); }; - })();} - - /* - * All the code that makes DOM Ready work nicely. - */ - - jQuery.isReady = false; - jQuery.readyList = []; - - // Handle when the DOM is ready - jQuery.ready = function() { - // Make sure that the DOM hasn't 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 - for ( var i = 0; i < jQuery.readyList.length; i++ ) - jQuery.apply( document, jQuery.readyList[i] ); - - // Reset the list of functions - jQuery.readyList = null; - } - } - }; + } // If Mozilla is used - if ( jQuery.browser == "mozilla" || jQuery.browser == "opera" ) { + if ( jQuery.browser.mozilla || jQuery.browser.opera ) { // Use the handy event callback - jQuery.event.add( document, "DOMContentLoaded", jQuery.ready ); + document.addEventListener( "DOMContentLoaded", jQuery.ready, false ); // 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 ( jQuery.browser == "msie" ) { + } else if ( jQuery.browser.msie ) { // Only works if you document.write() it document.write("<\/script>"); + "src=//:><\/script>"); // Use the defer script hack var script = document.getElementById("__ie_init"); @@ -156,7 +161,7 @@ jQuery.prototype.ready = function(f) { script = null; // If Safari is used - } else if ( jQuery.browser == "safari" ) { + } else if ( jQuery.browser.safari ) { // Continually check to see if the document.readyState is valid jQuery.safariTimer = setInterval(function(){ // loaded and complete are both valid states @@ -176,4 +181,4 @@ jQuery.prototype.ready = function(f) { // A fallback to window.onload, that will always work jQuery.event.add( window, "load", jQuery.ready ); -})(); +}