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" ) || jQuery.data( elem, "handle", function() {
41 // Handle the second event of a trigger and when
42 // an event is called after a page has unloaded
43 return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
44 jQuery.event.handle.apply( arguments.callee.elem, arguments ) :
47 // Add elem as a property of the handle function
48 // This is to prevent a memory leak with non-native
52 // Handle multiple events separated by a space
53 // jQuery(...).bind("mouseover mouseout", fn);
54 types = types.split( /\s+/ );
56 while ( (type = types[ i++ ]) ) {
57 // Namespaced event handlers
58 var namespaces = type.split(".");
59 type = namespaces.shift();
60 handler.type = namespaces.slice().sort().join(".");
62 // Get the current list of functions bound to this event
63 var handlers = events[ type ],
64 special = this.special[ type ] || {};
67 var modifiedHandler = special.add.call( elem, handler, data, namespaces );
68 if ( modifiedHandler && jQuery.isFunction( modifiedHandler ) ) {
69 modifiedHandler.guid = modifiedHandler.guid || handler.guid;
70 handler = modifiedHandler;
74 // Init the event handler queue
76 handlers = events[ type ] = {};
78 // Check for a special event handler
79 // Only use addEventListener/attachEvent if the special
80 // events handler returns false
81 if ( !special.setup || special.setup.call( elem, data, namespaces ) === false ) {
82 // Bind the global event handler to the element
83 if ( elem.addEventListener ) {
84 elem.addEventListener( type, handle, false );
85 } else if ( elem.attachEvent ) {
86 elem.attachEvent( "on" + type, handle );
91 // Add the function to the element's handler list
92 handlers[ handler.guid ] = handler;
94 // Keep track of which events have been used, for global triggering
95 this.global[ type ] = true;
98 // Nullify elem to prevent memory leaks in IE
105 // Detach an event or set of events from an element
106 remove: function( elem, types, handler ) {
107 // don't do events on text and comment nodes
108 if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
112 var events = jQuery.data( elem, "events" ), ret, type;
115 // Unbind all events for the element
116 if ( types === undefined || (typeof types === "string" && types.charAt(0) === ".") ) {
117 for ( type in events ) {
118 this.remove( elem, type + (types || "") );
121 // types is actually an event object here
123 handler = types.handler;
127 // Handle multiple events seperated by a space
128 // jQuery(...).unbind("mouseover mouseout", fn);
129 types = types.split(/\s+/);
131 while ( (type = types[ i++ ]) ) {
132 // Namespaced event handlers
133 var namespaces = type.split(".");
134 type = namespaces.shift();
135 var all = !namespaces.length,
136 namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)"),
137 special = this.special[ type ] || {};
139 if ( events[ type ] ) {
140 // remove the given handler for the given type
142 delete events[ type ][ handler.guid ];
144 // remove all handlers for the given type
146 for ( var handle in events[ type ] ) {
147 // Handle the removal of namespaced events
148 if ( all || namespace.test( events[ type ][ handle ].type ) ) {
149 delete events[ type ][ handle ];
154 if ( special.remove ) {
155 special.remove.call( elem, namespaces );
158 // remove generic event handler if no more handlers exist
159 for ( ret in events[ type ] ) {
163 if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {
164 if ( elem.removeEventListener ) {
165 elem.removeEventListener( type, jQuery.data( elem, "handle" ), false );
166 } else if ( elem.detachEvent ) {
167 elem.detachEvent( "on" + type, jQuery.data( elem, "handle" ) );
171 delete events[ type ];
177 // Remove the expando if it's no longer used
178 for ( ret in events ) {
182 var handle = jQuery.data( elem, "handle" );
186 jQuery.removeData( elem, "events" );
187 jQuery.removeData( elem, "handle" );
192 // bubbling is internal
193 trigger: function( event, data, elem, bubbling ) {
194 // Event object or event type
195 var type = event.type || event;
198 event = typeof event === "object" ?
199 // jQuery.Event object
200 event[expando] ? event :
202 jQuery.extend( jQuery.Event(type), event ) :
203 // Just the event type (string)
206 if ( type.indexOf("!") >= 0 ) {
207 event.type = type = type.slice(0, -1);
208 event.exclusive = true;
211 // Handle a global trigger
213 // Don't bubble custom events when global (to avoid too much overhead)
214 event.stopPropagation();
215 // Only trigger if we've ever bound an event for it
216 if ( this.global[ type ] ) {
217 jQuery.each( jQuery.cache, function() {
218 if ( this.events && this.events[type] ) {
219 jQuery.event.trigger( event, data, this.handle.elem );
225 // Handle triggering a single element
227 // don't do events on text and comment nodes
228 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
232 // Clean up in case it is reused
233 event.result = undefined;
236 // Clone the incoming data, if any
237 data = jQuery.makeArray( data );
238 data.unshift( event );
241 event.currentTarget = elem;
243 // Trigger the event, it is assumed that "handle" is a function
244 var handle = jQuery.data( elem, "handle" );
246 handle.apply( elem, data );
249 // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
250 if ( (!elem[ type ] || (jQuery.nodeName(elem, 'a') && type === "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false ) {
251 event.result = false;
254 // Trigger the native events (except for clicks on links)
255 if ( !bubbling && elem[ type ] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type === "click") ) {
256 this.triggered = true;
259 // prevent IE from throwing an error for some hidden elements
263 this.triggered = false;
265 if ( !event.isPropagationStopped() ) {
266 var parent = elem.parentNode || elem.ownerDocument;
268 jQuery.event.trigger( event, data, parent, true );
273 handle: function( event ) {
274 // returned undefined or false
277 event = arguments[0] = jQuery.event.fix( event || window.event );
278 event.currentTarget = this;
280 // Namespaced event handlers
281 var namespaces = event.type.split(".");
282 event.type = namespaces.shift();
284 // Cache this now, all = true means, any handler
285 all = !namespaces.length && !event.exclusive;
287 var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
289 handlers = ( jQuery.data(this, "events") || {} )[ event.type ];
291 for ( var j in handlers ) {
292 var handler = handlers[ j ];
294 // Filter the functions by class
295 if ( all || namespace.test(handler.type) ) {
296 // Pass in a reference to the handler function itself
297 // So that we can later remove it
298 event.handler = handler;
299 event.data = handler.data;
301 var ret = handler.apply( this, arguments );
303 if ( ret !== undefined ) {
305 if ( ret === false ) {
306 event.preventDefault();
307 event.stopPropagation();
311 if ( event.isImmediatePropagationStopped() ) {
319 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(" "),
321 fix: function( event ) {
322 if ( event[ expando ] ) {
326 // store a copy of the original event object
327 // and "clone" to set read-only properties
328 var originalEvent = event;
329 event = jQuery.Event( originalEvent );
331 for ( var i = this.props.length, prop; i; ) {
332 prop = this.props[ --i ];
333 event[ prop ] = originalEvent[ prop ];
336 // Fix target property, if necessary
337 if ( !event.target ) {
338 event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
341 // check if target is a textnode (safari)
342 if ( event.target.nodeType === 3 ) {
343 event.target = event.target.parentNode;
346 // Add relatedTarget, if necessary
347 if ( !event.relatedTarget && event.fromElement ) {
348 event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
351 // Calculate pageX/Y if missing and clientX/Y available
352 if ( event.pageX == null && event.clientX != null ) {
353 var doc = document.documentElement, body = document.body;
354 event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
355 event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
358 // Add which for key events
359 if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) {
360 event.which = event.charCode || event.keyCode;
363 // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
364 if ( !event.metaKey && event.ctrlKey ) {
365 event.metaKey = event.ctrlKey;
368 // Add which for click: 1 == left; 2 == middle; 3 == right
369 // Note: button is not normalized, so don't use it
370 if ( !event.which && event.button ) {
371 event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
377 proxy: function( fn, proxy, thisObject ) {
378 if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
382 // FIXME: Should proxy be redefined to be applied with thisObject if defined?
383 proxy = proxy || function() { return fn.apply( thisObject !== undefined ? thisObject : this, arguments ); };
384 // Set the guid of unique handler to the same of original handler, so it can be removed
385 proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
386 // So proxy can be declared as an argument
392 // Make sure the ready event is setup
394 teardown: function() {}
398 add: function( proxy, data, namespaces ) {
399 jQuery.extend( proxy, data || {} );
400 proxy.guid += data.selector + data.live;
401 jQuery.event.add( this, data.live, liveHandler );
404 teardown: function( namespaces ) {
405 jQuery.event.remove( this, namespaces[0], liveHandler );
411 jQuery.Event = function( src ){
412 // Allow instantiation without the 'new' keyword
413 if ( !this.preventDefault ) {
414 return new jQuery.Event( src );
418 if ( src && src.type ) {
419 this.originalEvent = src;
420 this.type = src.type;
426 // timeStamp is buggy for some events on Firefox(#3843)
427 // So we won't rely on the native value
428 this.timeStamp = now();
431 this[ expando ] = true;
434 function returnFalse() {
437 function returnTrue() {
441 // jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
442 // http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
443 jQuery.Event.prototype = {
444 preventDefault: function() {
445 this.isDefaultPrevented = returnTrue;
447 var e = this.originalEvent;
451 // if preventDefault exists run it on the original event
452 if ( e.preventDefault ) {
455 // otherwise set the returnValue property of the original event to false (IE)
456 e.returnValue = false;
458 stopPropagation: function() {
459 this.isPropagationStopped = returnTrue;
461 var e = this.originalEvent;
465 // if stopPropagation exists run it on the original event
466 if ( e.stopPropagation ) {
469 // otherwise set the cancelBubble property of the original event to true (IE)
470 e.cancelBubble = true;
472 stopImmediatePropagation: function(){
473 this.isImmediatePropagationStopped = returnTrue;
474 this.stopPropagation();
476 isDefaultPrevented: returnFalse,
477 isPropagationStopped: returnFalse,
478 isImmediatePropagationStopped: returnFalse
480 // Checks if an event happened on an element within another element
481 // Used in jQuery.event.special.mouseenter and mouseleave handlers
482 var withinElement = function( event ) {
483 // Check if mouse(over|out) are still within the same parent element
484 var parent = event.relatedTarget;
485 // Traverse up the tree
486 while ( parent && parent != this ) {
487 // Firefox sometimes assigns relatedTarget a XUL element
488 // which we cannot access the parentNode property of
489 try { parent = parent.parentNode; }
490 // assuming we've left the element since we most likely mousedover a xul element
494 if ( parent != this ) {
495 // set the correct event type
496 event.type = event.data;
497 // handle event if we actually just moused on to a non sub-element
498 jQuery.event.handle.apply( this, arguments );
503 mouseover: 'mouseenter',
504 mouseout: 'mouseleave'
505 }, function( orig, fix ) {
506 jQuery.event.special[ fix ] = {
508 jQuery.event.add( this, orig, withinElement, fix );
510 teardown: function(){
511 jQuery.event.remove( this, orig, withinElement );
517 bind: function( type, data, fn, thisObject ) {
518 if ( jQuery.isFunction( data ) ) {
519 if ( fn !== undefined ) {
525 fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
526 return type === "unload" ? this.one(type, data, fn, thisObject) : this.each(function() {
527 jQuery.event.add( this, type, fn, data );
531 one: function( type, data, fn, thisObject ) {
532 if ( jQuery.isFunction( data ) ) {
533 if ( fn !== undefined ) {
539 fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
540 var one = jQuery.event.proxy( fn, function( event ) {
541 jQuery( this ).unbind( event, one );
542 return fn.apply( this, arguments );
544 return this.each(function() {
545 jQuery.event.add( this, type, one, data );
549 unbind: function( type, fn ) {
550 return this.each(function() {
551 jQuery.event.remove( this, type, fn );
555 trigger: function( type, data ) {
556 return this.each(function() {
557 jQuery.event.trigger( type, data, this );
561 triggerHandler: function( type, data ) {
563 var event = jQuery.Event( type );
564 event.preventDefault();
565 event.stopPropagation();
566 jQuery.event.trigger( event, data, this[0] );
571 toggle: function( fn ) {
572 // Save reference to arguments for access in closure
573 var args = arguments, i = 1;
575 // link all the functions, so any of them can unbind this click handler
576 while( i < args.length ) {
577 jQuery.event.proxy( fn, args[ i++ ] );
580 return this.click( jQuery.event.proxy( fn, function( event ) {
581 // Figure out which function to execute
582 this.lastToggle = ( this.lastToggle || 0 ) % i;
584 // Make sure that clicks stop
585 event.preventDefault();
587 // and execute the function
588 return args[ this.lastToggle++ ].apply( this, arguments ) || false;
592 hover: function( fnOver, fnOut ) {
593 return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
596 ready: function( fn ) {
597 // Attach the listeners
600 // If the DOM is already ready
601 if ( jQuery.isReady ) {
602 // Execute the function immediately
603 fn.call( document, jQuery );
605 // Otherwise, remember the function for later
607 // Add the function to the wait list
608 jQuery.readyList.push( fn );
614 live: function( type, data, fn, thisObject ) {
615 if ( jQuery.isFunction( data ) ) {
616 if ( fn !== undefined ) {
622 jQuery( this.context ).bind( liveConvert( type, this.selector ), {
623 data: data, selector: this.selector, live: type
628 die: function( type, fn ) {
629 jQuery( this.context ).unbind( liveConvert( type, this.selector ), fn ? { guid: fn.guid + this.selector + type } : null );
634 function liveHandler( event ) {
635 var stop = true, elems = [], args = arguments;
637 jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
638 if ( fn.live === event.type ) {
639 var elem = jQuery( event.target ).closest( fn.selector )[0];
641 elems.push({ elem: elem, fn: fn });
646 elems.sort(function( a, b ) {
647 return jQuery.data( a.elem, "closest" ) - jQuery.data( b.elem, "closest" );
650 jQuery.each(elems, function() {
651 event.currentTarget = this.elem;
652 event.data = this.fn.data;
653 if ( this.fn.apply( this.elem, args ) === false ) {
654 return (stop = false);
661 function liveConvert( type, selector ) {
662 return ["live", type, selector.replace(/\./g, "`").replace(/ /g, "|")].join(".");
668 // Handle when the DOM is ready
670 // Make sure that the DOM is not already loaded
671 if ( !jQuery.isReady ) {
672 // Remember that the DOM is ready
673 jQuery.isReady = true;
675 // If there are functions bound, to execute
676 if ( jQuery.readyList ) {
677 // Execute all of them
679 while ( (fn = jQuery.readyList[ i++ ]) ) {
680 fn.call( document, jQuery );
683 // Reset the list of functions
684 jQuery.readyList = null;
687 // Trigger any bound ready events
688 jQuery( document ).triggerHandler( "ready" );
693 var readyBound = false;
695 function bindReady() {
696 if ( readyBound ) return;
699 // Mozilla, Opera and webkit nightlies currently support this event
700 if ( document.addEventListener ) {
701 // Use the handy event callback
702 document.addEventListener( "DOMContentLoaded", function() {
703 document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
707 // If IE event model is used
708 } else if ( document.attachEvent ) {
709 // ensure firing before onload,
710 // maybe late but safe also for iframes
711 document.attachEvent("onreadystatechange", function() {
712 if ( document.readyState === "complete" ) {
713 document.detachEvent( "onreadystatechange", arguments.callee );
718 // If IE and not an iframe
719 // continually check to see if the document is ready
720 if ( document.documentElement.doScroll && window === window.top ) (function() {
721 if ( jQuery.isReady ) {
726 // If IE is used, use the trick by Diego Perini
727 // http://javascript.nwbox.com/IEContentLoaded/
728 document.documentElement.doScroll("left");
730 setTimeout( arguments.callee, 0 );
734 // and execute any waiting functions
739 // A fallback to window.onload, that will always work
740 jQuery.event.add( window, "load", jQuery.ready );
743 jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
744 "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
745 "change,select,submit,keydown,keypress,keyup,error").split(","), function( i, name ) {
747 // Handle event binding
748 jQuery.fn[ name ] = function( fn ) {
749 return fn ? this.bind( name, fn ) : this.trigger( name );
753 // Prevent memory leaks in IE
754 // And prevent errors on refresh with events like mouseover in other browsers
755 // Window isn't included so as not to unbind existing unload events
757 // - http://isaacschlueter.com/2006/10/msie-memory-leaks/
758 // - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
759 jQuery( window ).bind( 'unload', function() {
760 for ( var id in jQuery.cache ) {
762 if ( id != 1 && jQuery.cache[ id ].handle ) {
763 jQuery.event.remove( jQuery.cache[ id ].handle.elem );