Skip to main content

Bug Tracker

Side navigation

Ticket #3662: event-object[5997].diff
File event-object[5997].diff, 10.1 KB (added by flesler, December 25, 2008 07:02PM UTC)

Updated to new changes. Supports object literals as event objects (they're then copied to an actual instance)

Index: src/event.js
===================================================================
--- src/event.js	(revision 5997)
+++ src/event.js	(working copy)
@@ -163,51 +163,48 @@
 		}
 	},
 
-	trigger: function(type, data, elem, donative, extra, dohandlers) {
-		// Clone the incoming data, if any
-		data = jQuery.makeArray(data);
-
-		if ( type.indexOf("!") >= 0 ) {
-			type = type.slice(0, -1);
-			var exclusive = true;
-		}
-
+	trigger: function( e, data, elem, donative, extra, dohandlers) {
+		// Event object or event type
+		var type = e.type || e;
+		
 		// Handle a global trigger
 		if ( !elem ) {
 			// 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( type, data, this.handle.elem, false );
+						jQuery.event.trigger( e, data, this.handle.elem, false );
 				});
 
 		// Handle triggering a single element
 		} else {
+
 			// don't do events on text and comment nodes
 			if ( elem.nodeType == 3 || elem.nodeType == 8 )
 				return undefined;
 
-			var val, ret, fn = jQuery.isFunction( elem[ type ] || null ),
-				// Check to see if we need to provide a fake event, or not
-				event = !data[0] || !data[0].preventDefault;
-
-			// Pass along a fake event
-			if ( event ) {
-				data.unshift({
-					type: type,
-					target: elem,
-					preventDefault: function(){},
-					stopPropagation: function(){},
-					stopImmediatePropagation:stopImmediatePropagation,
-					timeStamp: now()
-				});
-				data[0][expando] = true; // no need to fix fake event
+			// Clone the incoming data, if any
+			data = jQuery.makeArray(data);
+			
+			if ( type.indexOf("!") >= 0 ) {
+				type = type.slice(0, -1);
+				var exclusive = true;
 			}
+			
+			e = typeof e === "object" ?
+				// jQuery.Event object
+				e[expando] ? e :
+				// Object literal
+				jQuery.extend( new jQuery.Event(type), e ) :
+				// Just the event type (string)
+				new jQuery.Event(type);
+				
+			e.target = elem;
+			e.exclusive = exclusive;
+			
+			data.unshift( e );
 
-			// Enforce the right trigger type
-			data[0].type = type;
-			if ( exclusive )
-				data[0].exclusive = true;
+			var val, ret, fn = jQuery.isFunction( elem[ type ] );
 
 			if ( dohandlers !== false ) {
 				// Trigger the event, it is assumed that "handle" is a function
@@ -227,8 +224,7 @@
 			}
 
 			// Extra functions don't get the custom event object
-			if ( event )
-				data.shift();
+			data.shift();
 
 			// Handle triggering of extra function
 			if ( extra && jQuery.isFunction( extra ) ) {
@@ -300,7 +296,7 @@
 		return val;
 	},
 
-	props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" "),
+	props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
 
 	fix: function(event) {
 		if ( event[expando] )
@@ -309,38 +305,13 @@
 		// store a copy of the original event object
 		// and "clone" to set read-only properties
 		var originalEvent = event;
-		event = { originalEvent: originalEvent };
+		event = new jQuery.Event( originalEvent );
 
 		for ( var i = this.props.length, prop; i; ){
 			prop = this.props[ --i ];
 			event[ prop ] = originalEvent[ prop ];
 		}
 
-		// Mark it as fixed
-		event[expando] = true;
-
-		// add preventDefault and stopPropagation since
-		// they will not work on the clone
-		event.preventDefault = function() {
-			// if preventDefault exists run it on the original event
-			if (originalEvent.preventDefault)
-				originalEvent.preventDefault();
-			// otherwise set the returnValue property of the original event to false (IE)
-			originalEvent.returnValue = false;
-		};
-		event.stopPropagation = function() {
-			// if stopPropagation exists run it on the original event
-			if (originalEvent.stopPropagation)
-				originalEvent.stopPropagation();
-			// otherwise set the cancelBubble property of the original event to true (IE)
-			originalEvent.cancelBubble = true;
-		};
-
-		event.stopImmediatePropagation = stopImmediatePropagation;
-
-		// Fix timeStamp
-		event.timeStamp = event.timeStamp || now();
-
 		// Fix target property, if necessary
 		if ( !event.target )
 			event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
@@ -413,11 +384,50 @@
 	}
 };
 
