From 0b2be30492a91f7392845ee82befe16e65b061a2 Mon Sep 17 00:00:00 2001 From: Richard Worth Date: Wed, 4 Jun 2008 08:47:26 +0000 Subject: Added simulate "drag" method. Fixed some issues in resizable tests. --- ui/tests/jquery.simulate.js | 44 ++++++++++-- ui/tests/resizable.html | 13 ++-- ui/tests/resizable.js | 164 ++++++++++++++++++++++---------------------- 3 files changed, 126 insertions(+), 95 deletions(-) diff --git a/ui/tests/jquery.simulate.js b/ui/tests/jquery.simulate.js index 7f3109994..9fa137734 100644 --- a/ui/tests/jquery.simulate.js +++ b/ui/tests/jquery.simulate.js @@ -12,19 +12,28 @@ ;(function($) { $.fn.extend({ - simulate: function(type, options) { + simulate: function(type, options, complete) { return this.each(function() { - new $.simulate(this, type, options); + var opt = $.extend({ complete: complete }, options); + new $.simulate(this, type, opt); }); } }); $.simulate = function(el, type, options) { - var evt = this.createEvent(type, options); - this.dispatchEvent(el, type, evt); + this.target = el; + if (/^drag$/.test(type)) { + this[type].apply(this, [this.target, options]); + } else { + this.simulateEvent(type, options); + } } $.extend($.simulate.prototype, { + simulateEvent: function(el, type, options) { + var evt = this.createEvent(type, options); + this.dispatchEvent(el, type, evt); + }, createEvent: function(type, options) { if (/^mouse(over|out|down|up|move)|(dbl)?click$/.test(type)) { return this.mouseEvent(type, options); @@ -89,7 +98,34 @@ $.extend($.simulate.prototype, { } else if (el.fireEvent) { el.fireEvent('on' + type, evt); } + }, + + findCenter: function(el) { + var el = $(this.target), o = el.offset(); + return { + x: o.left + el.outerWidth() / 2, + y: o.top + el.outerHeight() / 2 + }; + }, + drag: function(el, options) { + var center = this.findCenter(this.target), + x = center.x, y = center.y, + dx = options.dx || 0, + dy = options.dy || 0; + this.simulateEvent(this.target, "mouseover"); + this.simulateEvent(this.target, "mousedown", { clientX: x, clientY: y }); + this.simulateEvent(this.target, "mousemove", { clientX: x, clientY: y }); + this.simulateEvent(this.target, "mousemove", { clientX: x, clientY: y }); + this.simulateEvent(this.target, "mousemove", { clientX: x, clientY: y }); + this.simulateEvent(document, "mousemove", { clientX: x + dx, clientY: y + dy }); + this.simulateEvent(document, "mousemove", { clientX: x + dx, clientY: y + dy }); + this.simulateEvent(document, "mousemove", { clientX: x + dx, clientY: y + dy }); + this.simulateEvent(this.target, "mouseup", { clientX: x + dx, clientY: y + dy }); + this.simulateEvent(this.target, "click", { clientX: x + dx, clientY: y + dy }); + this.simulateEvent(this.target, "mouseout"); + (options.complete && options.complete()); } + }); })(jQuery); diff --git a/ui/tests/resizable.html b/ui/tests/resizable.html index 00c6a5352..955798293 100644 --- a/ui/tests/resizable.html +++ b/ui/tests/resizable.html @@ -12,18 +12,13 @@ - + +