forked from adopted-ember-addons/ember-sortable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorder.js
41 lines (35 loc) · 1.15 KB
/
reorder.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
import { find, findAll } from '@ember/test-helpers';
import { drag } from './drag';
import { getOffset } from '../utils/offset';
const OVERSHOOT = 2;
/**
Reorders elements to the specified state.
Examples
reorder(
'mouse',
'.some-list li',
'[data-id="66278893"]',
'[data-id="66278894"]',
'[data-id="66278892"]'
);
@method reorder
@param {'mouse'|'touch'} [mode]
event mode
@param {String} [itemSelector]
selector for all items
@param {...String} [resultSelectors]
selectors for the resultant order
@return {Promise}
*/
export async function reorder(mode, itemSelector, ...resultSelectors) {
for (let targetIndex = 0; targetIndex < resultSelectors.length; targetIndex++) {
const items = findAll(itemSelector);
const sourceElement = find(resultSelectors[targetIndex]);
const targetElement = items[targetIndex];
const dx = getOffset(targetElement).left - OVERSHOOT - getOffset(sourceElement).left;
const dy = getOffset(targetElement).top - OVERSHOOT - getOffset(sourceElement).top;
await drag(mode, sourceElement, () => {
return { dx: dx, dy: dy };
});
}
}