2023-04-28 21:20:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DragDropManager {
|
|
|
|
dragContainer;
|
2023-04-30 13:38:30 +02:00
|
|
|
wrapper;
|
|
|
|
pageDirection;
|
2023-04-28 21:20:56 +02:00
|
|
|
movePageTo;
|
|
|
|
pageDragging;
|
|
|
|
draggelEl;
|
|
|
|
draggedImageEl;
|
|
|
|
hoveredEl;
|
2023-04-30 13:38:30 +02:00
|
|
|
endInsertionElement;
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2023-04-30 13:38:30 +02:00
|
|
|
constructor(id, wrapperId) {
|
2023-04-28 21:20:56 +02:00
|
|
|
this.dragContainer = document.getElementById(id);
|
2023-04-30 13:38:30 +02:00
|
|
|
this.pageDirection = document.documentElement.getAttribute("lang-direction");
|
|
|
|
this.wrapper = document.getElementById(wrapperId);
|
2023-04-28 21:20:56 +02:00
|
|
|
this.pageDragging = false;
|
|
|
|
this.hoveredEl = undefined;
|
|
|
|
this.draggelEl = undefined
|
|
|
|
this.draggedImageEl = undefined;
|
|
|
|
|
2023-04-30 13:38:30 +02:00
|
|
|
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 = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-arrow-down" viewBox="0 0 16 16">
|
|
|
|
<path d="M8.5 6.5a.5.5 0 0 0-1 0v3.793L6.354 9.146a.5.5 0 1 0-.708.708l2 2a.5.5 0 0 0 .708 0l2-2a.5.5 0 0 0-.708-.708L8.5 10.293V6.5z"/>
|
|
|
|
<path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/>
|
|
|
|
</svg>`
|
|
|
|
this.endInsertionElement = div;
|
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
this.startDraggingPage = this.startDraggingPage.bind(this);
|
|
|
|
this.onDragEl = this.onDragEl.bind(this);
|
|
|
|
this.stopDraggingPage = this.stopDraggingPage.bind(this);
|
2023-04-30 13:38:30 +02:00
|
|
|
|
|
|
|
this.adapt(div);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 13:38:30 +02:00
|
|
|
startDraggingPage(div,) {
|
2023-04-28 21:20:56 +02:00
|
|
|
this.pageDragging = true;
|
|
|
|
this.draggedEl = div;
|
2023-04-30 13:38:30 +02:00
|
|
|
const img = div.querySelector('img');
|
|
|
|
div.classList.add('drag-manager_dragging');
|
|
|
|
const imageSrc = img.src;
|
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
const imgEl = document.createElement('img');
|
|
|
|
imgEl.classList.add('dragged-img');
|
|
|
|
imgEl.src = imageSrc;
|
|
|
|
this.draggedImageEl = imgEl;
|
2023-04-30 13:38:30 +02:00
|
|
|
imgEl.style.left = screenX;
|
|
|
|
imgEl.style.right = screenY;
|
|
|
|
imgEl.style.transform = `rotate(${img.style.rotate === '' ? '0deg' : img.style.rotate}) translate(-50%, -50%)`;
|
2023-04-28 21:20:56 +02:00
|
|
|
this.dragContainer.appendChild(imgEl);
|
2023-04-30 13:38:30 +02:00
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
window.addEventListener('mouseup', this.stopDraggingPage)
|
2023-04-28 21:20:56 +02:00
|
|
|
window.addEventListener('mousemove', this.onDragEl)
|
2023-04-30 13:38:30 +02:00
|
|
|
this.wrapper.classList.add('drag-manager_dragging-container');
|
|
|
|
this.wrapper.appendChild(this.endInsertionElement);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onDragEl(mouseEvent) {
|
|
|
|
const { clientX, clientY } = mouseEvent;
|
|
|
|
if(this.draggedImageEl) {
|
|
|
|
this.draggedImageEl.style.left = `${clientX}px`;
|
2023-04-30 13:38:30 +02:00
|
|
|
this.draggedImageEl.style.top = `${clientY}px`;
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stopDraggingPage() {
|
|
|
|
window.removeEventListener('mousemove', this.onDragEl);
|
2023-04-30 13:38:30 +02:00
|
|
|
this.wrapper.classList.remove('drag-manager_dragging-container');
|
|
|
|
this.wrapper.removeChild(this.endInsertionElement);
|
2023-04-29 12:43:12 +02:00
|
|
|
window.removeEventListener('mouseup', this.stopDraggingPage)
|
2023-04-28 21:20:56 +02:00
|
|
|
this.draggedImageEl = undefined;
|
|
|
|
this.pageDragging = false;
|
2023-04-30 13:38:30 +02:00
|
|
|
this.draggedEl.classList.remove('drag-manager_dragging');
|
|
|
|
this.hoveredEl?.classList.remove('drag-manager_draghover');
|
2023-04-28 21:20:56 +02:00
|
|
|
this.dragContainer.childNodes.forEach((dragChild) => {
|
|
|
|
this.dragContainer.removeChild(dragChild);
|
|
|
|
})
|
2023-04-30 13:38:30 +02:00
|
|
|
if(!this.hoveredEl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this.hoveredEl === this.endInsertionElement) {
|
|
|
|
this.movePageTo(this.draggedEl);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-28 21:20:56 +02:00
|
|
|
this.movePageTo(this.draggedEl, this.hoveredEl);
|
|
|
|
}
|
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
setActions({ movePageTo }) {
|
|
|
|
this.movePageTo = movePageTo;
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2023-04-29 12:43:12 +02:00
|
|
|
adapt(div) {
|
2023-04-28 21:20:56 +02:00
|
|
|
const onDragStart = () => {
|
2023-04-30 13:38:30 +02:00
|
|
|
this.startDraggingPage(div);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const onMouseEnter = () => {
|
|
|
|
if (this.pageDragging) {
|
|
|
|
this.hoveredEl = div;
|
2023-04-30 13:38:30 +02:00
|
|
|
div.classList.add('drag-manager_draghover');
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onMouseLeave = () => {
|
|
|
|
this.hoveredEl = undefined
|
2023-04-30 13:38:30 +02:00
|
|
|
div.classList.remove('drag-manager_draghover');
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
div.addEventListener('dragstart', onDragStart);
|
|
|
|
div.addEventListener('mouseenter', onMouseEnter);
|
|
|
|
div.addEventListener('mouseleave', onMouseLeave);
|
2023-04-29 12:43:12 +02:00
|
|
|
|
|
|
|
return div;
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DragDropManager;
|