forked from adopted-ember-addons/ember-sortable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
118 lines (101 loc) · 2.88 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
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
import { triggerEvent, find, settled, waitUntil } from '@ember/test-helpers';
import { getOffset } from '../utils/offset';
/**
Drags elements by an offset specified in pixels.
Examples
drag(
'mouse',
'.some-list li[data-item=uno]',
function() {
return { dy: 50, dx: 20 };
}
);
@method drag
@param {'mouse'|'touch'} [mode]
event mode
@param {String} [itemSelector]
selector for the element to drag
@param {Function} [offsetFn]
function returning the offset by which to drag
@param {Object} [callbacks]
callbacks that are fired at the different stages of the interaction
@return {Promise}
*/
export async function drag(mode, itemSelector, offsetFn, callbacks = {}) {
let start;
let move;
let end;
let which;
if (mode === 'mouse') {
start = 'mousedown';
move = 'mousemove';
end = 'mouseup';
which = 1;
} else if (mode === 'touch') {
start = 'touchstart';
move = 'touchmove';
end = 'touchend';
} else {
throw new Error(`Unsupported mode: '${mode}'`);
}
const itemElement = find(itemSelector);
const itemOffset = getOffset(itemElement);
const offset = offsetFn();
const rect = itemElement.getBoundingClientRect();
// firefox gives some elements, like <svg>, a clientHeight of 0.
// we can try to grab it off the parent instead to have a better
// guess at what the scale is.
// https://bugzilla.mozilla.org/show_bug.cgi?id=874811#c14
// https://stackoverflow.com/a/13647345
// https://stackoverflow.com/a/5042051
const dx = offset.dx || 0;
const dy = offset.dy || 0;
const clientHeight = itemElement.clientHeight || itemElement.offsetHeight || itemElement.parentNode.offsetHeight;
const scale = clientHeight / (rect.bottom - rect.top);
const halfwayX = itemOffset.left + (dx * scale) / 2;
const halfwayY = itemOffset.top + (dy * scale) / 2;
const targetX = itemOffset.left + dx * scale;
const targetY = itemOffset.top + dy * scale;
await triggerEvent(itemElement, start, {
clientX: itemOffset.left,
clientY: itemOffset.top,
which,
});
if (callbacks.dragstart) {
await callbacks.dragstart();
await settled();
}
await triggerEvent(itemElement, move, {
clientX: itemOffset.left,
clientY: itemOffset.top,
});
if (callbacks.dragmove) {
await callbacks.dragmove();
await settled();
}
await triggerEvent(itemElement, move, {
clientX: halfwayX,
clientY: halfwayY,
});
await triggerEvent(itemElement, move, {
clientX: targetX,
clientY: targetY,
});
if (callbacks.beforedragend) {
await callbacks.beforedragend();
}
await triggerEvent(itemElement, end, {
clientX: targetX,
clientY: targetY,
});
if (callbacks.dragend) {
await callbacks.dragend();
await settled();
}
await waitUntil(
() => {
return !find('.is-dropping');
},
{ timeout: 2000 }
);
}