-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdragdrop.js
98 lines (93 loc) · 2.62 KB
/
dragdrop.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
/**
* GDragDrop
* Javascript drag drop
*
* @filesource js/dragdrop.js
* @link https://www.kotchasan.com/
* @copyright 2019 Goragod.com
* @license https://www.kotchasan.com/license/
*/
(function() {
"use strict";
window.GDragDrop = GClass.create();
GDragDrop.prototype = {
initialize: function(id, options) {
this.options = {
dragClass: "icon-drag",
itemClass: "sort",
endDrag: $K.emptyFunction
};
for (var property in options) {
this.options[property] = options[property];
}
this.changed = false;
var self = this,
dropitems = new Array(),
hoverItem = null,
position = 0;
function checkMouseOver(item, mousePos) {
var elemPos = item.viewportOffset();
var elemSize = item.getDimensions();
var mouseover = mousePos.x > elemPos.left && mousePos.y > elemPos.top;
return (
mouseover &&
mousePos.x < elemPos.left + elemSize.width &&
mousePos.y < elemPos.top + elemSize.height
);
}
function doBeginDrag() {
self.changed = false;
self.dragItem = this;
hoverItem = this;
position = this.mousePos.y;
}
function doMoveDrag() {
var temp = this;
forEach(dropitems, function() {
if (checkMouseOver(this, temp.mousePos)) {
if (this != hoverItem) {
self.changed = true;
if (temp.mousePos.y > position) {
temp.move.parentNode.insertBefore(temp.move, this.nextSibling);
} else {
temp.move.parentNode.insertBefore(temp.move, this);
}
hoverItem = this;
return true;
}
}
});
position = this.mousePos.y;
}
function doEndDrag() {
if (self.changed) {
self.options.endDrag.call(this);
}
}
function _find(elem) {
if (elem.hasClass(self.options.dragClass)) {
return elem;
} else {
var els = $E(elem).getElementsByTagName("*");
for (var i = 0; i < els.length; i++) {
if ($G(els[i]).hasClass(self.options.dragClass)) {
return els[i];
}
}
}
}
var o = {
beginDrag: doBeginDrag,
moveDrag: doMoveDrag,
endDrag: doEndDrag
};
forEach($E(id).getElementsByTagName("*"), function() {
if ($G(this).hasClass(self.options.itemClass)) {
var drag = new GDrag(_find(this), o);
drag.move = this;
dropitems.push(this);
}
});
}
};
})();