annotate src/dad.js @ 9:eee8862be4c7

update text
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 28 Apr 2023 21:07:17 -0600
parents 969d5980b375
children ad2f6525c4c5
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) {
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
26 {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
27 event.preventDefault();
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
28 let rect = dragging.getBoundingClientRect();
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
29 dragging.style.left = `${rect.x+event.movementX}px`;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
30 let y = rect.y + event.movementY;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
31 if( y < 0 ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
32 window.scrollBy( 0, y );
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
33 } else if( y + rect.height > window.innerHeight ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
34 window.scrollBy( 0, y + rect.height - window.innerHeight );
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
35 }
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
36 dragging.style.top = `${y}px`;
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
38 {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
39 let x = event.clientX;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
40 let y = event.clientY;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
41 if( !(dropzone && isIn(x,y,dropzone.getBoundingClientRect())) ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
42 if( dropzone ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
43 dad.onLeave({
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
44 original: original,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
45 dragging: dragging,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
46 dropzone: dropzone,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
47 mouseEvent: event,
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 dropzone = null;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
50 }
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
51 let dropzones = document.querySelectorAll('[dad-dropzone]');
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
52 for( let i=0; i<dropzones.length; i++ ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
53 let dz = dropzones[i];
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
54 if( dz === dragging )
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
55 continue;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
56 if( isIn(x,y,dz.getBoundingClientRect()) ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
57 dropzone = dz;
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
58 dad.onEnter({
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
59 original: original,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
60 dragging: dragging,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
61 dropzone: dropzone,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
62 mouseEvent: event,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
63 });
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) {
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
88 dad.onDrop({
5
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
89 original: original,
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
90 dragging: dragging,
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
91 dropzone: dropzone,
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
92 mouseEvent: event,
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
93 });
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
94 if( dropzone ) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
95 dad.onLeave({
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
96 original: original,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
97 dragging: dragging,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
98 dropzone: dropzone,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
99 mouseEvent: event,
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
100 });
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
101 }
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 original.removeAttribute('dad-original');
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
103 dragging.parentNode.removeChild(dragging);
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 document.removeEventListener('mousemove',onMouseMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 document.removeEventListener('mouseup',onMouseUp);
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('touchend',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 original.scrollIntoViewIfNeeded(false);
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
109 let droppedEvent = {
5
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
110 original: original,
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
111 };
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 original = null;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 dragging = null;
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
114 dropzone = null;
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
115 dad.onDropped(droppedEvent);
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 function start(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 original = dad.whatToDrag(event.target);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 dragging = original.cloneNode(true);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 original.setAttribute('dad-original','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 dragging.setAttribute('dad-dragging','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 let rect = original.getBoundingClientRect();
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 dragging.style.left = `${rect.x}px`;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 dragging.style.top = `${rect.y}px`;
8
969d5980b375 add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
126 dragging.style.width = `${rect.width}px`;
969d5980b375 add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 6
diff changeset
127 dragging.style.heigh = `${rect.heigh}px`;
4
0130ae25ef94 add drop
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
128 original.parentNode.appendChild(dragging);
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 function onMouseDown(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 start(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 document.addEventListener('mousemove',onMouseMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 document.addEventListener('mouseup',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 function onTouchStart(event) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 start(event);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 let touches = event.touches;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 if( touches.length !== 1 )
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 return;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 let touch = touches[0];
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 touchX = touch.clientX;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 touchY = touch.clientY;
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 document.addEventListener('touchmove',onTouchMove);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 document.addEventListener('touchend',onMouseUp);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 }
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 dad.setDraggable = function(el) {
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 el.setAttribute('dad-drag','');
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 el.addEventListener('mousedown',onMouseDown);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 el.addEventListener('touchstart',onTouchStart);
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 };
6
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
154
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
155 dad.setDropzone = function(el) {
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
156 el.setAttribute('dad-dropzone','');
4d699321068f add dropzones
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
157 };
3
6d5e4b38b4fb start drag
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 }