-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdragonfly.js
338 lines (269 loc) · 10.7 KB
/
dragonfly.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
/* eslint-env browser */
/* jslint-env browser */
/* global window */
/* global document */
/* global console */
/*
* DragonflyJS - v1.2.0 - 2018-07-17
* https://getbutterfly.com/dragonflyjs-vanilla-javascript-drag-and-drop/
* Copyright (c) 2018 Ciprian Popescu
* Licensed GPLv3
*
* iMouseDown represents the current mouse button state: up or down
* lMouseState represents the previous mouse button state so that we can check for button clicks and button releases:
*
* if (iMouseDown && !lMouseState) {} // button clicked
* if (!iMouseDown && lMouseState) {} // button released
*/
var iMouseDown = false;
var lMouseState = false;
var dragObject = null;
var DragDrops = [];
var curTarget = null;
var lastTarget = null;
var rootParent = null;
var rootSibling = null;
Number.prototype.NaN0 = function () {
'use strict';
return isNaN(this) ? 0 : this;
};
function createDragContainer(element) {
'use strict';
var j = 0,
cDrag = DragDrops.length;
DragDrops[cDrag] = [];
element.setAttribute('DropObj', cDrag);
DragDrops[cDrag].push(element);
for (j = 0; j < element.childNodes.length; j += 1) {
if (element.childNodes[j].nodeName !== '#text') {
element.childNodes[j].setAttribute('DragObj', cDrag);
}
}
}
function getPosition(e) {
'use strict';
var left = 0,
top = 0;
while (e.offsetParent) {
left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth, 10)).NaN0() : 0);
top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth, 10)).NaN0() : 0);
e = e.offsetParent;
}
left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth, 10)).NaN0() : 0);
top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth, 10)).NaN0() : 0);
return {
x: left,
y: top
};
}
function mouseCoords(ev) {
'use strict';
if (ev.pageX || ev.pageY) {
return {
x: ev.pageX,
y: ev.pageY
};
}
return {
x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function mouseMove(ev) {
'use strict';
ev = ev || window.event;
/*
* We are setting target to whatever item the mouse is currently on
* Firefox uses event.target here, MSIE uses event.srcElement
*/
var elementInstance,
dragHelper = document.querySelector('.drag-helper'),
activeCont = null,
target = ev.target || ev.srcElement,
mousePos = mouseCoords(ev),
origClass,
dragConts,
dragObj,
pos,
i,
j;
// mouseOut event - fires if the item the mouse is on has changed
if (lastTarget && (target !== lastTarget)) {
// Reset the classname for the target element
origClass = lastTarget.getAttribute('origClass');
if (origClass) {
lastTarget.className = origClass;
}
}
/*
* dragObj is the grouping the item is in (set from the createDragContainer function)
* if the item is not in a grouping we ignore it since it can't be dragged with this script
*/
dragObj = target.getAttribute('DragObj');
// If the mouse was moved over an element that is draggable
if (dragObj !== null) {
// If the user is just starting to drag the element
if (iMouseDown && !lMouseState) {
// mouseDown target
curTarget = target;
// Record the mouse x and y offset for the element
rootParent = curTarget.parentNode;
rootSibling = curTarget.nextSibling;
// Remove anything that is in the dragHelper div so we can put a new item in it
for (i = 0; i < dragHelper.childNodes.length; i += 1) {
dragHelper.removeChild(dragHelper.childNodes[i]);
}
// Make a copy of the current item and put it in the drag helper
dragHelper.appendChild(curTarget.cloneNode(true));
dragHelper.style.display = 'block';
// Set the class on the helper div if necessary
dragHelper.classList.add('drag-box-dragging');
// Disable dragging from the helper div (it's already being dragged)
dragHelper.firstChild.removeAttribute('DragObj');
// Record the current position of all drag/drop targets related to the element
dragConts = DragDrops[dragObj];
// First record the width/height of the drag item, then hide it since it is going to (potentially) be moved out of its parent
curTarget.setAttribute('startWidth', parseInt(curTarget.offsetWidth, 10));
curTarget.setAttribute('startHeight', parseInt(curTarget.offsetHeight, 10));
curTarget.style.display = 'none';
// Loop through each possible drop container
for (i = 0; i < dragConts.length; i += 1) {
elementInstance = dragConts[i];
pos = getPosition(dragConts[i]);
// Save the width, height and position of each container
elementInstance.setAttribute('startWidth', parseInt(elementInstance.offsetWidth, 10));
elementInstance.setAttribute('startHeight', parseInt(elementInstance.offsetHeight, 10));
elementInstance.setAttribute('startLeft', pos.x);
elementInstance.setAttribute('startTop', pos.y);
// Loop through each child element of each container
for (j = 0; j < dragConts[i].childNodes.length; j += 1) {
elementInstance = dragConts[i].childNodes[j];
if ((elementInstance.nodeName === '#text') || (dragConts[i].childNodes[j] === curTarget)) {
continue;
}
pos = getPosition(dragConts[i].childNodes[j]);
// Save the width, height and position of each element
elementInstance.setAttribute('startWidth', parseInt(elementInstance.offsetWidth, 10));
elementInstance.setAttribute('startHeight', parseInt(elementInstance.offsetHeight, 10));
elementInstance.setAttribute('startLeft', pos.x);
elementInstance.setAttribute('startTop', pos.y);
}
}
}
}
// If we get in here we are dragging something
if (curTarget) {
// Move helper div to wherever the mouse is
dragHelper.style.top = mousePos.y + 'px';
dragHelper.style.left = mousePos.x + 'px';
dragConts = DragDrops[curTarget.getAttribute('DragObj')];
var xPos = mousePos.x + (parseInt(curTarget.getAttribute('startWidth'), 10) / 2),
yPos = mousePos.y + (parseInt(curTarget.getAttribute('startHeight'), 10) / 2);
// Check each drop container to see if target object is "inside" the container
for (i = 0; i < dragConts.length; i += 1) {
activeCont = dragConts[i];
}
// beforeNode will hold the first node AFTER where div belongs
var beforeNode = null;
// Loop through each child node (skipping text nodes)
for (i = activeCont.childNodes.length - 1; i >= 0; i -= 1) {
elementInstance = activeCont.childNodes[i];
if (elementInstance.nodeName === '#text') {
continue;
}
// If the current item is "After" the item being dragged
if (curTarget !== activeCont.childNodes[i] && ((parseInt(elementInstance.getAttribute('startLeft'), 10) + parseInt(elementInstance.getAttribute('startWidth'), 10)) > xPos) && ((parseInt(elementInstance.getAttribute('startTop'), 10) + parseInt(elementInstance.getAttribute('startHeight'), 10)) > yPos)) {
beforeNode = activeCont.childNodes[i];
}
}
// The item being dragged belongs before another item
if (beforeNode) {
if (beforeNode !== curTarget.nextSibling) {
activeCont.insertBefore(curTarget, beforeNode);
}
// The item being dragged belongs at the end of the current container
} else {
if ((curTarget.nextSibling) || (curTarget.parentNode !== activeCont)) {
activeCont.appendChild(curTarget);
}
}
// Make drag item visible
if (curTarget.style.display !== '') {
curTarget.style.display = '';
curTarget.style.visibility = 'hidden';
}
}
// Track the current mouse state so we can compare against it next time
lMouseState = iMouseDown;
// mouseMove target
lastTarget = target;
if (dragObject) {
dragObject.style.position = 'absolute';
dragObject.style.top = mousePos.y;
dragObject.style.left = mousePos.x;
}
// Track the current mouse state so we can compare against it next time
lMouseState = iMouseDown;
// Prevent items on the page from being highlighted while dragging
if (curTarget || dragObject) {
return false;
}
}
function mouseUp(callback) {
'use strict';
var dragHelper = document.querySelector('.drag-helper');
if (curTarget) {
dragHelper.style.display = 'none';
if (curTarget.style.display === 'none') {
if (rootSibling) {
rootParent.insertBefore(curTarget, rootSibling);
} else {
rootParent.appendChild(curTarget);
}
}
curTarget.style.display = '';
curTarget.style.visibility = 'visible';
}
curTarget = null;
dragObject = null;
iMouseDown = false;
// Add AJAX event here
if (typeof callback === 'function') {
callback();
}
}
function mouseDown(ev) {
'use strict';
ev = ev || window.event;
var target = ev.target || ev.srcElement;
iMouseDown = true;
if (target.onmousedown || target.getAttribute('DragObj')) {
return false;
}
}
function dragonfly(element, callback) {
'use strict';
createDragContainer(document.querySelector(element));
var dragHelper = document.createElement('div');
dragHelper.style.cssText = 'position: absolute; display: none;';
dragHelper.classList.add('drag-helper');
document.body.appendChild(dragHelper);
document.addEventListener('mousemove', function () {
mouseMove();
});
document.addEventListener('mousedown', function () {
mouseDown();
});
document.addEventListener('mouseup', function () {
mouseUp(callback);
});
}
/**
* DragonflyJS Usage
*/
document.addEventListener('DOMContentLoaded', function () {
'use strict';
dragonfly('.drag-container', function () {
console.log('This is a callback');
});
});