forked from lukasolson/drag-n-drop-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
228 lines (185 loc) · 6.27 KB
/
app.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
var droppables = new Array();
var itemBeingDragged = null;
var mouseDownPoint = {x: 0, y: 0};
function onDocumentMouseMove(mouseMoveEvent) {
var point = {x: mouseMoveEvent.pageX, y: mouseMoveEvent.pageY};
// Drag the draggable to this position
itemBeingDragged.dragTo(point);
// If there are any droppable elements at this position, notify them
for (var i = 0; i < droppables.length; i++) {
if (droppables[i].contains(point) && !droppables[i].isBeingDraggedOver) {
droppables[i].onDragEnter();
} else if (!droppables[i].contains(point) && droppables[i].isBeingDraggedOver) {
droppables[i].onDragExit();
}
}
}
function onDocumentMouseUp(mouseUpEvent) {
// If any droppable is being dragged over, accept the drop
for (var i = 0; i < droppables.length; i++) {
if (droppables[i].isBeingDraggedOver) {
droppables[i].onDragDrop(itemBeingDragged);
}
}
// Reset the position of the item being dragged and clear out the document event handlers
itemBeingDragged.reset(mouseUpEvent);
}
var Draggable = function(elementId) {
this.init(elementId);
};
Draggable.prototype = {
init: function(element) {
if (typeof element === "string") element = document.getElementById(element);
this.element = element;
this.element.className += "draggable";
var self = this;
this.element.onmousedown = function(mouseDownEvent) {
this.style.zIndex = "1000";
itemBeingDragged = self;
mouseDownPoint.x = mouseDownEvent.pageX;
mouseDownPoint.y = mouseDownEvent.pageY;
document.onmousemove = onDocumentMouseMove;
document.onmouseup = onDocumentMouseUp;
};
},
// Called when the mouse is moved (after having been pressed on this element)
dragTo: function(point) {
this.element.style.left = (point.x - mouseDownPoint.x) + "px";
this.element.style.top = (point.y - mouseDownPoint.y) + "px";
},
// Called when the mouse is lifted (after having been pressed on this element)
reset: function() {
this.element.style.zIndex = "";
this.element.style.left = "";
this.element.style.top = "";
itemBeingDragged = null;
mouseDownPoint.x = 0;
mouseDownPoint.y = 0;
document.onmousemove = null;
document.onmouseup = null;
}
};
// customDragDrop is a custom function which will be called when a Draggable is dropped
// on this Droppable; it is passed the Draggable that was dropped
var Droppable = function(element, customDragDrop) {
this.init(element, customDragDrop);
};
Droppable.prototype = {
init: function(element, customDragDrop) {
if (typeof element === "string") element = document.getElementById(element);
this.element = element;
this.isBeingDraggedOver = false;
this.customDragDrop = customDragDrop;
droppables.push(this);
},
// Calculate the top-left coordinate of this element
position: function() {
var position = {x: this.element.offsetLeft, y: this.element.offsetTop};
var offsetParent = this.element.offsetParent;
while (offsetParent) {
position.x += offsetParent.offsetLeft;
position.y += offsetParent.offsetTop;
offsetParent = offsetParent.offsetParent;
}
return position;
},
// Calculate whether the given coordinate falls within this element's boundaries
contains: function(point) {
var topLeft = this.position();
var bottomRight = {
x: topLeft.x + this.element.offsetWidth,
y: topLeft.y + this.element.offsetHeight
};
return (
topLeft.x < point.x
&& topLeft.y < point.y
&& point.x < bottomRight.x
&& point.y < bottomRight.y
);
},
// Called when an item is dragged into this element
onDragEnter: function() {
this.isBeingDraggedOver = true;
this.element.className += "dragOver";
},
// Called when an item is dragged out of this element
onDragExit: function() {
this.isBeingDraggedOver = false;
this.element.className = this.element.className.replace(/\bdragOver\b/, "");
},
// Called when an item is dropped on this element
onDragDrop: function(draggable) {
this.onDragExit();
this.customDragDrop(draggable);
}
};
function touchHandler(event)
{
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type)
{
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
// initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
function mapDocumentTouchToMouse()
{
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
mapDocumentTouchToMouse();
window.onload = function() {
var data = [
"These",
"Words",
"Can",
"Be",
"Dragged",
"And",
"Dropped",
"Over",
"On",
"The",
"Right",
"Side",
"Of",
"The",
"Screen"
];
var availableMetrics = document.getElementById("available_metrics_list");
for (var i = 0; i < data.length; i++) {
var liElement = document.createElement("li");
liElement.appendChild(document.createTextNode(data[i]));
availableMetrics.appendChild(liElement);
var liElementDraggable = new Draggable(liElement);
}
var availableMetricsDroppable = new Droppable(availableMetrics, function(draggable) {
if (this.element !== draggable.element.parentNode) {
this.element.appendChild(draggable.element);
}
});
var selectedMetricsDroppable = new Droppable("next_metric", function(draggable) {
if (this.element.parentNode !== draggable.element.parentNode) {
this.element.parentNode.insertBefore(draggable.element, this.element);
}
});
new Droppable("remove_metric", function(draggable) {
availableMetricsDroppable.onDragDrop(draggable);
});
};