2 * A number of helper functions used for managing events.
3 * Many of the ideas behind this code originated from
4 * Dean Edwards' addEvent library.
8 // Bind an event to an element
9 // Original by Dean Edwards
10 add: function( elem, types, handler, data ) {
11 if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
15 // For whatever reason, IE has trouble passing the window object
16 // around, causing it to be cloned in the process
17 if ( elem.setInterval && ( elem !== window && !elem.frameElement ) ) {
21 // Make sure that the function being executed has a unique ID
22 if ( !handler.guid ) {
23 handler.guid = this.guid++;
26 // if data is passed, bind to handler
27 if ( data !== undefined ) {
28 // Create temporary function pointer to original handler
31 // Create unique handler function, wrapped around original handler
32 handler = this.proxy( fn );
34 // Store data in unique handler
38 // Init the element's event structure
39 var events = jQuery.data( elem, "events" ) || jQuery.data( elem, "events", {} ),
40 handle = jQuery.data( elem, "handle" ), eventHandle;
43 eventHandle = function() {
44 // Handle the second event of a trigger and when
45 // an event is called after a page has unloaded
46 return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
47 jQuery.event.handle.apply( eventHandle.elem, arguments ) :
51 handle = jQuery.data( elem, "handle", eventHandle );
54 // Add elem as a property of the handle function
55 // This is to prevent a memory leak with non-native
59 // Handle multiple events separated by a space
60 // jQuery(...).bind("mouseover mouseout", fn);
61 types = types.split( /\s+/ );
63 while ( (type = types[ i++ ]) ) {
64 // Namespaced event handlers
65 var namespaces = type.split(".");
66 type = namespaces.shift();
67 handler.type = namespaces.slice(0).sort().join(".");
69 // Get the current list of functions bound to this event
70 var handlers = events[ type ],
71 special = this.special[ type ] || {};
75 // Init the event handler queue
77 handlers = events[ type ] = {};
79 // Check for a special event handler
80 // Only use addEventListener/attachEvent if the special
81 // events handler returns false
82 if ( !special.setup || special.setup.call( elem, data, namespaces, handler) === false ) {
83 // Bind the global event handler to the element
84 if ( elem.addEventListener ) {
85 elem.addEventListener( type, handle, false );
86 } else if ( elem.attachEvent ) {
87 elem.attachEvent( "on" + type, handle );
93 var modifiedHandler = special.add.call( elem, handler, data, namespaces, handlers );
94 if ( modifiedHandler && jQuery.isFunction( modifiedHandler ) ) {
95 modifiedHandler.guid = modifiedHandler.guid || handler.guid;
96 handler = modifiedHandler;
100 // Add the function to the element's handler list
101 handlers[ handler.guid ] = handler;
103 // Keep track of which events have been used, for global triggering
104 this.global[ type ] = true;
107 // Nullify elem to prevent memory leaks in IE
114 // Detach an event or set of events from an element
115 remove: function( elem, types, handler ) {
116 // don't do events on text and comment nodes
117 if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
121 var events = jQuery.data( elem, "events" ), ret, type, fn;
124 // Unbind all events for the element
125 if ( types === undefined || (typeof types === "string" && types.charAt(0) === ".") ) {
126 for ( type in events ) {
127 this.remove( elem, type + (types || "") );
130 // types is actually an event object here
132 handler = types.handler;
136 // Handle multiple events separated by a space
137 // jQuery(...).unbind("mouseover mouseout", fn);
138 types = types.split(/\s+/);
140 while ( (type = types[ i++ ]) ) {
141 // Namespaced event handlers
142 var namespaces = type.split(".");
143 type = namespaces.shift();
144 var all = !namespaces.length,
145 cleaned = jQuery.map( namespaces.slice(0).sort() , function(nm){ return nm.replace(/[^\w\s\.\|`]/g, function(ch){return "\\"+ch; }); }),
146 namespace = new RegExp("(^|\\.)" + cleaned.join("\\.(?:.*\\.)?") + "(\\.|$)"),
147 special = this.special[ type ] || {};
149 if ( events[ type ] ) {
150 // remove the given handler for the given type
152 fn = events[ type ][ handler.guid ];
153 delete events[ type ][ handler.guid ];
155 // remove all handlers for the given type
157 for ( var handle in events[ type ] ) {
158 // Handle the removal of namespaced events
159 if ( all || namespace.test( events[ type ][ handle ].type ) ) {
160 delete events[ type ][ handle ];
165 if ( special.remove ) {
166 special.remove.call( elem, namespaces, fn);
169 // remove generic event handler if no more handlers exist
170 for ( ret in events[ type ] ) {
174 if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {
175 if ( elem.removeEventListener ) {
176 elem.removeEventListener( type, jQuery.data( elem, "handle" ), false );
177 } else if ( elem.detachEvent ) {
178 elem.detachEvent( "on" + type, jQuery.data( elem, "handle" ) );
182 delete events[ type ];
188 // Remove the expando if it's no longer used
189 for ( ret in events ) {
193 var handle = jQuery.data( elem, "handle" );
197 jQuery.removeData( elem, "events" );
198 jQuery.removeData( elem, "handle" );
203 // bubbling is internal
204 trigger: function( event, data, elem /*, bubbling */ ) {
205 // Event object or event type
206 var type = event.type || event,
207 bubbling = arguments[3];
210 event = typeof event === "object" ?
211 // jQuery.Event object
212 event[expando] ? event :
214 jQuery.extend( jQuery.Event(type), event ) :
215 // Just the event type (string)
218 if ( type.indexOf("!") >= 0 ) {
219 event.type = type = type.slice(0, -1);
220 event.exclusive = true;
223 // Handle a global trigger
225 // Don't bubble custom events when global (to avoid too much overhead)
226 event.stopPropagation();
227 // Only trigger if we've ever bound an event for it
228 if ( this.global[ type ] ) {
229 jQuery.each( jQuery.cache, function() {
230 if ( this.events && this.events[type] ) {
231 jQuery.event.trigger( event, data, this.handle.elem );
237 // Handle triggering a single element
239 // don't do events on text and comment nodes
240 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
244 // Clean up in case it is reused
245 event.result = undefined;
248 // Clone the incoming data, if any
249 data = jQuery.makeArray( data );
250 data.unshift( event );
253 event.currentTarget = elem;
255 // Trigger the event, it is assumed that "handle" is a function
256 var handle = jQuery.data( elem, "handle" );
258 handle.apply( elem, data );
261 var nativeFn, nativeHandler;
263 if ( !(elem && elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()]) ) {
264 nativeFn = elem[ type ];
265 nativeHandler = elem[ "on" + type ];
267 // prevent IE from throwing an error for some elements with some event types, see #3533
270 var isClick = jQuery.nodeName(elem, "a") && type === "click";
272 // Trigger the native events (except for clicks on links)
273 if ( !bubbling && nativeFn && !event.isDefaultPrevented() && !isClick ) {
274 this.triggered = true;
277 // prevent IE from throwing an error for some hidden elements
280 // Handle triggering native .onfoo handlers
281 } else if ( nativeHandler && elem[ "on" + type ].apply( elem, data ) === false ) {
282 event.result = false;
285 this.triggered = false;
287 if ( !event.isPropagationStopped() ) {
288 var parent = elem.parentNode || elem.ownerDocument;
290 jQuery.event.trigger( event, data, parent, true );
295 handle: function( event ) {
296 // returned undefined or false
299 event = arguments[0] = jQuery.event.fix( event || window.event );
300 event.currentTarget = this;
302 // Namespaced event handlers
303 var namespaces = event.type.split(".");
304 event.type = namespaces.shift();
306 // Cache this now, all = true means, any handler
307 all = !namespaces.length && !event.exclusive;
309 var namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join("\\.(?:.*\\.)?") + "(\\.|$)");
311 handlers = ( jQuery.data(this, "events") || {} )[ event.type ];
313 for ( var j in handlers ) {
314 var handler = handlers[ j ];
316 // Filter the functions by class
317 if ( all || namespace.test(handler.type) ) {
318 // Pass in a reference to the handler function itself
319 // So that we can later remove it
320 event.handler = handler;
321 event.data = handler.data;
323 var ret = handler.apply( this, arguments );
325 if ( ret !== undefined ) {
327 if ( ret === false ) {
328 event.preventDefault();
329 event.stopPropagation();
333 if ( event.isImmediatePropagationStopped() ) {
343 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(" "),
345 fix: function( event ) {
346 if ( event[ expando ] ) {
350 // store a copy of the original event object
351 // and "clone" to set read-only properties
352 var originalEvent = event;
353 event = jQuery.Event( originalEvent );
355 for ( var i = this.props.length, prop; i; ) {
356 prop = this.props[ --i ];
357 event[ prop ] = originalEvent[ prop ];
360 // Fix target property, if necessary
361 if ( !event.target ) {
362 event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
365 // check if target is a textnode (safari)
366 if ( event.target.nodeType === 3 ) {
367 event.target = event.target.parentNode;
370 // Add relatedTarget, if necessary
371 if ( !event.relatedTarget && event.fromElement ) {
372 event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
375 // Calculate pageX/Y if missing and clientX/Y available
376 if ( event.pageX == null && event.clientX != null ) {
377 var doc = document.documentElement, body = document.body;
378 event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
379 event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
382 // Add which for key events
383 if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) {
384 event.which = event.charCode || event.keyCode;
387 // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
388 if ( !event.metaKey && event.ctrlKey ) {
389 event.metaKey = event.ctrlKey;
392 // Add which for click: 1 == left; 2 == middle; 3 == right
393 // Note: button is not normalized, so don't use it
394 if ( !event.which && event.button !== undefined ) {
395 event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
401 proxy: function( fn, proxy, thisObject ) {
402 if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
406 // FIXME: Should proxy be redefined to be applied with thisObject if defined?
407 proxy = proxy || function() { return fn.apply( thisObject !== undefined ? thisObject : this, arguments ); };
408 // Set the guid of unique handler to the same of original handler, so it can be removed
409 proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
410 // So proxy can be declared as an argument
416 // Make sure the ready event is setup
417 setup: jQuery.bindReady,
418 teardown: function() {}
422 add: function( proxy, data, namespaces, live ) {
423 jQuery.extend( proxy, data || {} );
425 proxy.guid += data.selector + data.live;
426 jQuery.event.add( this, data.live, liveHandler, data );
430 remove: function( namespaces ) {
431 if ( namespaces.length ) {
432 var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)");
434 jQuery.each( (jQuery.data(this, "events").live || {}), function() {
435 if ( name.test(this.type) ) {
441 jQuery.event.remove( this, namespaces[0], liveHandler );
448 setup: function( data, namespaces, fn ) {
449 // We only want to do this special case on windows
450 if ( this.setInterval ) {
451 this.onbeforeunload = fn;
456 teardown: function( namespaces, fn ) {
457 if ( this.onbeforeunload === fn ) {
458 this.onbeforeunload = null;
465 jQuery.Event = function( src ){
466 // Allow instantiation without the 'new' keyword
467 if ( !this.preventDefault ) {
468 return new jQuery.Event( src );
472 if ( src && src.type ) {
473 this.originalEvent = src;
474 this.type = src.type;
480 // timeStamp is buggy for some events on Firefox(#3843)
481 // So we won't rely on the native value
482 this.timeStamp = now();
485 this[ expando ] = true;
488 function returnFalse() {
491 function returnTrue() {
495 // jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
496 // http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
497 jQuery.Event.prototype = {
498 preventDefault: function() {
499 this.isDefaultPrevented = returnTrue;
501 var e = this.originalEvent;
506 // if preventDefault exists run it on the original event
507 if ( e.preventDefault ) {
510 // otherwise set the returnValue property of the original event to false (IE)
511 e.returnValue = false;
513 stopPropagation: function() {
514 this.isPropagationStopped = returnTrue;
516 var e = this.originalEvent;
520 // if stopPropagation exists run it on the original event
521 if ( e.stopPropagation ) {
524 // otherwise set the cancelBubble property of the original event to true (IE)
525 e.cancelBubble = true;
527 stopImmediatePropagation: function(){
528 this.isImmediatePropagationStopped = returnTrue;
529 this.stopPropagation();
531 isDefaultPrevented: returnFalse,
532 isPropagationStopped: returnFalse,
533 isImmediatePropagationStopped: returnFalse
535 // Checks if an event happened on an element within another element
536 // Used in jQuery.event.special.mouseenter and mouseleave handlers
537 var withinElement = function( event ) {
538 // Check if mouse(over|out) are still within the same parent element
539 var parent = event.relatedTarget;
540 // Traverse up the tree
541 while ( parent && parent != this ) {
542 // Firefox sometimes assigns relatedTarget a XUL element
543 // which we cannot access the parentNode property of
544 try { parent = parent.parentNode; }
545 // assuming we've left the element since we most likely mousedover a xul element
549 if ( parent != this ) {
550 // set the correct event type
551 event.type = event.data;
552 // handle event if we actually just moused on to a non sub-element
553 jQuery.event.handle.apply( this, arguments );
558 // In case of event delegation, we only need to rename the event.type,
559 // liveHandler will take care of the rest.
560 delegate = function( event ) {
561 event.type = event.data;
562 jQuery.event.handle.apply( this, arguments );
565 // Create mouseenter and mouseleave events
567 mouseenter: "mouseover",
568 mouseleave: "mouseout"
569 }, function( orig, fix ) {
570 jQuery.event.special[ orig ] = {
571 setup: function(data){
572 jQuery.event.add( this, fix, data && data.selector ? delegate : withinElement, orig );
574 teardown: function(data){
575 jQuery.event.remove( this, fix, data && data.selector ? delegate : withinElement );
581 if ( !jQuery.support.submitBubbles ) {
583 jQuery.event.special.submit = {
584 setup: function( data, namespaces, fn ) {
585 if ( this.nodeName.toLowerCase() !== "form" ) {
586 jQuery.event.add(this, "click.specialSubmit." + fn.guid, function( e ) {
587 var elem = e.target, type = elem.type;
589 if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) {
590 return trigger( "submit", this, arguments );
594 jQuery.event.add(this, "keypress.specialSubmit." + fn.guid, function( e ) {
595 var elem = e.target, type = elem.type;
597 if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) {
598 return trigger( "submit", this, arguments );
604 remove: function( namespaces, fn ) {
605 jQuery.event.remove( this, "click.specialSubmit" + (fn ? "."+fn.guid : "") );
606 jQuery.event.remove( this, "keypress.specialSubmit" + (fn ? "."+fn.guid : "") );
612 // change delegation, happens here so we have bind.
613 if ( !jQuery.support.changeBubbles ) {
615 jQuery.event.special.change = {
617 click: function( e ) {
620 if ( elem.nodeName.toLowerCase() === "input" && elem.type === "checkbox" ) {
621 return trigger( "change", this, arguments );
624 return changeFilters.keyup.call( this, e );
626 keyup: function( e ) {
627 var elem = e.target, data, index = elem.selectedIndex + "";
629 if ( elem.nodeName.toLowerCase() === "select" ) {
630 data = jQuery.data( elem, "_change_data" );
631 jQuery.data( elem, "_change_data", index );
633 if ( (elem.type === "select-multiple" || data != null) && data !== index ) {
634 return trigger( "change", this, arguments );
638 beforeactivate: function( e ) {
641 if ( elem.nodeName.toLowerCase() === "input" && elem.type === "radio" && !elem.checked ) {
642 return trigger( "change", this, arguments );
645 blur: function( e ) {
646 var elem = e.target, nodeName = elem.nodeName.toLowerCase();
648 if ( (nodeName === "textarea" || (nodeName === "input" && (elem.type === "text" || elem.type === "password")))
649 && jQuery.data(elem, "_change_data") !== elem.value ) {
651 return trigger( "change", this, arguments );
654 focus: function( e ) {
655 var elem = e.target, nodeName = elem.nodeName.toLowerCase();
657 if ( nodeName === "textarea" || (nodeName === "input" && (elem.type === "text" || elem.type === "password" ) ) ) {
658 jQuery.data( elem, "_change_data", elem.value );
662 setup: function( data, namespaces, fn ) {
663 for ( var type in changeFilters ) {
664 jQuery.event.add( this, type + ".specialChange." + fn.guid, changeFilters[type] );
667 // always want to listen for change for trigger
670 remove: function( namespaces, fn ) {
671 for ( var type in changeFilters ) {
672 jQuery.event.remove( this, type + ".specialChange" + (fn ? "."+fn.guid : ""), changeFilters[type] );
677 var changeFilters = jQuery.event.special.change.filters;
681 function trigger( type, elem, args ) {
683 return jQuery.event.handle.apply( elem, args );
686 // Create "bubbling" focus and blur events
687 if ( !jQuery.support.focusBubbles ) {
689 jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){
690 jQuery.event.special[ orig ] = {
692 jQuery.event.add( this, fix, ieHandler );
694 teardown: function() {
695 jQuery.event.remove( this, fix, ieHandler );
699 function ieHandler() {
700 arguments[0].type = orig;
701 return jQuery.event.handle.apply(this, arguments);
707 jQuery.each(["bind", "one"], function(i, name) {
708 jQuery.fn[ name ] = function( type, data, fn, thisObject ) {
709 // Handle object literals
710 if ( typeof type === "object" ) {
711 for ( var key in type ) {
712 this[ name ](key, data, type[key], fn);
717 if ( jQuery.isFunction( data ) ) {
722 fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
723 var handler = name == "one" ? jQuery.event.proxy( fn, function( event ) {
724 jQuery( this ).unbind( event, handler );
725 return fn.apply( this, arguments );
727 return type === "unload" ? this.one(type, data, handler, thisObject) : this.each(function() {
728 jQuery.event.add( this, type, handler, data );
734 unbind: function( type, fn ) {
735 // Handle object literals
736 if ( typeof type === "object" && !type.preventDefault ) {
737 for ( var key in type ) {
738 this.unbind(key, type[key]);
743 return this.each(function() {
744 jQuery.event.remove( this, type, fn );
747 trigger: function( type, data ) {
748 return this.each(function() {
749 jQuery.event.trigger( type, data, this );
753 triggerHandler: function( type, data ) {
755 var event = jQuery.Event( type );
756 event.preventDefault();
757 event.stopPropagation();
758 jQuery.event.trigger( event, data, this[0] );
763 toggle: function( fn ) {
764 // Save reference to arguments for access in closure
765 var args = arguments, i = 1;
767 // link all the functions, so any of them can unbind this click handler
768 while( i < args.length ) {
769 jQuery.event.proxy( fn, args[ i++ ] );
772 return this.click( jQuery.event.proxy( fn, function( event ) {
773 // Figure out which function to execute
774 var lastToggle = ( jQuery.data( this, 'lastToggle' + fn.guid ) || 0 ) % i;
775 jQuery.data( this, 'lastToggle' + fn.guid, lastToggle + 1 );
777 // Make sure that clicks stop
778 event.preventDefault();
780 // and execute the function
781 return args[ lastToggle ].apply( this, arguments ) || false;
785 hover: function( fnOver, fnOut ) {
786 return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
789 live: function( type, data, fn, thisObject ) {
790 if ( jQuery.isFunction( data ) ) {
791 if ( fn !== undefined ) {
797 jQuery( this.context ).bind( liveConvert( type, this.selector ), {
798 data: data, selector: this.selector, live: type
803 die: function( type, fn ) {
804 jQuery( this.context ).unbind( liveConvert( type, this.selector ), fn ? { guid: fn.guid + this.selector + type } : null );
809 function liveHandler( event ) {
810 var stop = true, elems = [], selectors = [], args = arguments,
811 related, match, fn, elem, j, i, data,
812 live = jQuery.extend({}, jQuery.data( this, "events" ).live);
816 if ( fn.live === event.type ||
817 fn.altLive && jQuery.inArray(event.type, fn.altLive) > -1 ) {
820 if ( !(data.beforeFilter && data.beforeFilter[event.type] &&
821 !data.beforeFilter[event.type](event)) ) {
822 selectors.push( fn.selector );
829 match = jQuery( event.target ).closest( selectors, event.currentTarget );
831 for ( i = 0, l = match.length; i < l; i++ ) {
834 elem = match[i].elem;
837 if ( match[i].selector === fn.selector ) {
838 // Those two events require additional checking
839 if ( fn.live === "mouseenter" || fn.live === "mouseleave" ) {
840 related = jQuery( event.relatedTarget ).closest( fn.selector )[0];
843 if ( !related || related !== elem ) {
844 elems.push({ elem: elem, fn: fn });
850 for ( i = 0, l = elems.length; i < l; i++ ) {
852 event.currentTarget = match.elem;
853 event.data = match.fn.data;
854 if ( match.fn.apply( match.elem, args ) === false ) {
863 function liveConvert( type, selector ) {
864 return ["live", type, selector.replace(/\./g, "`").replace(/ /g, "&")].join(".");
867 jQuery.each( ("blur focus load resize scroll unload click dblclick " +
868 "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
869 "change select submit keydown keypress keyup error").split(" "), function( i, name ) {
871 // Handle event binding
872 jQuery.fn[ name ] = function( fn ) {
873 return fn ? this.bind( name, fn ) : this.trigger( name );
876 if ( jQuery.fnAttr ) {
877 jQuery.fnAttr[ name ] = true;
881 // Prevent memory leaks in IE
882 // Window isn't included so as not to unbind existing unload events
884 // - http://isaacschlueter.com/2006/10/msie-memory-leaks/
886 window.attachEvent("onunload", function() {
887 for ( var id in jQuery.cache ) {
888 if ( jQuery.cache[ id ].handle ) {
889 // Try/Catch is to handle iframes being unloaded, see #4280
891 jQuery.event.remove( jQuery.cache[ id ].handle.elem );