X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent.js;h=1aafcfa28a4b02aa7fab26cab3b84aa8c08d3179;hb=811891785f5bfa3f42ec9cb18e68fc3a114935e9;hp=ebdcc59dba1ab803b2f0e69f7185d26aa385fff0;hpb=cbda6c541b9f2dd8fbaa084ecce7f421e8dc3dc4;p=jquery.git diff --git a/src/event.js b/src/event.js index ebdcc59..1aafcfa 100644 --- a/src/event.js +++ b/src/event.js @@ -374,8 +374,13 @@ jQuery.event = { return event; }, - proxy: function( fn, proxy ) { - proxy = proxy || function() { return fn.apply( this, arguments ); }; + proxy: function( fn, proxy, thisObject ) { + if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) { + thisObject = proxy; + proxy = undefined; + } + // FIXME: Should proxy be redefined to be applied with thisObject if defined? + proxy = proxy || function() { return fn.apply( thisObject !== undefined ? thisObject : this, arguments ); }; // Set the guid of unique handler to the same of original handler, so it can be removed proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++; // So proxy can be declared as an argument @@ -388,14 +393,14 @@ jQuery.event = { setup: bindReady, teardown: function() {} }, - + live: { add: function( proxy, data, namespaces ) { jQuery.extend( proxy, data || {} ); proxy.guid += data.selector + data.live; jQuery.event.add( this, data.live, liveHandler ); }, - + teardown: function( namespaces ) { jQuery.event.remove( this, namespaces[0], liveHandler ); } @@ -479,8 +484,11 @@ var withinElement = function( event ) { var parent = event.relatedTarget; // Traverse up the tree while ( parent && parent != this ) { + // Firefox sometimes assigns relatedTarget a XUL element + // which we cannot access the parentNode property of try { parent = parent.parentNode; } - catch(e) { parent = this; } + // assuming we've left the element since we most likely mousedover a xul element + catch(e) { break; } } if ( parent != this ) { @@ -506,19 +514,35 @@ jQuery.each({ }); jQuery.fn.extend({ - bind: function( type, data, fn ) { - return type === "unload" ? this.one(type, data, fn) : this.each(function() { - jQuery.event.add( this, type, fn || data, fn && data ); + bind: function( type, data, fn, thisObject ) { + if ( jQuery.isFunction( data ) ) { + if ( fn !== undefined ) { + thisObject = fn; + } + fn = data; + data = undefined; + } + fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject ); + return type === "unload" ? this.one(type, data, fn, thisObject) : this.each(function() { + jQuery.event.add( this, type, fn, data, thisObject ); }); }, - one: function( type, data, fn ) { - var one = jQuery.event.proxy( fn || data, function( event ) { + one: function( type, data, fn, thisObject ) { + if ( jQuery.isFunction( data ) ) { + if ( fn !== undefined ) { + thisObject = fn; + } + fn = data; + data = undefined; + } + fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject ); + var one = jQuery.event.proxy( fn, function( event ) { jQuery( this ).unbind( event, one ); - return (fn || data).apply( this, arguments ); + return fn.apply( this, arguments ); }); return this.each(function() { - jQuery.event.add( this, type, one, fn && data ); + jQuery.event.add( this, type, one, data, thisObject ); }); }, @@ -566,7 +590,7 @@ jQuery.fn.extend({ }, hover: function( fnOver, fnOut ) { - return this.mouseenter( fnOver ).mouseleave( fnOut ); + return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver ); }, ready: function( fn ) { @@ -587,10 +611,17 @@ jQuery.fn.extend({ return this; }, - live: function( type, data, fn ) { + live: function( type, data, fn, thisObject ) { + if ( jQuery.isFunction( data ) ) { + if ( fn !== undefined ) { + thisObject = fn; + } + fn = data; + data = undefined; + } jQuery( this.context ).bind( liveConvert( type, this.selector ), { - data: fn && data, selector: this.selector, live: type - }, fn || data ); + data: data, selector: this.selector, live: type + }, fn, thisObject ); return this; }, @@ -618,7 +649,7 @@ function liveHandler( event ) { jQuery.each(elems, function() { event.currentTarget = this.elem; - event.data = this.fn.data + event.data = this.fn.data; if ( this.fn.apply( this.elem, args ) === false ) { return (stop = false); } @@ -715,7 +746,7 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + // Handle event binding jQuery.fn[ name ] = function( fn ) { - return fn ? this.bind (name, fn ) : this.trigger( name ); + return fn ? this.bind( name, fn ) : this.trigger( name ); }; });