jquery/src/event.js

1120 lines
32 KiB
JavaScript
Raw Normal View History

(function( jQuery ) {
var rnamespaces = /\.(.*)$/,
rformElems = /^(?:textarea|input|select)$/i,
rperiod = /\./g,
rspaces = / /g,
rescape = /[^\w\s.|`]/g,
rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/,
rhoverHack = /\bhover(\.\S+)?/,
2011-09-20 17:14:59 +00:00
rmouseEvent = /^(?:mouse|contextmenu)|click/,
rkeyEvent = /^(?:key)/,
rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?(?::(first-child|last-child|empty))?$/,
quickPseudoMap = {
"empty": "firstChild",
"first-child": "previousSibling",
"last-child": "nextSibling"
},
quickParse = function( selector ) {
var quick = rquickIs.exec( selector );
if ( quick ) {
// 0 1 2 3 4 5 6
// [ _, tag, id, class, attrName, attrValue, :(empty first-child last-child) ]
quick[1] = ( quick[1] || "" ).toLowerCase();
quick[3] = quick[3] && new RegExp( "\\b" + quick[3] + "\\b" );
quick[6] = quickPseudoMap[ quick[6] ];
}
return quick;
},
quickIs = function( elem, m ) {
return (
(!m[1] || elem.nodeName.toLowerCase() === m[1]) &&
(!m[2] || elem.id === m[2]) &&
(!m[3] || m[3].test( elem.className )) &&
(!m[4] || elem.getAttribute( m[4] ) == m[5]) &&
(!m[6] || !elem[ m[6] ])
);
},
useNativeMethod = function( event ) {
// IE throws error on focus/blur of a hidden element (#1486)
var type = event.type;
if ( !event.isDefaultPrevented() && this[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) ) {
this[ type ]();
return false;
}
};
/*
2011-09-08 03:21:33 +00:00
* Helper functions for managing events -- not part of the public interface.
* Props to Dean Edwards' addEvent library for many of the ideas.
*/
jQuery.event = {
add: function( elem, types, handler, data, selector ) {
var elemData, eventHandle, events,
t, tns, type, namespaces, handleObj,
handleObjIn, quick, handlers, special;
// Don't attach events to noData or text/comment nodes (allow plain objects tho)
if ( elem.nodeType === 3 || elem.nodeType === 8 || !types || !handler || !(elemData = jQuery._data( elem )) ) {
2011-02-15 21:03:23 +00:00
return;
}
// Caller can pass in an object of custom data in lieu of the handler
if ( handler.handler ) {
handleObjIn = handler;
handler = handleObjIn.handler;
}
// Make sure that the handler has a unique ID, used to find/remove it later
2009-04-30 01:26:09 +00:00
if ( !handler.guid ) {
2009-12-31 20:06:45 +00:00
handler.guid = jQuery.guid++;
2009-04-30 01:26:09 +00:00
}
// Init the element's event structure and main handler, if this is the first
events = elemData.events;
if ( !events ) {
elemData.events = events = {};
}
eventHandle = elemData.handle;
if ( !eventHandle ) {
elemData.handle = eventHandle = function( e ) {
// Discard the second event of a jQuery.event.trigger() and
// when an event is called after a page has unloaded
return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
jQuery.event.handle.apply( eventHandle.elem, arguments ) :
undefined;
};
// Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
eventHandle.elem = elem;
}
// Handle multiple events separated by a space
// jQuery(...).bind("mouseover mouseout", fn);
types = types.replace( rhoverHack, "mouseover$1 mouseout$1" ).split( " " );
for ( t = 0; t < types.length; t++ ) {
tns = rtypenamespace.exec( types[t] ) || [];
type = tns[1];
namespaces = (tns[2] || "").split( "." ).sort();
// If event changes its type, use the special event handlers for the changed type
special = jQuery.event.special[ type ] || {};
type = (selector? special.delegateType : special.bindType ) || type;
special = jQuery.event.special[ type ] || {};
// handleObj is passed to all event handlers
handleObj = jQuery.extend({
type: type,
origType: tns[1],
data: data,
handler: handler,
guid: handler.guid,
selector: selector,
namespace: namespaces.join(".")
}, handleObjIn);
// Delegated event; pre-analyze selector so it's processed quickly on event dispatch
if ( selector ) {
handleObj.quick = quickParse( selector );
if ( !handleObj.quick && jQuery.expr.match.POS.test( selector ) ) {
handleObj.isPositional = true;
}
}
// Init the event handler queue if we're the first
handlers = events[ type ];
2009-04-30 01:26:09 +00:00
if ( !handlers ) {
handlers = events[ type ] = [];
handlers.delegateCount = 0;
// Only use addEventListener/attachEvent if the special events handler returns false
if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
// Bind the global event handler to the element
2009-04-30 01:26:09 +00:00
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
2009-04-30 01:26:09 +00:00
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
2009-04-30 01:26:09 +00:00
}
}
}
2010-12-26 20:28:13 +00:00
if ( special.add ) {
special.add.call( elem, handleObj );
if ( !handleObj.handler.guid ) {
handleObj.handler.guid = handler.guid;
}
}
// Add to the element's handler list, delegates in front
if ( selector ) {
handlers.splice( handlers.delegateCount++, 0, handleObj );
} else {
handlers.push( handleObj );
}
// Keep track of which events have ever been used, for event optimization
jQuery.event.global[ type ] = true;
2009-04-30 01:26:09 +00:00
}
// Nullify elem to prevent memory leaks in IE
elem = null;
},
global: {},
// Detach an event or set of events from an element
remove: function( elem, types, handler, selector ) {
var elemData = jQuery.hasData( elem ) && jQuery._data( elem ),
t, tns, type, namespaces, origCount,
j, events, special, handle, eventType, handleObj;
if ( !elemData || !(events = elemData.events) ) {
return;
}
2010-12-26 20:28:13 +00:00
// For removal, types can be an Event object
if ( types && types.type && types.handler ) {
handler = types.handler;
types = types.type;
selector = types.selector;
}
// Once for each type.namespace in types; type may be omitted
types = (types || "").replace( rhoverHack, "mouseover$1 mouseout$1" ).split(" ");
for ( t = 0; t < types.length; t++ ) {
tns = rtypenamespace.exec( types[t] ) || [];
type = tns[1];
namespaces = tns[2];
// Unbind all events (on this namespace, if provided) for the element
if ( !type ) {
namespaces = namespaces? "." + namespaces : "";
for ( j in events ) {
jQuery.event.remove( elem, j + namespaces, handler, selector );
}
return;
}
2009-03-23 01:55:17 +00:00
special = jQuery.event.special[ type ] || {};
type = (selector? special.delegateType : special.bindType ) || type;
eventType = events[ type ] || [];
origCount = eventType.length;
namespaces = namespaces? new RegExp("(^|\\.)" + namespaces.split(".").sort().join("\\.(?:.*\\.)?") + "(\\.|$)") : null;
// Only need to loop for special events or selective removal
if ( handler || namespaces || selector || special.remove ) {
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
if ( !handler || handler.guid === handleObj.guid ) {
if ( !namespaces || namespaces.test( handleObj.namespace ) ) {
if ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) {
eventType.splice( j--, 1 );
if ( handleObj.selector ) {
eventType.delegateCount--;
}
if ( special.remove ) {
special.remove.call( elem, handleObj );
}
}
}
}
2009-04-30 01:26:09 +00:00
}
} else {
// Removing all events
eventType.length = 0;
}
// Remove generic event handler if we removed something and no more handlers exist
// (avoids potential for endless recursion during removal of special event handlers)
if ( eventType.length === 0 && origCount !== eventType.length ) {
if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {
jQuery.removeEvent( elem, type, elemData.handle );
}
delete events[ type ];
2009-04-30 01:26:09 +00:00
}
}
// Remove the expando if it's no longer used
if ( jQuery.isEmptyObject( events ) ) {
handle = elemData.handle;
if ( handle ) {
handle.elem = null;
}
// removeData also checks for emptiness and clears the expando if empty
// so use it instead of delete
jQuery.removeData( elem, [ "events", "handle" ], true );
}
},
// Events that are safe to short-circuit if no handlers are attached.
// Native DOM events should not be added, they may have inline handlers.
customEvent: {
"getData": true,
"setData": true,
"changeData": true
},
trigger: function( event, data, elem, onlyHandlers ) {
// Don't do events on text and comment nodes
if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
return;
}
// Event object or event type
2009-05-10 14:38:35 +00:00
var type = event.type || event,
namespaces = [],
exclusive, i, cur, old, ontype, special, doc, eventPath, bubbleType,
addHandlers = function( elem, type ) {
2011-09-08 03:21:33 +00:00
// Defer getting handler so we don't waste time in case propagation is stopped
if ( (jQuery._data( elem, "events" ) || {})[ type ] ) {
eventPath.push({ elem: elem, type: type /*, handler: jQuery._data( elem, "handle" ) */ });
}
// IE doesn't like method names with a colon (#3533, #8272)
if ( ontype && jQuery.acceptData( elem ) && elem[ ontype ] ) {
eventPath.push({ elem: elem, type: type, handler: elem[ ontype ] });
}
};
2011-09-08 03:21:33 +00:00
if ( type.indexOf( "!" ) >= 0 ) {
// Exclusive events trigger only for the exact event (no namespaces)
type = type.slice(0, -1);
exclusive = true;
}
2011-04-12 18:58:55 +00:00
2011-09-08 03:21:33 +00:00
if ( type.indexOf( "." ) >= 0 ) {
// Namespaced trigger; create a regexp to match event type in handle()
namespaces = type.split(".");
type = namespaces.shift();
namespaces.sort();
}
if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) {
// No jQuery handlers for this event type, and it can't have inline handlers
return;
}
// Caller can pass in an Event, Object, or just an event type string
event = typeof event === "object" ?
// jQuery.Event object
event[ jQuery.expando ] ? event :
// Object literal
new jQuery.Event( type, event ) :
// Just the event type (string)
new jQuery.Event( type );
2011-04-12 18:58:55 +00:00
event.type = type;
event.exclusive = exclusive;
2011-09-08 03:21:33 +00:00
event.namespace = namespaces.join( "." );
event.namespace_re = event.namespace? new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.)?") + "(\\.|$)") : null;
ontype = type.indexOf( ":" ) < 0 ? "on" + type : "";
// triggerHandler() and global events don't bubble or run the default action
if ( onlyHandlers || !elem ) {
event.preventDefault();
event.stopPropagation();
}
// Handle a global trigger
if ( !elem ) {
// TODO: Stop taunting the data cache; remove global events and always attach to document
var cache = jQuery.cache;
for ( i in cache ) {
if ( cache[ i ].events && cache[ i ].events[ type ] ) {
jQuery.event.trigger( event, data, cache[ i ].handle.elem );
}
}
return;
}
// Clean up the event in case it is being reused
event.result = undefined;
event.target = elem;
2009-03-23 01:55:17 +00:00
// Clone any incoming data and prepend the event, creating the handler arg list
data = data != null ? jQuery.makeArray( data ) : [];
data.unshift( event );
// Allow special events to draw outside the lines
special = jQuery.event.special[ type ] || {};
if ( special.trigger && special.trigger.apply( elem, data ) === false ) {
return;
}
// Determine event propagation path in advance, per W3C events spec (#9951)
// Bubble up to document, then to window; watch for a global ownerDocument var (#9724)
// Always fire handlers for the target, even if prop is stopped in advance
eventPath = [];
addHandlers( elem, special.bindType || type );
doc = elem.ownerDocument;
if ( doc && !jQuery.isWindow( elem ) & !event.isPropagationStopped() ) {
bubbleType = special.delegateType || type;
for ( cur = elem.parentNode; cur; cur = cur.parentNode ) {
addHandlers( cur, bubbleType );
}
addHandlers( doc.defaultView || doc.parentWindow || window, bubbleType );
}
// Bubble up the DOM tree
for ( i = 0; i < eventPath.length; i++ ) {
cur = eventPath[ i ];
event.type = cur.type;
(cur.handler || jQuery._data( cur.elem, "handle" )).apply( cur.elem, data );
if ( event.isPropagationStopped() ) {
break;
}
}
event.type = type;
// If nobody prevented the default action, do it now
if ( !event.isDefaultPrevented() ) {
if ( (!special._default || special._default.call( elem.ownerDocument, event, data ) === false) &&
!(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {
// Call a native DOM method on the target with the same name name as the event.
// Can't use an .isFunction() check here because IE6/7 fails that test.
// Don't do default actions on window, that's where global variables be (#6170)
// IE<9 dies on focus/blur to hidden element (#1486)
if ( ontype && elem[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) && !jQuery.isWindow( elem ) ) {
// Don't re-trigger an onFOO event when we call its FOO() method
old = elem[ ontype ];
if ( old ) {
elem[ ontype ] = null;
}
// Prevent re-triggering of the same event, since we already bubbled it above
jQuery.event.triggered = type;
elem[ type ]();
jQuery.event.triggered = undefined;
if ( old ) {
elem[ ontype ] = old;
}
}
2009-04-30 01:26:09 +00:00
}
}
return event.result;
},
2009-04-30 01:26:09 +00:00
handle: function( event ) {
// Make a writable jQuery.Event from the native event object
event = jQuery.event.fix( event || window.event );
2011-09-22 15:12:42 +00:00
var propHook = jQuery.event.propHooks[ event.type ];
if ( propHook ) {
event = propHook( event );
}
var handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []),
delegateCount = handlers.delegateCount,
args = Array.prototype.slice.call( arguments, 0 ),
handlerQueue = [],
i, cur, selMatch, matches, handleObj, sel, hit, related;
// Use the fix-ed jQuery.Event rather than the (read-only) native event
args[0] = event;
// Determine handlers that should run if there are delegated events
// Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
if ( delegateCount && !event.target.disabled && !(event.button && event.type === "click") ) {
for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
selMatch = {};
matches = [];
for ( i = 0; i < delegateCount; i++ ) {
handleObj = handlers[ i ];
sel = handleObj.selector;
hit = selMatch[ sel ];
if ( handleObj.isPositional ) {
// Since .is() does not work for positionals; see http://jsfiddle.net/eJ4yd/3/
hit = (hit || (selMatch[ sel ] = jQuery( sel ))).index( cur ) >= 0;
2011-08-17 14:34:13 +00:00
} else if ( hit === undefined ) {
hit = selMatch[ sel ] = (handleObj.quick? quickIs( cur, handleObj.quick ) : jQuery( cur ).is( sel ));
}
if ( hit ) {
matches.push( handleObj );
}
}
if ( matches.length ) {
handlerQueue.push({ elem: cur, matches: matches });
2009-04-30 01:26:09 +00:00
}
}
}
// Copy the remaining (bound) handlers in case they're changed
handlers = handlers.slice( delegateCount );
// Run delegates first; they may want to stop propagation beneath us
event.delegateTarget = this;
2011-09-08 03:21:33 +00:00
for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
matched = handlerQueue[ i ];
dispatch( matched.elem, event, matched.matches, args );
}
delete event.delegateTarget;
// Run non-delegated handlers for this level
if ( handlers.length ) {
dispatch( this, event, handlers, args );
}
return event.result;
},
2011-09-20 18:54:33 +00:00
props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
propHooks: {},
2009-04-30 01:26:09 +00:00
fix: function( event ) {
if ( event[ jQuery.expando ] ) {
return event;
2009-04-30 01:26:09 +00:00
}
// store a copy of the original event object
// and "clone" to set read-only properties
var originalEvent = event,
propHook;
event = jQuery.Event( originalEvent );
propHook = jQuery.event.propHooks[ event.type ];
2009-04-30 01:26:09 +00:00
for ( var i = this.props.length, prop; i; ) {
prop = this.props[ --i ];
event[ prop ] = originalEvent[ prop ];
}
// Fix target property, if necessary
// Removal will crash IE6,7,8
2009-04-30 01:26:09 +00:00
if ( !event.target ) {
// Fixes #1925 where srcElement might not be defined either
event.target = event.srcElement || document;
2009-04-30 01:26:09 +00:00
}
// check if target is a textnode (safari)
// Removal will crash IE6,7,8
2009-04-30 01:26:09 +00:00
if ( event.target.nodeType === 3 ) {
event.target = event.target.parentNode;
2009-04-30 01:26:09 +00:00
}
// Add relatedTarget, if necessary
2009-04-30 01:26:09 +00:00
if ( !event.relatedTarget && event.fromElement ) {
event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
}
if ( propHook ) {
event = propHook( event, originalEvent );
2009-04-30 01:26:09 +00:00
}
return event;
},
// Deprecated, use jQuery.guid instead
guid: 1E8,
// Deprecated, use jQuery.proxy instead
2009-12-31 20:06:45 +00:00
proxy: jQuery.proxy,
special: {
ready: {
// Make sure the ready event is setup
setup: jQuery.bindReady
},
focus: {
delegateType: "focusin",
trigger: useNativeMethod
},
blur: {
delegateType: "focusout",
trigger: useNativeMethod
},
beforeunload: {
setup: function( data, namespaces, eventHandle ) {
// We only want to do this special case on windows
if ( jQuery.isWindow( this ) ) {
this.onbeforeunload = eventHandle;
}
},
teardown: function( namespaces, eventHandle ) {
if ( this.onbeforeunload === eventHandle ) {
this.onbeforeunload = null;
}
}
}
}
};
// Run jQuery handler functions; called from jQuery.event.handle
function dispatch( target, event, handlers, args ) {
var run_all = !event.exclusive && !event.namespace,
specialHandle = (jQuery.event.special[event.type] || {}).handle,
j, handleObj, ret;
event.currentTarget = target;
2011-09-12 00:44:44 +00:00
for ( j = 0; j < handlers.length && !event.isImmediatePropagationStopped(); j++ ) {
handleObj = handlers[ j ];
// Triggered event must either 1) be non-exclusive and have no namespace, or
// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {
// Pass in a reference to the handler function itself
// So that we can later remove it
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;
ret = (specialHandle || handleObj.handler).apply( target, args );
if ( ret !== undefined ) {
event.result = ret;
if ( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
}
}
}
jQuery.removeEvent = document.removeEventListener ?
function( elem, type, handle ) {
if ( elem.removeEventListener ) {
elem.removeEventListener( type, handle, false );
}
2010-12-26 20:28:13 +00:00
} :
function( elem, type, handle ) {
if ( elem.detachEvent ) {
elem.detachEvent( "on" + type, handle );
}
};
jQuery.Event = function( src, props ) {
// Allow instantiation without the 'new' keyword
if ( !(this instanceof jQuery.Event) ) {
return new jQuery.Event( src, props );
2009-04-30 01:26:09 +00:00
}
2009-03-23 01:55:17 +00:00
// Event object
2009-04-30 01:26:09 +00:00
if ( src && src.type ) {
this.originalEvent = src;
this.type = src.type;
// Events bubbling up the document may have been marked as prevented
// by a handler lower down the tree; reflect the correct value.
this.isDefaultPrevented = (src.defaultPrevented || src.returnValue === false ||
src.getPreventDefault && src.getPreventDefault()) ? returnTrue : returnFalse;
// Event type
2009-04-30 01:26:09 +00:00
} else {
this.type = src;
2009-04-30 01:26:09 +00:00
}
// Put explicitly provided properties onto the event object
if ( props ) {
jQuery.extend( this, props );
}
// Create a timestamp if incoming event doesn't have one
this.timeStamp = src && src.timeStamp || jQuery.now();
2009-03-23 01:55:17 +00:00
// Mark it as fixed
this[ jQuery.expando ] = true;
};
2009-04-30 01:26:09 +00:00
function returnFalse() {
return false;
}
2009-04-30 01:26:09 +00:00
function returnTrue() {
return true;
}
// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
jQuery.Event.prototype = {
preventDefault: function() {
this.isDefaultPrevented = returnTrue;
var e = this.originalEvent;
2009-04-30 01:26:09 +00:00
if ( !e ) {
return;
2009-04-30 01:26:09 +00:00
}
2010-12-26 20:28:13 +00:00
// if preventDefault exists run it on the original event
2009-04-30 01:26:09 +00:00
if ( e.preventDefault ) {
e.preventDefault();
// otherwise set the returnValue property of the original event to false (IE)
} else {
e.returnValue = false;
}
},
stopPropagation: function() {
this.isPropagationStopped = returnTrue;
var e = this.originalEvent;
2009-04-30 01:26:09 +00:00
if ( !e ) {
return;
2009-04-30 01:26:09 +00:00
}
// if stopPropagation exists run it on the original event
2009-04-30 01:26:09 +00:00
if ( e.stopPropagation ) {
e.stopPropagation();
2009-04-30 01:26:09 +00:00
}
// otherwise set the cancelBubble property of the original event to true (IE)
e.cancelBubble = true;
},
stopImmediatePropagation: function() {
this.isImmediatePropagationStopped = returnTrue;
this.stopPropagation();
},
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse
};
// Create mouseenter/leave events using mouseover/out and event-time checks
2009-03-23 01:55:17 +00:00
jQuery.each({
mouseenter: "mouseover",
mouseleave: "mouseout"
2009-04-30 01:26:09 +00:00
}, function( orig, fix ) {
jQuery.event.special[ orig ] = jQuery.event.special[ fix ] = {
delegateType: fix,
bindType: fix,
handle: function( event ) {
var target = this,
related = event.relatedTarget,
handleObj = event.handleObj,
selector = handleObj.selector,
oldType, ret;
// For a real mouseover/out, always call the handler; for
// mousenter/leave call the handler if related is outside the target.
// NB: No relatedTarget if the mouse left/entered the browser window
if ( !related || handleObj.origType === event.type || (related !== target && !jQuery.contains( target, related )) ) {
oldType = event.type;
event.type = handleObj.origType;
ret = handleObj.handler.apply( this, arguments );
event.type = oldType;
}
return ret;
}
2009-03-23 01:55:17 +00:00
};
});
// submit delegation
if ( !jQuery.support.submitBubbles ) {
jQuery.event.special.submit = {
setup: function() {
// Only need this for delegated form submit events
if ( jQuery.nodeName( this, "form" ) ) {
return false;
}
2010-12-26 20:28:13 +00:00
2011-09-08 03:21:33 +00:00
jQuery.event.add( this, "click._submit keypress._submit", function( e ) {
var elem = e.target,
type = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.type : "";
// Do the elem.form check after type to avoid VML-related crash in IE (#9807)
if ( (e.type === "click" && (type === "submit" || type === "image") && elem.form) ||
(e.type === "keypress" && e.keyCode === 13 && (type === "text" || type === "password") && elem.form) ) {
simulate( "submit", this, e );
}
});
},
teardown: function() {
jQuery.event.remove( this, "._submit" );
}
};
}
// change delegation, happens here so we have bind.
if ( !jQuery.support.changeBubbles ) {
var getVal = function( elem ) {
var type = jQuery.nodeName( elem, "input" ) ? elem.type : "",
val = elem.value;
if ( type === "radio" || type === "checkbox" ) {
val = elem.checked;
} else if ( type === "select-one" ) {
val = elem.selectedIndex;
} else if ( type === "select-multiple" ) {
val = elem.selectedIndex > -1 ?
jQuery.map( elem.options, function( elem ) {
return elem.selected;
2011-09-08 03:21:33 +00:00
}).join( "-" ) :
"";
}
return val;
},
testChange = function testChange( e ) {
var elem = e.target,
old, val;
if ( !rformElems.test( elem.nodeName ) || elem.readOnly ) {
return;
}
old = jQuery._data( elem, "_change_data" );
2011-09-08 03:21:33 +00:00
val = getVal( elem );
// the current data will be also retrieved by beforeactivate
if ( e.type !== "focusout" || elem.type !== "radio" ) {
jQuery._data( elem, "_change_data", val );
}
2010-12-26 20:28:13 +00:00
if ( val !== old && old != null ) {
simulate( "change", elem, e, true );
}
},
changeFilters = {
2011-09-07 19:41:23 +00:00
focusout: testChange,
2011-09-07 19:41:23 +00:00
beforedeactivate: testChange,
2011-09-07 19:41:23 +00:00
click: function( e ) {
2011-09-08 03:21:33 +00:00
var elem = e.target,
name = elem.nodeName.toLowerCase(),
type = name === "input"? elem.type : "";
2011-09-08 03:21:33 +00:00
if ( type === "radio" || type === "checkbox" || name === "select" ) {
testChange.call( this, e );
2011-09-07 19:41:23 +00:00
}
},
2011-09-07 19:41:23 +00:00
// Change has to be called before submit
// Keydown will be called before keypress, which is used in submit-event delegation
keydown: function( e ) {
2011-09-08 03:21:33 +00:00
var elem = e.target,
name = elem.nodeName.toLowerCase(),
type = name === "input"? elem.type : "";
if ( (e.keyCode === 13 && name !== "textarea") ||
(e.keyCode === 32 && (type === "checkbox" || type === "radio")) ||
type === "select-multiple" ) {
testChange.call( this, e );
2011-09-07 19:41:23 +00:00
}
},
2011-09-07 19:41:23 +00:00
// Beforeactivate happens also before the previous element is blurred
// here, you can't trigger a change event, but you can store data
2011-09-08 03:21:33 +00:00
beforeactivate: function( e ) {
var elem = e.target;
jQuery._data( elem, "_change_data", getVal( elem ) );
},
// Update the current value if we're not re-focusing (#8157)
focus: function( e ) {
var elem = e.target;
if ( elem != document.activeElement ) {
2011-09-08 03:21:33 +00:00
jQuery._data( elem, "_change_data", getVal( elem ) );
}
}
};
jQuery.event.special.change = {
setup: function( data, namespaces ) {
if ( this.type === "file" ) {
return false;
}
for ( var type in changeFilters ) {
2011-09-08 03:21:33 +00:00
jQuery.event.add( this, type + "._change", changeFilters[ type ] );
}
return rformElems.test( this.nodeName );
},
teardown: function( namespaces ) {
jQuery.event.remove( this, "._change" );
return rformElems.test( this.nodeName );
}
};
}
function simulate( type, elem, event, bubble ) {
// Piggyback on a donor event to simulate a different one.
// Fake originalEvent to avoid donor's stopPropagation, but if the
// simulated event prevents default then we do the same on the donor.
var e = jQuery.extend( new jQuery.Event(), event, { type: type, originalEvent: {} } );
if ( bubble ) {
jQuery.event.trigger( e, null, elem );
} else {
jQuery.event.handle.call( elem, e );
}
if ( e.isDefaultPrevented() ) {
event.preventDefault();
}
}
// Create "bubbling" focus and blur events
if ( !jQuery.support.focusinBubbles ) {
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
// Attach a single capturing handler while someone wants focusin/focusout
var attaches = 0,
handler = function( event ) {
simulate( fix, event.target, jQuery.event.fix( event ), true );
};
jQuery.event.special[ fix ] = {
setup: function() {
if ( attaches++ === 0 ) {
document.addEventListener( orig, handler, true );
}
},
teardown: function() {
if ( --attaches === 0 ) {
document.removeEventListener( orig, handler, true );
}
}
};
});
}
jQuery.fn.extend({
on: function( types, selector, data, fn, /*INTERNAL*/ one ) {
var origFn, type;
// Types can be a map of types/handlers
if ( typeof types === "object" ) {
// ( types-Object, selector, data )
if ( typeof selector !== "string" ) {
// ( types-Object, data )
data = selector;
selector = undefined;
}
for ( type in types ) {
2011-09-08 03:21:33 +00:00
this.on( type, selector, data, types[ type ], one );
}
return this;
}
2010-12-26 20:28:13 +00:00
if ( data == null && fn == null ) {
// ( types, fn )
fn = selector;
data = selector = undefined;
} else if ( fn == null ) {
if ( typeof selector === "string" ) {
// ( types, selector, fn )
fn = data;
data = undefined;
} else {
// ( types, data, fn )
fn = data;
data = selector;
selector = undefined;
}
}
if ( fn === false ) {
fn = returnFalse;
} else if ( !fn ) {
return this;
}
if ( one === 1 ) {
origFn = fn;
fn = function( event ) {
jQuery.event.remove( event.delegateTarget || this, event );
return origFn.apply( this, arguments );
};
// Use same guid so caller can remove using origFn
2011-09-08 03:21:33 +00:00
fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ );
}
return this.each( function() {
jQuery.event.add( this, types, fn, data, selector );
});
},
one: function( types, selector, data, fn ) {
return this.on.call( this, types, selector, data, fn, 1 );
},
off: function( types, selector, fn ) {
if ( types && types.preventDefault ) {
// ( event ) native or jQuery.Event
return this.off( types.type, types.handler, types.selector );
}
if ( typeof types === "object" ) {
// ( types-object [, selector] )
for ( var type in types ) {
2011-09-08 03:21:33 +00:00
this.off( type, selector, types[ type ] );
}
return this;
}
if ( typeof selector !== "string" ) {
// ( types [, fn] )
fn = selector;
selector = undefined;
}
if ( fn === false ) {
fn = returnFalse;
}
return this.each(function() {
jQuery.event.remove( this, types, fn, selector );
});
},
bind: function( types, data, fn ) {
return this.on( types, null, data, fn );
},
unbind: function( types, fn ) {
return this.off( types, null, fn );
},
live: function( types, data, fn ) {
jQuery( this.context ).on( types, this.selector, data, fn );
return this;
},
die: function( types, fn ) {
jQuery( this.context ).off( types, this.selector || "**", fn );
return this;
},
2010-12-26 20:28:13 +00:00
delegate: function( selector, types, data, fn ) {
return this.on( types, selector, data, fn );
},
undelegate: function( selector, types, fn ) {
// ( namespace ) or ( selector, types [, fn] )
return arguments.length == 1? this.off( selector, "**" ) : this.off( types, selector, fn );
},
2010-12-26 20:28:13 +00:00
trigger: function( type, data ) {
return this.each(function() {
jQuery.event.trigger( type, data, this );
});
},
triggerHandler: function( type, data ) {
2009-04-30 01:26:09 +00:00
if ( this[0] ) {
return jQuery.event.trigger( type, data, this[0], true );
2009-03-23 01:55:17 +00:00
}
},
toggle: function( fn ) {
// Save reference to arguments for access in closure
var args = arguments,
guid = fn.guid || jQuery.guid++,
i = 0,
toggler = function( event ) {
// Figure out which function to execute
var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
// Make sure that clicks stop
event.preventDefault();
// and execute the function
return args[ lastToggle ].apply( this, arguments ) || false;
};
// link all the functions, so any of them can unbind this click handler
toggler.guid = guid;
while ( i < args.length ) {
args[ i++ ].guid = guid;
2009-04-30 01:26:09 +00:00
}
return this.click( toggler );
},
2009-04-30 01:26:09 +00:00
hover: function( fnOver, fnOut ) {
return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
}
});
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
"change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {
// Handle event binding
jQuery.fn[ name ] = function( data, fn ) {
if ( fn == null ) {
fn = data;
data = null;
}
return arguments.length > 0 ?
this.bind( name, data, fn ) :
this.trigger( name );
};
if ( jQuery.attrFn ) {
jQuery.attrFn[ name ] = true;
}
// Key Event property hooks
if ( rkeyEvent.test( name ) ) {
jQuery.event.propHooks[ name ] = function( event, original ) {
2011-09-22 15:22:56 +00:00
var charCode = event.charCode,
keyCode = event.keyCode,
ctrlKey = event.ctrlKey;
// Add which for key events
2011-09-22 15:22:56 +00:00
if ( event.which == null && (charCode != null || keyCode != null) ) {
event.which = charCode != null ? charCode : keyCode;
}
// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
2011-09-22 15:22:56 +00:00
if ( !event.metaKey && ctrlKey ) {
event.metaKey = ctrlKey;
}
return event;
};
}
// Mouse Event property hooks
if ( rmouseEvent.test( name ) ) {
jQuery.event.propHooks[ name ] = function( event, original ) {
2011-09-22 15:22:56 +00:00
var eventDoc, doc, body,
button = event.button;
// Calculate pageX/Y if missing and clientX/Y available
if ( event.pageX == null && event.clientX != null ) {
2011-09-22 15:22:56 +00:00
eventDoc = event.target.ownerDocument || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}
// Add which for click: 1 === left; 2 === middle; 3 === right
// Note: button is not normalized, so don't use it
2011-09-22 15:22:56 +00:00
if ( !event.which && button !== undefined ) {
event.which = (button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ));
}
return event;
};
}});
})( jQuery );