jquery event: closes #5250. bind(), unbind() and one() support Object Literals (needs...
[jquery.git] / src / event.js
index 9c1f287..76b1dfb 100644 (file)
@@ -57,7 +57,7 @@ jQuery.event = {
                        // Namespaced event handlers
                        var namespaces = type.split(".");
                        type = namespaces.shift();
-                       handler.type = namespaces.slice().sort().join(".");
+                       handler.type = namespaces.slice(0).sort().join(".");
 
                        // Get the current list of functions bound to this event
                        var handlers = events[ type ],
@@ -133,7 +133,7 @@ jQuery.event = {
                                        var namespaces = type.split(".");
                                        type = namespaces.shift();
                                        var all = !namespaces.length,
-                                               namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)"),
+                                               namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join(".*\\.") + "(\\.|$)"),
                                                special = this.special[ type ] || {};
 
                                        if ( events[ type ] ) {
@@ -291,7 +291,7 @@ jQuery.event = {
                // Cache this now, all = true means, any handler
                all = !namespaces.length && !event.exclusive;
 
-               var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
+               var namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join(".*\\.") + "(\\.|$)");
 
                handlers = ( jQuery.data(this, "events") || {} )[ event.type ];
 
@@ -518,9 +518,10 @@ var withinElement = function( event ) {
        }
 };
 
+// Create mouseenter and mouseleave events
 jQuery.each({
-       mouseover: 'mouseenter',
-       mouseout: 'mouseleave'
+       mouseover: "mouseenter",
+       mouseout: "mouseleave"
 }, function( orig, fix ) {
        jQuery.event.special[ fix ] = {
                setup: function(){
@@ -532,12 +533,49 @@ jQuery.each({
        };
 });
 
+// Create "bubbling" focus and blur events
+jQuery.each({
+       focus: "focusin",
+       blur: "focusout"
+}, function( orig, fix ){
+       var event = jQuery.event,
+               special = event.special,
+               handle = event.handle;
+       
+       function ieHandler() { 
+               arguments[0].type = orig;
+               return handle.apply(this, arguments);
+       }
+
+       special[orig] = {
+               setup:function() {
+                       if ( this.addEventListener )
+                               this.addEventListener( orig, handle, true );
+                       else
+                               jQuery.event.add( this, fix, ieHandler );
+               }, 
+               teardown:function() { 
+                       if ( this.removeEventListener )
+                               this.removeEventListener( orig, handle, true );
+                       else
+                               jQuery.event.remove( this, fix, ieHandler );
+               }
+       };
+});
+
 jQuery.fn.extend({
+       // TODO: make bind(), unbind() and one() DRY!
        bind: function( type, data, fn, thisObject ) {
-               if ( jQuery.isFunction( data ) ) {
-                       if ( fn !== undefined ) {
-                               thisObject = fn;
+               // Handle object literals
+               if ( typeof type === "object" ) {
+                       for ( var key in type ) {
+                               this.bind(key, data, type[key], fn);
                        }
+                       return this;
+               }
+               
+               if ( jQuery.isFunction( data ) ) {
+                       thisObject = fn;
                        fn = data;
                        data = undefined;
                }
@@ -548,10 +586,16 @@ jQuery.fn.extend({
        },
 
        one: function( type, data, fn, thisObject ) {
-               if ( jQuery.isFunction( data ) ) {
-                       if ( fn !== undefined ) {
-                               thisObject = fn;
+               // Handle object literals
+               if ( typeof type === "object" ) {
+                       for ( var key in type ) {
+                               this.one(key, data, type[key], fn);
                        }
+                       return this;
+               }
+               
+               if ( jQuery.isFunction( data ) ) {
+                       thisObject = fn;
                        fn = data;
                        data = undefined;
                }
@@ -566,6 +610,14 @@ jQuery.fn.extend({
        },
 
        unbind: function( type, fn ) {
+               // Handle object literals
+               if ( typeof type === "object" && !type.preventDefault ) {
+                       for ( var key in type ) {
+                               this.unbind(key, type[key]);
+                       }
+                       return this;
+               }
+               
                return this.each(function() {
                        jQuery.event.remove( this, type, fn );
                });
@@ -715,6 +767,12 @@ 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
@@ -770,11 +828,10 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
 });
 
 // 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
 // More info:
 //  - http://isaacschlueter.com/2006/10/msie-memory-leaks/
-//  - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
+/*@cc_on
 jQuery( window ).bind( 'unload', function() {
        for ( var id in jQuery.cache ) {
                // Skip the window
@@ -783,3 +840,4 @@ jQuery( window ).bind( 'unload', function() {
                }
        }
 });
+@*/