-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdrag.js
78 lines (63 loc) · 2.23 KB
/
drag.js
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
var mouseEvent = require('../assets/mouse_event');
/*
* drag: grab a node and drag it (dx, dy) pixels
* optionally specify an edge ('n', 'se', 'w' etc)
* to grab it by an edge or corner (otherwise the middle is used)
*/
module.exports = function(node, dx, dy, edge) {
edge = edge || '';
var bbox = node.getBoundingClientRect(),
fromX, fromY;
if(edge.indexOf('n') !== -1) fromY = bbox.top;
else if(edge.indexOf('s') !== -1) fromY = bbox.bottom;
else fromY = (bbox.bottom + bbox.top) / 2;
if(edge.indexOf('w') !== -1) fromX = bbox.left;
else if(edge.indexOf('e') !== -1) fromX = bbox.right;
else fromX = (bbox.left + bbox.right) / 2;
var toX = fromX + dx,
toY = fromY + dy;
mouseEvent('mousemove', fromX, fromY, {element: node});
mouseEvent('mousedown', fromX, fromY, {element: node});
var promise = waitForDragCover().then(function(dragCoverNode) {
mouseEvent('mousemove', toX, toY, {element: dragCoverNode});
mouseEvent('mouseup', toX, toY, {element: dragCoverNode});
return waitForDragCoverRemoval();
});
return promise;
};
function waitForDragCover() {
return new Promise(function(resolve) {
var interval = 5,
timeout = 5000;
var id = setInterval(function() {
var dragCoverNode = document.querySelector('.dragcover');
if(dragCoverNode) {
clearInterval(id);
resolve(dragCoverNode);
}
timeout -= interval;
if(timeout < 0) {
clearInterval(id);
throw new Error('waitForDragCover: timeout');
}
}, interval);
});
}
function waitForDragCoverRemoval() {
return new Promise(function(resolve) {
var interval = 5,
timeout = 5000;
var id = setInterval(function() {
var dragCoverNode = document.querySelector('.dragcover');
if(!dragCoverNode) {
clearInterval(id);
resolve(dragCoverNode);
}
timeout -= interval;
if(timeout < 0) {
clearInterval(id);
throw new Error('waitForDragCoverRemoval: timeout');
}
}, interval);
});
}