Skip to content

Commit bc5d45d

Browse files
committed
add "moves" options to complex drag event
1 parent 7670cdb commit bc5d45d

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

jquery.simulate.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,31 @@ function findCenter( elem ) {
279279

280280
$.extend( $.simulate.prototype, {
281281
simulateDrag: function() {
282-
var target = this.target,
282+
var i = 0,
283+
target = this.target,
283284
options = this.options,
284285
center = findCenter( target ),
285286
x = Math.floor( center.x ),
286287
y = Math.floor( center.y ),
287288
dx = options.dx || 0,
288289
dy = options.dy || 0,
290+
moves = options.moves || 3,
289291
coord = { clientX: x, clientY: y };
292+
290293
this.simulateEvent( target, "mousedown", coord );
291-
coord = { clientX: x + 1, clientY: y + 1 };
292-
this.simulateEvent( document, "mousemove", coord );
293-
coord = { clientX: x + dx, clientY: y + dy };
294-
this.simulateEvent( document, "mousemove", coord );
295-
this.simulateEvent( document, "mousemove", coord );
294+
295+
for ( ; i < moves ; i++ ) {
296+
x += dx / moves;
297+
y += dy / moves;
298+
299+
coord = {
300+
clientX: parseInt( x, 10 ),
301+
clientY: parseInt( y, 10 )
302+
};
303+
304+
this.simulateEvent( document, "mousemove", coord );
305+
}
306+
296307
this.simulateEvent( target, "mouseup", coord );
297308
this.simulateEvent( target, "click", coord );
298309
}

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<title>jQuery-Simulate Test Suite</title>
66
<link rel="Stylesheet" media="screen" href="qunit/qunit/qunit.css" />
7+
<link rel="Stylesheet" media="screen" href="testinit.css" />
78
<script src="qunit/qunit/qunit.js"></script>
89
<script src="testinit.js"></script>
910
<script src="data/jquery.js"></script>

test/testinit.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.top-left {
2+
position: fixed;
3+
top: 1;
4+
left: 1;
5+
width: 1px;
6+
height: 1px;
7+
margin: 0;
8+
padding: 0;
9+
border: none;
10+
}

test/unit/simulate.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,47 @@ for ( ; i < keyEvents.length; i++ ) {
2222
testKeyEvent( keyEvents[ i ] );
2323
}
2424

25+
module( "complex events" );
26+
27+
asyncTest( "drag moves option", function() {
28+
29+
var moves = 15,
30+
calls = 0,
31+
el = jQuery("<div class='top-left'></div>").appendTo("#qunit-fixture");
32+
33+
expect( moves );
34+
35+
jQuery( document ).bind( "mousemove", function() {
36+
ok( true, "mousemove event fired at the document" );
37+
if ( ++calls === moves ) {
38+
jQuery( document ).unbind("mousemove");
39+
start();
40+
}
41+
});
42+
43+
el.simulate( "drag", {
44+
moves: 1,
45+
dx: 10,
46+
dy: 20
47+
}).simulate( "drag", {
48+
moves: 5,
49+
dx: 10,
50+
dy: 20
51+
});
52+
53+
// falsey defaults to 3
54+
el.simulate( "drag", {
55+
moves: 0,
56+
dx: 10,
57+
dy: 20
58+
}).simulate( "drag", {
59+
dx: 10,
60+
dy: 20
61+
}).simulate( "drag", {
62+
moves: null,
63+
dx: 10,
64+
dy: 20
65+
});
66+
});
67+
2568
})();

0 commit comments

Comments
 (0)