annotate src/dad.js @ 11:80ef70ca5860

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