class DragDropManager { dragContainer; wrapper; pageDirection; movePageTo; pageDragging; draggelEl; draggedImageEl; hoveredEl; endInsertionElement; constructor(id, wrapperId) { this.dragContainer = document.getElementById(id); this.pageDirection = document.documentElement.getAttribute("lang-direction"); this.wrapper = document.getElementById(wrapperId); this.pageDragging = false; this.hoveredEl = undefined; this.draggelEl = undefined this.draggedImageEl = undefined; var styleElement = document.createElement('link'); styleElement.rel = 'stylesheet'; styleElement.href = 'css/dragdrop.css' document.head.appendChild(styleElement); const div = document.createElement('div'); div.classList.add('drag-manager_endpoint'); div.innerHTML = ` ` this.endInsertionElement = div; this.startDraggingPage = this.startDraggingPage.bind(this); this.onDragEl = this.onDragEl.bind(this); this.stopDraggingPage = this.stopDraggingPage.bind(this); this.adapt(div); } startDraggingPage(div,) { this.pageDragging = true; this.draggedEl = div; const img = div.querySelector('img'); div.classList.add('drag-manager_dragging'); const imageSrc = img.src; const imgEl = document.createElement('img'); imgEl.classList.add('dragged-img'); imgEl.src = imageSrc; this.draggedImageEl = imgEl; imgEl.style.left = screenX; imgEl.style.right = screenY; imgEl.style.transform = `rotate(${img.style.rotate === '' ? '0deg' : img.style.rotate}) translate(-50%, -50%)`; this.dragContainer.appendChild(imgEl); window.addEventListener('mouseup', this.stopDraggingPage) window.addEventListener('mousemove', this.onDragEl) this.wrapper.classList.add('drag-manager_dragging-container'); this.wrapper.appendChild(this.endInsertionElement); } onDragEl(mouseEvent) { const { clientX, clientY } = mouseEvent; if(this.draggedImageEl) { this.draggedImageEl.style.left = `${clientX}px`; this.draggedImageEl.style.top = `${clientY}px`; } } stopDraggingPage() { window.removeEventListener('mousemove', this.onDragEl); this.wrapper.classList.remove('drag-manager_dragging-container'); this.wrapper.removeChild(this.endInsertionElement); window.removeEventListener('mouseup', this.stopDraggingPage) this.draggedImageEl = undefined; this.pageDragging = false; this.draggedEl.classList.remove('drag-manager_dragging'); this.hoveredEl?.classList.remove('drag-manager_draghover'); this.dragContainer.childNodes.forEach((dragChild) => { this.dragContainer.removeChild(dragChild); }) if(!this.hoveredEl) { return; } if(this.hoveredEl === this.endInsertionElement) { this.movePageTo(this.draggedEl); return; } this.movePageTo(this.draggedEl, this.hoveredEl); } setActions({ movePageTo }) { this.movePageTo = movePageTo; } adapt(div) { const onDragStart = () => { this.startDraggingPage(div); } const onMouseEnter = () => { if (this.pageDragging) { this.hoveredEl = div; div.classList.add('drag-manager_draghover'); } } const onMouseLeave = () => { this.hoveredEl = undefined div.classList.remove('drag-manager_draghover'); } div.addEventListener('dragstart', onDragStart); div.addEventListener('mouseenter', onMouseEnter); div.addEventListener('mouseleave', onMouseLeave); return div; } } export default DragDropManager;