X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=81c193980bc16d7a8bc98b5a041e0dd285d0cac8;hb=43f235f4257359dfcf9270cf31c56aa547d01fd3;hp=6e8a45f81a24069055a86c8267215893b6f353ac;hpb=d7b73ea5c7440c49c804b3cafe67e84aafbdd064;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index 6e8a45f..81c1939 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -69,33 +69,41 @@ jQuery.event = { this.remove( element, j ); }, - trigger: function(type,data,element) { + trigger: function(type, data, element) { // Clone the incoming data, if any data = jQuery.makeArray(data || []); // Handle a global trigger - if ( !element ) { - var g = this.global[type]; - if ( g ) - for ( var i = 0, gl = g.length; i < gl; i++ ) - this.trigger( type, data, g[i] ); + if ( !element ) + jQuery.each( this.global[type] || [], function(){ + jQuery.event.trigger( type, data, this ); + }); // Handle triggering a single element - } else if ( element["on" + type] ) { - if ( element[ type ] && element[ type ].constructor == Function ) - element[ type ](); - else { + else { + var handler = element["on" + type ], val, + fn = jQuery.isFunction( element[ type ] ); + + if ( handler ) { // Pass along a fake event data.unshift( this.fix({ type: type, target: element }) ); // Trigger the event - element["on" + type].apply( element, data ); + if ( (val = handler.apply( element, data )) !== false ) + this.triggered = true; } + + if ( fn && val !== false ) + element[ type ](); + + this.triggered = false; } }, handle: function(event) { - if ( typeof jQuery == "undefined" ) return false; + // 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 ) return; // Empty object is for triggered events with no data event = jQuery.event.fix( event || window.event || {} ); @@ -133,7 +141,7 @@ jQuery.event = { event.target = event.srcElement; // Calculate pageX/Y if missing and clientX/Y available - if ( typeof event.pageX == "undefined" && typeof event.clientX != "undefined" ) { + if ( event.pageX == undefined && event.clientX != undefined ) { var e = document.documentElement, b = document.body; event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft); event.pageY = event.clientY + (e.scrollTop || b.scrollTop); @@ -187,32 +195,32 @@ jQuery.fn.extend({ * data as the second paramter (and the handler function as the third), see * second example. * - * @example $("p").bind( "click", function() { + * @example $("p").bind("click", function(){ * alert( $(this).text() ); - * } ) + * }); * @before

Hello

* @result alert("Hello") * - * @example var handler = function(event) { + * @example function handler(event) { * alert(event.data.foo); - * }; - * $("p").bind( "click", {foo: "bar"}, handler) + * } + * $("p").bind("click", {foo: "bar"}, handler) * @result alert("bar") * @desc Pass some additional data to the event handler. * - * @example $("form").bind( "submit", function() { return false; } ) + * @example $("form").bind("submit", function() { return false; }) * @desc Cancel a default action and prevent it from bubbling by returning false * from your function. * - * @example $("form").bind( "submit", function(event) { + * @example $("form").bind("submit", function(event){ * event.preventDefault(); - * } ); + * }); * @desc Cancel only the default action by using the preventDefault method. * * - * @example $("form").bind( "submit", function(event) { + * @example $("form").bind("submit", function(event){ * event.stopPropagation(); - * } ) + * }); * @desc Stop only an event from bubbling by using the stopPropagation method. * * @name bind @@ -241,9 +249,9 @@ jQuery.fn.extend({ * data as the second paramter (and the handler function as the third), see * second example. * - * @example $("p").one( "click", function() { + * @example $("p").one("click", function(){ * alert( $(this).text() ); - * } ) + * }); * @before

Hello

* @result alert("Hello") * @@ -305,9 +313,19 @@ jQuery.fn.extend({ * @before

Hello

* @result alert('hello') * + * @example $("p").click(function(event, a, b) { + * // when a normal click fires, a and b are undefined + * // for a trigger like below a refers too "foo" and b refers to "bar" + * }).trigger("click", ["foo", "bar"]); + * @desc Example of how to pass arbitrary to an event + * + * @before

Hello

+ * @result alert('hello') + * * @name trigger * @type jQuery * @param String type An event type to trigger. + * @param Array data (optional) Additional data to pass as arguments (after the event object) to the event handler * @cat Events */ trigger: function( type, data ) { @@ -316,10 +334,6 @@ jQuery.fn.extend({ }); }, - // We're overriding the old toggle function, so - // remember it for later - _toggle: jQuery.fn.toggle, - /** * Toggle between two function calls every other click. * Whenever a matched element is clicked, the first specified function @@ -341,9 +355,10 @@ jQuery.fn.extend({ * @cat Events */ toggle: function() { - // save reference to arguments for access in closure + // Save reference to arguments for access in closure var a = arguments; - return typeof a[0] == "function" && typeof a[1] == "function" ? this.click(function(e) { + + return this.click(function(e) { // Figure out which function to execute this.lastToggle = this.lastToggle == 0 ? 1 : 0; @@ -352,10 +367,7 @@ jQuery.fn.extend({ // and execute the function return a[this.lastToggle].apply( this, [e] ) || false; - }) : - - // Otherwise, execute the old toggle function - this._toggle.apply( this, arguments ); + }); }, /** @@ -414,6 +426,10 @@ jQuery.fn.extend({ * and attaching a function to that. By using this method, your bound Function * will be called the instant the DOM is ready to be read and manipulated, * which is exactly what 99.99% of all Javascript code needs to run. + * + * There is one argument passed to the ready event handler: A reference to + * the jQuery function. You can name that argument whatever you like, and + * can therefore stick with the $ alias without risc of naming collisions. * * Please ensure you have no code in your <body> onload event handler, * otherwise $(document).ready() may not fire. @@ -423,21 +439,30 @@ jQuery.fn.extend({ * * @example $(document).ready(function(){ Your code here... }); * + * @example jQuery(function($) { + * // Your code using failsafe $ alias here... + * }); + * @desc Uses both the shortcut for $(document).ready() and the argument + * to write failsafe jQuery code using the $ alias, without relying on the + * global alias. + * * @name ready * @type jQuery * @param Function fn The function to be executed when the DOM is ready. * @cat Events + * @see $.noConflict() + * @see $(Function) */ ready: function(f) { // If the DOM is already ready if ( jQuery.isReady ) // Execute the function immediately - f.apply( document ); + f.apply( document, [jQuery] ); // Otherwise, remember the function for later else { // Add the function to the wait list - jQuery.readyList.push( f ); + jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } ); } return this; @@ -461,8 +486,9 @@ jQuery.extend({ // If there are functions bound, to execute if ( jQuery.readyList ) { // Execute all of them - for ( var i = 0; i < jQuery.readyList.length; i++ ) - jQuery.readyList[i].apply( document ); + jQuery.each( jQuery.readyList, function(){ + this.apply( document ); + }); // Reset the list of functions jQuery.readyList = null; @@ -881,4 +907,4 @@ if (jQuery.browser.msie) jQuery.event.remove(els[i-1], type); while (--i); } - }); \ No newline at end of file + });