Moved .ready() to core.js from event.js.
[jquery.git] / src / event.js
index 3e673d6..d59ef64 100644 (file)
@@ -126,7 +126,7 @@ jQuery.event = {
                                        types = types.type;
                                }
 
-                               // Handle multiple events seperated by a space
+                               // Handle multiple events separated by a space
                                // jQuery(...).unbind("mouseover mouseout", fn);
                                types = types.split(/\s+/);
                                var i = 0;
@@ -135,7 +135,7 @@ jQuery.event = {
                                        var namespaces = type.split(".");
                                        type = namespaces.shift();
                                        var all = !namespaces.length,
-                                               cleaned = jQuery.map( namespaces.slice(0).sort() , function(nm){ return nm.replace(/[^\w\s\.\|`]/g, function(ch){return "\\"+ch  }) }),
+                                               cleaned = jQuery.map( namespaces.slice(0).sort() , function(nm){ return nm.replace(/[^\w\s\.\|`]/g, function(ch){return "\\"+ch;  }); }),
                                                namespace = new RegExp("(^|\\.)" + cleaned.join("\\.(?:.*\\.)?") + "(\\.|$)"),
                                                special = this.special[ type ] || {};
 
@@ -327,6 +327,8 @@ jQuery.event = {
 
                        }
                }
+
+               return event.result;
        },
 
        props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
@@ -403,7 +405,7 @@ jQuery.event = {
        special: {
                ready: {
                        // Make sure the ready event is setup
-                       setup: bindReady,
+                       setup: jQuery.bindReady,
                        teardown: function() {}
                },
 
@@ -432,6 +434,21 @@ jQuery.event = {
                                }
                        },
                        special: {}
+               },
+               beforeunload: {
+                       setup: function( data, namespaces, fn ) {
+                               // We only want to do this special case on windows
+                               if ( this.setInterval ) {
+                                       this.onbeforeunload = fn;
+                               }
+
+                               return false;
+                       },
+                       teardown: function( namespaces, fn ) {
+                               if ( this.onbeforeunload === fn ) {
+                                       this.onbeforeunload = null;
+                               }
+                       }
                }
        }
 };
