Skip to content
Permalink
Browse files

jquery event: Now using an internal argument to detect if bubbling on…

… $.event.trigger. The other would fail when it receives an event object with target. Also, the target isn't nulled anymore.

In addition, there was a lot of code running O(n) during bubbling while not needed.
  • Loading branch information
flesler committed Jan 2, 2009
1 parent 989e3f6 commit b2552e6ea35a1e00675d70a615d3af83137965df
Showing with 59 additions and 64 deletions.
  1. +59 −64 src/event.js
@@ -163,90 +163,85 @@ jQuery.event = {
}
},

trigger: function( event, data, elem) {
trigger: function( event, data, elem, bubbling /* internal */ ) {
// Event object or event type
var type = event.type || event;

event = typeof event === "object" ?
// jQuery.Event object
event[expando] ? event :
// Object literal
jQuery.extend( jQuery.Event(type), event ) :
// Just the event type (string)
jQuery.Event(type);

if ( type.indexOf("!") >= 0 ) {
event.type = type = type.slice(0, -1);
event.exclusive = true;
}

// Handle a global trigger
if ( !elem ) {
// Don't bubble custom events when global (to avoid too much overhead)
event.stopPropagation();
// Only trigger if we've ever bound an event for it
if ( this.global[type] )
jQuery.each( jQuery.cache, function(){
if ( this.events && this.events[type] )
jQuery.event.trigger( event, data, this.handle.elem );
});
if( !bubbling ){
event = typeof event === "object" ?
// jQuery.Event object
event[expando] ? event :
// Object literal
jQuery.extend( jQuery.Event(type), event ) :
// Just the event type (string)
jQuery.Event(type);

if ( type.indexOf("!") >= 0 ) {
event.type = type = type.slice(0, -1);
event.exclusive = true;
}

// Handle triggering a single element
} else {
// Handle a global trigger
if ( !elem ) {
// Don't bubble custom events when global (to avoid too much overhead)
event.stopPropagation();
// Only trigger if we've ever bound an event for it
if ( this.global[type] )
jQuery.each( jQuery.cache, function(){
if ( this.events && this.events[type] )
jQuery.event.trigger( event, data, this.handle.elem );
});
}

// Handle triggering a single element

// don't do events on text and comment nodes
if ( elem.nodeType == 3 || elem.nodeType == 8 )
if ( !elem || elem.nodeType == 3 || elem.nodeType == 8 )
return undefined;

// Clone the incoming data, if any
data = jQuery.makeArray(data);


// AT_TARGET phase (not bubbling)
if( !event.target ){
if( !bubbling ){
// Clean up in case it is reused
event.result = undefined;
event.target = elem;

// Clone the incoming data, if any
data = jQuery.makeArray(data);
data.unshift( event );
}
}

// Fix for custom events
event.currentTarget = elem;

data.unshift( event );

var fn = jQuery.isFunction( elem[ type ] );

// Trigger the event, it is assumed that "handle" is a function
var handle = jQuery.data(elem, "handle");
if ( handle )
handle.apply( elem, data );
event.currentTarget = elem;

// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
event.result = false;
var fn = jQuery.isFunction( elem[ type ] );

// Extra functions don't get the custom event object
data.shift();
// Trigger the event, it is assumed that "handle" is a function
var handle = jQuery.data(elem, "handle");
if ( handle )
handle.apply( elem, data );

// Trigger the native events (except for clicks on links)
if ( event.target === elem && fn && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
this.triggered = true;
try {
elem[ type ]();
// prevent IE from throwing an error for some hidden elements
} catch (e) {}
}
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
event.result = false;

if ( !event.isPropagationStopped() ) {
var parent = elem.parentNode || elem.ownerDocument;
if ( parent )
jQuery.event.trigger(event, data, parent);
}
// data.shift();

// Clean up, in case the event object is reused
event.target = null;
// Trigger the native events (except for clicks on links)
if ( !bubbling && fn && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {
this.triggered = true;
try {
elem[ type ]();
// prevent IE from throwing an error for some hidden elements
} catch (e) {}
}

this.triggered = false;
if ( !event.isPropagationStopped() ) {
var parent = elem.parentNode || elem.ownerDocument;
if ( parent )
jQuery.event.trigger(event, data, parent, true);
}

this.triggered = false;
},

handle: function(event) {

0 comments on commit b2552e6

Please sign in to comment.
You can’t perform that action at this time.