From d288b26ebbbdc1162a16482df7af88220c0236ff Mon Sep 17 00:00:00 2001 From: Rob Kinyon Date: Mon, 11 Mar 2013 20:34:57 -0400 Subject: [PATCH 1/2] Broke out the dragging simulation into a dragger object that can be manipulated as desired --- jquery.simulate.js | 84 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/jquery.simulate.js b/jquery.simulate.js index 4ac556a..fe61aab 100644 --- a/jquery.simulate.js +++ b/jquery.simulate.js @@ -279,34 +279,74 @@ function findCenter( elem ) { $.extend( $.simulate.prototype, { simulateDrag: function() { - 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 ); - - for ( ; i < moves ; i++ ) { - x += dx / moves; - y += dy / moves; - - coord = { + var dragger = this.makeDragger(); + dragger.start(this.target); + dragger.move(this.options); + dragger.end(); + }, + makeDragger: function () { + return new $.simulate.dragger(this); + }, +}); + +$.simulate.dragger = function (simulator) { + this.started = false; + this.ended = false; + this.simulator = simulator; +}; +$.extend( $.simulate.dragger.prototype, { + start: function (target) { + if ( this.started || this.ended ) return; + + this.started = true; + this.target = target; + var center = findCenter( target ); + this.coord = { + clientX: Math.floor(center.x), + clientY: Math.floor(center.y), + }; + + this.simulator.simulateEvent( target, "mousedown", this.coord ); + }, + move: function (delta) { + if (!this.started || this.ended) return; + + var moves = delta.steps || 3, + dx = Math.floor(delta.dx || 0), + dy = Math.floor(delta.dy || 0); + + var final_coord = { + clientX: this.coord.clientX + dx, + clientY: this.coord.clientY + dy, + }; + + for ( var i=0 ; i < moves-1 ; i++ ) { + var x = (dx / moves); + var y = (dy / moves); + + var coord = { clientX: Math.round( x ), clientY: Math.round( y ) }; - this.simulateEvent( document, "mousemove", coord ); + this.simulator.simulateEvent( document, "mousemove", coord ); } - this.simulateEvent( target, "mouseup", coord ); - this.simulateEvent( target, "click", coord ); - } + this.simulator.simulateEvent( document, "mousemove", final_coord ); + this.coord = final_coord; + + }, + end: function (delta) { + if (!this.started || this.ended) return; + if ( delta ) { + this.move(delta); + } + + this.ended = true; + + this.simulator.simulateEvent( this.target, "mouseup", this.coord ); + this.simulator.simulateEvent( this.target, "click", this.coord ); + }, }); })( jQuery ); From a566ba54c08dcdb6640811ddb3e3ba120e3e5b34 Mon Sep 17 00:00:00 2001 From: Rob Kinyon Date: Tue, 12 Mar 2013 21:52:50 -0400 Subject: [PATCH 2/2] Lint fixes per grunt --- jquery.simulate.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/jquery.simulate.js b/jquery.simulate.js index fe61aab..9338f2a 100644 --- a/jquery.simulate.js +++ b/jquery.simulate.js @@ -286,7 +286,7 @@ $.extend( $.simulate.prototype, { }, makeDragger: function () { return new $.simulate.dragger(this); - }, + } }); $.simulate.dragger = function (simulator) { @@ -296,35 +296,41 @@ $.simulate.dragger = function (simulator) { }; $.extend( $.simulate.dragger.prototype, { start: function (target) { - if ( this.started || this.ended ) return; + if ( this.started || this.ended ) { + return; + } this.started = true; this.target = target; var center = findCenter( target ); this.coord = { clientX: Math.floor(center.x), - clientY: Math.floor(center.y), + clientY: Math.floor(center.y) }; this.simulator.simulateEvent( target, "mousedown", this.coord ); }, move: function (delta) { - if (!this.started || this.ended) return; + if (!this.started || this.ended) { + return; + } var moves = delta.steps || 3, dx = Math.floor(delta.dx || 0), - dy = Math.floor(delta.dy || 0); - - var final_coord = { - clientX: this.coord.clientX + dx, - clientY: this.coord.clientY + dy, - }; - - for ( var i=0 ; i < moves-1 ; i++ ) { - var x = (dx / moves); - var y = (dy / moves); - - var coord = { + dy = Math.floor(delta.dy || 0), + final_coord = { + clientX: this.coord.clientX + dx, + clientY: this.coord.clientY + dy + }, + i = 0, + x = 0, + y = 0, + coord = {}; + + for ( i=0 ; i < moves-1 ; i++ ) { + x = (dx / moves); + y = (dy / moves); + coord = { clientX: Math.round( x ), clientY: Math.round( y ) }; @@ -337,7 +343,9 @@ $.extend( $.simulate.dragger.prototype, { }, end: function (delta) { - if (!this.started || this.ended) return; + if (!this.started || this.ended) { + return; + } if ( delta ) { this.move(delta); } @@ -346,7 +354,7 @@ $.extend( $.simulate.dragger.prototype, { this.simulator.simulateEvent( this.target, "mouseup", this.coord ); this.simulator.simulateEvent( this.target, "click", this.coord ); - }, + } }); })( jQuery );