-
Notifications
You must be signed in to change notification settings - Fork 3
/
juicy-draggable.html
352 lines (338 loc) · 15.2 KB
/
juicy-draggable.html
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
<!--
`juicy-draggable element` - Custom element to make items draggable and droppable.
@element juicy-draggable
version: 1.0.0
-->
<script>
(function () {
var script = document._currentScript || document.currentScript;
var scriptDocument = script.ownerDocument;
//https://github.com/gtramontina/draggable.js/blob/master/draggable.js
var draggable = (function definition() {
function addEventListener(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, handler);
} else {
element['on' + eventName] = handler;
}
}
function removeEventListener(element, eventName, handler) {
if (element.removeEventListener) {
element.removeEventListener(eventName, handler, false);
} else if (element.detachEvent) {
element.detachEvent('on' + eventName, handler);
} else {
element['on' + eventName] = null;
}
}
function isDisabled(element) {
return element.hasAttribute("data-draggable-disabled");
}
var currentElement;
var currentDropSelector;
var currentDropElement;
var currentOptions;
var fairlyHighZIndex = '10';
function draggable(options) {
var element = options.element;
var handle = options.handle || element;
var position = options.position;
var dropSelector = options.dropSelector;
var dragElement = setPositionType(element, position, options.dragHelperClass);
var on = 0;
var started = false;
setDraggableListeners(dragElement);
addEventListener(document, "mousemove", function (event) {
if (isDisabled(element)) {
return;
}
if (on != 2 || started) {
return;
}
if (position == "static") {
copyElement(element, dragElement);
}
currentOptions = options;
started = startDragging(event, dragElement);
});
addEventListener(handle, "mousemove", function (event) {
if (isDisabled(element)) {
return;
}
event.preventDefault();
});
addEventListener(handle, "mousedown", function (event) {
if (isDisabled(element)) {
return;
}
on = 1;
setTimeout(function () {
if (on == 1) {
on = 2;
}
}, 50);
});
addEventListener(document, "mouseup", function (event) {
on = 0;
started = false;
});
return dragElement;
}
function setPositionType(element, position, dragHelperClass) {
if (position == "static") {
var div = document.createElement("div");
div.className = dragHelperClass;
div.style.position = "fixed";
element.parentNode.insertBefore(div, element);
return div;
}
if (!position) {
position = "absolute";
}
element.style.position = position;
return element;
}
function setDraggableListeners(element) {
element.draggableListeners = {
start: [],
drag: [],
stop: [],
highlight: []
};
element.whenDragStarts = addListener(element, 'start');
element.whenDragging = addListener(element, 'drag');
element.whenDragStops = addListener(element, 'stop');
element.whenDragHighlight = addListener(element, 'highlight');
}
function copyElement(from, to) {
var rec = from.getBoundingClientRect();
to.innerText = from.innerText;
to.style.width = inPixels(from.offsetWidth);
to.style.height = inPixels(from.offsetHeight);
to.style.left = inPixels(rec.left);
to.style.top = inPixels(rec.top);
}
function startDragging(event, element) {
clearSelection();
currentElement && sendToBack(currentElement);
currentElement = bringToFront(element);
currentElement.classList.add(currentOptions.draggingClass);
var initialPosition = getInitialPosition(currentElement);
currentElement.style.left = inPixels(initialPosition.left);
currentElement.style.top = inPixels(initialPosition.top);
currentElement.lastXPosition = event.clientX;
currentElement.lastYPosition = event.clientY;
var okToGoOn = triggerEvent('start', { x: initialPosition.left, y: initialPosition.top, mouseEvent: event });
if (!okToGoOn) {
currentElement.classList.remove(currentOptions.draggingClass);
return false;
};
addDocumentListeners();
return true;
}
function stopDragging(event) {
removeDocumentListeners();
var left = parseInt(currentElement.style.left, 10);
var top = parseInt(currentElement.style.top, 10);
if (currentDropElement) {
currentDropElement.classList.remove(currentOptions.dragHoverClass);
}
triggerEvent('stop', { x: left, y: top, mouseEvent: event, dropElement: currentDropElement, dragElement: currentElement });
currentElement.classList.remove(currentOptions.draggingClass);
}
function addListener(element, type) {
return function (listener) {
element.draggableListeners[type].push(listener);
};
}
function triggerEvent(type, args) {
var result = true;
var listeners = currentElement.draggableListeners[type];
for (var i = listeners.length - 1; i >= 0; i--) {
if (listeners[i](args) === false) result = false;
};
return result;
}
function sendToBack(element) {
var decreasedZIndex = fairlyHighZIndex - 1;
element.style['z-index'] = decreasedZIndex;
element.style['zIndex'] = decreasedZIndex;
}
function bringToFront(element) {
element.style['z-index'] = fairlyHighZIndex;
element.style['zIndex'] = fairlyHighZIndex;
return element;
}
function addDocumentListeners() {
addEventListener(document, 'selectstart', cancelDocumentSelection);
addEventListener(document, 'mousemove', repositionElement);
addEventListener(document, 'mouseup', stopDragging);
}
function getInitialPosition(element) {
var rec = element.getBoundingClientRect();
var style = element.currentStyle || window.getComputedStyle(element);
return {
top: rec.top - parseInt(style.marginTop),
left: rec.left - parseInt(style.marginLeft)
};
}
function inPixels(value) {
return value + 'px';
}
function cancelDocumentSelection(event) {
event.preventDefault && event.preventDefault();
event.stopPropagation && event.stopPropagation();
event.returnValue = false;
return false;
}
function repositionElement(event) {
event.preventDefault && event.preventDefault();
event.returnValue = false;
var style = currentElement.style;
var elementXPosition = parseInt(style.left, 10);
var elementYPosition = parseInt(style.top, 10);
var elementNewXPosition = elementXPosition + (event.clientX - currentElement.lastXPosition);
var elementNewYPosition = elementYPosition + (event.clientY - currentElement.lastYPosition);
style.left = inPixels(elementNewXPosition);
style.top = inPixels(elementNewYPosition);
style.right = "auto";
style.bottom = "auto";
currentElement.lastXPosition = event.clientX;
currentElement.lastYPosition = event.clientY;
highlightDropElement(event);
triggerEvent('drag', { x: elementNewXPosition, y: elementNewYPosition, mouseEvent: event });
}
function highlightDropElement(event) {
currentDropElement = null;
if (!currentOptions.dropSelector) {
return null;
}
var items = document.querySelectorAll(currentOptions.dropSelector);
for (var i = 0; i < items.length; i++) {
var item = items[i];
var rec = item.getBoundingClientRect();
var x = event.clientX;
var y = event.clientY;
if (x >= rec.left && x <= rec.right && y >= rec.top && y <= rec.bottom) {
currentDropElement = item;
item.classList.add(currentOptions.dragHoverClass);
triggerEvent('highlight', { x: x, y: y, item: item, mouseEvent: event });
} else {
item.classList.remove(currentOptions.dragHoverClass);
}
}
}
function removeDocumentListeners() {
removeEventListener(document, 'selectstart', cancelDocumentSelection);
removeEventListener(document, 'mousemove', repositionElement);
removeEventListener(document, 'mouseup', stopDragging);
}
function clearSelection() {
if (window.getSelection) window.getSelection().removeAllRanges();
else if (document.selection) document.selection.empty();
}
return draggable;
})();
class JuicyDraggable extends HTMLElement {
constructor() {
super();
this.element = null;
}
static get observedAttributes() {
return ["disabled"]
}
attributeChangedCallback(attributeName) {
switch (attributeName) {
case "disabled":
var value = this.hasAttribute("disabled");
if (value) {
this.element.setAttribute("data-draggable-disabled", "disabled");
} else {
this.element.removeAttribute("data-draggable-disabled");
}
break;
}
}
connectedCallback() {
var element = null;
var handle = null;
var that = this;
var root;
if (this.attachShadow) {
root = this.attachShadow({ mode: 'open' });
root.innerHTML = '<slot id="content"></slot>';
} else {
root = this.createShadowRoot();
root.innerHTML = '<content id="content"></content>';
}
this.refreshAttributeValues();
if (!this.elementId) {
element = this.children[0];
} else {
element = this.querySelector("#" + this.elementId);
}
if (this.handleId) {
handle = this.querySelector("#" + this.handleId);
}
this.dragHelperClass = this.dragHelperClass || "drag-helper";
this.dragHoverClass = this.dragHoverClass || "drag-hover";
this.draggingClass = this.draggingClass || "dragging";
this.element = element;
element = draggable({
element: element,
handle: handle,
position: this.position,
dropSelector: this.dropSelector,
dragHelperClass: this.dragHelperClass,
dragHoverClass: this.dragHoverClass,
draggingClass: this.draggingClass
});
element.whenDragStarts(function (e) {
var event = new Event("juicy-draggable-start");
event.detail = e;
that.dispatchEvent(event);
return !(event.cancel === true);
});
element.whenDragging(function (e) {
var event = new Event("juicy-draggable-move");
event.detail = e;
that.dispatchEvent(event);
});
element.whenDragStops(function (e) {
var event = new Event("juicy-draggable-stop");
event.detail = e;
that.dispatchEvent(event);
});
element.whenDragHighlight(function (e) {
var event = new Event("juicy-draggable-highligh");
event.detail = e;
that.dispatchEvent(event);
});
}
getScreenSize() {
var w = window;
var d = document;
var e = d.documentElement;
var g = d.getElementsByTagName('body')[0];
var x = w.innerWidth || e.clientWidth || g.clientWidth;
var y = w.innerHeight || e.clientHeight || g.clientHeight;
return { x: x, y: y };
}
refreshAttributeValues() {
var attributes = ["elementId", "handleId", "position", "dropSelector", "dragHelperClass", "dragHoverClass", "draggingClass", "disabled"];
for (var i = 0; i < attributes.length; i++) {
var a = attributes[i];
if (this.hasAttribute(a)) {
this[a] = this.attributes[a].nodeValue;
this.attributeChangedCallback(a);
}
}
}
}
//Throws a weird exception in IE;
//JuicyDraggable.disabled = false;
customElements.define("juicy-draggable", JuicyDraggable);
})();
</script>