diff src/dad.js @ 4:0130ae25ef94

add drop
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 22 Apr 2023 21:17:53 -0600
parents 6d5e4b38b4fb
children 1293cb0d30da
line wrap: on
line diff
--- a/src/dad.js	Fri Apr 21 00:58:31 2023 -0600
+++ b/src/dad.js	Sat Apr 22 21:17:53 2023 -0600
@@ -3,10 +3,12 @@
 let dad = {};
 
 {
-	// override this if needed
+	// override these if needed
 	dad.whatToDrag = function(draggable) {
 		return draggable;
 	};
+	dad.onDrop = function(event) {};
+	dad.onDropped = function(event) {};
 
 
 	let original = null;
@@ -43,15 +45,24 @@
 
 	function onMouseUp(event) {
 		//console.log(event);
+		dad.onDrop({
+			draggable: original,
+			dragged: dragging,
+			mouseEvent: event,
+		});
 		original.removeAttribute('dad-original');
-		document.body.removeChild(dragging);
+		dragging.parentNode.removeChild(dragging);
 		document.removeEventListener('mousemove',onMouseMove);
 		document.removeEventListener('mouseup',onMouseUp);
 		document.removeEventListener('mousemove',onMouseMove);
 		document.removeEventListener('touchend',onMouseUp);
 		original.scrollIntoViewIfNeeded(false);
+		let droppedEvent = {
+			draggable: original,
+		};
 		original = null;
 		dragging = null;
+		dad.onDropped(droppedEvent);
 	}
 
 	function start(event) {
@@ -63,7 +74,7 @@
 		let rect = original.getBoundingClientRect();
 		dragging.style.left = `${rect.x}px`;
 		dragging.style.top = `${rect.y}px`;
-		document.body.appendChild(dragging);
+		original.parentNode.appendChild(dragging);
 	}
 
 	function onMouseDown(event) {