Ticket #3662: event-object[5997].diff
File event-object[5997].diff, 10.1 KB (added by , 13 years ago) |
---|
-
src/event.js
163 163 } 164 164 }, 165 165 166 trigger: function(type, data, elem, donative, extra, dohandlers) { 167 // Clone the incoming data, if any 168 data = jQuery.makeArray(data); 169 170 if ( type.indexOf("!") >= 0 ) { 171 type = type.slice(0, -1); 172 var exclusive = true; 173 } 174 166 trigger: function( e, data, elem, donative, extra, dohandlers) { 167 // Event object or event type 168 var type = e.type || e; 169 175 170 // Handle a global trigger 176 171 if ( !elem ) { 177 172 // Only trigger if we've ever bound an event for it 178 173 if ( this.global[type] ) 179 174 jQuery.each( jQuery.cache, function(){ 180 175 if ( this.events && this.events[type] ) 181 jQuery.event.trigger( type, data, this.handle.elem, false );176 jQuery.event.trigger( e, data, this.handle.elem, false ); 182 177 }); 183 178 184 179 // Handle triggering a single element 185 180 } else { 181 186 182 // don't do events on text and comment nodes 187 183 if ( elem.nodeType == 3 || elem.nodeType == 8 ) 188 184 return undefined; 189 185 190 var val, ret, fn = jQuery.isFunction( elem[ type ] || null ), 191 // Check to see if we need to provide a fake event, or not 192 event = !data[0] || !data[0].preventDefault; 193 194 // Pass along a fake event 195 if ( event ) { 196 data.unshift({ 197 type: type, 198 target: elem, 199 preventDefault: function(){}, 200 stopPropagation: function(){}, 201 stopImmediatePropagation:stopImmediatePropagation, 202 timeStamp: now() 203 }); 204 data[0][expando] = true; // no need to fix fake event 186 // Clone the incoming data, if any 187 data = jQuery.makeArray(data); 188 189 if ( type.indexOf("!") >= 0 ) { 190 type = type.slice(0, -1); 191 var exclusive = true; 205 192 } 193 194 e = typeof e === "object" ? 195 // jQuery.Event object 196 e[expando] ? e : 197 // Object literal 198 jQuery.extend( new jQuery.Event(type), e ) : 199 // Just the event type (string) 200 new jQuery.Event(type); 201 202 e.target = elem; 203 e.exclusive = exclusive; 204 205 data.unshift( e ); 206 206 207 // Enforce the right trigger type 208 data[0].type = type; 209 if ( exclusive ) 210 data[0].exclusive = true; 207 var val, ret, fn = jQuery.isFunction( elem[ type ] ); 211 208 212 209 if ( dohandlers !== false ) { 213 210 // Trigger the event, it is assumed that "handle" is a function … … 227 224 } 228 225 229 226 // Extra functions don't get the custom event object 230 if ( event ) 231 data.shift(); 227 data.shift(); 232 228 233 229 // Handle triggering of extra function 234 230 if ( extra && jQuery.isFunction( extra ) ) { … … 300 296 return val; 301 297 }, 302 298 303 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 t imeStamp toElement typeview wheelDelta which".split(" "),299 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(" "), 304 300 305 301 fix: function(event) { 306 302 if ( event[expando] ) … … 309 305 // store a copy of the original event object 310 306 // and "clone" to set read-only properties 311 307 var originalEvent = event; 312 event = { originalEvent: originalEvent };308 event = new jQuery.Event( originalEvent ); 313 309 314 310 for ( var i = this.props.length, prop; i; ){ 315 311 prop = this.props[ --i ]; 316 312 event[ prop ] = originalEvent[ prop ]; 317 313 } 318 314 319 // Mark it as fixed320 event[expando] = true;321 322 // add preventDefault and stopPropagation since323 // they will not work on the clone324 event.preventDefault = function() {325 // if preventDefault exists run it on the original event326 if (originalEvent.preventDefault)327 originalEvent.preventDefault();328 // otherwise set the returnValue property of the original event to false (IE)329 originalEvent.returnValue = false;330 };331 event.stopPropagation = function() {332 // if stopPropagation exists run it on the original event333 if (originalEvent.stopPropagation)334 originalEvent.stopPropagation();335 // otherwise set the cancelBubble property of the original event to true (IE)336 originalEvent.cancelBubble = true;337 };338 339 event.stopImmediatePropagation = stopImmediatePropagation;340 341 // Fix timeStamp342 event.timeStamp = event.timeStamp || now();343 344 315 // Fix target property, if necessary 345 316 if ( !event.target ) 346 317 event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either … … 413 384 } 414 385 }; 415 386 416 function stopImmediatePropagation(){ 417 this._sip = 1; 418 this.stopPropagation(); 419 } 387 jQuery.Event = function( src ){ 388 // Event object 389 if( src && src.type ){ 390 this.originalEvent = src; 391 this.type = src.type; 392 393 // Fix timeStamp 394 this.timeStamp = src.timeStamp || now(); 395 // Event type 396 }else 397 this.type = src; 420 398 399 // Mark it as fixed 400 this[expando] = true; 401 }; 402 403 jQuery.Event.prototype = { 404 // add preventDefault and stopPropagation since 405 // they will not work on the clone 406 preventDefault: function() { 407 var e = this.originalEvent; 408 if( !e ) 409 return; 410 // if preventDefault exists run it on the original event 411 if (e.preventDefault) 412 e.preventDefault(); 413 // otherwise set the returnValue property of the original event to false (IE) 414 e.returnValue = false; 415 }, 416 stopPropagation: function() { 417 var e = this.originalEvent; 418 if( !e ) 419 return; 420 // if stopPropagation exists run it on the original event 421 if (e.stopPropagation) 422 e.stopPropagation(); 423 // otherwise set the cancelBubble property of the original event to true (IE) 424 e.cancelBubble = true; 425 }, 426 stopImmediatePropagation:function(){ 427 this._sip = true; 428 this.stopPropagation(); 429 } 430 }; 421 431 // Checks if an event happened on an element within another element 422 432 // Used in jQuery.event.special.mouseenter and mouseleave handlers 423 433 var withinElement = function(event) { -
test/unit/event.js
270 270 }); 271 271 272 272 test("trigger(event, [data], [fn])", function() { 273 expect(6 7);273 expect(62); 274 274 275 275 var handler = function(event, a, b, c) { 276 276 equals( event.type, "click", "check passed data" ); … … 301 301 equals( c, "abc", "check passed data" ); 302 302 equals( v, "test", "check current value" ); 303 303 }; 304 305 var $elem = jQuery("#firstp"); 304 306 305 307 // Simulate a "native" click 306 jQuery("#firstp")[0].click = function(){308 $elem[0].click = function(){ 307 309 ok( true, "Native call was triggered" ); 308 310 }; 309 311 310 312 // Triggers handlrs and native 311 313 // Trigger 5 312 jQuery("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);314 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]); 313 315 314 316 // Triggers handlers, native, and extra fn 315 317 // Triggers 9 316 jQuery("#firstp").trigger("click", [1, "2", "abc"], handler4);318 $elem.trigger("click", [1, "2", "abc"], handler4); 317 319 318 320 // Simulate a "native" click 319 jQuery("#firstp")[0].click = function(){321 $elem[0].click = function(){ 320 322 ok( false, "Native call was triggered" ); 321 323 }; 322 324 323 // Triggers handlers, native, and extra fn324 // Triggers 7325 jQuery("#firstp").trigger("click", [1, "2", "abc"], handler2);326 327 325 // Trigger only the handlers (no native) 328 326 // Triggers 5 329 equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );327 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" ); 330 328 331 329 // Trigger only the handlers (no native) and extra fn 332 330 // Triggers 8 333 equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );331 equals( $elem.triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" ); 334 332 335 333 // Build fake click event to pass in 336 var eventObj = jQuery.event.fix({ type: "foo", target: document.body });334 var eventObj = new jQuery.Event("click"); 337 335 338 336 // Trigger only the handlers (no native), with external event obj 339 337 // Triggers 5 340 equals( jQuery("#firstp").triggerHandler("click", [eventObj,1, "2", "abc"]), "test", "Verify handler response" );338 equals( $elem.triggerHandler(eventObj, [1, "2", "abc"]), "test", "Verify handler response" ); 341 339 342 340 // Trigger only the handlers (no native) and extra fn, with external event obj 343 341 // Triggers 9 344 eventObj = jQuery.event.fix({ type: "foo", target: document.body });345 equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );342 eventObj = new jQuery.Event("click"); 343 equals( $elem.triggerHandler(eventObj, [1, "2", "abc"], handler2), false, "Verify handler response" ); 346 344 347 345 var pass = true; 348 346 try { … … 356 354 357 355 // have the extra handler override the return 358 356 // Triggers 9 359 equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );357 equals( $elem.triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" ); 360 358 361 359 // have the extra handler leave the return value alone 362 360 // Triggers 9 363 equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" ); 361 equals( $elem.triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" ); 362 363 eventObj = new jQuery.Event('foo'); 364 eventObj.secret = 'boo!'; 365 366 $elem.unbind('click').bind('foo',function(e){ 367 equals( e.type, 'foo', 'Verify event type when passed passing an event object' ); 368 equals( e.target.id, 'firstp', 'Verify event.target when passed passing an event object' ); 369 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' ); 370 }); 371 372 $elem.triggerHandler( eventObj ); 373 $elem.unbind('foo'); 364 374 }); 365 375 366 376 test("toggle(Function, Function, ...)", function() {