2008-06-04 03:41:17 +00:00
|
|
|
/*
|
|
|
|
* 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({
|
2008-06-05 00:03:24 +00:00
|
|
|
simulate: function(type, options) {
|
2008-06-04 03:41:17 +00:00
|
|
|
return this.each(function() {
|
2008-06-05 00:03:24 +00:00
|
|
|
var opt = $.extend({}, $.simulate.defaults, options || {});
|
2008-06-04 08:47:26 +00:00
|
|
|
new $.simulate(this, type, opt);
|
2008-06-04 03:41:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.simulate = function(el, type, options) {
|
2008-06-04 08:47:26 +00:00
|
|
|
this.target = el;
|
2008-06-05 00:03:24 +00:00
|
|
|
this.options = options;
|
|
|
|
|
2008-06-05 11:56:20 +00:00
|
|
|
if (/^drag$/.test(type)) {
|
2008-06-04 08:47:26 +00:00
|
|
|
this[type].apply(this, [this.target, options]);
|
|
|
|
} else {
|
2008-06-05 00:03:24 +00:00
|
|
|
this.simulateEvent(el, type, options);
|
2008-06-04 08:47:26 +00:00
|
|
|
}
|
2008-06-04 03:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$.extend($.simulate.prototype, {
|
2008-06-04 08:47:26 +00:00
|
|
|
simulateEvent: function(el, type, options) {
|
|
|
|
var evt = this.createEvent(type, options);
|
2008-06-05 13:05:28 +00:00
|
|
|
this.dispatchEvent(el, type, evt, options);
|
|
|
|
return evt;
|
2008-06-04 08:47:26 +00:00
|
|
|
},
|
2008-06-04 03:41:17 +00:00
|
|
|
createEvent: function(type, options) {
|
2008-06-05 11:56:20 +00:00
|
|
|
if (/^mouse(over|out|down|up|move)|(dbl)?click$/.test(type)) {
|
2008-06-04 03:41:17 +00:00
|
|
|
return this.mouseEvent(type, options);
|
2008-06-05 11:56:20 +00:00
|
|
|
} else if (/^key(up|down|press)$/.test(type)) {
|
2008-06-04 03:41:17 +00:00
|
|
|
return this.keyboardEvent(type, options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mouseEvent: function(type, options) {
|
|
|
|
var evt;
|
|
|
|
var e = $.extend({
|
|
|
|
bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
|
2008-06-05 12:16:42 +00:00
|
|
|
screenX: 0, screenY: 0, clientX: 0, clientY: 0,
|
2008-06-04 03:41:17 +00:00
|
|
|
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
|
2008-06-05 12:11:30 +00:00
|
|
|
button: 0, relatedTarget: undefined
|
2008-06-04 03:41:17 +00:00
|
|
|
}, options);
|
2008-06-05 00:03:24 +00:00
|
|
|
|
|
|
|
var relatedTarget = $(e.relatedTarget)[0];
|
|
|
|
|
2008-06-04 03:41:17 +00:00
|
|
|
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,
|
2008-06-05 12:11:30 +00:00
|
|
|
e.button, e.relatedTarget || document.body.parentNode);
|
2008-06-04 03:41:17 +00:00
|
|
|
} 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;
|
2008-06-05 00:03:24 +00:00
|
|
|
|
2008-06-04 03:41:17 +00:00
|
|
|
var e = $.extend({ bubbles: true, cancelable: true, view: window,
|
|
|
|
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
|
|
|
|
keyCode: 0, charCode: 0
|
|
|
|
}, options);
|
2008-06-05 00:03:24 +00:00
|
|
|
|
2008-06-04 03:41:17 +00:00
|
|
|
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;
|
|
|
|
},
|
2008-06-05 00:03:24 +00:00
|
|
|
|
2008-06-05 13:05:28 +00:00
|
|
|
dispatchEvent: function(el, type, evt) {
|
2008-06-04 03:41:17 +00:00
|
|
|
if (el.dispatchEvent) {
|
|
|
|
el.dispatchEvent(evt);
|
|
|
|
} else if (el.fireEvent) {
|
|
|
|
el.fireEvent('on' + type, evt);
|
|
|
|
}
|
2008-06-05 00:03:24 +00:00
|
|
|
return evt;
|
|
|
|
},
|
|
|
|
|
|
|
|
drag: function(el) {
|
|
|
|
var self = this, center = this.findCenter(this.target),
|
2008-06-06 03:59:57 +00:00
|
|
|
options = this.options, x = Math.floor(center.x), y = Math.floor(center.y),
|
2008-06-05 00:03:24 +00:00
|
|
|
dx = options.dx || 0, dy = options.dy || 0, target = this.target;
|
2008-06-05 13:05:28 +00:00
|
|
|
var coord = { clientX: x, clientY: y };
|
|
|
|
this.simulateEvent(target, "mouseover");
|
2008-06-05 00:03:24 +00:00
|
|
|
this.simulateEvent(target, "mousedown", coord);
|
2008-06-05 13:05:28 +00:00
|
|
|
coord = { clientX: x + dx, clientY: y + dy };
|
|
|
|
this.simulateEvent(document, "mousemove", coord);
|
|
|
|
this.simulateEvent(document, "mousemove", coord);
|
2008-06-05 00:03:24 +00:00
|
|
|
this.simulateEvent(target, "mouseup", coord);
|
2008-06-05 13:05:28 +00:00
|
|
|
this.simulateEvent(target, "mouseout");
|
2008-06-05 00:03:24 +00:00
|
|
|
},
|
2008-06-04 08:47:26 +00:00
|
|
|
findCenter: function(el) {
|
|
|
|
var el = $(this.target), o = el.offset();
|
|
|
|
return {
|
|
|
|
x: o.left + el.outerWidth() / 2,
|
|
|
|
y: o.top + el.outerHeight() / 2
|
|
|
|
};
|
2008-06-04 03:41:17 +00:00
|
|
|
}
|
2008-06-05 00:03:24 +00:00
|
|
|
});
|
2008-06-04 08:47:26 +00:00
|
|
|
|
2008-06-05 00:03:24 +00:00
|
|
|
$.extend($.simulate, {
|
|
|
|
defaults: {
|
|
|
|
speed: 'sync'
|
|
|
|
}
|
2008-06-04 03:41:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|