X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=623bcf32a7a2abc3ea2faa4b0d897fb48429f4f0;hb=8b683b891f2cfd299c1d0708bb41ea26393ea417;hp=b49e1755dc2d897f747cd6397301f61ca3332914;hpb=eabac3f6a189342cf8bb615de904f9f125374c67;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index b49e175..623bcf3 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -13,6 +13,10 @@ jQuery.event = { if ( jQuery.browser.msie && element.setInterval != undefined ) element = window; + // Make sure that the function being executed has a unique ID + if ( !handler.guid ) + handler.guid = this.guid++; + // if data is passed, bind to handler if( data != undefined ) { // Create temporary function pointer to original handler @@ -31,20 +35,23 @@ jQuery.event = { handler.guid = fn.guid; } - // Make sure that the function being executed has a unique ID - 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 = {}; if (!element.$handle) element.$handle = function() { - jQuery.event.handle.apply(element, arguments); + // 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 val; + + val = jQuery.event.handle.apply(element, arguments); + + return val; }; // Get the current list of functions bound to this event @@ -67,7 +74,9 @@ jQuery.event = { // Remember the function in a global list (for triggering) if (!this.global[type]) this.global[type] = []; - this.global[type].push( element ); + // Only add the element to the global list once + if (jQuery.inArray(element, this.global[type]) == -1) + this.global[type].push( element ); }, guid: 1, @@ -75,7 +84,7 @@ jQuery.event = { // Detach an event or set of events from an element remove: function(element, type, handler) { - var events = element.$events, ret; + var events = element.$events, ret, index; if ( events ) { // type is actually an event object here @@ -107,6 +116,10 @@ jQuery.event = { element.detachEvent("on" + type, element.$handle); ret = null; delete events[type]; + + // Remove element from the global event type cache + while ( this.global[type] && ( (index = jQuery.inArray(element, this.global[type])) >= 0 ) ) + delete this.global[type][index]; } } @@ -135,7 +148,7 @@ jQuery.event = { data.unshift( this.fix({ type: type, target: element }) ); // Trigger the event - if ( (val = this.handle.apply( element, data )) !== false ) + if ( (val = element.$handle.apply( element, data )) !== false ) this.triggered = true; if ( fn && val !== false && !jQuery.nodeName(element, 'a') ) @@ -148,11 +161,6 @@ 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 val; // Empty object is for triggered events with no data event = jQuery.event.fix( event || window.event || {} ); @@ -182,68 +190,59 @@ jQuery.event = { }, fix: function(event) { + // store a copy of the original event object + // and clone to set read-only properties + var originalEvent = event; + event = jQuery.extend({}, originalEvent); + + // add preventDefault and stopPropagation since + // they will not work on the clone + event.preventDefault = function() { + // if preventDefault exists run it on the original event + if (originalEvent.preventDefault) + return originalEvent.preventDefault(); + // otherwise set the returnValue property of the original event to false (IE) + originalEvent.returnValue = false; + }; + event.stopPropagation = function() { + // if stopPropagation exists run it on the original event + if (originalEvent.stopPropagation) + return originalEvent.stopPropagation(); + // otherwise set the cancelBubble property of the original event to true (IE) + originalEvent.cancelBubble = true; + }; + // Fix target property, if necessary if ( !event.target && event.srcElement ) event.target = event.srcElement; + + // check if target is a textnode (safari) + if (jQuery.browser.safari && event.target.nodeType == 3) + event.target = originalEvent.target.parentNode; // 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 == 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) { - // store a copy of the original event object - // and clone because target is read only - var originalEvent = event; - event = jQuery.extend({}, originalEvent); - // get parentnode from textnode - event.target = originalEvent.target.parentNode; - - // add preventDefault and stopPropagation since - // they will not work on the clone - event.preventDefault = function() { - return originalEvent.preventDefault(); - }; - event.stopPropagation = function() { - return originalEvent.stopPropagation(); - }; - } + // Add which for key events + if ( !event.which && (event.charCode || event.keyCode) ) + event.which = event.charCode || event.keyCode; - // fix preventDefault and stopPropagation - if (!event.preventDefault) - event.preventDefault = function() { - this.returnValue = false; - }; - - if (!event.stopPropagation) - event.stopPropagation = function() { - this.cancelBubble = true; - }; + // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs) + if ( !event.metaKey && event.ctrlKey ) + 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 && event.button ) + event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); return event; } @@ -262,6 +261,9 @@ jQuery.fn.extend({ * data as the second parameter (and the handler function as the third), see * second example. * + * Calling bind with an event type of "unload" will automatically + * use the one method instead of bind to prevent memory leaks. + * * @example $("p").bind("click", function(){ * alert( $(this).text() ); * }); @@ -298,7 +300,7 @@ jQuery.fn.extend({ * @cat Events */ bind: function( type, data, fn ) { - return this.each(function(){ + return type == "unload" ? this.one(type, data, fn) : this.each(function(){ jQuery.event.add( this, type, fn || data, fn && data ); }); },