jquery-simulate-hacks.js
1.46 KB
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
(function($) {
/*
Addresses a shortcoming with jquery-simulate[-ext] where a click event doesn't
provide pageX/pageY coordinates. Should be the centered coordinates of the element.
This file needs to be loaded after jquery-simulate and jquery-simulate-ext.
*/
var originalMouseEvent = $.simulate.prototype.mouseEvent;
$.simulate.prototype.mouseEvent = function(type, options) {
if (type === 'click' && options.pageX === undefined && options.pageY === undefined) {
$.extend(options, findCenterPageXY(this.target));
}
// after this, jquery-simulate-ext will calculate screenX/screenY
// and jquery-simulate will calculate clientX/clientY...
return originalMouseEvent.apply(this, [ type, options ]);
};
function findCenterPageXY( elem ) {
var offset,
$elem = $(elem),
offset = $elem.offset();
return {
pageX: Math.round(offset.left + $elem.outerWidth() / 2),
pageY: Math.round(offset.top + $elem.outerHeight() / 2)
};
}
/*
Give jquery-simulate-ext drag-n-drop a default interpolation.
*/
var originalSimulate = $.fn.simulate;
$.fn.simulate = function(eventName, options) {
if (eventName == 'drag' || eventName == 'drop' || eventName == 'drag-n-drop') {
options = options || {};
if (options.interpolation === undefined) {
options.interpolation = {
stepCount: 10,
duration: 100
};
}
return originalSimulate.call(this, eventName, options);
}
return originalSimulate.apply(this, arguments);
};
})(jQuery);