2
* jquery.simulate - simulate browser mouse and keyboard events
4
* Copyright (c) 2007 Eduardo Lundgren (eduardolundgren@gmail.com)
5
* and Richard D. Worth (rdworth@gmail.com)
7
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
8
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
15
simulate: function(type, options) {
16
return this.each(function() {
17
var opt = $.extend({}, $.simulate.defaults, options || {});
18
new $.simulate(this, type, opt);
23
$.simulate = function(el, type, options) {
25
this.options = options;
27
if (/^drag$/.test(type)) {
28
this[type].apply(this, [this.target, options]);
30
this.simulateEvent(el, type, options);
34
$.extend($.simulate.prototype, {
35
simulateEvent: function(el, type, options) {
36
var evt = this.createEvent(type, options);
37
this.dispatchEvent(el, type, evt, options);
40
createEvent: function(type, options) {
41
if (/^mouse(over|out|down|up|move)|(dbl)?click$/.test(type)) {
42
return this.mouseEvent(type, options);
43
} else if (/^key(up|down|press)$/.test(type)) {
44
return this.keyboardEvent(type, options);
47
mouseEvent: function(type, options) {
50
bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
51
screenX: 0, screenY: 0, clientX: 0, clientY: 0,
52
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
53
button: 0, relatedTarget: undefined
56
var relatedTarget = $(e.relatedTarget)[0];
58
if ($.isFunction(document.createEvent)) {
59
evt = document.createEvent("MouseEvents");
60
evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail,
61
e.screenX, e.screenY, e.clientX, e.clientY,
62
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
63
e.button, e.relatedTarget || document.body.parentNode);
64
} else if (document.createEventObject) {
65
evt = document.createEventObject();
67
evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button;
71
keyboardEvent: function(type, options) {
74
var e = $.extend({ bubbles: true, cancelable: true, view: window,
75
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
76
keyCode: 0, charCode: 0
79
if ($.isFunction(document.createEvent)) {
81
evt = document.createEvent("KeyEvents");
82
evt.initKeyEvent(type, e.bubbles, e.cancelable, e.view,
83
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
84
e.keyCode, e.charCode);
86
evt = document.createEvent("Events");
87
evt.initEvent(type, e.bubbles, e.cancelable);
88
$.extend(evt, { view: e.view,
89
ctrlKey: e.ctrlKey, altKey: e.altKey, shiftKey: e.shiftKey, metaKey: e.metaKey,
90
keyCode: e.keyCode, charCode: e.charCode
93
} else if (document.createEventObject) {
94
evt = document.createEventObject();
97
if ($.browser.msie || $.browser.opera) {
98
evt.keyCode = (e.charCode > 0) ? e.charCode : e.keyCode;
99
evt.charCode = undefined;
104
dispatchEvent: function(el, type, evt) {
105
if (el.dispatchEvent) {
106
el.dispatchEvent(evt);
107
} else if (el.fireEvent) {
108
el.fireEvent('on' + type, evt);
114
var self = this, center = this.findCenter(this.target),
115
options = this.options, x = Math.floor(center.x), y = Math.floor(center.y),
116
dx = options.dx || 0, dy = options.dy || 0, target = this.target;
117
var coord = { clientX: x, clientY: y };
118
this.simulateEvent(target, "mousedown", coord);
119
coord = { clientX: x + 1, clientY: y + 1 };
120
this.simulateEvent(document, "mousemove", coord);
121
coord = { clientX: x + dx, clientY: y + dy };
122
this.simulateEvent(document, "mousemove", coord);
123
this.simulateEvent(document, "mousemove", coord);
124
this.simulateEvent(target, "mouseup", coord);
126
findCenter: function(el) {
127
var el = $(this.target), o = el.offset();
129
x: o.left + el.outerWidth() / 2,
130
y: o.top + el.outerHeight() / 2
135
$.extend($.simulate, {