Skip to content
Permalink
Browse files

Added jquery.simulate.js as possible refactor of (low-level) parts of…

… userAction and testmouse
  • Loading branch information
rdworth committed Jun 4, 2008
1 parent 9047a63 commit 0646f774adee4ec804ac86a0355e3d7a212af097
Showing with 127 additions and 60 deletions.
  1. +2 −1 ui/tests/autodrag.html
  2. +95 −0 ui/tests/jquery.simulate.js
  3. +30 −59 ui/tests/ui.testmouse.js
@@ -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 @@
if ($.isFunction(fn)) fn.apply();
}
var plugin = "draggable";
plugin = "selectable"
//plugin = "selectable"
var testFn = "testMouse";
var testArgs = ["drag", 40, 50];
//1
@@ -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);
@@ -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
});

}

0 comments on commit 0646f77

Please sign in to comment.
You can’t perform that action at this time.