Simplifying the DOM ready code, no longer attempting to handle stylesheet loading...
[jquery.git] / src / event.js
index 2bdf9ab..5be849f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * A number of helper functions used for managing events.
- * Many of the ideas behind this code orignated from
+ * Many of the ideas behind this code originated from
  * Dean Edwards' addEvent library.
  */
 jQuery.event = {
@@ -21,7 +21,7 @@ jQuery.event = {
                        handler.guid = this.guid++;
 
                // if data is passed, bind to handler
-               if( data != undefined ) {
+               if ( data !== undefined ) {
                        // Create temporary function pointer to original handler
                        var fn = handler;
 
@@ -40,7 +40,7 @@ jQuery.event = {
                        handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
                                // Handle the second event of a trigger and when
                                // an event is called after a page has unloaded
-                               if ( typeof jQuery != "undefined" && !jQuery.event.triggered )
+                               if ( typeof jQuery !== "undefined" && !jQuery.event.triggered )
                                        return jQuery.event.handle.apply(arguments.callee.elem, arguments);
                        });
                // Add elem as a property of the handle function
@@ -66,7 +66,7 @@ jQuery.event = {
                                // Check for a special event handler
                                // Only use addEventListener/attachEvent if the special
                                // events handler returns false
-                               if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) {
+                               if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem,data) === false ) {
                                        // Bind the global event handler to the element
                                        if (elem.addEventListener)
                                                elem.addEventListener(type, handle, false);
@@ -99,7 +99,7 @@ jQuery.event = {
 
                if ( events ) {
                        // Unbind all events for the element
-                       if ( types == undefined || (typeof types == "string" && types.charAt(0) == ".") )
+                       if ( types === undefined || (typeof types === "string" && types.charAt(0) == ".") )
                                for ( var type in events )
                                        this.remove( elem, type + (types || "") );
                        else {
@@ -168,7 +168,10 @@ jQuery.event = {
                if ( !elem ) {
                        // Only trigger if we've ever bound an event for it
                        if ( this.global[type] )
-                               jQuery("*").add([window, document]).trigger(type, data);
+                               jQuery.each( jQuery.cache, function(){
+                                       if ( this.events && this.events[type] )
+                                               jQuery.event.trigger( type, data, this.handle.elem );
+                               });
 
                // Handle triggering a single element
                } else {
@@ -187,6 +190,7 @@ jQuery.event = {
                                        target: elem,
                                        preventDefault: function(){},
                                        stopPropagation: function(){},
+                                       stopImmediatePropagation:stopImmediatePropagation,
                                        timeStamp: now()
                                });
                                data[0][expando] = true; // no need to fix fake event
@@ -215,7 +219,7 @@ jQuery.event = {
                                // call the extra function and tack the current return value on the end for possible inspection
                                ret = extra.apply( elem, val == null ? data : data.concat( val ) );
                                // if anything is returned, give it precedence and have it overwrite the previous value
-                               if (ret !== undefined)
+                               if ( ret !== undefined )
                                        val = ret;
                        }
 
@@ -268,23 +272,31 @@ jQuery.event = {
                                        event.preventDefault();
                                        event.stopPropagation();
                                }
+
+                               if( event._sip )
+                                       break;
+
                        }
                }
 
                return val;
        },
 
+       props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" "),
+
        fix: function(event) {
-               if ( event[expando] == true )
+               if ( event[expando] )
                        return event;
 
                // store a copy of the original event object
                // and "clone" to set read-only properties
                var originalEvent = event;
                event = { originalEvent: originalEvent };
-               var props = "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" ");
-               for ( var i=props.length; i; i-- )
-                       event[ props[i] ] = originalEvent[ props[i] ];
+
+               for ( var i = this.props.length, prop; i; ){
+                       prop = this.props[ --i ];
+                       event[ prop ] = originalEvent[ prop ];
+               }
 
                // Mark it as fixed
                event[expando] = true;
@@ -306,6 +318,8 @@ jQuery.event = {
                        originalEvent.cancelBubble = true;
                };
 
+               event.stopImmediatePropagation = stopImmediatePropagation;
+
                // Fix timeStamp
                event.timeStamp = event.timeStamp || now();
 
@@ -353,60 +367,51 @@ jQuery.event = {
 
        special: {
                ready: {
-                       setup: function() {
-                               // Make sure the ready event is setup
-                               bindReady();
-                               return;
-                       },
-
-                       teardown: function() { return; }
-               },
-
-               mouseenter: {
-                       setup: function() {
-                               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);
-                               return true;
-                       },
-
-                       handler: function(event) {
-                               // 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";
-                               return jQuery.event.handle.apply(this, arguments);
-                       }
-               },
+                       // Make sure the ready event is setup
+                       setup: bindReady,
+                       teardown: function() {}
+               }
+       }
+};
 
-               mouseleave: {
-                       setup: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).bind("mouseout", jQuery.event.special.mouseleave.handler);
-                               return true;
-                       },
+function stopImmediatePropagation(){
+       this._sip = 1;
+       this.stopPropagation();
+}
 
-                       teardown: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.handler);
-                               return true;
+if ( !jQuery.browser.msie ){   
+       // Checks if an event happened on an element within another element
+       // Used in jQuery.event.special.mouseenter and mouseleave handlers
+       var withinElement = function(event) {
+               // Check if mouse(over|out) are still within the same parent element
+               var parent = event.relatedTarget;
+               // Traverse up the tree
+               while ( parent && parent != this )
+                       try { parent = parent.parentNode; }
+                       catch(e) { parent = this; }
+               
+               if( parent != this ){
+                       // set the correct event type
+                       event.type = event.data;
+                       // handle event if we actually just moused on to a non sub-element
+                       jQuery.event.handle.apply( this, arguments );
+               }
+       };
+       
+       jQuery.each({ 
+               mouseover: 'mouseenter', 
+               mouseout: 'mouseleave'
+       }, function( orig, fix ){
+               jQuery.event.special[ fix ] = {
+                       setup: function(){
+                               jQuery.event.add( this, orig, withinElement, fix );
                        },
-
-                       handler: function(event) {
-                               // 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";
-                               return jQuery.event.handle.apply(this, arguments);
+                       teardown: function(){
+                               jQuery.event.remove( this, orig, withinElement );
                        }
-               }
-       }
-};
+               };                         
+       });
+}
 
 jQuery.fn.extend({
        bind: function( type, data, fn ) {
@@ -462,7 +467,7 @@ jQuery.fn.extend({
        },
 
        hover: function(fnOver, fnOut) {
-               return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut);
+               return this.mouseenter(fnOver).mouseleave(fnOut);
        },
 
        ready: function(fn) {
@@ -516,53 +521,39 @@ function bindReady(){
        if ( readyBound ) return;
        readyBound = true;
 
-       // Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
-       if ( document.addEventListener && !jQuery.browser.opera)
+       // Mozilla, Opera and webkit nightlies currently support this event
+       if ( document.addEventListener ) {
                // Use the handy event callback
-               document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
-
-       // If IE is used and is not in a frame
-       // Continually check to see if the document is ready
-       if ( jQuery.browser.msie && window == top ) (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();
-       })();
-
-       if ( jQuery.browser.opera )
-               document.addEventListener( "DOMContentLoaded", function () {
-                       if (jQuery.isReady) return;
-                       for (var i = 0; i < document.styleSheets.length; i++)
-                               if (document.styleSheets[i].disabled) {
-                                       setTimeout( arguments.callee, 0 );
-                                       return;
-                               }
-                       // and execute any waiting functions
+               document.addEventListener( "DOMContentLoaded", function(){
+                       document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
                        jQuery.ready();
-               }, false);
-
-       if ( jQuery.browser.safari ) {
-               var numStyles;
-               (function(){
-                       if (jQuery.isReady) return;
-                       if ( document.readyState != "loaded" && document.readyState != "complete" ) {
-                               setTimeout( arguments.callee, 0 );
-                               return;
+               }, 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(){
+                       if ( document.readyState === "complete" ) {
+                               document.detachEvent( "onreadystatechange", arguments.callee );
+                               jQuery.ready();
                        }
-                       if ( numStyles === undefined )
-                               numStyles = jQuery("style, link[rel=stylesheet]").length;
-                       if ( document.styleSheets.length != numStyles ) {
+               });
+
+               // If IE and not an iframe
+               // continually check to see if the document is ready
+               if ( document.documentElement.doScroll && !window.frameElement ) (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();
                })();
@@ -573,8 +564,8 @@ function bindReady(){
 }
 
 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){
+       "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
+       "change,select,submit,keydown,keypress,keyup,error").split(","), function(i, name){
 
        // Handle event binding
        jQuery.fn[name] = function(fn){
@@ -582,20 +573,12 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
        };
 });
 
-// Checks if an event happened on an element within another element
-// Used in jQuery.event.special.mouseenter and mouseleave handlers
-var withinElement = function(event, elem) {
-       // Check if mouse(over|out) are still within the same parent element
-       var parent = event.relatedTarget;
-       // Traverse up the tree
-       while ( parent && parent != elem ) try { parent = parent.parentNode; } catch(error) { parent = elem; }
-       // Return true if we actually just moused on to a sub-element
-       return parent == elem;
-};
-
 // Prevent memory leaks in IE
 // And prevent errors on refresh with events like mouseover in other browsers
 // Window isn't included so as not to unbind existing unload events
-jQuery(window).bind("unload", function() {
-       jQuery("*").add(document).unbind();
-});
+jQuery( window ).bind( 'unload', function(){ 
+       for ( var id in jQuery.cache )
+               // Skip the window
+               if ( id != 1 && jQuery.cache[ id ].handle )
+                       jQuery.event.remove( jQuery.cache[ id ].handle.elem );
+});