X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=b49e1755dc2d897f747cd6397301f61ca3332914;hb=970eb0f020b7a57f313e438490b4018c5569f8b3;hp=61bd4e8852e4266ef6cca82e29913c037cea2c66;hpb=e0aa10a664aabbc9ab900a07ea55cb892a7b8d3f;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index 61bd4e8..b49e175 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -32,35 +32,38 @@ jQuery.event = { } // Make sure that the function being executed has a unique ID - if ( !handler.guid ) + if ( !handler.guid ) { handler.guid = this.guid++; + // Don't forget to set guid for the original handler function + if (fn) fn.guid = handler.guid; + } // Init the element's event structure if (!element.$events) element.$events = {}; - - // Get the current list of functions bound to this event - var handlers = element.$events[type]; - - // Init the event handler queue - if (!handlers) - handlers = element.$events[type] = {}; - - // Add the function to the element's handler list - handlers[handler.guid] = handler; - if (!element.$handle) { + if (!element.$handle) element.$handle = function() { jQuery.event.handle.apply(element, arguments); }; + // Get the current list of functions bound to this event + var handlers = element.$events[type]; + + // Init the event handler queue + if (!handlers) { + handlers = element.$events[type] = {}; + // And bind the global event handler to the element if (element.addEventListener) element.addEventListener(type, element.$handle, false); else if (element.attachEvent) - element.attachEvent("on" + type, element.$handle, false); + element.attachEvent("on" + type, element.$handle); } + // Add the function to the element's handler list + handlers[handler.guid] = handler; + // Remember the function in a global list (for triggering) if (!this.global[type]) this.global[type] = []; @@ -101,8 +104,8 @@ jQuery.event = { if (element.removeEventListener) element.removeEventListener(type, element.$handle, false); else if (element.detachEvent) - element.detachEvent("on" + type, element.$handle, false); - ret = element.$handle = null; + element.detachEvent("on" + type, element.$handle); + ret = null; delete events[type]; } } @@ -110,7 +113,7 @@ jQuery.event = { // Remove the expando if it's no longer used for ( ret in events ) break; if ( !ret ) - element.$events = null; + element.$handle = element.$events = null; } }, @@ -126,7 +129,7 @@ jQuery.event = { // Handle triggering a single element else { - var val, ret, fn = jQuery.isFunction( element[ type ] ); + var val, ret, fn = jQuery.isFunction( element[ type ] || null ); // Pass along a fake event data.unshift( this.fix({ type: type, target: element }) ); @@ -143,19 +146,18 @@ jQuery.event = { }, handle: function(event) { + // returned undefined or false + var val; + // 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; + if ( typeof jQuery == "undefined" || jQuery.event.triggered ) + return val; // Empty object is for triggered events with no data event = jQuery.event.fix( event || window.event || {} ); - // returned undefined or false - var returnValue; - - var c = this.$events[event.type]; - - var args = [].slice.call( arguments, 1 ); + var c = this.$events && this.$events[event.type], args = [].slice.call( arguments, 1 ); args.unshift( event ); for ( var j in c ) { @@ -167,14 +169,16 @@ jQuery.event = { if ( c[j].apply( this, args ) === false ) { event.preventDefault(); event.stopPropagation(); - returnValue = false; + val = false; } } // Clean up added properties in IE to prevent memory leak - if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null; + if (jQuery.browser.msie) + event.target = event.preventDefault = event.stopPropagation = + event.handler = event.data = null; - return returnValue; + return val; }, fix: function(event) { @@ -182,12 +186,33 @@ jQuery.event = { if ( !event.target && event.srcElement ) event.target = event.srcElement; + // Add relatedTarget, if necessary + if ( !event.relatedTarget && event.fromElement ) + event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement; + + // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs) + if ( event.metaKey == null && event.ctrlKey != null ) + event.metaKey = event.ctrlKey; + + // Add which for click: 1 == left; 2 == middle; 3 == right + // Note: button is not normalized, so don't use it + if ( event.which == null && event.button != null ) + event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); + // Calculate pageX/Y if missing and clientX/Y available - 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); + if ( event.pageX == null && event.clientX != null ) { + var e = document.documentElement || document.body; + event.pageX = event.clientX + e.scrollLeft; + event.pageY = event.clientY + e.scrollTop; } + + // Add which for keypresses: keyCode + if ( (event.which == null || event.type == "keypress") && event.keyCode != null ) + event.which = event.keyCode; + + // If it's a keypress event, add charCode to IE + if ( event.charCode == null && event.type == "keypress" ) + event.charCode = event.keyCode; // check if target is a textnode (safari) if (jQuery.browser.safari && event.target.nodeType == 3) { @@ -274,7 +299,7 @@ jQuery.fn.extend({ */ bind: function( type, data, fn ) { return this.each(function(){ - jQuery.event.add( this, type, fn || data, data ); + jQuery.event.add( this, type, fn || data, fn && data ); }); }, @@ -282,7 +307,7 @@ jQuery.fn.extend({ * Binds a handler to a particular event (like click) for each matched element. * The handler is executed only once for each element. Otherwise, the same rules * as described in bind() apply. - The event handler is passed an event object that you can use to prevent + * The event handler is passed an event object that you can use to prevent * default behaviour. To stop both default action and event bubbling, your handler * has to return false. * @@ -309,7 +334,7 @@ jQuery.fn.extend({ jQuery.event.add( this, type, function(event) { jQuery(this).unbind(event); return (fn || data).apply( this, arguments); - }, data); + }, fn && data); }); }, @@ -412,7 +437,7 @@ jQuery.fn.extend({ return this.click(function(e) { // Figure out which function to execute - this.lastToggle = this.lastToggle == 0 ? 1 : 0; + this.lastToggle = 0 == this.lastToggle ? 1 : 0; // Make sure that clicks stop e.preventDefault(); @@ -452,7 +477,7 @@ jQuery.fn.extend({ // A private function for handling mouse 'hovering' function handleHover(e) { // Check if mouse(over|out) are still within the same parent element - var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; + var p = e.relatedTarget; // Traverse up the tree while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; }; @@ -545,9 +570,12 @@ jQuery.extend({ // Reset the list of functions jQuery.readyList = null; } - // Remove event lisenter to avoid memory leak + // Remove event listener to avoid memory leak if ( jQuery.browser.mozilla || jQuery.browser.opera ) document.removeEventListener( "DOMContentLoaded", jQuery.ready, false ); + + // Remove script element used by IE hack + jQuery(window).load(function(){ jQuery("#__ie_init").remove(); }); } } }); @@ -925,7 +953,6 @@ new function(){ if ( script ) script.onreadystatechange = function() { if ( this.readyState != "complete" ) return; - this.parentNode.removeChild( this ); jQuery.ready(); };