event clean up
This commit is contained in:
parent
45b8d2531e
commit
287ecdbf67
347
src/event.js
347
src/event.js
|
@ -7,18 +7,21 @@ jQuery.event = {
|
||||||
|
|
||||||
// Bind an event to an element
|
// Bind an event to an element
|
||||||
// Original by Dean Edwards
|
// Original by Dean Edwards
|
||||||
add: function(elem, types, handler, data) {
|
add: function( elem, types, handler, data ) {
|
||||||
if ( elem.nodeType == 3 || elem.nodeType == 8 )
|
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// For whatever reason, IE has trouble passing the window object
|
// For whatever reason, IE has trouble passing the window object
|
||||||
// around, causing it to be cloned in the process
|
// 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;
|
elem = window;
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure that the function being executed has a unique ID
|
// Make sure that the function being executed has a unique ID
|
||||||
if ( !handler.guid )
|
if ( !handler.guid ) {
|
||||||
handler.guid = this.guid++;
|
handler.guid = this.guid++;
|
||||||
|
}
|
||||||
|
|
||||||
// if data is passed, bind to handler
|
// if data is passed, bind to handler
|
||||||
if ( data !== undefined ) {
|
if ( data !== undefined ) {
|
||||||
|
@ -33,12 +36,12 @@ jQuery.event = {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init the element's event structure
|
// Init the element's event structure
|
||||||
var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}),
|
var events = jQuery.data( elem, "events" ) || jQuery.data( elem, "events", {} ),
|
||||||
handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
|
handle = jQuery.data( elem, "handle" ) || jQuery.data( elem, "handle", function() {
|
||||||
// Handle the second event of a trigger and when
|
// Handle the second event of a trigger and when
|
||||||
// an event is called after a page has unloaded
|
// an event is called after a page has unloaded
|
||||||
return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
|
return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
|
||||||
jQuery.event.handle.apply(arguments.callee.elem, arguments) :
|
jQuery.event.handle.apply( arguments.callee.elem, arguments ) :
|
||||||
undefined;
|
undefined;
|
||||||
});
|
});
|
||||||
// Add elem as a property of the handle function
|
// Add elem as a property of the handle function
|
||||||
|
@ -48,40 +51,44 @@ jQuery.event = {
|
||||||
|
|
||||||
// Handle multiple events separated by a space
|
// Handle multiple events separated by a space
|
||||||
// jQuery(...).bind("mouseover mouseout", fn);
|
// 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
|
// Namespaced event handlers
|
||||||
var namespaces = type.split(".");
|
var namespaces = type.split(".");
|
||||||
type = namespaces.shift();
|
type = namespaces.shift();
|
||||||
handler.type = namespaces.slice().sort().join(".");
|
handler.type = namespaces.slice().sort().join(".");
|
||||||
|
|
||||||
// Get the current list of functions bound to this event
|
// Get the current list of functions bound to this event
|
||||||
var handlers = events[type];
|
var handlers = events[ type ];
|
||||||
|
|
||||||
if ( jQuery.event.specialAll[type] )
|
if ( this.specialAll[ type ] ) {
|
||||||
jQuery.event.specialAll[type].setup.call(elem, data, namespaces);
|
this.specialAll[ type ].setup.call( elem, data, namespaces );
|
||||||
|
}
|
||||||
|
|
||||||
// Init the event handler queue
|
// Init the event handler queue
|
||||||
if (!handlers) {
|
if ( !handlers ) {
|
||||||
handlers = events[type] = {};
|
handlers = events[ type ] = {};
|
||||||
|
|
||||||
// Check for a special event handler
|
// Check for a special event handler
|
||||||
// Only use addEventListener/attachEvent if the special
|
// Only use addEventListener/attachEvent if the special
|
||||||
// events handler returns false
|
// 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
|
// Bind the global event handler to the element
|
||||||
if (elem.addEventListener)
|
if ( elem.addEventListener ) {
|
||||||
elem.addEventListener(type, handle, false);
|
elem.addEventListener( type, handle, false );
|
||||||
else if (elem.attachEvent)
|
} else if ( elem.attachEvent ) {
|
||||||
elem.attachEvent("on" + type, handle);
|
elem.attachEvent( "on" + type, handle );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the function to the element's handler list
|
// Add the function to the element's handler list
|
||||||
handlers[handler.guid] = handler;
|
handlers[ handler.guid ] = handler;
|
||||||
|
|
||||||
// Keep track of which events have been used, for global triggering
|
// 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
|
// Nullify elem to prevent memory leaks in IE
|
||||||
elem = null;
|
elem = null;
|
||||||
|
@ -91,19 +98,21 @@ jQuery.event = {
|
||||||
global: {},
|
global: {},
|
||||||
|
|
||||||
// Detach an event or set of events from an element
|
// Detach an event or set of events from an element
|
||||||
remove: function(elem, types, handler) {
|
remove: function( elem, types, handler ) {
|
||||||
// don't do events on text and comment nodes
|
// don't do events on text and comment nodes
|
||||||
if ( elem.nodeType == 3 || elem.nodeType == 8 )
|
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var events = jQuery.data(elem, "events"), ret, index;
|
var events = jQuery.data( elem, "events" ), ret, type;
|
||||||
|
|
||||||
if ( events ) {
|
if ( events ) {
|
||||||
// Unbind all events for the element
|
// Unbind all events for the element
|
||||||
if ( types === undefined || (typeof types === "string" && types.charAt(0) == ".") )
|
if ( types === undefined || (typeof types === "string" && types.charAt(0) === ".") ) {
|
||||||
for ( var type in events )
|
for ( type in events ) {
|
||||||
this.remove( elem, type + (types || "") );
|
this.remove( elem, type + (types || "") );
|
||||||
else {
|
}
|
||||||
|
} else {
|
||||||
// types is actually an event object here
|
// types is actually an event object here
|
||||||
if ( types.type ) {
|
if ( types.type ) {
|
||||||
handler = types.handler;
|
handler = types.handler;
|
||||||
|
@ -112,7 +121,9 @@ jQuery.event = {
|
||||||
|
|
||||||
// Handle multiple events seperated by a space
|
// Handle multiple events seperated by a space
|
||||||
// jQuery(...).unbind("mouseover mouseout", fn);
|
// 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
|
// Namespaced event handlers
|
||||||
var namespaces = type.split(".");
|
var namespaces = type.split(".");
|
||||||
type = namespaces.shift();
|
type = namespaces.shift();
|
||||||
|
@ -121,40 +132,51 @@ jQuery.event = {
|
||||||
|
|
||||||
if ( events[type] ) {
|
if ( events[type] ) {
|
||||||
// remove the given handler for the given type
|
// remove the given handler for the given type
|
||||||
if ( handler )
|
if ( handler ) {
|
||||||
delete events[type][handler.guid];
|
delete events[ type ][ handler.guid ];
|
||||||
|
|
||||||
// remove all handlers for the given type
|
// remove all handlers for the given type
|
||||||
else
|
} else {
|
||||||
for ( var handle in events[type] )
|
for ( var handle in events[ type ] ) {
|
||||||
// Handle the removal of namespaced events
|
// 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];
|
delete events[ type ][ handle ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( jQuery.event.specialAll[type] )
|
if ( this.specialAll[ type ] ) {
|
||||||
jQuery.event.specialAll[type].teardown.call(elem, namespaces);
|
this.specialAll[ type ].teardown.call( elem, namespaces );
|
||||||
|
}
|
||||||
|
|
||||||
// remove generic event handler if no more handlers exist
|
// remove generic event handler if no more handlers exist
|
||||||
for ( ret in events[type] ) break;
|
for ( ret in events[ type ] ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if ( !ret ) {
|
if ( !ret ) {
|
||||||
if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem, namespaces) === false ) {
|
if ( !this.special[ type ] || this.special[ type ].teardown.call( elem, namespaces ) === false ) {
|
||||||
if (elem.removeEventListener)
|
if ( elem.removeEventListener ) {
|
||||||
elem.removeEventListener(type, jQuery.data(elem, "handle"), false);
|
elem.removeEventListener( type, jQuery.data( elem, "handle" ), false );
|
||||||
else if (elem.detachEvent)
|
} else if ( elem.detachEvent ) {
|
||||||
elem.detachEvent("on" + type, jQuery.data(elem, "handle"));
|
elem.detachEvent( "on" + type, jQuery.data( elem, "handle" ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ret = null;
|
ret = null;
|
||||||
delete events[type];
|
delete events[ type ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the expando if it's no longer used
|
// Remove the expando if it's no longer used
|
||||||
for ( ret in events ) break;
|
for ( ret in events ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if ( !ret ) {
|
if ( !ret ) {
|
||||||
var handle = jQuery.data( elem, "handle" );
|
var handle = jQuery.data( elem, "handle" );
|
||||||
if ( handle ) handle.elem = null;
|
if ( handle ) {
|
||||||
|
handle.elem = null;
|
||||||
|
}
|
||||||
jQuery.removeData( elem, "events" );
|
jQuery.removeData( elem, "events" );
|
||||||
jQuery.removeData( elem, "handle" );
|
jQuery.removeData( elem, "handle" );
|
||||||
}
|
}
|
||||||
|
@ -166,7 +188,7 @@ jQuery.event = {
|
||||||
// Event object or event type
|
// Event object or event type
|
||||||
var type = event.type || event;
|
var type = event.type || event;
|
||||||
|
|
||||||
if( !bubbling ){
|
if ( !bubbling ) {
|
||||||
event = typeof event === "object" ?
|
event = typeof event === "object" ?
|
||||||
// jQuery.Event object
|
// jQuery.Event object
|
||||||
event[expando] ? event :
|
event[expando] ? event :
|
||||||
|
@ -185,41 +207,46 @@ jQuery.event = {
|
||||||
// Don't bubble custom events when global (to avoid too much overhead)
|
// Don't bubble custom events when global (to avoid too much overhead)
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
// Only trigger if we've ever bound an event for it
|
// Only trigger if we've ever bound an event for it
|
||||||
if ( this.global[type] )
|
if ( this.global[ type ] ) {
|
||||||
jQuery.each( jQuery.cache, function(){
|
for ( var cached in jQuery.cache ) {
|
||||||
if ( this.events && this.events[type] )
|
if ( cached.events && cached.events[ type ] ) {
|
||||||
jQuery.event.trigger( event, data, this.handle.elem );
|
this.trigger( event, data, cached.handle.elem );
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle triggering a single element
|
// Handle triggering a single element
|
||||||
|
|
||||||
// don't do events on text and comment nodes
|
// 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;
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up in case it is reused
|
// Clean up in case it is reused
|
||||||
event.result = undefined;
|
event.result = undefined;
|
||||||
event.target = elem;
|
event.target = elem;
|
||||||
|
|
||||||
// Clone the incoming data, if any
|
// Clone the incoming data, if any
|
||||||
data = jQuery.makeArray(data);
|
data = jQuery.makeArray( data );
|
||||||
data.unshift( event );
|
data.unshift( event );
|
||||||
}
|
}
|
||||||
|
|
||||||
event.currentTarget = elem;
|
event.currentTarget = elem;
|
||||||
|
|
||||||
// Trigger the event, it is assumed that "handle" is a function
|
// Trigger the event, it is assumed that "handle" is a function
|
||||||
var handle = jQuery.data(elem, "handle");
|
var handle = jQuery.data( elem, "handle" );
|
||||||
if ( handle )
|
if ( handle ) {
|
||||||
handle.apply( elem, data );
|
handle.apply( elem, data );
|
||||||
|
}
|
||||||
|
|
||||||
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
|
// 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;
|
event.result = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Trigger the native events (except for clicks on links)
|
// 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;
|
this.triggered = true;
|
||||||
try {
|
try {
|
||||||
elem[ type ]();
|
elem[ type ]();
|
||||||
|
@ -231,12 +258,13 @@ jQuery.event = {
|
||||||
|
|
||||||
if ( !event.isPropagationStopped() ) {
|
if ( !event.isPropagationStopped() ) {
|
||||||
var parent = elem.parentNode || elem.ownerDocument;
|
var parent = elem.parentNode || elem.ownerDocument;
|
||||||
if ( parent )
|
if ( parent ) {
|
||||||
jQuery.event.trigger(event, data, parent, true);
|
jQuery.event.trigger( event, data, parent, true );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
handle: function(event) {
|
handle: function( event ) {
|
||||||
// returned undefined or false
|
// returned undefined or false
|
||||||
var all, handlers;
|
var all, handlers;
|
||||||
|
|
||||||
|
@ -252,10 +280,10 @@ jQuery.event = {
|
||||||
|
|
||||||
var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
|
var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
|
||||||
|
|
||||||
handlers = ( jQuery.data(this, "events") || {} )[event.type];
|
handlers = ( jQuery.data(this, "events") || {} )[ event.type ];
|
||||||
|
|
||||||
for ( var j in handlers ) {
|
for ( var j in handlers ) {
|
||||||
var handler = handlers[j];
|
var handler = handlers[ j ];
|
||||||
|
|
||||||
// Filter the functions by class
|
// Filter the functions by class
|
||||||
if ( all || namespace.test(handler.type) ) {
|
if ( all || namespace.test(handler.type) ) {
|
||||||
|
@ -264,9 +292,9 @@ jQuery.event = {
|
||||||
event.handler = handler;
|
event.handler = handler;
|
||||||
event.data = handler.data;
|
event.data = handler.data;
|
||||||
|
|
||||||
var ret = handler.apply(this, arguments);
|
var ret = handler.apply( this, arguments );
|
||||||
|
|
||||||
if( ret !== undefined ){
|
if ( ret !== undefined ) {
|
||||||
event.result = ret;
|
event.result = ret;
|
||||||
if ( ret === false ) {
|
if ( ret === false ) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
@ -274,8 +302,9 @@ jQuery.event = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( event.isImmediatePropagationStopped() )
|
if ( event.isImmediatePropagationStopped() ) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -283,57 +312,64 @@ 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(" "),
|
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) {
|
fix: function( event ) {
|
||||||
if ( event[expando] )
|
if ( event[ expando ] ) {
|
||||||
return event;
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
// store a copy of the original event object
|
// store a copy of the original event object
|
||||||
// and "clone" to set read-only properties
|
// and "clone" to set read-only properties
|
||||||
var originalEvent = event;
|
var originalEvent = event;
|
||||||
event = jQuery.Event( originalEvent );
|
event = jQuery.Event( originalEvent );
|
||||||
|
|
||||||
for ( var i = this.props.length, prop; i; ){
|
for ( var i = this.props.length, prop; i; ) {
|
||||||
prop = this.props[ --i ];
|
prop = this.props[ --i ];
|
||||||
event[ prop ] = originalEvent[ prop ];
|
event[ prop ] = originalEvent[ prop ];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix target property, if necessary
|
// 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
|
event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
|
||||||
|
}
|
||||||
|
|
||||||
// check if target is a textnode (safari)
|
// check if target is a textnode (safari)
|
||||||
if ( event.target.nodeType == 3 )
|
if ( event.target.nodeType === 3 ) {
|
||||||
event.target = event.target.parentNode;
|
event.target = event.target.parentNode;
|
||||||
|
}
|
||||||
|
|
||||||
// Add relatedTarget, if necessary
|
// Add relatedTarget, if necessary
|
||||||
if ( !event.relatedTarget && event.fromElement )
|
if ( !event.relatedTarget && event.fromElement ) {
|
||||||
event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
|
event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate pageX/Y if missing and clientX/Y available
|
// Calculate pageX/Y if missing and clientX/Y available
|
||||||
if ( event.pageX == null && event.clientX != null ) {
|
if ( event.pageX == null && event.clientX != null ) {
|
||||||
var doc = document.documentElement, body = document.body;
|
var doc = document.documentElement, body = document.body;
|
||||||
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
|
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
|
||||||
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
|
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add which for key events
|
// 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;
|
event.which = event.charCode || event.keyCode;
|
||||||
|
}
|
||||||
|
|
||||||
// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
|
// 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;
|
event.metaKey = event.ctrlKey;
|
||||||
|
}
|
||||||
|
|
||||||
// Add which for click: 1 == left; 2 == middle; 3 == right
|
// Add which for click: 1 == left; 2 == middle; 3 == right
|
||||||
// Note: button is not normalized, so don't use it
|
// 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 ) ));
|
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
|
||||||
|
}
|
||||||
|
|
||||||
return event;
|
return event;
|
||||||
},
|
},
|
||||||
|
|
||||||
proxy: function( fn, proxy ){
|
proxy: function( fn, proxy ) {
|
||||||
proxy = proxy || function(){ return fn.apply(this, arguments); };
|
proxy = proxy || function() { return fn.apply( this, arguments ); };
|
||||||
// Set the guid of unique handler to the same of original handler, so it can be removed
|
// Set the guid of unique handler to the same of original handler, so it can be removed
|
||||||
proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
|
proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
|
||||||
// So proxy can be declared as an argument
|
// So proxy can be declared as an argument
|
||||||
|
@ -350,20 +386,22 @@ jQuery.event = {
|
||||||
|
|
||||||
specialAll: {
|
specialAll: {
|
||||||
live: {
|
live: {
|
||||||
setup: function( selector, namespaces ){
|
setup: function( selector, namespaces ) {
|
||||||
jQuery.event.add( this, namespaces[0], liveHandler );
|
jQuery.event.add( this, namespaces[0], liveHandler );
|
||||||
},
|
},
|
||||||
teardown: function( namespaces ){
|
teardown: function( namespaces ) {
|
||||||
if ( namespaces.length ) {
|
if ( namespaces.length ) {
|
||||||
var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)");
|
var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)");
|
||||||
|
|
||||||
jQuery.each( (jQuery.data(this, "events").live || {}), function(){
|
jQuery.each( (jQuery.data(this, "events").live || {}), function() {
|
||||||
if ( name.test(this.type) )
|
if ( name.test(this.type) ) {
|
||||||
remove++;
|
remove++;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if ( remove < 1 )
|
if ( remove < 1 ) {
|
||||||
jQuery.event.remove( this, namespaces[0], liveHandler );
|
jQuery.event.remove( this, namespaces[0], liveHandler );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -372,29 +410,31 @@ jQuery.event = {
|
||||||
|
|
||||||
jQuery.Event = function( src ){
|
jQuery.Event = function( src ){
|
||||||
// Allow instantiation without the 'new' keyword
|
// Allow instantiation without the 'new' keyword
|
||||||
if( !this.preventDefault )
|
if ( !this.preventDefault ) {
|
||||||
return new jQuery.Event(src);
|
return new jQuery.Event( src );
|
||||||
|
}
|
||||||
|
|
||||||
// Event object
|
// Event object
|
||||||
if( src && src.type ){
|
if ( src && src.type ) {
|
||||||
this.originalEvent = src;
|
this.originalEvent = src;
|
||||||
this.type = src.type;
|
this.type = src.type;
|
||||||
// Event type
|
// Event type
|
||||||
}else
|
} else {
|
||||||
this.type = src;
|
this.type = src;
|
||||||
|
}
|
||||||
|
|
||||||
// timeStamp is buggy for some events on Firefox(#3843)
|
// timeStamp is buggy for some events on Firefox(#3843)
|
||||||
// So we won't rely on the native value
|
// So we won't rely on the native value
|
||||||
this.timeStamp = now();
|
this.timeStamp = now();
|
||||||
|
|
||||||
// Mark it as fixed
|
// Mark it as fixed
|
||||||
this[expando] = true;
|
this[ expando ] = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
function returnFalse(){
|
function returnFalse() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
function returnTrue(){
|
function returnTrue() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,11 +445,13 @@ jQuery.Event.prototype = {
|
||||||
this.isDefaultPrevented = returnTrue;
|
this.isDefaultPrevented = returnTrue;
|
||||||
|
|
||||||
var e = this.originalEvent;
|
var e = this.originalEvent;
|
||||||
if( !e )
|
if ( !e ) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
// if preventDefault exists run it on the original event
|
// if preventDefault exists run it on the original event
|
||||||
if (e.preventDefault)
|
if ( e.preventDefault ) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
}
|
||||||
// otherwise set the returnValue property of the original event to false (IE)
|
// otherwise set the returnValue property of the original event to false (IE)
|
||||||
e.returnValue = false;
|
e.returnValue = false;
|
||||||
},
|
},
|
||||||
|
@ -417,15 +459,17 @@ jQuery.Event.prototype = {
|
||||||
this.isPropagationStopped = returnTrue;
|
this.isPropagationStopped = returnTrue;
|
||||||
|
|
||||||
var e = this.originalEvent;
|
var e = this.originalEvent;
|
||||||
if( !e )
|
if ( !e ) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
// if stopPropagation exists run it on the original event
|
// if stopPropagation exists run it on the original event
|
||||||
if (e.stopPropagation)
|
if ( e.stopPropagation ) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
}
|
||||||
// otherwise set the cancelBubble property of the original event to true (IE)
|
// otherwise set the cancelBubble property of the original event to true (IE)
|
||||||
e.cancelBubble = true;
|
e.cancelBubble = true;
|
||||||
},
|
},
|
||||||
stopImmediatePropagation:function(){
|
stopImmediatePropagation: function(){
|
||||||
this.isImmediatePropagationStopped = returnTrue;
|
this.isImmediatePropagationStopped = returnTrue;
|
||||||
this.stopPropagation();
|
this.stopPropagation();
|
||||||
},
|
},
|
||||||
|
@ -435,15 +479,16 @@ jQuery.Event.prototype = {
|
||||||
};
|
};
|
||||||
// Checks if an event happened on an element within another element
|
// Checks if an event happened on an element within another element
|
||||||
// Used in jQuery.event.special.mouseenter and mouseleave handlers
|
// Used in jQuery.event.special.mouseenter and mouseleave handlers
|
||||||
var withinElement = function(event) {
|
var withinElement = function( event ) {
|
||||||
// Check if mouse(over|out) are still within the same parent element
|
// Check if mouse(over|out) are still within the same parent element
|
||||||
var parent = event.relatedTarget;
|
var parent = event.relatedTarget;
|
||||||
// Traverse up the tree
|
// Traverse up the tree
|
||||||
while ( parent && parent != this )
|
while ( parent && parent != this ) {
|
||||||
try { parent = parent.parentNode; }
|
try { parent = parent.parentNode; }
|
||||||
catch(e) { parent = this; }
|
catch(e) { parent = this; }
|
||||||
|
}
|
||||||
|
|
||||||
if( parent != this ){
|
if ( parent != this ) {
|
||||||
// set the correct event type
|
// set the correct event type
|
||||||
event.type = event.data;
|
event.type = event.data;
|
||||||
// handle event if we actually just moused on to a non sub-element
|
// handle event if we actually just moused on to a non sub-element
|
||||||
|
@ -454,7 +499,7 @@ var withinElement = function(event) {
|
||||||
jQuery.each({
|
jQuery.each({
|
||||||
mouseover: 'mouseenter',
|
mouseover: 'mouseenter',
|
||||||
mouseout: 'mouseleave'
|
mouseout: 'mouseleave'
|
||||||
}, function( orig, fix ){
|
}, function( orig, fix ) {
|
||||||
jQuery.event.special[ fix ] = {
|
jQuery.event.special[ fix ] = {
|
||||||
setup: function(){
|
setup: function(){
|
||||||
jQuery.event.add( this, orig, withinElement, fix );
|
jQuery.event.add( this, orig, withinElement, fix );
|
||||||
|
@ -467,36 +512,36 @@ jQuery.each({
|
||||||
|
|
||||||
jQuery.fn.extend({
|
jQuery.fn.extend({
|
||||||
bind: function( type, data, fn ) {
|
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 );
|
jQuery.event.add( this, type, fn || data, fn && data );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
one: function( type, data, fn ) {
|
one: function( type, data, fn ) {
|
||||||
var one = jQuery.event.proxy( fn || data, function(event) {
|
var one = jQuery.event.proxy( fn || data, function( event ) {
|
||||||
jQuery(this).unbind(event, one);
|
jQuery( this ).unbind( event, one );
|
||||||
return (fn || data).apply( this, arguments );
|
return (fn || data).apply( this, arguments );
|
||||||
});
|
});
|
||||||
return this.each(function(){
|
return this.each(function() {
|
||||||
jQuery.event.add( this, type, one, fn && data);
|
jQuery.event.add( this, type, one, fn && data );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
unbind: function( type, fn ) {
|
unbind: function( type, fn ) {
|
||||||
return this.each(function(){
|
return this.each(function() {
|
||||||
jQuery.event.remove( this, type, fn );
|
jQuery.event.remove( this, type, fn );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
trigger: function( type, data ) {
|
trigger: function( type, data ) {
|
||||||
return this.each(function(){
|
return this.each(function() {
|
||||||
jQuery.event.trigger( type, data, this );
|
jQuery.event.trigger( type, data, this );
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
triggerHandler: function( type, data ) {
|
triggerHandler: function( type, data ) {
|
||||||
if( this[0] ){
|
if ( this[0] ) {
|
||||||
var event = jQuery.Event(type);
|
var event = jQuery.Event( type );
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
jQuery.event.trigger( event, data, this[0] );
|
jQuery.event.trigger( event, data, this[0] );
|
||||||
|
@ -509,10 +554,11 @@ jQuery.fn.extend({
|
||||||
var args = arguments, i = 1;
|
var args = arguments, i = 1;
|
||||||
|
|
||||||
// link all the functions, so any of them can unbind this click handler
|
// 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++] );
|
jQuery.event.proxy( fn, args[ i++ ] );
|
||||||
|
}
|
||||||
|
|
||||||
return this.click( jQuery.event.proxy( fn, function(event) {
|
return this.click( jQuery.event.proxy( fn, function( event ) {
|
||||||
// Figure out which function to execute
|
// Figure out which function to execute
|
||||||
this.lastToggle = ( this.lastToggle || 0 ) % i;
|
this.lastToggle = ( this.lastToggle || 0 ) % i;
|
||||||
|
|
||||||
|
@ -524,69 +570,71 @@ jQuery.fn.extend({
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
hover: function(fnOver, fnOut) {
|
hover: function( fnOver, fnOut ) {
|
||||||
return this.mouseenter(fnOver).mouseleave(fnOut);
|
return this.mouseenter( fnOver ).mouseleave( fnOut );
|
||||||
},
|
},
|
||||||
|
|
||||||
ready: function(fn) {
|
ready: function( fn ) {
|
||||||
// Attach the listeners
|
// Attach the listeners
|
||||||
bindReady();
|
bindReady();
|
||||||
|
|
||||||
// If the DOM is already ready
|
// If the DOM is already ready
|
||||||
if ( jQuery.isReady )
|
if ( jQuery.isReady ) {
|
||||||
// Execute the function immediately
|
// Execute the function immediately
|
||||||
fn.call( document, jQuery );
|
fn.call( document, jQuery );
|
||||||
|
|
||||||
// Otherwise, remember the function for later
|
// Otherwise, remember the function for later
|
||||||
else
|
} else {
|
||||||
// Add the function to the wait list
|
// Add the function to the wait list
|
||||||
jQuery.readyList.push( fn );
|
jQuery.readyList.push( fn );
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
live: function( type, fn ){
|
live: function( type, fn ) {
|
||||||
var proxy = jQuery.event.proxy( fn );
|
var proxy = jQuery.event.proxy( fn );
|
||||||
proxy.guid += this.selector + type;
|
proxy.guid += this.selector + type;
|
||||||
|
|
||||||
jQuery( this.context ).bind( liveConvert(type, this.selector), this.selector, proxy );
|
jQuery( this.context ).bind( liveConvert( type, this.selector ), this.selector, proxy );
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
die: function( type, fn ){
|
die: function( type, fn ) {
|
||||||
jQuery( this.context ).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector + type } : null );
|
jQuery( this.context ).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector + type } : null );
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function liveHandler( event ){
|
function liveHandler( event ) {
|
||||||
var check = new RegExp("(^|\\.)" + event.type + "(\\.|$)"),
|
var check = new RegExp("(^|\\.)" + event.type + "(\\.|$)"),
|
||||||
stop = true,
|
stop = true, elems = [];
|
||||||
elems = [];
|
|
||||||
|
|
||||||
jQuery.each(jQuery.data(this, "events").live || [], function(i, fn){
|
jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
|
||||||
if ( check.test(fn.type) ) {
|
if ( check.test( fn.type ) ) {
|
||||||
var elem = jQuery(event.target).closest(fn.data)[0];
|
var elem = jQuery( event.target ).closest( fn.data )[0];
|
||||||
if ( elem )
|
if ( elem ) {
|
||||||
elems.push({ elem: elem, fn: fn });
|
elems.push({ elem: elem, fn: fn });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
elems.sort(function(a,b) {
|
elems.sort(function( a, b ) {
|
||||||
return jQuery.data(a.elem, "closest") - jQuery.data(b.elem, "closest");
|
return jQuery.data( a.elem, "closest" ) - jQuery.data( b.elem, "closest" );
|
||||||
});
|
});
|
||||||
|
|
||||||
jQuery.each(elems, function(){
|
jQuery.each(elems, function() {
|
||||||
event.currentTarget = this.elem;
|
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 = false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return stop;
|
return stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
function liveConvert(type, selector){
|
function liveConvert( type, selector ) {
|
||||||
return ["live", type, selector.replace(/\./g, "`").replace(/ /g, "|")].join(".");
|
return ["live", type, selector.replace(/\./g, "`").replace(/ /g, "|")].join(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -603,30 +651,31 @@ jQuery.extend({
|
||||||
// If there are functions bound, to execute
|
// If there are functions bound, to execute
|
||||||
if ( jQuery.readyList ) {
|
if ( jQuery.readyList ) {
|
||||||
// Execute all of them
|
// Execute all of them
|
||||||
jQuery.each( jQuery.readyList, function(){
|
var fn, i = 0;
|
||||||
this.call( document, jQuery );
|
while ( (fn = jQuery.readyList[ i++ ]) ) {
|
||||||
});
|
fn.call( document, jQuery );
|
||||||
|
}
|
||||||
|
|
||||||
// Reset the list of functions
|
// Reset the list of functions
|
||||||
jQuery.readyList = null;
|
jQuery.readyList = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trigger any bound ready events
|
// Trigger any bound ready events
|
||||||
jQuery(document).triggerHandler("ready");
|
jQuery( document ).triggerHandler( "ready" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var readyBound = false;
|
var readyBound = false;
|
||||||
|
|
||||||
function bindReady(){
|
function bindReady() {
|
||||||
if ( readyBound ) return;
|
if ( readyBound ) return;
|
||||||
readyBound = true;
|
readyBound = true;
|
||||||
|
|
||||||
// Mozilla, Opera and webkit nightlies currently support this event
|
// Mozilla, Opera and webkit nightlies currently support this event
|
||||||
if ( document.addEventListener ) {
|
if ( document.addEventListener ) {
|
||||||
// Use the handy event callback
|
// Use the handy event callback
|
||||||
document.addEventListener( "DOMContentLoaded", function(){
|
document.addEventListener( "DOMContentLoaded", function() {
|
||||||
document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
|
document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
|
||||||
jQuery.ready();
|
jQuery.ready();
|
||||||
}, false );
|
}, false );
|
||||||
|
@ -635,7 +684,7 @@ function bindReady(){
|
||||||
} else if ( document.attachEvent ) {
|
} else if ( document.attachEvent ) {
|
||||||
// ensure firing before onload,
|
// ensure firing before onload,
|
||||||
// maybe late but safe also for iframes
|
// maybe late but safe also for iframes
|
||||||
document.attachEvent("onreadystatechange", function(){
|
document.attachEvent("onreadystatechange", function() {
|
||||||
if ( document.readyState === "complete" ) {
|
if ( document.readyState === "complete" ) {
|
||||||
document.detachEvent( "onreadystatechange", arguments.callee );
|
document.detachEvent( "onreadystatechange", arguments.callee );
|
||||||
jQuery.ready();
|
jQuery.ready();
|
||||||
|
@ -644,8 +693,10 @@ function bindReady(){
|
||||||
|
|
||||||
// If IE and not an iframe
|
// If IE and not an iframe
|
||||||
// continually check to see if the document is ready
|
// continually check to see if the document is ready
|
||||||
if ( document.documentElement.doScroll && window == window.top ) (function(){
|
if ( document.documentElement.doScroll && window === window.top ) (function() {
|
||||||
if ( jQuery.isReady ) return;
|
if ( jQuery.isReady ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// If IE is used, use the trick by Diego Perini
|
// If IE is used, use the trick by Diego Perini
|
||||||
|
@ -667,11 +718,11 @@ function bindReady(){
|
||||||
|
|
||||||
jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
|
jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
|
||||||
"mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
|
"mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
|
||||||
"change,select,submit,keydown,keypress,keyup,error").split(","), function(i, name){
|
"change,select,submit,keydown,keypress,keyup,error").split(","), function( i, name ) {
|
||||||
|
|
||||||
// Handle event binding
|
// Handle event binding
|
||||||
jQuery.fn[name] = function(fn){
|
jQuery.fn[ name ] = function( fn ) {
|
||||||
return fn ? this.bind(name, fn) : this.trigger(name);
|
return fn ? this.bind (name, fn ) : this.trigger( name );
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -681,9 +732,11 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
|
||||||
// More info:
|
// More info:
|
||||||
// - http://isaacschlueter.com/2006/10/msie-memory-leaks/
|
// - http://isaacschlueter.com/2006/10/msie-memory-leaks/
|
||||||
// - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
|
||||||
jQuery( window ).bind( 'unload', function(){
|
jQuery( window ).bind( 'unload', function() {
|
||||||
for ( var id in jQuery.cache )
|
for ( var id in jQuery.cache ) {
|
||||||
// Skip the window
|
// 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 );
|
jQuery.event.remove( jQuery.cache[ id ].handle.elem );
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue