Skip to content

Commit d288b26

Browse files
committed
Broke out the dragging simulation into a dragger object that can be manipulated as desired
1 parent d5964e4 commit d288b26

File tree

1 file changed

+62
-22
lines changed

1 file changed

+62
-22
lines changed

jquery.simulate.js

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -279,34 +279,74 @@ function findCenter( elem ) {
279279

280280
$.extend( $.simulate.prototype, {
281281
simulateDrag: function() {
282-
var i = 0,
283-
target = this.target,
284-
options = this.options,
285-
center = findCenter( target ),
286-
x = Math.floor( center.x ),
287-
y = Math.floor( center.y ),
288-
dx = options.dx || 0,
289-
dy = options.dy || 0,
290-
moves = options.moves || 3,
291-
coord = { clientX: x, clientY: y };
292-
293-
this.simulateEvent( target, "mousedown", coord );
294-
295-
for ( ; i < moves ; i++ ) {
296-
x += dx / moves;
297-
y += dy / moves;
298-
299-
coord = {
282+
var dragger = this.makeDragger();
283+
dragger.start(this.target);
284+
dragger.move(this.options);
285+
dragger.end();
286+
},
287+
makeDragger: function () {
288+
return new $.simulate.dragger(this);
289+
},
290+
});
291+
292+
$.simulate.dragger = function (simulator) {
293+
this.started = false;
294+
this.ended = false;
295+
this.simulator = simulator;
296+
};
297+
$.extend( $.simulate.dragger.prototype, {
298+
start: function (target) {
299+
if ( this.started || this.ended ) return;
300+
301+
this.started = true;
302+
this.target = target;
303+
var center = findCenter( target );
304+
this.coord = {
305+
clientX: Math.floor(center.x),
306+
clientY: Math.floor(center.y),
307+
};
308+
309+
this.simulator.simulateEvent( target, "mousedown", this.coord );
310+
},
311+
move: function (delta) {
312+
if (!this.started || this.ended) return;
313+
314+
var moves = delta.steps || 3,
315+
dx = Math.floor(delta.dx || 0),
316+
dy = Math.floor(delta.dy || 0);
317+
318+
var final_coord = {
319+
clientX: this.coord.clientX + dx,
320+
clientY: this.coord.clientY + dy,
321+
};
322+
323+
for ( var i=0 ; i < moves-1 ; i++ ) {
324+
var x = (dx / moves);
325+
var y = (dy / moves);
326+
327+
var coord = {
300328
clientX: Math.round( x ),
301329
clientY: Math.round( y )
302330
};
303331

304-
this.simulateEvent( document, "mousemove", coord );
332+
this.simulator.simulateEvent( document, "mousemove", coord );
305333
}
306334

307-
this.simulateEvent( target, "mouseup", coord );
308-
this.simulateEvent( target, "click", coord );
309-
}
335+
this.simulator.simulateEvent( document, "mousemove", final_coord );
336+
this.coord = final_coord;
337+
338+
},
339+
end: function (delta) {
340+
if (!this.started || this.ended) return;
341+
if ( delta ) {
342+
this.move(delta);
343+
}
344+
345+
this.ended = true;
346+
347+
this.simulator.simulateEvent( this.target, "mouseup", this.coord );
348+
this.simulator.simulateEvent( this.target, "click", this.coord );
349+
},
310350
});
311351

312352
})( jQuery );

0 commit comments

Comments
 (0)