-function stopImmediatePropagation(){
-	this._sip = 1;
-	this.stopPropagation();
-}
+jQuery.Event = function( src ){
+	// Event object
+	if( src && src.type ){
+		this.originalEvent = src;
+		this.type = src.type;
+		
+		// Fix timeStamp
+		this.timeStamp = src.timeStamp || now();
+	// Event type
+	}else
+		this.type = src;
 
+	// Mark it as fixed
+	this[expando] = true;
+};
+
+jQuery.Event.prototype = {
+	// add preventDefault and stopPropagation since
+	// they will not work on the clone
+	preventDefault: function() {
+		var e = this.originalEvent;
+		if( !e )
+			return;
+		// if preventDefault exists run it on the original event
+		if (e.preventDefault)
+			e.preventDefault();
+		// otherwise set the returnValue property of the original event to false (IE)
+		e.returnValue = false;
+	},
+	stopPropagation: function() {
+		var e = this.originalEvent;
+		if( !e )
+			return;
+		// if stopPropagation exists run it on the original event
+		if (e.stopPropagation)
+			e.stopPropagation();
+		// otherwise set the cancelBubble property of the original event to true (IE)
+		e.cancelBubble = true;
+	},
+	stopImmediatePropagation:function(){
+		this._sip = true;
+		this.stopPropagation();
+	}
+};
 // Checks if an event happened on an element within another element
 // Used in jQuery.event.special.mouseenter and mouseleave handlers
 var withinElement = function(event) {
Index: test/unit/event.js
===================================================================
--- test/unit/event.js	(revision 5996)
+++ test/unit/event.js	(working copy)
@@ -270,7 +270,7 @@
 });
 
 test("trigger(event, [data], [fn])", function() {
-	expect(67);
+	expect(62);
 
 	var handler = function(event, a, b, c) {
 		equals( event.type, "click", "check passed data" );
@@ -301,48 +301,46 @@
 		equals( c, "abc", "check passed data" );
 		equals( v, "test", "check current value" );
 	};
+	
+	var $elem = jQuery("#firstp");
 
 	// Simulate a "native" click
-	jQuery("#firstp")[0].click = function(){
+	$elem[0].click = function(){
 		ok( true, "Native call was triggered" );
 	};
 
 	// Triggers handlrs and native
 	// Trigger 5
-	jQuery("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
+	$elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
 
 	// Triggers handlers, native, and extra fn
 	// Triggers 9
-	jQuery("#firstp").trigger("click", [1, "2", "abc"], handler4);
+	$elem.trigger("click", [1, "2", "abc"], handler4);
 
 	// Simulate a "native" click
-	jQuery("#firstp")[0].click = function(){
+	$elem[0].click = function(){
 		ok( false, "Native call was triggered" );
 	};
 
-	// Triggers handlers, native, and extra fn
-	// Triggers 7
-	jQuery("#firstp").trigger("click", [1, "2", "abc"], handler2);
-
 	// Trigger only the handlers (no native)
 	// Triggers 5
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
 
 	// Trigger only the handlers (no native) and extra fn
 	// Triggers 8
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
 
 	// Build fake click event to pass in
-	var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
+	var eventObj = new jQuery.Event("click");
 
 	// Trigger only the handlers (no native), with external event obj
 	// Triggers 5
-	equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
+	equals( $elem.triggerHandler(eventObj, [1, "2", "abc"]), "test", "Verify handler response" );
 
 	// Trigger only the handlers (no native) and extra fn, with external event obj
 	// Triggers 9
-	eventObj = jQuery.event.fix({ type: "foo", target: document.body });
-	equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
+	eventObj = new jQuery.Event("click");
+	equals( $elem.triggerHandler(eventObj, [1, "2", "abc"], handler2), false, "Verify handler response" );
 	
 	var pass = true;
 	try {
@@ -356,11 +354,23 @@
 
 	// have the extra handler override the return
 	// Triggers 9
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
 
 	// have the extra handler leave the return value alone
 	// Triggers 9
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
+	
+	eventObj = new jQuery.Event('foo');
+	eventObj.secret = 'boo!';
+	
+	$elem.unbind('click').bind('foo',function(e){
+		equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
+		equals( e.target.id, 'firstp', 'Verify event.target when passed passing an event object' );
+		equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
+	});
+	
+	$elem.triggerHandler( eventObj );
+	$elem.unbind('foo');
 });
 
 test("toggle(Function, Function, ...)", function() {

Download in other formats:

Original Format