Added support for multiple-namespaced events (in bind, trigger, and unbind).
[jquery.git] / src / event.js
index 324024f..7bbca2a 100644 (file)
@@ -21,7 +21,7 @@ jQuery.event = {
                        handler.guid = this.guid++;
 
                // if data is passed, bind to handler
                        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;
 
                        // 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
                        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
                                        return jQuery.event.handle.apply(arguments.callee.elem, arguments);
                        });
                // Add elem as a property of the handle function
@@ -53,8 +53,8 @@ jQuery.event = {
                jQuery.each(types.split(/\s+/), function(index, type) {
                        // Namespaced event handlers
                        var parts = type.split(".");
                jQuery.each(types.split(/\s+/), function(index, type) {
                        // Namespaced event handlers
                        var parts = type.split(".");
-                       type = parts[0];
-                       handler.type = parts[1];
+                       type = parts.shift();
+                       handler.type = parts.sort().join(".");
 
                        // Get the current list of functions bound to this event
                        var handlers = events[type];
 
                        // Get the current list of functions bound to this event
                        var handlers = events[type];
@@ -99,7 +99,7 @@ jQuery.event = {
 
                if ( events ) {
                        // Unbind all events for the element
 
                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 {
                                for ( var type in events )
                                        this.remove( elem, type + (types || "") );
                        else {
@@ -113,8 +113,9 @@ jQuery.event = {
                                // jQuery(...).unbind("mouseover mouseout", fn);
                                jQuery.each(types.split(/\s+/), function(index, type){
                                        // Namespaced event handlers
                                // jQuery(...).unbind("mouseover mouseout", fn);
                                jQuery.each(types.split(/\s+/), function(index, type){
                                        // Namespaced event handlers
-                                       var parts = type.split(".");
-                                       type = parts[0];
+                                       var namespace = type.split(".");
+                                       type = namespace.shift();
+                                       namespace = RegExp(namespace.sort().join(".*\\.") + "(\\.|$)");
 
                                        if ( events[type] ) {
                                                // remove the given handler for the given type
 
                                        if ( events[type] ) {
                                                // remove the given handler for the given type
@@ -125,7 +126,7 @@ jQuery.event = {
                                                else
                                                        for ( handler in events[type] )
                                                                // Handle the removal of namespaced events
                                                else
                                                        for ( handler in events[type] )
                                                                // Handle the removal of namespaced events
-                                                               if ( !parts[1] || events[type][handler].type == parts[1] )
+                                                               if ( namespace.test(events[type][handler].type) )
                                                                        delete events[type][handler];
 
                                                // remove generic event handler if no more handlers exist
                                                                        delete events[type][handler];
 
                                                // remove generic event handler if no more handlers exist
@@ -190,6 +191,7 @@ jQuery.event = {
                                        target: elem,
                                        preventDefault: function(){},
                                        stopPropagation: function(){},
                                        target: elem,
                                        preventDefault: function(){},
                                        stopPropagation: function(){},
+                                       stopImmediatePropagation:stopImmediatePropagation,
                                        timeStamp: now()
                                });
                                data[0][expando] = true; // no need to fix fake event
                                        timeStamp: now()
                                });
                                data[0][expando] = true; // no need to fix fake event
@@ -218,7 +220,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
                                // 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;
                        }
 
                                        val = ret;
                        }
 
@@ -245,8 +247,8 @@ jQuery.event = {
 
                // Namespaced event handlers
                namespace = event.type.split(".");
 
                // Namespaced event handlers
                namespace = event.type.split(".");
-               event.type = namespace[0];
-               namespace = namespace[1];
+               event.type = namespace.shift();
+               namespace = RegExp(namespace.sort().join(".*\\.") + "(\\.|$)");
                // Cache this now, all = true means, any handler
                all = !namespace && !event.exclusive;
 
                // Cache this now, all = true means, any handler
                all = !namespace && !event.exclusive;
 
@@ -256,7 +258,7 @@ jQuery.event = {
                        var handler = handlers[j];
 
                        // Filter the functions by class
                        var handler = handlers[j];
 
                        // Filter the functions by class
-                       if ( all || handler.type == namespace ) {
+                       if ( all || namespace.test(handler.type) ) {
                                // Pass in a reference to the handler function itself
                                // So that we can later remove it
                                event.handler = handler;
                                // Pass in a reference to the handler function itself
                                // So that we can later remove it
                                event.handler = handler;
@@ -271,6 +273,10 @@ jQuery.event = {
                                        event.preventDefault();
                                        event.stopPropagation();
                                }
                                        event.preventDefault();
                                        event.stopPropagation();
                                }
+
+                               if( event._sip )
+                                       break;
+
                        }
                }
 
                        }
                }
 
@@ -280,7 +286,7 @@ jQuery.event = {
        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) {
        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
                        return event;
 
                // store a copy of the original event object
@@ -313,6 +319,8 @@ jQuery.event = {
                        originalEvent.cancelBubble = true;
                };
 
                        originalEvent.cancelBubble = true;
                };
 
+               event.stopImmediatePropagation = stopImmediatePropagation;
+
                // Fix timeStamp
                event.timeStamp = event.timeStamp || now();
 
                // Fix timeStamp
                event.timeStamp = event.timeStamp || now();
 
@@ -367,6 +375,11 @@ jQuery.event = {
        }
 };
 
        }
 };
 
+function stopImmediatePropagation(){
+       this._sip = 1;
+       this.stopPropagation();
+}
+
 if ( !jQuery.browser.msie ){   
        // Checks if an event happened on an element within another element
        // Used in jQuery.event.special.mouseenter and mouseleave handlers
 if ( !jQuery.browser.msie ){   
        // Checks if an event happened on an element within another element
        // Used in jQuery.event.special.mouseenter and mouseleave handlers
@@ -455,7 +468,7 @@ jQuery.fn.extend({
        },
 
        hover: function(fnOver, fnOut) {
        },
 
        hover: function(fnOver, fnOut) {
-               return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut);
+               return this.mouseenter(fnOver).mouseleave(fnOut);
        },
 
        ready: function(fn) {
        },
 
        ready: function(fn) {
@@ -509,53 +522,39 @@ function bindReady(){
        if ( readyBound ) return;
        readyBound = true;
 
        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
                // 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();
                        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;
                        }
                                setTimeout( arguments.callee, 0 );
                                return;
                        }
+
                        // and execute any waiting functions
                        jQuery.ready();
                })();
                        // and execute any waiting functions
                        jQuery.ready();
                })();
@@ -566,8 +565,8 @@ function bindReady(){
 }
 
 jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
 }
 
 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){
 
        // Handle event binding
        jQuery.fn[name] = function(fn){