forked from adopted-ember-addons/ember-sortable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
105 lines (87 loc) · 2.27 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
import Ember from 'ember';
const { $ } = Ember;
/**
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 function drag(app, mode, itemSelector, offsetFn, callbacks = {}) {
let start, move, end, which;
const {
andThen,
findWithAssert,
wait
} = app.testHelpers;
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}'`);
}
andThen(() => {
let item = findWithAssert(itemSelector);
let itemOffset = item.offset();
let offset = offsetFn();
let itemElement = item.get(0);
let rect = itemElement.getBoundingClientRect();
let scale = itemElement.clientHeight / (rect.bottom - rect.top);
let targetX = itemOffset.left + offset.dx * scale;
let targetY = itemOffset.top + offset.dy * scale;
triggerEvent(app, item, start, {
pageX: itemOffset.left,
pageY: itemOffset.top,
which
});
if (callbacks.dragstart) {
andThen(callbacks.dragstart);
}
triggerEvent(app, item, move, {
pageX: itemOffset.left,
pageY: itemOffset.top
});
if (callbacks.dragmove) {
andThen(callbacks.dragmove);
}
triggerEvent(app, item, move, {
pageX: targetX,
pageY: targetY
});
triggerEvent(app, item, end, {
pageX: targetX,
pageY: targetY
});
if (callbacks.dragend) {
andThen(callbacks.dragend);
}
});
return wait();
}
function triggerEvent(app, el, type, props) {
return app.testHelpers.andThen(() => {
let event = $.Event(type, props);
$(el).trigger(event);
});
}
export default Ember.Test.registerAsyncHelper('drag', drag);