3
|
1 'use strict';
|
|
2
|
|
3 let dad = {};
|
|
4
|
|
5 {
|
4
|
6 // override these if needed
|
3
|
7 dad.whatToDrag = function(draggable) {
|
|
8 return draggable;
|
|
9 };
|
11
|
10 dad.onStart = function(event) {};
|
|
11 dad.onEnter = function(event) {};
|
|
12 dad.onLeave = function(event) {};
|
4
|
13 dad.onDrop = function(event) {};
|
|
14 dad.onDropped = function(event) {};
|
3
|
15
|
|
16
|
|
17 let original = null;
|
|
18 let dragging = null;
|
6
|
19 let dropzone = null;
|
3
|
20 let touchX, touchY;
|
|
21
|
6
|
22 function isIn(x,y,rect) {
|
|
23 return rect.x <= x && x <= rect.x+rect.width && rect.y <= y && y <= rect.y+rect.height;
|
|
24 }
|
|
25
|
11
|
26 function myEvent(mouseEvent) {
|
|
27 return {
|
|
28 original: original,
|
|
29 dragging: dragging,
|
|
30 dropzone: dropzone,
|
|
31 mouseEvent: mouseEvent,
|
|
32 };
|
|
33 }
|
|
34
|
3
|
35 function onMouseMove(event) {
|
6
|
36 {
|
|
37 event.preventDefault();
|
|
38 let rect = dragging.getBoundingClientRect();
|
|
39 dragging.style.left = `${rect.x+event.movementX}px`;
|
|
40 let y = rect.y + event.movementY;
|
|
41 if( y < 0 ) {
|
|
42 window.scrollBy( 0, y );
|
|
43 } else if( y + rect.height > window.innerHeight ) {
|
|
44 window.scrollBy( 0, y + rect.height - window.innerHeight );
|
|
45 }
|
|
46 dragging.style.top = `${y}px`;
|
3
|
47 }
|
6
|
48 {
|
|
49 let x = event.clientX;
|
|
50 let y = event.clientY;
|
|
51 if( !(dropzone && isIn(x,y,dropzone.getBoundingClientRect())) ) {
|
|
52 if( dropzone ) {
|
11
|
53 dad.onLeave(myEvent(event));
|
6
|
54 dropzone = null;
|
|
55 }
|
|
56 let dropzones = document.querySelectorAll('[dad-dropzone]');
|
|
57 for( let i=0; i<dropzones.length; i++ ) {
|
|
58 let dz = dropzones[i];
|
|
59 if( dz === dragging )
|
|
60 continue;
|
|
61 if( isIn(x,y,dz.getBoundingClientRect()) ) {
|
|
62 dropzone = dz;
|
11
|
63 dad.onEnter(myEvent(event));
|
6
|
64 break;
|
|
65 }
|
|
66 }
|
|
67 }
|
|
68 }
|
3
|
69 }
|
|
70
|
|
71 function onTouchMove(event) {
|
|
72 let touches = event.touches;
|
|
73 if( touches.length !== 1 )
|
|
74 return;
|
|
75 let touch = touches[0];
|
|
76 let x = touch.clientX;
|
|
77 let y = touch.clientY;
|
6
|
78 event.clientX = x;
|
|
79 event.clientY = y;
|
3
|
80 event.movementX = x - touchX;
|
|
81 event.movementY = y - touchY;
|
|
82 touchX = x;
|
|
83 touchY = y;
|
|
84 onMouseMove(event);
|
|
85 }
|
|
86
|
|
87 function onMouseUp(event) {
|
11
|
88 dad.onDrop(myEvent(event));
|
6
|
89 if( dropzone ) {
|
11
|
90 dad.onLeave(myEvent(event));
|
6
|
91 }
|
3
|
92 original.removeAttribute('dad-original');
|
4
|
93 dragging.parentNode.removeChild(dragging);
|
3
|
94 document.removeEventListener('mousemove',onMouseMove);
|
|
95 document.removeEventListener('mouseup',onMouseUp);
|
|
96 document.removeEventListener('mousemove',onMouseMove);
|
|
97 document.removeEventListener('touchend',onMouseUp);
|
|
98 original.scrollIntoViewIfNeeded(false);
|
4
|
99 let droppedEvent = {
|
5
|
100 original: original,
|
4
|
101 };
|
3
|
102 original = null;
|
|
103 dragging = null;
|
6
|
104 dropzone = null;
|
4
|
105 dad.onDropped(droppedEvent);
|
3
|
106 }
|
|
107
|
|
108 function start(event) {
|
|
109 original = dad.whatToDrag(event.target);
|
|
110 dragging = original.cloneNode(true);
|
|
111 original.setAttribute('dad-original','');
|
|
112 dragging.setAttribute('dad-dragging','');
|
|
113 let rect = original.getBoundingClientRect();
|
|
114 dragging.style.left = `${rect.x}px`;
|
|
115 dragging.style.top = `${rect.y}px`;
|
8
|
116 dragging.style.width = `${rect.width}px`;
|
|
117 dragging.style.heigh = `${rect.heigh}px`;
|
4
|
118 original.parentNode.appendChild(dragging);
|
11
|
119 dad.onStart(myEvent(event));
|
3
|
120 }
|
|
121
|
|
122 function onMouseDown(event) {
|
|
123 start(event);
|
|
124 document.addEventListener('mousemove',onMouseMove);
|
|
125 document.addEventListener('mouseup',onMouseUp);
|
|
126 }
|
|
127
|
|
128 function onTouchStart(event) {
|
10
|
129 event.preventDefault();
|
3
|
130 start(event);
|
|
131 let touches = event.touches;
|
|
132 if( touches.length !== 1 )
|
|
133 return;
|
|
134 let touch = touches[0];
|
|
135 touchX = touch.clientX;
|
|
136 touchY = touch.clientY;
|
|
137 document.addEventListener('touchmove',onTouchMove);
|
|
138 document.addEventListener('touchend',onMouseUp);
|
|
139 }
|
|
140
|
|
141 dad.setDraggable = function(el) {
|
|
142 el.setAttribute('dad-drag','');
|
|
143 el.addEventListener('mousedown',onMouseDown);
|
|
144 el.addEventListener('touchstart',onTouchStart);
|
|
145 };
|
6
|
146
|
|
147 dad.setDropzone = function(el) {
|
|
148 el.setAttribute('dad-dropzone','');
|
|
149 };
|
3
|
150 }
|