X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=3df1ae716ef1fea5bb3fe5b83f3aa737b9e7c957;hb=a5c319f922efcf34652d0632a945d20a0ab36ca6;hp=cbad528f3e9f3a4e8a67c4fc006b9cf899fa70c9;hpb=bd78d4f65dea2fc5fbfd58a0f099f8839ee9707d;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index cbad528..3df1ae7 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -122,7 +122,7 @@ jQuery.event = { } }, - trigger: function(type, data, element) { + trigger: function(type, data, element, native, extra) { // Clone the incoming data, if any data = jQuery.makeArray(data || []); @@ -130,28 +130,44 @@ jQuery.event = { if ( !element ) { // Only trigger if we've ever bound an event for it if ( this.global[type] ) - jQuery("*").trigger(type, data); + jQuery("*").add([window, document]).trigger(type, data); // Handle triggering a single element } else { - var val, ret, fn = jQuery.isFunction( element[ type ] || null ); + var val, ret, fn = jQuery.isFunction( element[ type ] || null ), + // Check to see if we need to provide a fake event, or not + evt = !data[0] || !data[0].preventDefault; // Pass along a fake event - data.unshift( this.fix({ type: type, target: element }) ); + if ( evt ) + data.unshift( this.fix({ type: type, target: element }) ); // Trigger the event if ( jQuery.isFunction( element.$handle ) ) val = element.$handle.apply( element, data ); + + // Handle triggering native .onfoo handlers if ( !fn && element["on"+type] && element["on"+type].apply( element, data ) === false ) val = false; - if ( fn && val !== false && !(jQuery.nodeName(element, 'a') && type == "click") ) { + // Extra functions don't get the custom event object + if ( evt ) + data.shift(); + + // Handle triggering of extra function + if ( extra && extra.apply( element, data ) === false ) + val = false; + + // Trigger the native events (except for clicks on links) + if ( fn && native !== false && val !== false && !(jQuery.nodeName(element, 'a') && type == "click") ) { this.triggered = true; element[ type ](); } this.triggered = false; } + + return val; }, handle: function(event) { @@ -170,10 +186,14 @@ jQuery.event = { args[0].handler = c[j]; args[0].data = c[j].data; - if ( c[j].apply( this, args ) === false ) { + var tmp = c[j].apply( this, args ); + + if ( val !== false ) + val = tmp; + + if ( tmp === false ) { event.preventDefault(); event.stopPropagation(); - val = false; } } @@ -403,12 +423,17 @@ jQuery.fn.extend({ * @param Array data (optional) Additional data to pass as arguments (after the event object) to the event handler * @cat Events */ - trigger: function( type, data ) { + trigger: function( type, data, fn ) { return this.each(function(){ - jQuery.event.trigger( type, data, this ); + jQuery.event.trigger( type, data, this, true, fn ); }); }, + triggerHandler: function( type, data, fn ) { + if ( this[0] ) + return jQuery.event.trigger( type, data, this[0], false, fn ); + }, + /** * Toggle between two function calls every other click. * Whenever a matched element is clicked, the first specified function @@ -529,6 +554,9 @@ jQuery.fn.extend({ * @see $(Function) */ ready: function(f) { + // Attach the listeners + bindReady(); + // If the DOM is already ready if ( jQuery.isReady ) // Execute the function immediately @@ -578,8 +606,6 @@ jQuery.extend({ } }); -new function(){ - /** * Bind a function to the scroll event of each matched element. * @@ -930,7 +956,13 @@ new function(){ }; }); - + +var readyBound = false; + +function bindReady(){ + if ( readyBound ) return; + readyBound = true; + // If Mozilla is used if ( jQuery.browser.mozilla || jQuery.browser.opera ) // Use the handy event callback @@ -950,7 +982,7 @@ new function(){ // script does not exist if jQuery is loaded dynamically if ( script ) script.onreadystatechange = function() { - if ( document.readyState != "complete" ) return; + if ( this.readyState != "complete" ) return; jQuery.ready(); }; @@ -976,5 +1008,4 @@ new function(){ // A fallback to window.onload, that will always work jQuery.event.add( window, "load", jQuery.ready ); - -}; +}