forked from bevacqua/dragula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragula.js
443 lines (389 loc) · 11.4 KB
/
dragula.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
'use strict';
var emitter = require('contra.emitter');
var crossvent = require('crossvent');
function dragula (initialContainers, options) {
var body = document.body;
var documentElement = document.documentElement;
var _mirror; // mirror image
var _source; // source container
var _item; // item being dragged
var _offsetX; // reference x
var _offsetY; // reference y
var _initialSibling; // reference sibling when grabbed
var _currentSibling; // reference sibling now
var _copy; // item used for copying
var _containers = []; // containers managed by the drake
var o = options || {};
if (o.moves === void 0) { o.moves = always; }
if (o.accepts === void 0) { o.accepts = always; }
if (o.copy === void 0) { o.copy = false; }
if (o.revertOnSpill === void 0) { o.revertOnSpill = false; }
if (o.removeOnSpill === void 0) { o.removeOnSpill = false; }
if (o.direction === void 0) { o.direction = 'vertical'; }
var api = emitter({
addContainer: manipulateContainers('add'),
removeContainer: manipulateContainers('remove'),
start: start,
end: end,
cancel: cancel,
remove: remove,
destroy: destroy,
dragging: false
});
events();
api.addContainer(initialContainers);
return api;
function manipulateContainers (op) {
return function addOrRemove (all) {
var changes = Array.isArray(all) ? all : [all];
changes.forEach(track);
if (op === 'add') {
_containers = _containers.concat(changes);
} else {
_containers = _containers.filter(removals);
}
function track (container) {
touchy(container, op, 'mousedown', grab);
}
function removals (container) {
return changes.indexOf(container) === -1;
}
};
}
function events (remove) {
var op = remove ? 'remove' : 'add';
touchy(documentElement, op, 'mouseup', release);
}
function destroy () {
events(true);
api.removeContainer(_containers);
release({});
}
function grab (e) {
var item = e.target;
if ((e.which !== 0 && e.which !== 1) || e.metaKey || e.ctrlKey) {
return; // we only care about honest-to-god left clicks and touch events
}
if (start(item) !== true) {
return;
}
var offset = getOffset(_item);
_offsetX = getCoord('pageX', e) - offset.left;
_offsetY = getCoord('pageY', e) - offset.top;
renderMirrorImage();
drag(e);
e.preventDefault();
}
function start (item) {
var handle = item;
if (api.dragging && _mirror) {
return;
}
if (_containers.indexOf(item) !== -1) {
return; // don't drag container itself
}
while (_containers.indexOf(item.parentElement) === -1) {
if (invalidTarget(item)) {
return;
}
item = item.parentElement; // drag target should be a top element
}
if (invalidTarget(item)) {
return;
}
var container = item.parentElement;
var movable = o.moves(item, container, handle);
if (!movable) {
return;
}
end();
if (o.copy) {
_copy = item.cloneNode(true);
addClass(_copy, 'gu-transit');
} else {
addClass(item, 'gu-transit');
}
_source = container;
_item = item;
_initialSibling = _currentSibling = nextEl(item);
api.dragging = true;
api.emit('drag', _item, _source);
return true;
}
function invalidTarget (el) {
return el.tagName === 'A' || el.tagName === 'BUTTON';
}
function end () {
if (!api.dragging) {
return;
}
var item = _copy || _item;
drop(item, item.parentElement);
}
function release (e) {
if (!api.dragging) {
return;
}
var item = _copy || _item;
var clientX = getCoord('clientX', e);
var clientY = getCoord('clientY', e);
var elementBehindCursor = getElementBehindPoint(_mirror, clientX, clientY);
var dropTarget = findDropTarget(elementBehindCursor, clientX, clientY);
if (dropTarget && (o.copy === false || dropTarget !== _source)) {
drop(item, dropTarget);
} else if (o.removeOnSpill) {
remove();
} else {
cancel();
}
}
function drop (item, target) {
if (isInitialPlacement(target)) {
api.emit('cancel', item, _source);
} else {
api.emit('drop', item, target, _source);
}
cleanup();
}
function remove () {
if (!api.dragging) {
return;
}
var item = _copy || _item;
var parent = item.parentElement;
if (parent) {
parent.removeChild(item);
}
api.emit(o.copy ? 'cancel' : 'remove', item, parent);
cleanup();
}
function cancel (revert) {
if (!api.dragging) {
return;
}
var reverts = arguments.length > 0 ? revert : o.revertOnSpill;
var item = _copy || _item;
var parent = item.parentElement;
if (parent === _source && o.copy) {
parent.removeChild(_copy);
}
var initial = isInitialPlacement(parent);
if (initial === false && o.copy === false && reverts) {
_source.insertBefore(item, _initialSibling);
}
if (initial || reverts) {
api.emit('cancel', item, _source);
} else {
api.emit('drop', item, parent, _source);
}
cleanup();
}
function cleanup () {
var item = _copy || _item;
removeMirrorImage();
rmClass(item, 'gu-transit');
_source = _item = _copy = _initialSibling = _currentSibling = null;
api.dragging = false;
api.emit('dragend', item);
}
function isInitialPlacement (target, s) {
var sibling;
if (s !== void 0) {
sibling = s;
} else if (_mirror) {
sibling = _currentSibling;
} else {
sibling = nextEl(_item || _copy);
}
return target === _source && sibling === _initialSibling;
}
function findDropTarget (elementBehindCursor, clientX, clientY) {
var target = elementBehindCursor;
while (target && !accepted()) {
target = target.parentElement;
}
return target;
function accepted () {
var droppable = _containers.indexOf(target) !== -1;
if (droppable === false) {
return false;
}
var immediate = getImmediateChild(target, elementBehindCursor);
var reference = getReference(target, immediate, clientX, clientY);
var initial = isInitialPlacement(target, reference);
if (initial) {
return true; // should always be able to drop it right back where it was
}
return o.accepts(_item, target, _source, reference);
}
}
function drag (e) {
if (!_mirror) {
return;
}
var clientX = getCoord('clientX', e);
var clientY = getCoord('clientY', e);
var x = clientX - _offsetX;
var y = clientY - _offsetY;
_mirror.style.left = x + 'px';
_mirror.style.top = y + 'px';
var elementBehindCursor = getElementBehindPoint(_mirror, clientX, clientY);
var dropTarget = findDropTarget(elementBehindCursor, clientX, clientY);
if (dropTarget === _source && o.copy) {
return;
}
var item = _copy || _item;
var immediate = getImmediateChild(dropTarget, elementBehindCursor);
if (immediate === null) {
return;
}
var reference = getReference(dropTarget, immediate, clientX, clientY);
if (reference === null || reference !== item && reference !== nextEl(item)) {
_currentSibling = reference;
dropTarget.insertBefore(item, reference);
api.emit('shadow', item, dropTarget);
}
}
function renderMirrorImage () {
if (_mirror) {
return;
}
var rect = _item.getBoundingClientRect();
_mirror = _item.cloneNode(true);
_mirror.style.width = rect.width + 'px';
_mirror.style.height = rect.height + 'px';
rmClass(_mirror, 'gu-transit');
addClass(_mirror, ' gu-mirror');
body.appendChild(_mirror);
touchy(documentElement, 'add', 'mousemove', drag);
addClass(body, 'gu-unselectable');
}
function removeMirrorImage () {
if (_mirror) {
rmClass(body, 'gu-unselectable');
touchy(documentElement, 'remove', 'mousemove', drag);
_mirror.parentElement.removeChild(_mirror);
_mirror = null;
}
}
function getImmediateChild (dropTarget, target) {
var immediate = target;
while (immediate !== dropTarget && immediate.parentElement !== dropTarget) {
immediate = immediate.parentElement;
}
if (immediate === documentElement) {
return null;
}
return immediate;
}
function getReference (dropTarget, target, x, y) {
var horizontal = o.direction === 'horizontal';
var reference = target !== dropTarget ? inside() : outside();
return reference;
function outside () { // slower, but able to figure out any position
var len = dropTarget.children.length;
var i;
var el;
var rect;
for (i = 0; i < len; i++) {
el = dropTarget.children[i];
rect = el.getBoundingClientRect();
if (horizontal && rect.left > x) { return el; }
if (!horizontal && rect.top > y) { return el; }
}
return null;
}
function inside () { // faster, but only available if dropped inside a child element
var rect = target.getBoundingClientRect();
if (horizontal) {
return resolve(x > rect.left + rect.width / 2);
}
return resolve(y > rect.top + rect.height / 2);
}
function resolve (after) {
return after ? nextEl(target) : target;
}
}
}
function touchy (el, op, type, fn) {
var touch = {
mouseup: 'touchend',
mousedown: 'touchstart',
mousemove: 'touchmove'
};
var microsoft = {
mouseup: 'MSPointerUp',
mousedown: 'MSPointerDown',
mousemove: 'MSPointerMove'
};
if (global.navigator.msPointerEnabled) {
crossvent[op](el, microsoft[type], fn);
}
crossvent[op](el, touch[type], fn);
crossvent[op](el, type, fn);
}
function getOffset (el) {
var rect = el.getBoundingClientRect();
return {
left: rect.left + getScroll('scrollLeft', 'pageXOffset'),
top: rect.top + getScroll('scrollTop', 'pageYOffset')
};
}
function getScroll (scrollProp, offsetProp) {
if (typeof global[offsetProp] !== 'undefined') {
return global[offsetProp];
}
var documentElement = document.documentElement;
if (documentElement.clientHeight) {
return documentElement[scrollProp];
}
var body = document.body;
return body[scrollProp];
}
function getElementBehindPoint (point, x, y) {
if (!x && !y) {
return null;
}
var p = point || {};
var state = p.className;
var el;
p.className += ' gu-hide';
el = document.elementFromPoint(x, y);
p.className = state;
return el;
}
function always () {
return true;
}
function nextEl (el) {
return el.nextElementSibling || manually();
function manually () {
var sibling = el;
do {
sibling = sibling.nextSibling;
} while (sibling && sibling.nodeType !== 1);
return sibling;
}
}
function addClass (el, className) {
if (el.className.indexOf(' ' + className) === -1) {
el.className += ' ' + className;
}
}
function rmClass (el, className) {
el.className = el.className.replace(new RegExp(' ' + className, 'g'), '');
}
function getCoord (coord, e) {
if (typeof e.targetTouches === 'undefined') {
return e[coord];
}
// on touchend event, we have to use `e.changedTouches`
// see http://stackoverflow.com/questions/7192563/touchend-event-properties
// see https://github.com/bevacqua/dragula/issues/34
return (
(e.targetTouches && e.targetTouches.length && e.targetTouches[0][coord]) ||
(e.changedTouches && e.changedTouches.length && e.changedTouches[0][coord]) ||
0
);
}
module.exports = dragula;