diff options
author | Richard Worth <rdworth@gmail.com> | 2008-06-04 03:41:17 +0000 |
---|---|---|
committer | Richard Worth <rdworth@gmail.com> | 2008-06-04 03:41:17 +0000 |
commit | 0646f774adee4ec804ac86a0355e3d7a212af097 (patch) | |
tree | ac40bd7d11d9df2ebc66eba6c303323a0baad608 /ui | |
parent | 9047a630494e63e7fd6262fcf3f39e0ae78f1c31 (diff) | |
download | jquery-ui-0646f774adee4ec804ac86a0355e3d7a212af097.tar.gz jquery-ui-0646f774adee4ec804ac86a0355e3d7a212af097.zip |
Added jquery.simulate.js as possible refactor of (low-level) parts of userAction and testmouse
Diffstat (limited to 'ui')
-rw-r--r-- | ui/tests/autodrag.html | 3 | ||||
-rw-r--r-- | ui/tests/jquery.simulate.js | 95 | ||||
-rw-r--r-- | ui/tests/ui.testmouse.js | 89 |
3 files changed, 127 insertions, 60 deletions
diff --git a/ui/tests/autodrag.html b/ui/tests/autodrag.html index df7590761..acc17fb4c 100644 --- a/ui/tests/autodrag.html +++ b/ui/tests/autodrag.html @@ -7,6 +7,7 @@ <script type="text/javascript" src="../../ui/source/ui.draggable.js"></script> <script type="text/javascript" src="../../ui/source/ui.selectable.js"></script> + <script type="text/javascript" src="jquery.simulate.js"></script> <script type="text/javascript" src="ui.testmouse.js"></script> <style type="text/css"> @@ -99,7 +100,7 @@ $(function() { if ($.isFunction(fn)) fn.apply(); } var plugin = "draggable"; - plugin = "selectable" + //plugin = "selectable" var testFn = "testMouse"; var testArgs = ["drag", 40, 50]; //1 diff --git a/ui/tests/jquery.simulate.js b/ui/tests/jquery.simulate.js new file mode 100644 index 000000000..7f3109994 --- /dev/null +++ b/ui/tests/jquery.simulate.js @@ -0,0 +1,95 @@ +/*
+ * jquery.simulate - simulate browser mouse and keyboard events
+ *
+ * Copyright (c) 2007 Eduardo Lundgren (eduardolundgren@gmail.com)
+ * and Richard D. Worth (rdworth@gmail.com)
+ *
+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
+ *
+ */
+
+;(function($) {
+
+$.fn.extend({
+ simulate: function(type, options) {
+ return this.each(function() {
+ new $.simulate(this, type, options);
+ });
+ }
+});
+
+$.simulate = function(el, type, options) {
+ var evt = this.createEvent(type, options);
+ this.dispatchEvent(el, type, evt);
+}
+
+$.extend($.simulate.prototype, {
+ createEvent: function(type, options) {
+ if (/^mouse(over|out|down|up|move)|(dbl)?click$/.test(type)) {
+ return this.mouseEvent(type, options);
+ } else if (/^key(up|down|press)$/.test(type)) {
+ return this.keyboardEvent(type, options);
+ }
+ },
+ mouseEvent: function(type, options) {
+ var evt;
+ var e = $.extend({
+ bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
+ screenX: 0, screenY: 0, clientX: 0, clientY: 0,
+ ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
+ button: 0, relatedTarget: null
+ }, options);
+ if ($.isFunction(document.createEvent)) {
+ evt = document.createEvent("MouseEvents");
+ evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail,
+ e.screenX, e.screenY, e.clientX, e.clientY,
+ e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
+ e.button, e.relatedTarget);
+ } else if (document.createEventObject) {
+ evt = document.createEventObject();
+ $.extend(evt, e);
+ evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button;
+ }
+ return evt;
+ },
+ keyboardEvent: function(type, options) {
+ var evt;
+ var e = $.extend({ bubbles: true, cancelable: true, view: window,
+ ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
+ keyCode: 0, charCode: 0
+ }, options);
+ if ($.isFunction(document.createEvent)) {
+ try {
+ evt = document.createEvent("KeyEvents");
+ evt.initKeyEvent(type, e.bubbles, e.cancelable, e.view,
+ e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
+ e.keyCode, e.charCode);
+ } catch(err) {
+ evt = document.createEvent("Events");
+ evt.initEvent(type, e.bubbles, e.cancelable);
+ $.extend(evt, { view: e.view,
+ ctrlKey: e.ctrlKey, altKey: e.altKey, shiftKey: e.shiftKey, metaKey: e.metaKey,
+ keyCode: e.keyCode, charCode: e.charCode
+ });
+ }
+ } else if (document.createEventObject) {
+ evt = document.createEventObject();
+ $.extend(evt, e);
+ }
+ if ($.browser.msie || $.browser.opera) {
+ evt.keyCode = (e.charCode > 0) ? e.charCode : e.keyCode;
+ evt.charCode = undefined;
+ }
+ return evt;
+ },
+ dispatchEvent: function(el, type, evt) {
+ if (el.dispatchEvent) {
+ el.dispatchEvent(evt);
+ } else if (el.fireEvent) {
+ el.fireEvent('on' + type, evt);
+ }
+ }
+});
+
+})(jQuery);
diff --git a/ui/tests/ui.testmouse.js b/ui/tests/ui.testmouse.js index b42a1036d..5c4d41204 100644 --- a/ui/tests/ui.testmouse.js +++ b/ui/tests/ui.testmouse.js @@ -34,40 +34,6 @@ y: (o.top + (offset || [0, 0])[1] || 0) + this.element.height() / 2 }; }, - dispatch: function(type, x, y, button, relatedTarget) { - var evt, e = {bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0, - screenX: 0, screenY: 0, clientX: x, clientY: y, - ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, - button: button || 0, relatedTarget: relatedTarget, isTrusted: false}; - if ($.isFunction(document.createEvent)) { - evt = document.createEvent("MouseEvents"); - if ($.isFunction(evt.initMouseEvent)) { - evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail, - e.screenX, e.screenY, e.clientX, e.clientY, - e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, - e.button, e.relatedTarget); - } else { - evt = document.createEvent("UIEvents"); - evt.initEvent(type, bubbles, cancelable); - $.extend(evt, e); - } - this.element[0].dispatchEvent(evt); - } else if (document.createEventObject) { - evt = document.createEventObject(); - $.extend(evt, e); - evt.button = 1; - this.element[0].fireEvent('on' + type, evt) - } - }, - down: function(x, y) { - this.dispatch("mousedown", x, y); - }, - move: function(x, y) { - this.dispatch("mousemove", x, y); - }, - up: function(x, y) { - this.dispatch("mouseup", x, y); - }, drag: function(dx, dy) { var self = this; @@ -130,35 +96,40 @@ testStart(); this.lastX = null; - + + var before = function() { + self.element.triggerHandler('mouseover'); + self.element.simulate("mousedown", { clientX: self.left, clientY: self.top }); + self.element.simulate("mousemove", { clientX: self.left, clientY: self.top }); + } + + var during = function(xory) { + if (!self.lastX) { + self.lastX = xory; + } else { + var x = self.lastX, y = xory; + self.element.simulate("mousemove", { clientX: x, clientY: y }); + self.lastX = null; + } + } + + var after = function() { + self.element.triggerHandler('mouseout'); + self.element.simulate("mouseup", { clientX: 0, clientY: 0 }); + testStop(); + } + fakemouse - .animate({ left: this.left, top: this.top }, this.options.speed, function() { - self.element.triggerHandler('mouseover'); - self.down(self.left, self.top); - self.move(self.left, self.top); + .animate({ left: this.left, top: this.top }, this.options.speed, before) + .animate({ left: this.left + dx, top: this.top + dy }, { + speed: this.options.speed, + easing: "swing", + step: during }) .animate({ left: this.left + dx, top: this.top + dy }, { - speed: self.options.speed, + speed: this.options.speed, easing: "swing", - step: function (xory) { - if (!self.lastX) { - self.lastX = xory; - } else { - var x = self.lastX, y = xory; - self.move(x, y); - self.lastX = null; - } - }, - complete: function() { - self.element.triggerHandler('mouseout'); - self.up(0, 0); - $(this).animate({ left: realmouse.css("left"), top: realmouse.css("top") }, { - speed: self.options.speed, - complete: function() { - testStop(); - } - }) - } + complete: after }); } |