Skip to content
Permalink
Browse files

Dev: update jquery.simulate.js to latest version from http://github.c…

  • Loading branch information...
mikesherov committed Dec 12, 2012
1 parent fec36fd commit a677ea7dc01b71028af94c3b5b9fa82d401cd98c
Showing with 97 additions and 39 deletions.
  1. +71 −18 tests/jquery.simulate.js
  2. +25 −21 tests/unit/resizable/resizable_events.js
  3. +1 −0 tests/unit/resizable/resizable_test_helpers.js
@@ -1,13 +1,15 @@
/*
* jquery.simulate - simulate browser mouse and keyboard events
* http://jqueryui.com
/*!
* jQuery Simulate v0.0.1 - simulate browser mouse and keyboard events
* https://github.com/jquery/jquery-simulate
*
* Copyright 2012 jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*
* Date: Sun Dec 9 12:15:33 2012 -0500
*/

;(function( $ ) {
;(function( $, undefined ) {

var rkeyEvent = /^key/,
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, {

simulateEvent: function( elem, type, options ) {
var event = this.createEvent( type, options );
this.dispatchEvent( elem, type, event, options );
@@ -56,7 +93,6 @@ $.extend( $.simulate.prototype, {
detail: 0,
screenX: 0,
screenY: 0,
// TODO: default clientX/Y to a position within the target element
clientX: 1,
clientY: 1,
ctrlKey: false,
@@ -101,8 +137,14 @@ $.extend( $.simulate.prototype, {
} else if ( document.createEventObject ) {
event = document.createEventObject();
$.extend( event, options );
// TODO: what is this mapping for?
event.button = { 0:1, 1:4, 2:2 }[ event.button ] || event.button;
// standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
// 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;
@@ -128,7 +170,10 @@ $.extend( $.simulate.prototype, {
event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
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 ) {
event = document.createEvent( "Events" );
event.initEvent( type, options.bubbles, options.cancelable );
@@ -147,17 +192,14 @@ $.extend( $.simulate.prototype, {
$.extend( event, options );
}

// TODO: can we hook into core's logic?
if ( $.ui.ie || (({}).toString.call( window.opera ) === "[object Opera]") ) {
// TODO: is charCode ever <0 ? Can we just use charCode || keyCode?
if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
event.charCode = undefined;
}

return event;
},

// TODO: does this need type? Can't we just check event.type?
dispatchEvent: function( elem, type, event ) {
if ( elem.dispatchEvent ) {
elem.dispatchEvent( event );
@@ -237,20 +279,31 @@ function findCenter( elem ) {

$.extend( $.simulate.prototype, {
simulateDrag: function() {
var target = this.target,
var i = 0,
target = this.target,
options = this.options,
center = findCenter( target ),
x = Math.floor( center.x ),
y = Math.floor( center.y ),
dx = options.dx || 0,
dy = options.dy || 0,
moves = options.moves || 3,
coord = { clientX: x, clientY: y };

this.simulateEvent( target, "mousedown", coord );
coord = { clientX: x + 1, clientY: y + 1 };
this.simulateEvent( document, "mousemove", coord );
coord = { clientX: x + dx, clientY: y + dy };
this.simulateEvent( document, "mousemove", coord );
this.simulateEvent( document, "mousemove", coord );

for ( ; i < moves ; i++ ) {
x += dx / moves;
y += dy / moves;

coord = {
clientX: Math.round( x ),
clientY: Math.round( y )
};

this.simulateEvent( document, "mousemove", coord );
}

this.simulateEvent( target, "mouseup", coord );
this.simulateEvent( target, "click", coord );
}
@@ -29,19 +29,19 @@ test("start", function() {

});

test("resize", function() {
test( "resize", function() {

expect(9);
expect( 9 );

var count = 0,
handle = ".ui-resizable-se";

$("#resizable1").resizable({
handles: "all",
resize: function(event, ui) {
if (count === 0) {
equal( ui.size.width, 101, "compare width" );
equal( ui.size.height, 101, "compare height" );
resize: function( event, ui ) {
if ( count === 0 ) {
equal( ui.size.width, 125, "compare width" );
equal( ui.size.height, 125, "compare height" );
equal( ui.originalSize.width, 100, "compare original width" );
equal( ui.originalSize.height, 100, "compare original height" );
} else {
@@ -54,15 +54,15 @@ test("resize", function() {
}
});

TestHelpers.resizable.drag(handle, 50, 50);
TestHelpers.resizable.drag( handle, 50, 50 );

equal(count, 2, "resize callback should happen exactly once per size adjustment");
equal( count, 2, "resize callback should happen exactly once per size adjustment" );

});

test("resize (min/max dimensions)", function() {
test( "resize (min/max dimensions)", function() {

expect(5);
expect( 5 );

var count = 0,
handle = ".ui-resizable-se";
@@ -73,7 +73,7 @@ test("resize (min/max dimensions)", function() {
minHeight: 60,
maxWidth: 100,
maxHeight: 100,
resize: function(event, ui) {
resize: function( event, ui ) {
equal( ui.size.width, 60, "compare width" );
equal( ui.size.height, 60, "compare height" );
equal( ui.originalSize.width, 100, "compare original width" );
@@ -82,15 +82,15 @@ 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" );

});

test("resize (containment)", function() {
test( "resize (containment)", function() {

expect(5);
expect( 5 );

var count = 0,
handle = ".ui-resizable-se",
@@ -102,18 +102,22 @@ test("resize (containment)", function() {
$("#resizable1").resizable({
handles: "all",
containment: container,
resize: function(event, ui) {
equal( ui.size.width, 50, "compare width" );
equal( ui.size.height, 50, "compare height" );
resize: function( event, ui ) {
equal( ui.size.width, 10, "compare width" );
equal( ui.size.height, 10, "compare height" );
equal( ui.originalSize.width, 100, "compare original width" );
equal( ui.originalSize.height, 100, "compare original height" );
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 );

equal(count, 1, "resize callback should happen exactly once per size adjustment");
// 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" );

});

@@ -148,7 +152,7 @@ test("stop", function() {

var count = 0,
handle = ".ui-resizable-se";

$("#resizable1").resizable({
handles: "all",
stop: function(event, ui) {
@@ -3,6 +3,7 @@ TestHelpers.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
$( el ).simulate("mouseover").simulate( "drag", {
moves: 2,
dx: dx,
dy: dy
});

0 comments on commit a677ea7

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