1 | /*
|
---|
2 | * jquery.simulate - simulate browser mouse and keyboard events
|
---|
3 | *
|
---|
4 | * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
|
---|
5 | * Dual licensed under the MIT (MIT-LICENSE.txt)
|
---|
6 | * and GPL (GPL-LICENSE.txt) licenses.
|
---|
7 | *
|
---|
8 | */
|
---|
9 |
|
---|
10 | ;(function($) {
|
---|
11 |
|
---|
12 | $.fn.extend({
|
---|
13 | simulate: function(type, options) {
|
---|
14 | return this.each(function() {
|
---|
15 | var opt = $.extend({}, $.simulate.defaults, options || {});
|
---|
16 | new $.simulate(this, type, opt);
|
---|
17 | });
|
---|
18 | }
|
---|
19 | });
|
---|
20 |
|
---|
21 | $.simulate = function(el, type, options) {
|
---|
22 | this.target = el;
|
---|
23 | this.options = options;
|
---|
24 |
|
---|
25 | if (/^drag$/.test(type)) {
|
---|
26 | this[type].apply(this, [this.target, options]);
|
---|
27 | } else {
|
---|
28 | this.simulateEvent(el, type, options);
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | $.extend($.simulate.prototype, {
|
---|
33 | simulateEvent: function(el, type, options) {
|
---|
34 | var evt = this.createEvent(type, options);
|
---|
35 | this.dispatchEvent(el, type, evt, options);
|
---|
36 | return evt;
|
---|
37 | },
|
---|
38 | createEvent: function(type, options) {
|
---|
39 | if (/^mouse(over|out|down|up|move)|(dbl)?click$/.test(type)) {
|
---|
40 | return this.mouseEvent(type, options);
|
---|
41 | } else if (/^key(up|down|press)$/.test(type)) {
|
---|
42 | return this.keyboardEvent(type, options);
|
---|
43 | }
|
---|
44 | },
|
---|
45 | mouseEvent: function(type, options) {
|
---|
46 | var evt;
|
---|
47 | var e = $.extend({
|
---|
48 | bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
|
---|
49 | screenX: 0, screenY: 0, clientX: 0, clientY: 0,
|
---|
50 | ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
|
---|
51 | button: 0, relatedTarget: undefined
|
---|
52 | }, options);
|
---|
53 |
|
---|
54 | var relatedTarget = $(e.relatedTarget)[0];
|
---|
55 |
|
---|
56 | if ($.isFunction(document.createEvent)) {
|
---|
57 | evt = document.createEvent("MouseEvents");
|
---|
58 | evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail,
|
---|
59 | e.screenX, e.screenY, e.clientX, e.clientY,
|
---|
60 | e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
|
---|
61 | e.button, e.relatedTarget || document.body.parentNode);
|
---|
62 | } else if (document.createEventObject) {
|
---|
63 | evt = document.createEventObject();
|
---|
64 | $.extend(evt, e);
|
---|
65 | evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button;
|
---|
66 | }
|
---|
67 | return evt;
|
---|
68 | },
|
---|
69 | keyboardEvent: function(type, options) {
|
---|
70 | var evt;
|
---|
71 |
|
---|
72 | var e = $.extend({ bubbles: true, cancelable: true, view: window,
|
---|
73 | ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
|
---|
74 | keyCode: 0, charCode: 0
|
---|
75 | }, options);
|
---|
76 |
|
---|
77 | if ($.isFunction(document.createEvent)) {
|
---|
78 | try {
|
---|
79 | evt = document.createEvent("KeyEvents");
|
---|
80 | evt.initKeyEvent(type, e.bubbles, e.cancelable, e.view,
|
---|
81 | e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
|
---|
82 | e.keyCode, e.charCode);
|
---|
83 | } catch(err) {
|
---|
84 | evt = document.createEvent("Events");
|
---|
85 | evt.initEvent(type, e.bubbles, e.cancelable);
|
---|
86 | $.extend(evt, { view: e.view,
|
---|
87 | ctrlKey: e.ctrlKey, altKey: e.altKey, shiftKey: e.shiftKey, metaKey: e.metaKey,
|
---|
88 | keyCode: e.keyCode, charCode: e.charCode
|
---|
89 | });
|
---|
90 | }
|
---|
91 | } else if (document.createEventObject) {
|
---|
92 | evt = document.createEventObject();
|
---|
93 | $.extend(evt, e);
|
---|
94 | }
|
---|
95 | if ($.browser.msie || $.browser.opera) {
|
---|
96 | evt.keyCode = (e.charCode > 0) ? e.charCode : e.keyCode;
|
---|
97 | evt.charCode = undefined;
|
---|
98 | }
|
---|
99 | return evt;
|
---|
100 | },
|
---|
101 |
|
---|
102 | dispatchEvent: function(el, type, evt) {
|
---|
103 | if (el.dispatchEvent) {
|
---|
104 | el.dispatchEvent(evt);
|
---|
105 | } else if (el.fireEvent) {
|
---|
106 | el.fireEvent('on' + type, evt);
|
---|
107 | }
|
---|
108 | return evt;
|
---|
109 | },
|
---|
110 |
|
---|
111 | drag: function(el) {
|
---|
112 | var self = this, center = this.findCenter(this.target),
|
---|
113 | options = this.options, x = Math.floor(center.x), y = Math.floor(center.y),
|
---|
114 | dx = options.dx || 0, dy = options.dy || 0, target = this.target;
|
---|
115 | var coord = { clientX: x, clientY: y };
|
---|
116 | this.simulateEvent(target, "mousedown", coord);
|
---|
117 | coord = { clientX: x + 1, clientY: y + 1 };
|
---|
118 | this.simulateEvent(document, "mousemove", coord);
|
---|
119 | coord = { clientX: x + dx, clientY: y + dy };
|
---|
120 | this.simulateEvent(document, "mousemove", coord);
|
---|
121 | this.simulateEvent(document, "mousemove", coord);
|
---|
122 | this.simulateEvent(target, "mouseup", coord);
|
---|
123 | },
|
---|
124 | findCenter: function(el) {
|
---|
125 | var el = $(this.target), o = el.offset();
|
---|
126 | return {
|
---|
127 | x: o.left + el.outerWidth() / 2,
|
---|
128 | y: o.top + el.outerHeight() / 2
|
---|
129 | };
|
---|
130 | }
|
---|
131 | });
|
---|
132 |
|
---|
133 | $.extend($.simulate, {
|
---|
134 | defaults: {
|
---|
135 | speed: 'sync'
|
---|
136 | },
|
---|
137 | VK_TAB: 9,
|
---|
138 | VK_ENTER: 13,
|
---|
139 | VK_ESC: 27,
|
---|
140 | VK_PGUP: 33,
|
---|
141 | VK_PGDN: 34,
|
---|
142 | VK_END: 35,
|
---|
143 | VK_HOME: 36,
|
---|
144 | VK_LEFT: 37,
|
---|
145 | VK_UP: 38,
|
---|
146 | VK_RIGHT: 39,
|
---|
147 | VK_DOWN: 40
|
---|
148 | });
|
---|
149 |
|
---|
150 | })(jQuery);
|
---|