event clean up

This commit is contained in:
Brandon Aaron 2009-04-30 01:26:09 +00:00
parent 45b8d2531e
commit 287ecdbf67

View file

@ -8,17 +8,20 @@ jQuery.event = {
// Bind an event to an element
// Original by Dean Edwards
add: function( elem, types, handler, data ) {
if ( elem.nodeType == 3 || elem.nodeType == 8 )
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
// For whatever reason, IE has trouble passing the window object
// around, causing it to be cloned in the process
if ( elem.setInterval && ( elem != window && !elem.frameElement ) )
if ( elem.setInterval && ( elem !== window && !elem.frameElement ) ) {
elem = window;
}
// Make sure that the function being executed has a unique ID
if ( !handler.guid )
if ( !handler.guid ) {
handler.guid = this.guid++;
}
// if data is passed, bind to handler
if ( data !== undefined ) {
@ -48,7 +51,9 @@ jQuery.event = {
// Handle multiple events separated by a space
// jQuery(...).bind("mouseover mouseout", fn);
jQuery.each(types.split(/\s+/), function(index, type) {
types = types.split( /\s+/ );
var type, i=0;
while ( (type = types[ i++ ]) ) {
// Namespaced event handlers
var namespaces = type.split(".");
type = namespaces.shift();
@ -57,8 +62,9 @@ jQuery.event = {
// Get the current list of functions bound to this event
var handlers = events[ type ];
if ( jQuery.event.specialAll[type] )
jQuery.event.specialAll[type].setup.call(elem, data, namespaces);
if ( this.specialAll[ type ] ) {
this.specialAll[ type ].setup.call( elem, data, namespaces );
}
// Init the event handler queue
if ( !handlers ) {
@ -67,21 +73,22 @@ jQuery.event = {
// Check for a special event handler
// Only use addEventListener/attachEvent if the special
// events handler returns false
if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem, data, namespaces) === false ) {
if ( !this.special[ type ] || this.special[ type ].setup.call( elem, data, namespaces ) === false ) {
// Bind the global event handler to the element
if (elem.addEventListener)
if ( elem.addEventListener ) {
elem.addEventListener( type, handle, false );
else if (elem.attachEvent)
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, handle );
}
}
}
// Add the function to the element's handler list
handlers[ handler.guid ] = handler;
// Keep track of which events have been used, for global triggering
jQuery.event.global[type] = true;
});
this.global[ type ] = true;
}
// Nullify elem to prevent memory leaks in IE
elem = null;
@ -93,17 +100,19 @@ jQuery.event = {
// Detach an event or set of events from an element
remove: function( elem, types, handler ) {
// don't do events on text and comment nodes
if ( elem.nodeType == 3 || elem.nodeType == 8 )
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
var events = jQuery.data(elem, "events"), ret, index;
var events = jQuery.data( elem, "events" ), ret, type;
if ( events ) {
// Unbind all events for the element
if ( types === undefined || (typeof types === "string" && types.charAt(0) == ".") )
for ( var type in events )
if ( types === undefined || (typeof types === "string" && types.charAt(0) === ".") ) {
for ( type in events ) {
this.remove( elem, type + (types || "") );
else {
}
} else {
// types is actually an event object here
if ( types.type ) {
handler = types.handler;
@ -112,7 +121,9 @@ jQuery.event = {
// Handle multiple events seperated by a space
// jQuery(...).unbind("mouseover mouseout", fn);
jQuery.each(types.split(/\s+/), function(index, type){
types = types.split(/\s+/);
var i = 0;
while ( (type = types[ i++ ]) ) {
// Namespaced event handlers
var namespaces = type.split(".");
type = namespaces.shift();
@ -121,40 +132,51 @@ jQuery.event = {
if ( events[type] ) {
// remove the given handler for the given type
if ( handler )
if ( handler ) {
delete events[ type ][ handler.guid ];
// remove all handlers for the given type
else
for ( var handle in events[type] )
} else {
for ( var handle in events[ type ] ) {
// Handle the removal of namespaced events
if ( all || namespace.test(events[type][handle].type) )
if ( all || namespace.test( events[ type ][ handle ].type ) ) {
delete events[ type ][ handle ];
}
}
}
if ( jQuery.event.specialAll[type] )
jQuery.event.specialAll[type].teardown.call(elem, namespaces);
if ( this.specialAll[ type ] ) {
this.specialAll[ type ].teardown.call( elem, namespaces );
}
// remove generic event handler if no more handlers exist
for ( ret in events[type] ) break;
for ( ret in events[ type ] ) {
break;
}
if ( !ret ) {
if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem, namespaces) === false ) {
if (elem.removeEventListener)
if ( !this.special[ type ] || this.special[ type ].teardown.call( elem, namespaces ) === false ) {
if ( elem.removeEventListener ) {
elem.removeEventListener( type, jQuery.data( elem, "handle" ), false );
else if (elem.detachEvent)
} else if ( elem.detachEvent ) {
elem.detachEvent( "on" + type, jQuery.data( elem, "handle" ) );
}
}
ret = null;
delete events[ type ];
}
}
});
}
}
// Remove the expando if it's no longer used
for ( ret in events ) break;
for ( ret in events ) {
break;
}
if ( !ret ) {
var handle = jQuery.data( elem, "handle" );
if ( handle ) handle.elem = null;
if ( handle ) {
handle.elem = null;
}
jQuery.removeData( elem, "events" );
jQuery.removeData( elem, "handle" );
}
@ -185,18 +207,21 @@ jQuery.event = {
// Don't bubble custom events when global (to avoid too much overhead)
event.stopPropagation();
// Only trigger if we've ever bound an event for it
if ( this.global[type] )
jQuery.each( jQuery.cache, function(){
if ( this.events && this.events[type] )
jQuery.event.trigger( event, data, this.handle.elem );
});
if ( this.global[ type ] ) {
for ( var cached in jQuery.cache ) {
if ( cached.events && cached.events[ type ] ) {
this.trigger( event, data, cached.handle.elem );
}
}
}
}
// Handle triggering a single element
// don't do events on text and comment nodes
if ( !elem || elem.nodeType == 3 || elem.nodeType == 8 )
if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
return undefined;
}
// Clean up in case it is reused
event.result = undefined;
@ -211,15 +236,17 @@ jQuery.event = {
// Trigger the event, it is assumed that "handle" is a function
var handle = jQuery.data( elem, "handle" );
if ( handle )
if ( handle ) {
handle.apply( elem, data );
}
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
if ( (!elem[ type ] || (jQuery.nodeName(elem, 'a') && type === "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false ) {
event.result = false;
}
// Trigger the native events (except for clicks on links)
if ( !bubbling && elem[type] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
if ( !bubbling && elem[ type ] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type === "click") ) {
this.triggered = true;
try {
elem[ type ]();
@ -231,9 +258,10 @@ jQuery.event = {
if ( !event.isPropagationStopped() ) {
var parent = elem.parentNode || elem.ownerDocument;
if ( parent )
if ( parent ) {
jQuery.event.trigger( event, data, parent, true );
}
}
},
handle: function( event ) {
@ -274,8 +302,9 @@ jQuery.event = {
}
}
if( event.isImmediatePropagationStopped() )
if ( event.isImmediatePropagationStopped() ) {
break;
}
}
}
@ -284,8 +313,9 @@ jQuery.event = {
props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
fix: function( event ) {
if ( event[expando] )
if ( event[ expando ] ) {
return event;
}
// store a copy of the original event object
// and "clone" to set read-only properties
@ -298,16 +328,19 @@ jQuery.event = {
}
// Fix target property, if necessary
if ( !event.target )
if ( !event.target ) {
event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
}
// check if target is a textnode (safari)
if ( event.target.nodeType == 3 )
if ( event.target.nodeType === 3 ) {
event.target = event.target.parentNode;
}
// Add relatedTarget, if necessary
if ( !event.relatedTarget && event.fromElement )
event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
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 ) {
@ -317,17 +350,20 @@ jQuery.event = {
}
// Add which for key events
if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) )
if ( !event.which && ((event.charCode || event.charCode === 0) ? 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 )
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 )
if ( !event.which && event.button ) {
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
}
return event;
},
@ -358,30 +394,34 @@ jQuery.event = {
var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)");
jQuery.each( (jQuery.data(this, "events").live || {}), function() {
if ( name.test(this.type) )
if ( name.test(this.type) ) {
remove++;
}
});
if ( remove < 1 )
if ( remove < 1 ) {
jQuery.event.remove( this, namespaces[0], liveHandler );
}
}
}
}
}
};
jQuery.Event = function( src ){
// Allow instantiation without the 'new' keyword
if( !this.preventDefault )
if ( !this.preventDefault ) {
return new jQuery.Event( src );
}
// Event object
if ( src && src.type ) {
this.originalEvent = src;
this.type = src.type;
// Event type
}else
} else {
this.type = src;
}
// timeStamp is buggy for some events on Firefox(#3843)
// So we won't rely on the native value
@ -405,11 +445,13 @@ jQuery.Event.prototype = {
this.isDefaultPrevented = returnTrue;
var e = this.originalEvent;
if( !e )
if ( !e ) {
return;
}
// if preventDefault exists run it on the original event
if (e.preventDefault)
if ( e.preventDefault ) {
e.preventDefault();
}
// otherwise set the returnValue property of the original event to false (IE)
e.returnValue = false;
},
@ -417,11 +459,13 @@ jQuery.Event.prototype = {
this.isPropagationStopped = returnTrue;
var e = this.originalEvent;
if( !e )
if ( !e ) {
return;
}
// if stopPropagation exists run it on the original event
if (e.stopPropagation)
if ( e.stopPropagation ) {
e.stopPropagation();
}
// otherwise set the cancelBubble property of the original event to true (IE)
e.cancelBubble = true;
},
@ -439,9 +483,10 @@ var withinElement = function(event) {
// Check if mouse(over|out) are still within the same parent element
var parent = event.relatedTarget;
// Traverse up the tree
while ( parent && parent != this )
while ( parent && parent != this ) {
try { parent = parent.parentNode; }
catch(e) { parent = this; }
}
if ( parent != this ) {
// set the correct event type
@ -467,7 +512,7 @@ jQuery.each({
jQuery.fn.extend({
bind: function( type, data, fn ) {
return type == "unload" ? this.one(type, data, fn) : this.each(function(){
return type === "unload" ? this.one(type, data, fn) : this.each(function() {
jQuery.event.add( this, type, fn || data, fn && data );
});
},
@ -509,8 +554,9 @@ jQuery.fn.extend({
var args = arguments, i = 1;
// link all the functions, so any of them can unbind this click handler
while( i < args.length )
while( i < args.length ) {
jQuery.event.proxy( fn, args[ i++ ] );
}
return this.click( jQuery.event.proxy( fn, function( event ) {
// Figure out which function to execute
@ -533,14 +579,15 @@ jQuery.fn.extend({
bindReady();
// If the DOM is already ready
if ( jQuery.isReady )
if ( jQuery.isReady ) {
// Execute the function immediately
fn.call( document, jQuery );
// Otherwise, remember the function for later
else
} else {
// Add the function to the wait list
jQuery.readyList.push( fn );
}
return this;
},
@ -562,15 +609,15 @@ jQuery.fn.extend({
function liveHandler( event ) {
var check = new RegExp("(^|\\.)" + event.type + "(\\.|$)"),
stop = true,
elems = [];
stop = true, elems = [];
jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
if ( check.test( fn.type ) ) {
var elem = jQuery( event.target ).closest( fn.data )[0];
if ( elem )
if ( elem ) {
elems.push({ elem: elem, fn: fn });
}
}
});
elems.sort(function( a, b ) {
@ -579,8 +626,9 @@ function liveHandler( event ){
jQuery.each(elems, function() {
event.currentTarget = this.elem;
if ( this.fn.call(this.elem, event, this.fn.data) === false )
if ( this.fn.call( this.elem, event, this.fn.data ) === false ) {
return (stop = false);
}
});
return stop;
@ -603,9 +651,10 @@ jQuery.extend({
// If there are functions bound, to execute
if ( jQuery.readyList ) {
// Execute all of them
jQuery.each( jQuery.readyList, function(){
this.call( document, jQuery );
});
var fn, i = 0;
while ( (fn = jQuery.readyList[ i++ ]) ) {
fn.call( document, jQuery );
}
// Reset the list of functions
jQuery.readyList = null;
@ -644,8 +693,10 @@ function bindReady(){
// If IE and not an iframe
// continually check to see if the document is ready
if ( document.documentElement.doScroll && window == window.top ) (function(){
if ( jQuery.isReady ) return;
if ( document.documentElement.doScroll && window === window.top ) (function() {
if ( jQuery.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
@ -682,8 +733,10 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
// - http://isaacschlueter.com/2006/10/msie-memory-leaks/
// - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
jQuery( window ).bind( 'unload', function() {
for ( var id in jQuery.cache )
for ( var id in jQuery.cache ) {
// Skip the window
if ( id != 1 && jQuery.cache[ id ].handle )
if ( id != 1 && jQuery.cache[ id ].handle ) {
jQuery.event.remove( jQuery.cache[ id ].handle.elem );
}
}
});