X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent.js;h=324024f951abbd96cbf6ef2a9164cd44755f795b;hb=b69dc841ff6d3a3a9bcb8ebe107f1d1cad8a634f;hp=bcfd0452cc9bc70e39cde679b9021f2d2c56d2c4;hpb=abb055bb498c4938b8663eb37c98e9f9e8fd8c9e;p=jquery.git diff --git a/src/event.js b/src/event.js index bcfd045..324024f 100644 --- a/src/event.js +++ b/src/event.js @@ -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 = { @@ -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); @@ -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 { @@ -357,61 +360,47 @@ 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); - } - }, - - mouseleave: { - setup: function() { - if ( jQuery.browser.msie ) return false; - jQuery(this).bind("mouseout", jQuery.event.special.mouseleave.handler); - return true; - }, - - teardown: function() { - if ( jQuery.browser.msie ) return false; - jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.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 mouseleave - event.type = "mouseleave"; - return jQuery.event.handle.apply(this, arguments); - } + // Make sure the ready event is setup + setup: bindReady, + teardown: function() {} } } }; +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 ); + }, + teardown: function(){ + jQuery.event.remove( this, orig, withinElement ); + } + }; + }); +} + jQuery.fn.extend({ bind: function( type, data, fn ) { return type == "unload" ? this.one(type, data, fn) : this.each(function(){ @@ -586,20 +575,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 ); +});