/
autodrag.html
138 lines (110 loc) · 3.7 KB
/
autodrag.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Draggable - Automated Test Page</title>
<script type="text/javascript" src="../../jquery/jquery-1.2.5.js"></script>
<script type="text/javascript" src="../../ui/source/ui.core.js"></script>
<script type="text/javascript" src="../../ui/source/ui.draggable.js"></script>
<style type="text/css">
html, body { height: 100%; }
#realmouse { display: none; position: absolute; z-index: 1000; }
#fakemouse { display: none; position: absolute; z-index: 2000; }
.testing, .testing * { cursor: url(images/blank.cur), crosshair; }
.testing #fakemouse { display: block !important; }
.testing #realmouse { display: block !important; }
#main { height: 100%; }
#drag { width: 200px; height: 200px; background: #eef; }
</style>
<script type="text/javascript">
var mouseX = 0, mouseY = 0;
$(function() {
$(document).mousemove(function(e) {
if (e.originalEvent && e.originalEvent.isTrusted) {
mouseX = e.pageX;
mouseY = e.pageY;
}
});
});
function testMouse(type, el, x, y) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mouse" + type, true, true, window, 0, 0, 0, x, y, false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
function testMouseDown(el, x, y) { testMouse("down", el, x, y); }
function testMouseMove(el, x, y) { testMouse("move", el, x, y); }
function testMouseUp(el, x, y) { testMouse("up", el, x, y); }
function draggableTest(el, dx, dy) {
var realmouse = $("#realmouse");
var findCenter = function(el, offset) {
var o = el.offset();
return {
x: (o.left + (offset || [0, 0])[0] || 0) + el.width() / 2,
y: (o.top + (offset || [0, 0])[1] || 0) + el.height() / 2
};
};
var center = findCenter($(el));
var left = center.x;
var top = center.y;
var fakemouse = $('<img src="images/cursor.png" id="fakemouse>');
var realmouse = $('<img src="images/cursor.png" id="realmouse>');
var mousescreen = $('<div id="mousescreen"/>');
var testStart = function() {
fakemouse.appendTo('body').css({ left: mouseX, top: mouseY, opacity: 1.0 });
realmouse.appendTo('body').css({ left: mouseX, top: mouseY, opacity: 0.2 })
.mousedown(function() { return false; });
mousescreen.appendTo('body').css({ width: '100%', height: '100%', position: 'absolute', top: 0, left: 0, zIndex: 5000 })
.mousemove(function(e) { realmouse.css({ left: e.pageX, top: e.pageY }); return false; })
.mousedown(function() { return false; })
.mouseup(function() { return false; })
$("body").addClass("testing");
}
var testStop = function() {
$("body").removeClass("testing");
mousescreen.remove();
realmouse.remove();
fakemouse.remove();
}
testStart();
var lastX = null;
fakemouse
.animate({ left: left, top: top, opacity: 1.0 }, "fast", function() {
testMouseDown(el, left, top);
})
.animate({ left: left + dx, top: top + dy }, {
speed: "fast",
easing: "swing",
step: function (xory) {
if (!lastX) { lastX = xory; }
else { testMouseMove(el, lastX, xory); lastX = null; }
},
complete: function() {
testMouseUp(el, 0, 0);
$(this).animate({ left: realmouse.css("left"), top: realmouse.css("top"), opacity: 1.0 }, {
speed: "fast",
complete: function() {
testStop();
}
})
}
});
}
</script>
<script type="text/javascript">
$(function() {
$("#drag").draggable();
$('#begin').click(function(e) {
draggableTest($("#drag")[0], 100, 10);
});
});
</script>
</head>
<body>
<div id="main">
<h1>jQuery UI Draggable - Automated Test</h1>
<div style="height: 3em;"><button id="begin">Run Test</button></div>
<div id="drag">
Drag me
</div>
</div>
</body>
</html>