1 var rformElems = /^(?:textarea|input|select)$/i,
2 rtypenamespace = /^([^\.]*|)(?:\.(.+)|)$/,
3 rhoverHack = /(?:^|\s)hover(\.\S+|)\b/,
5 rmouseEvent = /^(?:mouse|contextmenu)|click/,
6 rfocusMorph = /^(?:focusinfocus|focusoutblur)$/,
7 hoverHack = function( events ) {
8 return jQuery.event.special.hover ? events : events.replace( rhoverHack, "mouseenter$1 mouseleave$1" );
12 * Helper functions for managing events -- not part of the public interface.
13 * Props to Dean Edwards' addEvent library for many of the ideas.
17 add: function( elem, types, handler, data, selector ) {
19 var elemData, eventHandle, events,
20 t, tns, type, namespaces, handleObj,
21 handleObjIn, handlers, special;
23 // Don't attach events to noData or text/comment nodes (allow plain objects tho)
24 if ( elem.nodeType === 3 || elem.nodeType === 8 || !types || !handler || !(elemData = jQuery._data( elem )) ) {
28 // Caller can pass in an object of custom data in lieu of the handler
29 if ( handler.handler ) {
30 handleObjIn = handler;
31 handler = handleObjIn.handler;
32 selector = handleObjIn.selector;
35 // Make sure that the handler has a unique ID, used to find/remove it later
36 if ( !handler.guid ) {
37 handler.guid = jQuery.guid++;
40 // Init the element's event structure and main handler, if this is the first
41 events = elemData.events;
43 elemData.events = events = {};
45 eventHandle = elemData.handle;
47 elemData.handle = eventHandle = function( e ) {
48 // Discard the second event of a jQuery.event.trigger() and
49 // when an event is called after a page has unloaded
50 return typeof jQuery !== "undefined" && (!e || jQuery.event.triggered !== e.type) ?
51 jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
54 // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
55 eventHandle.elem = elem;
58 // Handle multiple events separated by a space
59 // jQuery(...).bind("mouseover mouseout", fn);
60 types = jQuery.trim( hoverHack(types) ).split( " " );
61 for ( t = 0; t < types.length; t++ ) {
63 tns = rtypenamespace.exec( types[t] ) || [];
65 namespaces = ( tns[2] || "" ).split( "." ).sort();
67 // If event changes its type, use the special event handlers for the changed type
68 special = jQuery.event.special[ type ] || {};
70 // If selector defined, determine special event api type, otherwise given type
71 type = ( selector ? special.delegateType : special.bindType ) || type;
73 // Update special based on newly reset type
74 special = jQuery.event.special[ type ] || {};
76 // handleObj is passed to all event handlers
77 handleObj = jQuery.extend({
84 needsContext: selector && jQuery.expr.match.needsContext.test( selector ),
85 namespace: namespaces.join(".")
88 // Init the event handler queue if we're the first
89 handlers = events[ type ];
91 handlers = events[ type ] = [];
92 handlers.delegateCount = 0;
94 // Only use addEventListener/attachEvent if the special events handler returns false
95 if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
96 // Bind the global event handler to the element
97 if ( elem.addEventListener ) {
98 elem.addEventListener( type, eventHandle, false );
100 } else if ( elem.attachEvent ) {
101 elem.attachEvent( "on" + type, eventHandle );
107 special.add.call( elem, handleObj );
109 if ( !handleObj.handler.guid ) {
110 handleObj.handler.guid = handler.guid;
114 // Add to the element's handler list, delegates in front
116 handlers.splice( handlers.delegateCount++, 0, handleObj );
118 handlers.push( handleObj );
121 // Keep track of which events have ever been used, for event optimization
122 jQuery.event.global[ type ] = true;
125 // Nullify elem to prevent memory leaks in IE
131 // Detach an event or set of events from an element
132 remove: function( elem, types, handler, selector, mappedTypes ) {
134 var t, tns, type, origType, namespaces, origCount,
135 j, events, special, eventType, handleObj,
136 elemData = jQuery.hasData( elem ) && jQuery._data( elem );
138 if ( !elemData || !(events = elemData.events) ) {
142 // Once for each type.namespace in types; type may be omitted
143 types = jQuery.trim( hoverHack( types || "" ) ).split(" ");
144 for ( t = 0; t < types.length; t++ ) {
145 tns = rtypenamespace.exec( types[t] ) || [];
146 type = origType = tns[1];
149 // Unbind all events (on this namespace, if provided) for the element
151 for ( type in events ) {
152 jQuery.event.remove( elem, type + types[ t ], handler, selector, true );
157 special = jQuery.event.special[ type ] || {};
158 type = ( selector? special.delegateType : special.bindType ) || type;
159 eventType = events[ type ] || [];
160 origCount = eventType.length;
161 namespaces = namespaces ? new RegExp("(^|\\.)" + namespaces.split(".").sort().join("\\.(?:.*\\.|)") + "(\\.|$)") : null;
163 // Remove matching events
164 for ( j = 0; j < eventType.length; j++ ) {
165 handleObj = eventType[ j ];
167 if ( ( mappedTypes || origType === handleObj.origType ) &&
168 ( !handler || handler.guid === handleObj.guid ) &&
169 ( !namespaces || namespaces.test( handleObj.namespace ) ) &&
170 ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {
171 eventType.splice( j--, 1 );
173 if ( handleObj.selector ) {
174 eventType.delegateCount--;
176 if ( special.remove ) {
177 special.remove.call( elem, handleObj );
182 // Remove generic event handler if we removed something and no more handlers exist
183 // (avoids potential for endless recursion during removal of special event handlers)
184 if ( eventType.length === 0 && origCount !== eventType.length ) {
185 if ( !special.teardown || special.teardown.call( elem, namespaces, elemData.handle ) === false ) {
186 jQuery.removeEvent( elem, type, elemData.handle );
189 delete events[ type ];
193 // Remove the expando if it's no longer used
194 if ( jQuery.isEmptyObject( events ) ) {
195 delete elemData.handle;
197 // removeData also checks for emptiness and clears the expando if empty
198 // so use it instead of delete
199 jQuery.removeData( elem, "events", true );
203 // Events that are safe to short-circuit if no handlers are attached.
204 // Native DOM events should not be added, they may have inline handlers.
211 trigger: function( event, data, elem, onlyHandlers ) {
212 // Don't do events on text and comment nodes
213 if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
217 // Event object or event type
218 var cache, exclusive, i, cur, old, ontype, special, handle, eventPath, bubbleType,
219 type = event.type || event,
222 // focus/blur morphs to focusin/out; ensure we're not firing them right now
223 if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
227 if ( type.indexOf( "!" ) >= 0 ) {
228 // Exclusive events trigger only for the exact event (no namespaces)
229 type = type.slice(0, -1);
233 if ( type.indexOf( "." ) >= 0 ) {
234 // Namespaced trigger; create a regexp to match event type in handle()
235 namespaces = type.split(".");
236 type = namespaces.shift();
240 if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) {
241 // No jQuery handlers for this event type, and it can't have inline handlers
245 // Caller can pass in an Event, Object, or just an event type string
246 event = typeof event === "object" ?
247 // jQuery.Event object
248 event[ jQuery.expando ] ? event :
250 new jQuery.Event( type, event ) :
251 // Just the event type (string)
252 new jQuery.Event( type );
255 event.isTrigger = true;
256 event.exclusive = exclusive;
257 event.namespace = namespaces.join( "." );
258 event.namespace_re = event.namespace? new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)") : null;
259 ontype = type.indexOf( ":" ) < 0 ? "on" + type : "";
261 // Handle a global trigger
264 // TODO: Stop taunting the data cache; remove global events and always attach to document
265 cache = jQuery.cache;
267 if ( cache[ i ].events && cache[ i ].events[ type ] ) {
268 jQuery.event.trigger( event, data, cache[ i ].handle.elem, true );
274 // Clean up the event in case it is being reused
275 event.result = undefined;
276 if ( !event.target ) {
280 // Clone any incoming data and prepend the event, creating the handler arg list
281 data = data != null ? jQuery.makeArray( data ) : [];
282 data.unshift( event );
284 // Allow special events to draw outside the lines
285 special = jQuery.event.special[ type ] || {};
286 if ( special.trigger && special.trigger.apply( elem, data ) === false ) {
290 // Determine event propagation path in advance, per W3C events spec (#9951)
291 // Bubble up to document, then to window; watch for a global ownerDocument var (#9724)
292 eventPath = [[ elem, special.bindType || type ]];
293 if ( !onlyHandlers && !special.noBubble && !jQuery.isWindow( elem ) ) {
295 bubbleType = special.delegateType || type;
296 cur = rfocusMorph.test( bubbleType + type ) ? elem : elem.parentNode;
297 for ( old = elem; cur; cur = cur.parentNode ) {
298 eventPath.push([ cur, bubbleType ]);
302 // Only add window if we got to document (e.g., not plain obj or detached DOM)
303 if ( old === (elem.ownerDocument || document) ) {
304 eventPath.push([ old.defaultView || old.parentWindow || window, bubbleType ]);
308 // Fire handlers on the event path
309 for ( i = 0; i < eventPath.length && !event.isPropagationStopped(); i++ ) {
311 cur = eventPath[i][0];
312 event.type = eventPath[i][1];
314 handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" );
316 handle.apply( cur, data );
318 // Note that this is a bare JS function and not a jQuery handler
319 handle = ontype && cur[ ontype ];
320 if ( handle && jQuery.acceptData( cur ) && handle.apply && handle.apply( cur, data ) === false ) {
321 event.preventDefault();
326 // If nobody prevented the default action, do it now
327 if ( !onlyHandlers && !event.isDefaultPrevented() ) {
329 if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) &&
330 !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {
332 // Call a native DOM method on the target with the same name name as the event.
333 // Can't use an .isFunction() check here because IE6/7 fails that test.
334 // Don't do default actions on window, that's where global variables be (#6170)
335 // IE<9 dies on focus/blur to hidden element (#1486)
336 if ( ontype && elem[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) && !jQuery.isWindow( elem ) ) {
338 // Don't re-trigger an onFOO event when we call its FOO() method
339 old = elem[ ontype ];
342 elem[ ontype ] = null;
345 // Prevent re-triggering of the same event, since we already bubbled it above
346 jQuery.event.triggered = type;
348 jQuery.event.triggered = undefined;
351 elem[ ontype ] = old;
360 dispatch: function( event ) {
362 // Make a writable jQuery.Event from the native event object
363 event = jQuery.event.fix( event || window.event );
365 var i, j, cur, ret, selMatch, matched, matches, handleObj, sel, related,
366 handlers = ( (jQuery._data( this, "events" ) || {} )[ event.type ] || []),
367 delegateCount = handlers.delegateCount,
368 args = core_slice.call( arguments ),
369 run_all = !event.exclusive && !event.namespace,
370 special = jQuery.event.special[ event.type ] || {},
373 // Use the fix-ed jQuery.Event rather than the (read-only) native event
375 event.delegateTarget = this;
377 // Call the preDispatch hook for the mapped type, and let it bail if desired
378 if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) {
382 // Determine handlers that should run if there are delegated events
383 // Avoid non-left-click bubbling in Firefox (#3861)
384 if ( delegateCount && !(event.button && event.type === "click") ) {
386 for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
388 // Don't process clicks (ONLY) on disabled elements (#6911, #8165, #11382, #11764)
389 if ( cur.disabled !== true || event.type !== "click" ) {
392 for ( i = 0; i < delegateCount; i++ ) {
393 handleObj = handlers[ i ];
394 sel = handleObj.selector;
396 if ( selMatch[ sel ] === undefined ) {
397 selMatch[ sel ] = handleObj.needsContext ?
398 jQuery( sel, this ).index( cur ) >= 0 :
399 jQuery.find( sel, this, null, [ cur ] ).length;
401 if ( selMatch[ sel ] ) {
402 matches.push( handleObj );
405 if ( matches.length ) {
406 handlerQueue.push({ elem: cur, matches: matches });
412 // Add the remaining (directly-bound) handlers
413 if ( handlers.length > delegateCount ) {
414 handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) });
417 // Run delegates first; they may want to stop propagation beneath us
418 for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
419 matched = handlerQueue[ i ];
420 event.currentTarget = matched.elem;
422 for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) {
423 handleObj = matched.matches[ j ];
425 // Triggered event must either 1) be non-exclusive and have no namespace, or
426 // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
427 if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {
429 event.data = handleObj.data;
430 event.handleObj = handleObj;
432 ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler )
433 .apply( matched.elem, args );
435 if ( ret !== undefined ) {
437 if ( ret === false ) {
438 event.preventDefault();
439 event.stopPropagation();
446 // Call the postDispatch hook for the mapped type
447 if ( special.postDispatch ) {
448 special.postDispatch.call( this, event );
454 // Includes some event props shared by KeyEvent and MouseEvent
455 // *** attrChange attrName relatedNode srcElement are not normalized, non-W3C, deprecated, will be removed in 1.8 ***
456 props: "attrChange attrName relatedNode srcElement altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which".split(" "),
461 props: "char charCode key keyCode".split(" "),
462 filter: function( event, original ) {
464 // Add which for key events
465 if ( event.which == null ) {
466 event.which = original.charCode != null ? original.charCode : original.keyCode;
474 props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "),
475 filter: function( event, original ) {
476 var eventDoc, doc, body,
477 button = original.button,
478 fromElement = original.fromElement;
480 // Calculate pageX/Y if missing and clientX/Y available
481 if ( event.pageX == null && original.clientX != null ) {
482 eventDoc = event.target.ownerDocument || document;
483 doc = eventDoc.documentElement;
484 body = eventDoc.body;
486 event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 );
487 event.pageY = original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 );
490 // Add relatedTarget, if necessary
491 if ( !event.relatedTarget && fromElement ) {
492 event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
495 // Add which for click: 1 === left; 2 === middle; 3 === right
496 // Note: button is not normalized, so don't use it
497 if ( !event.which && button !== undefined ) {
498 event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
505 fix: function( event ) {
506 if ( event[ jQuery.expando ] ) {
510 // Create a writable copy of the event object and normalize some properties
512 originalEvent = event,
513 fixHook = jQuery.event.fixHooks[ event.type ] || {},
514 copy = fixHook.props ? this.props.concat( fixHook.props ) : this.props;
516 event = jQuery.Event( originalEvent );
518 for ( i = copy.length; i; ) {
520 event[ prop ] = originalEvent[ prop ];
523 // Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
524 if ( !event.target ) {
525 event.target = originalEvent.srcElement || document;
528 // Target should not be a text node (#504, Safari)
529 if ( event.target.nodeType === 3 ) {
530 event.target = event.target.parentNode;
533 // For mouse/key events, metaKey==false if it's undefined (#3368, #11328; IE6/7/8)
534 event.metaKey = !!event.metaKey;
536 return fixHook.filter? fixHook.filter( event, originalEvent ) : event;
541 // Prevent triggered image.load events from bubbling to window.load
546 delegateType: "focusin"
549 delegateType: "focusout"
553 setup: function( data, namespaces, eventHandle ) {
554 // We only want to do this special case on windows
555 if ( jQuery.isWindow( this ) ) {
556 this.onbeforeunload = eventHandle;
560 teardown: function( namespaces, eventHandle ) {
561 if ( this.onbeforeunload === eventHandle ) {
562 this.onbeforeunload = null;
568 simulate: function( type, elem, event, bubble ) {
569 // Piggyback on a donor event to simulate a different one.
570 // Fake originalEvent to avoid donor's stopPropagation, but if the
571 // simulated event prevents default then we do the same on the donor.
572 var e = jQuery.extend(
581 jQuery.event.trigger( e, null, elem );
583 jQuery.event.dispatch.call( elem, e );
585 if ( e.isDefaultPrevented() ) {
586 event.preventDefault();
591 // Some plugins are using, but it's undocumented/deprecated and will be removed.
592 // The 1.7 special event interface should provide all the hooks needed now.
593 jQuery.event.handle = jQuery.event.dispatch;
595 jQuery.removeEvent = document.removeEventListener ?
596 function( elem, type, handle ) {
597 if ( elem.removeEventListener ) {
598 elem.removeEventListener( type, handle, false );
601 function( elem, type, handle ) {
602 var name = "on" + type;
604 if ( elem.detachEvent ) {
606 // #8545, #7054, preventing memory leaks for custom events in IE6-8
607 // detachEvent needed property on element, by name of that event, to properly expose it to GC
608 if ( typeof elem[ name ] === "undefined" ) {
612 elem.detachEvent( name, handle );
616 jQuery.Event = function( src, props ) {
617 // Allow instantiation without the 'new' keyword
618 if ( !(this instanceof jQuery.Event) ) {
619 return new jQuery.Event( src, props );
623 if ( src && src.type ) {
624 this.originalEvent = src;
625 this.type = src.type;
627 // Events bubbling up the document may have been marked as prevented
628 // by a handler lower down the tree; reflect the correct value.
629 this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false ||
630 src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse;
637 // Put explicitly provided properties onto the event object
639 jQuery.extend( this, props );
642 // Create a timestamp if incoming event doesn't have one
643 this.timeStamp = src && src.timeStamp || jQuery.now();
646 this[ jQuery.expando ] = true;
649 function returnFalse() {
652 function returnTrue() {
656 // jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
657 // http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
658 jQuery.Event.prototype = {
659 preventDefault: function() {
660 this.isDefaultPrevented = returnTrue;
662 var e = this.originalEvent;
667 // if preventDefault exists run it on the original event
668 if ( e.preventDefault ) {
671 // otherwise set the returnValue property of the original event to false (IE)
673 e.returnValue = false;
676 stopPropagation: function() {
677 this.isPropagationStopped = returnTrue;
679 var e = this.originalEvent;
683 // if stopPropagation exists run it on the original event
684 if ( e.stopPropagation ) {
687 // otherwise set the cancelBubble property of the original event to true (IE)
688 e.cancelBubble = true;
690 stopImmediatePropagation: function() {
691 this.isImmediatePropagationStopped = returnTrue;
692 this.stopPropagation();
694 isDefaultPrevented: returnFalse,
695 isPropagationStopped: returnFalse,
696 isImmediatePropagationStopped: returnFalse
699 // Create mouseenter/leave events using mouseover/out and event-time checks
701 mouseenter: "mouseover",
702 mouseleave: "mouseout"
703 }, function( orig, fix ) {
704 jQuery.event.special[ orig ] = {
708 handle: function( event ) {
711 related = event.relatedTarget,
712 handleObj = event.handleObj,
713 selector = handleObj.selector;
715 // For mousenter/leave call the handler if related is outside the target.
716 // NB: No relatedTarget if the mouse left/entered the browser window
717 if ( !related || (related !== target && !jQuery.contains( target, related )) ) {
718 event.type = handleObj.origType;
719 ret = handleObj.handler.apply( this, arguments );
727 // IE submit delegation
728 if ( !jQuery.support.submitBubbles ) {
730 jQuery.event.special.submit = {
732 // Only need this for delegated form submit events
733 if ( jQuery.nodeName( this, "form" ) ) {
737 // Lazy-add a submit handler when a descendant form may potentially be submitted
738 jQuery.event.add( this, "click._submit keypress._submit", function( e ) {
739 // Node name check avoids a VML-related crash in IE (#9807)
741 form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined;
742 if ( form && !jQuery._data( form, "_submit_attached" ) ) {
743 jQuery.event.add( form, "submit._submit", function( event ) {
744 event._submit_bubble = true;
746 jQuery._data( form, "_submit_attached", true );
749 // return undefined since we don't need an event listener
752 postDispatch: function( event ) {
753 // If form was submitted by the user, bubble the event up the tree
754 if ( event._submit_bubble ) {
755 delete event._submit_bubble;
756 if ( this.parentNode && !event.isTrigger ) {
757 jQuery.event.simulate( "submit", this.parentNode, event, true );
762 teardown: function() {
763 // Only need this for delegated form submit events
764 if ( jQuery.nodeName( this, "form" ) ) {
768 // Remove delegated handlers; cleanData eventually reaps submit handlers attached above
769 jQuery.event.remove( this, "._submit" );
774 // IE change delegation and checkbox/radio fix
775 if ( !jQuery.support.changeBubbles ) {
777 jQuery.event.special.change = {
781 if ( rformElems.test( this.nodeName ) ) {
782 // IE doesn't fire change on a check/radio until blur; trigger it on click
783 // after a propertychange. Eat the blur-change in special.change.handle.
784 // This still fires onchange a second time for check/radio after blur.
785 if ( this.type === "checkbox" || this.type === "radio" ) {
786 jQuery.event.add( this, "propertychange._change", function( event ) {
787 if ( event.originalEvent.propertyName === "checked" ) {
788 this._just_changed = true;
791 jQuery.event.add( this, "click._change", function( event ) {
792 if ( this._just_changed && !event.isTrigger ) {
793 this._just_changed = false;
795 // Allow triggered, simulated change events (#11500)
796 jQuery.event.simulate( "change", this, event, true );
801 // Delegated event; lazy-add a change handler on descendant inputs
802 jQuery.event.add( this, "beforeactivate._change", function( e ) {
805 if ( rformElems.test( elem.nodeName ) && !jQuery._data( elem, "_change_attached" ) ) {
806 jQuery.event.add( elem, "change._change", function( event ) {
807 if ( this.parentNode && !event.isSimulated && !event.isTrigger ) {
808 jQuery.event.simulate( "change", this.parentNode, event, true );
811 jQuery._data( elem, "_change_attached", true );
816 handle: function( event ) {
817 var elem = event.target;
819 // Swallow native change events from checkbox/radio, we already triggered them above
820 if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) {
821 return event.handleObj.handler.apply( this, arguments );
825 teardown: function() {
826 jQuery.event.remove( this, "._change" );
828 return !rformElems.test( this.nodeName );
833 // Create "bubbling" focus and blur events
834 if ( !jQuery.support.focusinBubbles ) {
835 jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
837 // Attach a single capturing handler while someone wants focusin/focusout
839 handler = function( event ) {
840 jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true );
843 jQuery.event.special[ fix ] = {
845 if ( attaches++ === 0 ) {
846 document.addEventListener( orig, handler, true );
849 teardown: function() {
850 if ( --attaches === 0 ) {
851 document.removeEventListener( orig, handler, true );
860 on: function( types, selector, data, fn, /*INTERNAL*/ one ) {
863 // Types can be a map of types/handlers
864 if ( typeof types === "object" ) {
865 // ( types-Object, selector, data )
866 if ( typeof selector !== "string" ) { // && selector != null
867 // ( types-Object, data )
868 data = data || selector;
869 selector = undefined;
871 for ( type in types ) {
872 this.on( type, selector, data, types[ type ], one );
877 if ( data == null && fn == null ) {
880 data = selector = undefined;
881 } else if ( fn == null ) {
882 if ( typeof selector === "string" ) {
883 // ( types, selector, fn )
887 // ( types, data, fn )
890 selector = undefined;
893 if ( fn === false ) {
901 fn = function( event ) {
902 // Can use an empty set, since event contains the info
903 jQuery().off( event );
904 return origFn.apply( this, arguments );
906 // Use same guid so caller can remove using origFn
907 fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ );
909 return this.each( function() {
910 jQuery.event.add( this, types, fn, data, selector );
913 one: function( types, selector, data, fn ) {
914 return this.on( types, selector, data, fn, 1 );
916 off: function( types, selector, fn ) {
918 if ( types && types.preventDefault && types.handleObj ) {
919 // ( event ) dispatched jQuery.Event
920 handleObj = types.handleObj;
921 jQuery( types.delegateTarget ).off(
922 handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType,
928 if ( typeof types === "object" ) {
929 // ( types-object [, selector] )
930 for ( type in types ) {
931 this.off( type, selector, types[ type ] );
935 if ( selector === false || typeof selector === "function" ) {
938 selector = undefined;
940 if ( fn === false ) {
943 return this.each(function() {
944 jQuery.event.remove( this, types, fn, selector );
948 bind: function( types, data, fn ) {
949 return this.on( types, null, data, fn );
951 unbind: function( types, fn ) {
952 return this.off( types, null, fn );
955 live: function( types, data, fn ) {
956 jQuery( this.context ).on( types, this.selector, data, fn );
959 die: function( types, fn ) {
960 jQuery( this.context ).off( types, this.selector || "**", fn );
964 delegate: function( selector, types, data, fn ) {
965 return this.on( types, selector, data, fn );
967 undelegate: function( selector, types, fn ) {
968 // ( namespace ) or ( selector, types [, fn] )
969 return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn );
972 trigger: function( type, data ) {
973 return this.each(function() {
974 jQuery.event.trigger( type, data, this );
977 triggerHandler: function( type, data ) {
979 return jQuery.event.trigger( type, data, this[0], true );
983 toggle: function( fn ) {
984 // Save reference to arguments for access in closure
985 var args = arguments,
986 guid = fn.guid || jQuery.guid++,
988 toggler = function( event ) {
989 // Figure out which function to execute
990 var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
991 jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
993 // Make sure that clicks stop
994 event.preventDefault();
996 // and execute the function
997 return args[ lastToggle ].apply( this, arguments ) || false;
1000 // link all the functions, so any of them can unbind this click handler
1001 toggler.guid = guid;
1002 while ( i < args.length ) {
1003 args[ i++ ].guid = guid;
1006 return this.click( toggler );
1009 hover: function( fnOver, fnOut ) {
1010 return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
1014 jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
1015 "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
1016 "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {
1018 // Handle event binding
1019 jQuery.fn[ name ] = function( data, fn ) {
1025 return arguments.length > 0 ?
1026 this.on( name, null, data, fn ) :
1027 this.trigger( name );
1030 if ( rkeyEvent.test( name ) ) {
1031 jQuery.event.fixHooks[ name ] = jQuery.event.keyHooks;
1034 if ( rmouseEvent.test( name ) ) {
1035 jQuery.event.fixHooks[ name ] = jQuery.event.mouseHooks;