| 1 |
/* |
| 2 |
* jquery.simulate - simulate browser mouse and keyboard events |
| 3 |
* |
| 4 |
* Copyright (c) 2009 Eduardo Lundgren ([email protected]) |
| 5 |
* and Richard D. Worth ([email protected]) |
| 6 |
* |
| 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. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
;(function($) { |
| 13 |
|
| 14 |
$.fn.extend({ |
| 15 |
simulate: function(type, options) { |
| 16 |
return this.each(function() { |
| 17 |
var opt = $.extend({}, $.simulate.defaults, options || {}); |
| 18 |
new $.simulate(this, type, opt); |
| 19 |
}); |
| 20 |
} |
| 21 |
}); |
| 22 |
|
| 23 |
$.simulate = function(el, type, options) { |
| 24 |
this.target = el; |
| 25 |
this.options = options; |
| 26 |
|
| 27 |
if (/^drag$/.test(type)) { |
| 28 |
this[type].apply(this, [this.target, options]); |
| 29 |
} else { |
| 30 |
this.simulateEvent(el, type, options); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$.extend($.simulate.prototype, { |
| 35 |
simulateEvent: function(el, type, options) { |
| 36 |
var evt = this.createEvent(type, options); |
| 37 |
this.dispatchEvent(el, type, evt, options); |
| 38 |
return evt; |
| 39 |
}, |
| 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); |
| 45 |
} |
| 46 |
}, |
| 47 |
mouseEvent: function(type, options) { |
| 48 |
var evt; |
| 49 |
var e = $.extend({ |
| 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 |
| 54 |
}, options); |
| 55 |
|
| 56 |
var relatedTarget = $(e.relatedTarget)[0]; |
| 57 |
|
| 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(); |
| 66 |
$.extend(evt, e); |
| 67 |
evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button; |
| 68 |
} |
| 69 |
return evt; |
| 70 |
}, |
| 71 |
keyboardEvent: function(type, options) { |
| 72 |
var evt; |
| 73 |
|
| 74 |
var e = $.extend({ bubbles: true, cancelable: true, view: window, |
| 75 |
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, |
| 76 |
keyCode: 0, charCode: 0 |
| 77 |
}, options); |
| 78 |
|
| 79 |
if ($.isFunction(document.createEvent)) { |
| 80 |
try { |
| 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); |
| 85 |
} catch(err) { |
| 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 |
| 91 |
}); |
| 92 |
} |
| 93 |
} else if (document.createEventObject) { |
| 94 |
evt = document.createEventObject(); |
| 95 |
$.extend(evt, e); |
| 96 |
} |
| 97 |
if ($.browser.msie || $.browser.opera) { |
| 98 |
evt.keyCode = (e.charCode > 0) ? e.charCode : e.keyCode; |
| 99 |
evt.charCode = undefined; |
| 100 |
} |
| 101 |
return evt; |
| 102 |
}, |
| 103 |
|
| 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); |
| 109 |
} |
| 110 |
return evt; |
| 111 |
}, |
| 112 |
|
| 113 |
drag: function(el) { |
| 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); |
| 125 |
}, |
| 126 |
findCenter: function(el) { |
| 127 |
var el = $(this.target), o = el.offset(); |
| 128 |
return { |
| 129 |
x: o.left + el.outerWidth() / 2, |
| 130 |
y: o.top + el.outerHeight() / 2 |
| 131 |
}; |
| 132 |
} |
| 133 |
}); |
| 134 |
|
| 135 |
$.extend($.simulate, { |
| 136 |
defaults: { |
| 137 |
speed: 'sync' |
| 138 |
}, |
| 139 |
VK_TAB: 9, |
| 140 |
VK_ENTER: 13, |
| 141 |
VK_ESC: 27, |
| 142 |
VK_PGUP: 33, |
| 143 |
VK_PGDN: 34, |
| 144 |
VK_END: 35, |
| 145 |
VK_HOME: 36, |
| 146 |
VK_LEFT: 37, |
| 147 |
VK_UP: 38, |
| 148 |
VK_RIGHT: 39, |
| 149 |
VK_DOWN: 40 |
| 150 |
}); |
| 151 |
|
| 152 |
})(jQuery); |
| 153 |
|