1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/* Copyright (c) 2007 Eduardo Lundgren (eduardolundgren@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.
*
* Version: 0.1a
* Date: May, 2008
* Requires jQuery 1.2.x+
* Docs: http://docs.jquery.com/Plugins/userAction
* Greetings: Richard Worth
*/
;(function($) {
$.fn.extend({
userAction: function(type) {
var args = arguments, opts = {}, a1 = args[1], a2 = args[2];
// transfer center offset
if (a1 && a1.length) {
opts.center = [a1[0], a1[1]];
}
// set x and y
else if (typeof a1 == StringPool.NUMBER) {
opts.x = a1; opts.y = a2;
}
// extend options
else {
$.extend(opts, a1);
}
return this.each(function() {
new $.userAction(this, type, opts);
});
}
});
$.userAction = function(el, type, options) {
this.type = type;
this.options = $.extend({}, $.userAction.defaults, options || {});
this.target = $(this.options.target || el)[0];
var self = this, o = this.options, c = o.center, center = { x: 0, y: 0 };
if (!o.x && !o.y) {
center = this.findCenter(
c && c.length ? c : [0, 0]
)
}
// if x and y not set, get the center of the element
o.x = o.x || center.x; o.y = o.y || center.y;
var EVENT_DEFAULT = {
target: this.target,
view: window,
bubbles: o.bubbles || true,
cancelable: o.cancelable || false,
ctrlKey: o.ctrlKey || false,
altKey: o.altKey || false,
shiftKey: o.shiftKey || false,
metaKey: o.metaKey || false
};
// Simulating drag and drop event
if (/^drag$/i.test(type)) {
var self = this, t = this.target, queue = $.data(t, StringPool.DATA_QUEUE),
data = [options.dx || options.x, options.dy || options.y, this];
var fire = function() {
self.drag(options.dx || options.x, options.dy || options.y);
};
if (/^sync$/i.test(o.speed)) {
fire(); return;
}
if (!queue) {
$.data(t, StringPool.DATA_QUEUE, [data]); fire(); return;
}
// queuing drags...
if (queue && queue.length) {
queue.push(data);
}
// if drag, stop here.
return;
}
var isMouse = /^mouse(over|out|down|up|move)|(dbl)?click$/i.test(type),
isKeyboard = /^textevent|key(up|down|press)$/i.test(type),
EVT = isMouse ?
$.extend({}, EVENT_DEFAULT, {
clientX: o.x, clientY: o.y,
screenX: o.screenX || 0, screenY: o.screenY || 0,
relatedTarget: $(o.relatedTarget)[0] || null, detail: 0,
button: o.button || ($.browser.msie ? 1 : 0), isTrusted: false
}) :
$.extend({}, EVENT_DEFAULT, {
keyCode: o.keyCode || 0, charCode: o.charCode || 0
});
// avoid e.type == undefined before dispatchment
EVT.type = type;
if (o.before) o.before.apply(this.target, [$.event.fix(EVT), o.x, o.y, this]);
// check event type for mouse events
if (isMouse) {
// simulating mouse event
EVT = this.mouseEvent(EVT)
}
// check event type for key events
if (isKeyboard) {
// simulating keuboard event
EVT = this.keyboardEvent(EVT);
}
if (o.after) o.after.apply(this.target, [$.event.fix(EVT), o.x, o.y, this]);
};
$.extend($.userAction.prototype, {
down: function(target) {
$(target).userAction(StringPool.MOUSEOVER).userAction(StringPool.MOUSEDOWN)
.userAction(StringPool.MOUSEMOVE);
},
up: function(target) {
$(target).userAction(StringPool.MOUSEUP).userAction(StringPool.MOUSEOUT);
},
move: function(target, x, y, after) {
$(target).userAction(StringPool.MOUSEMOVE, { x: x, y: y, after: after });
},
drag: function(dx, dy) {
// drag helper function, thanks Richard Worth's testmouse api.
var self = this, o = this.options, center = this.findCenter(),
target = $(this.target), lastx = center.x, lasty = center.y,
fake = $(StringPool.FAKE_CURSOR_EXP),
speed = o.speed || StringPool.SLOW,
easing = o.easing || StringPool.SWING;
var complete = function() {
// fire complete or after cb
if (o.after||o.complete) (o.after||o.complete).apply(self.target, [o, self]);
};
// drag synchronously
if (/^sync$/i.test(o.speed)) {
self.down(target);
var mdx = Math.abs(dx)||0, mdy = Math.abs(dy)||0, range = Math.max(mdx, mdy),
sx = dx/mdx||1, sy = dy/mdy||1;
for (var dt = 1; dt <= range; dt++) {
var x = center.x + sx*(dt <= mdx ? dt : 0), y = center.y + sy*(dt <= mdy ? dt : 0);
this.move(target, x, y, o.drag);
}
self.up(target);
complete();
return;
}
// drag asynchronously - animated
fake = fake.size() ? fake :
$(StringPool.FAKE_CURSOR_DIV)
.css({ position: StringPool.ABSOLUTE }).appendTo(document.body);
fake
.animate({ left: center.x, top: center.y }, speed, easing, function(){
self.down(target);
})
.animate({ left: center.x + (dx||0), top: center.y + (dy||0) }, {
speed: speed,
easing: easing,
step: function(i, anim) {
lastx = anim.prop == StringPool.LEFT ? i : lastx;
lasty = anim.prop == StringPool.TOP ? i : lasty;
self.move(target, lastx, lasty, o.drag);
},
complete: function() {
self.up(target);
// remove fake cursor
$(this).remove();
complete();
// trigger drag queue
var queue = $.data(self.target, StringPool.DATA_QUEUE);
if (queue) queue.shift();
if (queue && queue[0]) {
// trigger drag on correct instance
queue[0][2].drag(queue[0][0], queue[0][1]);
}
else
$.removeData(self.target, StringPool.DATA_QUEUE);
}
});
},
mouseEvent: function(EVT) {
var evt, type = this.type, o = this.options;
//check for DOM-compliant browsers
if ($.isFunction(document.createEvent)) {
evt = document.createEvent(StringPool.MOUSE_EVENTS);
//Safari 2.x doesn't implement initMouseEvent()
if ($.isFunction(evt.initMouseEvent)) {
evt.initMouseEvent(type,
EVT.bubbles, EVT.cancelable, EVT.view, EVT.detail,
EVT.screenX, EVT.screenY, EVT.clientX, EVT.clientY,
EVT.ctrlKey, EVT.altKey, EVT.shiftKey, EVT.metaKey,
EVT.button, EVT.relatedTarget);
} else {
// Safari
evt = document.createEvent(StringPool.UI_EVENTS);
customEvent.initEvent(type, EVT.bubbles, EVT.cancelable);
$.extend(evt, EVT);
}
// check to see if relatedTarget has been assigned
if (EVT.relatedTarget && !evt.relatedTarget){
if (type == StringPool.MOUSEOUT) {
evt.toElement = EVT.relatedTarget;
} else if (type == StringPool.MOUSEOVER) {
evt.fromElement = EVT.relatedTarget;
}
}
// fire the event
this.target.dispatchEvent(evt);
} else if (document.createEventObject) {
evt = document.createEventObject();
// assign available properties
$.extend(evt, EVT)
// IE won't allow assignment to toElement or fromElement
evt.relatedTarget = EVT.relatedTarget;
// fix for 2 pixels bug from mousecords
evt.pageX = o.x; evt.pageY = o.y;
// fire the event
this.target.fireEvent(StringPool.ON + type, evt);
}
return evt;
},
keyboardEvent: function(EVT) {
var evt, type = this.type, o = this.options;
// check for DOM-compliant browsers first
if ($.isFunction(document.createEvent)) {
try {
// try to create key event
evt = document.createEvent(StringPool.KEY_EVENTS);
evt.initKeyEvent(type,
EVT.bubbles, EVT.cancelable, EVT.view, EVT.ctrlKey,
EVT.altKey, EVT.shiftKey, EVT.metaKey, EVT.keyCode, EVT.charCode);
} catch (err) {
// we need another try-catch for Safari 2.x
try {
// generic event for opera and webkit nightlies, will fail in Safari 2.x
evt = document.createEvent(StringPool.EVENTS);
} catch (ierr){
// Safari 2.x - create a UIEvent
evt = document.createEvent(StringPool.UI_EVENTS);
} finally {
evt.initEvent(type, EVT.bubbles, EVT.cancelable);
// initializing
$.each(EVT, function(k, v) {
// using try-catch for avoiding Opera NO_MODIFICATION_ALLOWED_ERR
try { evt[k] = v; } catch(e) { }
});
}
}
// fire the event
this.target.dispatchEvent(evt);
} else if (document.createEventObject) {
// create an IE event object
evt = document.createEventObject();
// assign available properties
$.extend(evt, EVT);
// IE doesn't support charCode explicitly
evt.keyCode = (EVT.charCode > 0) ? EVT.charCode : EVT.keyCode;
// fire the event
this.target.fireEvent(StringPool.ON + type, evt);
}
return evt;
},
findCenter: function(offset) {
var el = $(this.target), o = el.offset();
return {
x: o.left + (((offset||[0, 0])[0]) || 0) + el.outerWidth() / 2,
y: o.top + (((offset||[0, 0])[1]) || 0) + el.outerHeight() / 2
};
}
});
$.extend($.userAction, {
defaults: {
center: true
}
});
var StringPool = {
ON: 'on',
NUMBER: 'number',
MOUSEOVER: 'mouseover',
MOUSEOUT: 'mouseout',
MOUSEDOWN: 'mousedown',
MOUSEUP: 'mouseup',
MOUSEMOVE: 'mousemove',
MOUSE_EVENTS: 'MouseEvents',
UI_EVENTS: 'UIEvents',
KEY_EVENTS: 'KeyEvents',
EVENTS: 'Events',
FAKE_CURSOR_EXP: 'div.ui-fake-cursor',
FAKE_CURSOR_DIV: '<div class="ui-fake-cursor"/>',
ABSOLUTE: 'absolute',
DATA_QUEUE: 'ua-drag-queue',
TOP: 'top',
LEFT: 'left',
SLOW: 'slow',
SWING: 'swing'
};
})(jQuery);
|