Dev: update jquery.simulate.js to latest version from http://github.com/jquery/jquery-simulate

This commit is contained in:
Mike Sherov 2012-12-12 03:59:11 -05:00
parent fec36fd214
commit a677ea7dc0
3 changed files with 97 additions and 39 deletions

View File

@ -1,13 +1,15 @@
/* /*!
* jquery.simulate - simulate browser mouse and keyboard events * jQuery Simulate v0.0.1 - simulate browser mouse and keyboard events
* http://jqueryui.com * https://github.com/jquery/jquery-simulate
* *
* Copyright 2012 jQuery Foundation and other contributors * Copyright 2012 jQuery Foundation and other contributors
* Released under the MIT license. * Released under the MIT license.
* http://jquery.org/license * http://jquery.org/license
*
* Date: Sun Dec 9 12:15:33 2012 -0500
*/ */
;(function( $ ) { ;(function( $, undefined ) {
var rkeyEvent = /^key/, var rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|contextmenu)|click/; rmouseEvent = /^(?:mouse|contextmenu)|click/;
@ -31,7 +33,42 @@ $.simulate = function( elem, type, options ) {
} }
}; };
$.extend( $.simulate, {
keyCode: {
BACKSPACE: 8,
COMMA: 188,
DELETE: 46,
DOWN: 40,
END: 35,
ENTER: 13,
ESCAPE: 27,
HOME: 36,
LEFT: 37,
NUMPAD_ADD: 107,
NUMPAD_DECIMAL: 110,
NUMPAD_DIVIDE: 111,
NUMPAD_ENTER: 108,
NUMPAD_MULTIPLY: 106,
NUMPAD_SUBTRACT: 109,
PAGE_DOWN: 34,
PAGE_UP: 33,
PERIOD: 190,
RIGHT: 39,
SPACE: 32,
TAB: 9,
UP: 38
},
buttonCode: {
LEFT: 0,
MIDDLE: 1,
RIGHT: 2
}
});
$.extend( $.simulate.prototype, { $.extend( $.simulate.prototype, {
simulateEvent: function( elem, type, options ) { simulateEvent: function( elem, type, options ) {
var event = this.createEvent( type, options ); var event = this.createEvent( type, options );
this.dispatchEvent( elem, type, event, options ); this.dispatchEvent( elem, type, event, options );
@ -56,7 +93,6 @@ $.extend( $.simulate.prototype, {
detail: 0, detail: 0,
screenX: 0, screenX: 0,
screenY: 0, screenY: 0,
// TODO: default clientX/Y to a position within the target element
clientX: 1, clientX: 1,
clientY: 1, clientY: 1,
ctrlKey: false, ctrlKey: false,
@ -101,8 +137,14 @@ $.extend( $.simulate.prototype, {
} else if ( document.createEventObject ) { } else if ( document.createEventObject ) {
event = document.createEventObject(); event = document.createEventObject();
$.extend( event, options ); $.extend( event, options );
// TODO: what is this mapping for? // standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
event.button = { 0:1, 1:4, 2:2 }[ event.button ] || event.button; // old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
// so we actually need to map the standard back to oldIE
event.button = {
0: 1,
1: 4,
2: 2
}[ event.button ] || event.button;
} }
return event; return event;
@ -128,7 +170,10 @@ $.extend( $.simulate.prototype, {
event.initKeyEvent( type, options.bubbles, options.cancelable, options.view, event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
options.keyCode, options.charCode ); options.keyCode, options.charCode );
// TODO: what is this supporting? // initKeyEvent throws an exception in WebKit
// see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
// and also https://bugs.webkit.org/show_bug.cgi?id=13368
// fall back to a generic event until we decide to implement initKeyboardEvent
} catch( err ) { } catch( err ) {
event = document.createEvent( "Events" ); event = document.createEvent( "Events" );
event.initEvent( type, options.bubbles, options.cancelable ); event.initEvent( type, options.bubbles, options.cancelable );
@ -147,9 +192,7 @@ $.extend( $.simulate.prototype, {
$.extend( event, options ); $.extend( event, options );
} }
// TODO: can we hook into core's logic? if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
if ( $.ui.ie || (({}).toString.call( window.opera ) === "[object Opera]") ) {
// TODO: is charCode ever <0 ? Can we just use charCode || keyCode?
event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode; event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
event.charCode = undefined; event.charCode = undefined;
} }
@ -157,7 +200,6 @@ $.extend( $.simulate.prototype, {
return event; return event;
}, },
// TODO: does this need type? Can't we just check event.type?
dispatchEvent: function( elem, type, event ) { dispatchEvent: function( elem, type, event ) {
if ( elem.dispatchEvent ) { if ( elem.dispatchEvent ) {
elem.dispatchEvent( event ); elem.dispatchEvent( event );
@ -237,20 +279,31 @@ function findCenter( elem ) {
$.extend( $.simulate.prototype, { $.extend( $.simulate.prototype, {
simulateDrag: function() { simulateDrag: function() {
var target = this.target, var i = 0,
target = this.target,
options = this.options, options = this.options,
center = findCenter( target ), center = findCenter( target ),
x = Math.floor( center.x ), x = Math.floor( center.x ),
y = Math.floor( center.y ), y = Math.floor( center.y ),
dx = options.dx || 0, dx = options.dx || 0,
dy = options.dy || 0, dy = options.dy || 0,
moves = options.moves || 3,
coord = { clientX: x, clientY: y }; coord = { clientX: x, clientY: y };
this.simulateEvent( target, "mousedown", coord ); this.simulateEvent( target, "mousedown", coord );
coord = { clientX: x + 1, clientY: y + 1 };
this.simulateEvent( document, "mousemove", coord ); for ( ; i < moves ; i++ ) {
coord = { clientX: x + dx, clientY: y + dy }; x += dx / moves;
this.simulateEvent( document, "mousemove", coord ); y += dy / moves;
coord = {
clientX: Math.round( x ),
clientY: Math.round( y )
};
this.simulateEvent( document, "mousemove", coord ); this.simulateEvent( document, "mousemove", coord );
}
this.simulateEvent( target, "mouseup", coord ); this.simulateEvent( target, "mouseup", coord );
this.simulateEvent( target, "click", coord ); this.simulateEvent( target, "click", coord );
} }

View File

@ -40,8 +40,8 @@ test("resize", function() {
handles: "all", handles: "all",
resize: function( event, ui ) { resize: function( event, ui ) {
if ( count === 0 ) { if ( count === 0 ) {
equal( ui.size.width, 101, "compare width" ); equal( ui.size.width, 125, "compare width" );
equal( ui.size.height, 101, "compare height" ); equal( ui.size.height, 125, "compare height" );
equal( ui.originalSize.width, 100, "compare original width" ); equal( ui.originalSize.width, 100, "compare original width" );
equal( ui.originalSize.height, 100, "compare original height" ); equal( ui.originalSize.height, 100, "compare original height" );
} else { } else {
@ -82,7 +82,7 @@ test("resize (min/max dimensions)", function() {
} }
}); });
TestHelpers.resizable.drag(handle, -50, -50); TestHelpers.resizable.drag( handle, -200, -200 );
equal( count, 1, "resize callback should happen exactly once per size adjustment" ); equal( count, 1, "resize callback should happen exactly once per size adjustment" );
@ -103,15 +103,19 @@ test("resize (containment)", function() {
handles: "all", handles: "all",
containment: container, containment: container,
resize: function( event, ui ) { resize: function( event, ui ) {
equal( ui.size.width, 50, "compare width" ); equal( ui.size.width, 10, "compare width" );
equal( ui.size.height, 50, "compare height" ); equal( ui.size.height, 10, "compare height" );
equal( ui.originalSize.width, 100, "compare original width" ); equal( ui.originalSize.width, 100, "compare original width" );
equal( ui.originalSize.height, 100, "compare original height" ); equal( ui.originalSize.height, 100, "compare original height" );
count++; count++;
} }
}); });
TestHelpers.resizable.drag(handle, -50, -50); // Prove you can't resize outside containment by dragging southeast corner southeast
TestHelpers.resizable.drag( handle, 100, 100 );
// Prove you can't resize outside containment by dragging southeast corner northwest
TestHelpers.resizable.drag( handle, -200, -200 );
equal( count, 1, "resize callback should happen exactly once per size adjustment" ); equal( count, 1, "resize callback should happen exactly once per size adjustment" );

View File

@ -3,6 +3,7 @@ TestHelpers.resizable = {
// this mouseover is to work around a limitation in resizable // this mouseover is to work around a limitation in resizable
// TODO: fix resizable so handle doesn't require mouseover in order to be used // TODO: fix resizable so handle doesn't require mouseover in order to be used
$( el ).simulate("mouseover").simulate( "drag", { $( el ).simulate("mouseover").simulate( "drag", {
moves: 2,
dx: dx, dx: dx,
dy: dy dy: dy
}); });