Mercurial Hosting > dad
diff 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 |
line wrap: on
line diff
--- a/src/dad.js Sun Apr 23 17:14:52 2023 -0600 +++ b/src/dad.js Sun Apr 23 18:34:12 2023 -0600 @@ -9,24 +9,64 @@ }; dad.onDrop = function(event) {}; dad.onDropped = function(event) {}; + dad.onEnter = function(event) {}; + dad.onLeave = function(event) {}; let original = null; let dragging = null; + let dropzone = null; let touchX, touchY; + function isIn(x,y,rect) { + return rect.x <= x && x <= rect.x+rect.width && rect.y <= y && y <= rect.y+rect.height; + } + function onMouseMove(event) { //console.log(event); - event.preventDefault(); - let rect = dragging.getBoundingClientRect(); - dragging.style.left = `${rect.x+event.movementX}px`; - let y = rect.y + event.movementY; - if( y < 0 ) { - window.scrollBy( 0, y ); - } else if( y + rect.height > window.innerHeight ) { - window.scrollBy( 0, y + rect.height - window.innerHeight ); + { + event.preventDefault(); + let rect = dragging.getBoundingClientRect(); + dragging.style.left = `${rect.x+event.movementX}px`; + let y = rect.y + event.movementY; + if( y < 0 ) { + window.scrollBy( 0, y ); + } else if( y + rect.height > window.innerHeight ) { + window.scrollBy( 0, y + rect.height - window.innerHeight ); + } + dragging.style.top = `${y}px`; } - dragging.style.top = `${y}px`; + { + let x = event.clientX; + let y = event.clientY; + if( !(dropzone && isIn(x,y,dropzone.getBoundingClientRect())) ) { + if( dropzone ) { + dad.onLeave({ + original: original, + dragging: dragging, + dropzone: dropzone, + mouseEvent: event, + }); + dropzone = null; + } + let dropzones = document.querySelectorAll('[dad-dropzone]'); + for( let i=0; i<dropzones.length; i++ ) { + let dz = dropzones[i]; + if( dz === dragging ) + continue; + if( isIn(x,y,dz.getBoundingClientRect()) ) { + dropzone = dz; + dad.onEnter({ + original: original, + dragging: dragging, + dropzone: dropzone, + mouseEvent: event, + }); + break; + } + } + } + } } function onTouchMove(event) { @@ -36,6 +76,8 @@ let touch = touches[0]; let x = touch.clientX; let y = touch.clientY; + event.clientX = x; + event.clientY = y; event.movementX = x - touchX; event.movementY = y - touchY; touchX = x; @@ -48,8 +90,17 @@ dad.onDrop({ original: original, dragging: dragging, + dropzone: dropzone, mouseEvent: event, }); + if( dropzone ) { + dad.onLeave({ + original: original, + dragging: dragging, + dropzone: dropzone, + mouseEvent: event, + }); + } original.removeAttribute('dad-original'); dragging.parentNode.removeChild(dragging); document.removeEventListener('mousemove',onMouseMove); @@ -62,6 +113,7 @@ }; original = null; dragging = null; + dropzone = null; dad.onDropped(droppedEvent); } @@ -100,4 +152,8 @@ el.addEventListener('mousedown',onMouseDown); el.addEventListener('touchstart',onTouchStart); }; + + dad.setDropzone = function(el) { + el.setAttribute('dad-dropzone',''); + }; }