X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent.js;h=2f5b1490232a09f9722eb5234f130d7db3b57cd5;hb=9a69b2cf08f11d58ba5e53e540b96b8f4ce5072d;hp=9c1f287343c52af16f19069faaa2f87c995a39e9;hpb=ee34b6982ab25cd7699f68506d7b18a02c3c4d44;p=jquery.git diff --git a/src/event.js b/src/event.js index 9c1f287..2f5b149 100644 --- a/src/event.js +++ b/src/event.js @@ -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 ]; @@ -405,7 +405,7 @@ jQuery.event = { add: function( proxy, data, namespaces ) { jQuery.extend( proxy, data || {} ); proxy.guid += data.selector + data.live; - jQuery.event.add( this, data.live, liveHandler ); + jQuery.event.add( this, data.live, liveHandler, data ); }, remove: function( namespaces ) { @@ -467,6 +467,7 @@ jQuery.Event.prototype = { if ( !e ) { return; } + // if preventDefault exists run it on the original event if ( e.preventDefault ) { e.preventDefault(); @@ -518,9 +519,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 +534,83 @@ jQuery.each({ }; }); +(function() { + + var event = jQuery.event, + special = event.special, + handle = event.handle; + + special.submit = { + setup: function(data, namespaces) { + if(data.selector) { + event.add(this, 'click.specialSubmit', function(e, eventData) { + if(jQuery(e.target).filter(":submit, :image").closest(data.selector).length) { + e.type = "submit"; + return handle.call( this, e, eventData ); + } + }); + + event.add(this, 'keypress.specialSubmit', function( e, eventData ) { + if(jQuery(e.target).filter(":text, :password").closest(data.selector).length) { + e.type = "submit"; + return handle.call( this, e, eventData ); + } + }); + } else { + return false; + } + }, + + remove: function(namespaces) { + event.remove(this, 'click.specialSubmit'); + event.remove(this, 'keypress.specialSubmit'); + } + }; + +})(); + +// Create "bubbling" focus and blur events +jQuery.each({ + focus: "focusin", + blur: "focusout" +}, function( orig, fix ){ + var event = jQuery.event, + handle = event.handle; + + function ieHandler() { + arguments[0].type = orig; + return handle.apply(this, arguments); + } + + event.special[orig] = { + setup:function() { + if ( this.addEventListener ) + this.addEventListener( orig, handle, true ); + else + event.add( this, fix, ieHandler ); + }, + teardown:function() { + if ( this.removeEventListener ) + this.removeEventListener( orig, handle, true ); + else + 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 +621,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 +645,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 ); }); @@ -655,15 +742,15 @@ function liveHandler( event ) { jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) { if ( fn.live === event.type ) { - var elem = jQuery( event.target ).closest( fn.selector )[0]; + var elem = jQuery( event.target ).closest( fn.selector, event.currentTarget )[0]; if ( elem ) { - elems.push({ elem: elem, fn: fn }); + elems.push({ elem: elem, fn: fn, closer: jQuery.lastCloser }); } } }); elems.sort(function( a, b ) { - return jQuery.data( a.elem, "closest" ) - jQuery.data( b.elem, "closest" ); + return a.closer - b.closer; }); jQuery.each(elems, function() { @@ -715,6 +802,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 @@ -728,7 +821,8 @@ function bindReady() { // ensure firing before onload, // maybe late but safe also for iframes document.attachEvent("onreadystatechange", function() { - if ( document.readyState === "complete" ) { + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( document.readyState === "complete" && document.body ) { document.detachEvent( "onreadystatechange", arguments.callee ); jQuery.ready(); } @@ -736,7 +830,8 @@ function bindReady() { // If IE and not an iframe // continually check to see if the document is ready - if ( document.documentElement.doScroll && window === window.top ) (function() { + // NOTE: DO NOT CHANGE TO ===, FAILS IN IE. + if ( document.documentElement.doScroll && window == window.top ) (function() { if ( jQuery.isReady ) { return; } @@ -759,9 +854,9 @@ function bindReady() { jQuery.event.add( window, "load", jQuery.ready ); } -jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + - "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," + - "change,select,submit,keydown,keypress,keyup,error").split(","), function( i, name ) { +jQuery.each( ("blur focus load resize scroll unload click dblclick " + + "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 ) { @@ -770,11 +865,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 +877,4 @@ jQuery( window ).bind( 'unload', function() { } } }); +@*/