@@ -538,23 +555,25 @@ delegate = function( event ) {
 
 // Create mouseenter and mouseleave events
 jQuery.each({
-       mouseover: "mouseenter",
-       mouseout: "mouseleave"
+       mouseenter: "mouseover",
+       mouseleave: "mouseout"
 }, function( orig, fix ) {
-       jQuery.event.special[ fix ] = {
+       jQuery.event.special[ orig ] = {
                setup: function(data){
-                       jQuery.event.add( this, orig, data && data.selector ? delegate : withinElement, fix );
+                       jQuery.event.add( this, fix, data && data.selector ? delegate : withinElement, orig );
                },
                teardown: function(data){
-                       jQuery.event.remove( this, orig, data && data.selector ? delegate : withinElement );
+                       jQuery.event.remove( this, fix, data && data.selector ? delegate : withinElement );
                }
        };
 });
 
 // submit delegation
+if ( !jQuery.support.submitBubbles ) {
+
 jQuery.event.special.submit = {
        setup: function( data, namespaces, fn ) {
-               if ( !jQuery.support.submitBubbles && this.nodeName.toLowerCase() !== "form" ) {
+               if ( this.nodeName.toLowerCase() !== "form" ) {
                        jQuery.event.add(this, "click.specialSubmit." + fn.guid, function( e ) {
                                var elem = e.target, type = elem.type;
 
@@ -571,8 +590,6 @@ jQuery.event.special.submit = {
                                }
                        });
                }
-
-               return false;
        },
 
        remove: function( namespaces, fn ) {
@@ -581,7 +598,11 @@ jQuery.event.special.submit = {
        }
 };
 
+}
+
 // change delegation, happens here so we have bind.
+if ( !jQuery.support.changeBubbles ) {
+
 jQuery.event.special.change = {
        filters: {
                click: function( e ) { 
@@ -630,63 +651,50 @@ jQuery.event.special.change = {
                }
        },
        setup: function( data, namespaces, fn ) {
-               // return false if we bubble
-               if ( !jQuery.support.changeBubbles ) {
-                       for ( var type in changeFilters ) {
-                               jQuery.event.add( this, type + ".specialChange." + fn.guid, changeFilters[type] );
-                       }
+               for ( var type in changeFilters ) {
+                       jQuery.event.add( this, type + ".specialChange." + fn.guid, changeFilters[type] );
                }
                
                // always want to listen for change for trigger
                return false;
        },
        remove: function( namespaces, fn ) {
-               if ( !jQuery.support.changeBubbles ) {
-                       for ( var type in changeFilters ) {
-                               jQuery.event.remove( this, type + ".specialChange" + (fn ? "."+fn.guid : ""), changeFilters[type] );
-                       }
+               for ( var type in changeFilters ) {
+                       jQuery.event.remove( this, type + ".specialChange" + (fn ? "."+fn.guid : ""), changeFilters[type] );
                }
        }
 };
 
 var changeFilters = jQuery.event.special.change.filters;
 
+}
+
 function trigger( type, elem, args ) {
        args[0].type = type;
        return jQuery.event.handle.apply( elem, args );
 }
 
 // Create "bubbling" focus and blur events
-jQuery.each({
-       focus: "focusin",
-       blur: "focusout"
-}, function( orig, fix ){
-       var event = jQuery.event,
-               handle = event.handle;
-       
-       function ieHandler() { 
-               arguments[0].type = orig;
-               return handle.apply(this, arguments);
-       }
+if ( !jQuery.support.focusBubbles ) {
 
-       event.special[orig] = {
-               setup:function() {
-                       if ( this.addEventListener ) {
-                               this.addEventListener( orig, handle, true );
-                       } else {
-                               event.add( this, fix, ieHandler );
-                       }
+jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){
+       event.special[ orig ] = {
+               setup: function() {
+                       jQuery.event.add( this, fix, ieHandler );
                }, 
-               teardown:function() { 
-                       if ( this.removeEventListener ) {
-                               this.removeEventListener( orig, handle, true );
-                       } else {
-                               event.remove( this, fix, ieHandler );
-                       }
+               teardown: function() { 
+                       jQuery.event.remove( this, fix, ieHandler );
                }
        };
+
+       function ieHandler() { 
+               arguments[0].type = orig;
+               return jQuery.event.handle.apply(this, arguments);
+       }
 });
 
+}
+
 jQuery.fn.extend({
        // TODO: make bind(), unbind() and one() DRY!
        bind: function( type, data, fn, thisObject ) {
@@ -789,24 +797,6 @@ jQuery.fn.extend({
                return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
        },
 
-       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( fn );
-               }
-
-               return this;
-       },
-
        live: function( type, data, fn, thisObject ) {
                if ( jQuery.isFunction( data ) ) {
                        if ( fn !== undefined ) {
@@ -887,103 +877,6 @@ function liveConvert( type, selector ) {
                                                                  .replace(/ /g, "|")].join(".");
 }
 
-jQuery.extend({
-       isReady: false,
-       readyList: [],
-       // Handle when the DOM is ready
-       ready: function() {
-               // Make sure that the DOM is not already loaded
-               if ( !jQuery.isReady ) {
-                       if ( !document.body ) {
-                               return setTimeout( jQuery.ready, 13 );
-                       }
-
-                       // Remember that the DOM is ready
-                       jQuery.isReady = true;
-
-                       // If there are functions bound, to execute
-                       if ( jQuery.readyList ) {
-                               // Execute all of them
-                               var fn, i = 0;
-                               while ( (fn = jQuery.readyList[ i++ ]) ) {
-                                       fn.call( document, jQuery );
-                               }
-
-                               // Reset the list of functions
-                               jQuery.readyList = null;
-                       }
-
-                       // Trigger any bound ready events
-                       jQuery( document ).triggerHandler( "ready" );
-               }
-       }
-});
-
-var readyBound = false;
-
-function bindReady() {
-       if ( readyBound ) { return; }
-       readyBound = true;
-
-       // Catch cases where $(document).ready() is called after the
-       // browser event has already occurred.
-       if ( document.readyState === "complete" ) {
-               return jQuery.ready();
-       }
-
-       // Mozilla, Opera and webkit nightlies currently support this event
-       if ( document.addEventListener ) {
-               // Use the handy event callback
-               document.addEventListener( "DOMContentLoaded", function() {
-                       document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
-                       jQuery.ready();
-               }, false );
-
-       // If IE event model is used
-       } else if ( document.attachEvent ) {
-               // ensure firing before onload,
-               // maybe late but safe also for iframes
-               document.attachEvent("onreadystatechange", function() {
-                       // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
-                       if ( document.readyState === "complete" ) {
-                               document.detachEvent( "onreadystatechange", arguments.callee );
-                               jQuery.ready();
-                       }
-               });
-
-               // If IE and not a frame
-               // continually check to see if the document is ready
-               var toplevel = false;
-
-               try {
-                       toplevel = window.frameElement == null;
-               } catch(e){}
-
-               if ( document.documentElement.doScroll && toplevel ) {
-                       (function() {
-                               if ( jQuery.isReady ) {
-                                       return;
-                               }
-
-                               try {
-                                       // If IE is used, use the trick by Diego Perini
-                                       // http://javascript.nwbox.com/IEContentLoaded/
-                                       document.documentElement.doScroll("left");
-                               } catch( error ) {
-                                       setTimeout( arguments.callee, 0 );
-                                       return;
-                               }
-
-                               // 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 mouseenter mouseleave " +
        "change select submit keydown keypress keyup error").split(" "), function( i, name ) {