Use .one() when doing a .bind() with an "unload" event type (#1242)
[jquery.git] / src / event / event.js
index eb6ee59..a1a0825 100644 (file)
@@ -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,13 +35,6 @@ 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 = {};
@@ -182,47 +179,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;
-
-               // 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);
-               }
                                
                // 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
+               if (jQuery.browser.safari && event.target.nodeType == 3)
                        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 relatedTarget, if necessary
+               if ( !event.relatedTarget && event.fromElement )
+                       event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
+
+               // 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;
                }
-               
-               // fix preventDefault and stopPropagation
-               if (!event.preventDefault)
-                       event.preventDefault = function() {
-                               this.returnValue = false;
-                       };
                        
-               if (!event.stopPropagation)
-                       event.stopPropagation = function() {
-                               this.cancelBubble = true;
-                       };
+               // Add which for key events
+               if ( !event.which && (event.charCode || event.keyCode) )
+                       event.which = event.charCode || event.keyCode;
+               
+               // 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;
        }
@@ -241,6 +250,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() );
         * });
@@ -277,7 +289,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 );
                });
        },
@@ -286,7 +298,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.
         *
@@ -456,7 +468,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; };
@@ -549,7 +561,7 @@ 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 );