annotate src/dad.js @ 6:4d699321068f

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