-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag_drop.js
96 lines (81 loc) · 3.19 KB
/
drag_drop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//6-t Drag and drop list
// Function to enable drag and drop functionality
import Sortable from "sortablejs";
function dragDrop() {
// Get all elements with class "todo"
const todos = document.querySelectorAll(".todo");
// Get the container element with class "todos"
const todoContainer = document.querySelector(".todos");
new Sortable(todoContainer, {
animation: 150,
ghostClass: "dragging",
dragClass: "dragging",
delay: 150,
group: "order",
store: {
//guardar el orden en un array
set: (sortable) => {
//toArray funcion de la libreria
const order = sortable.toArray();
localStorage.setItem(sortable.options.group.name, order.join(" ")) ||
[];
},
get: (sortable) => {
const order = localStorage.getItem(sortable.options.group.name);
return order ? order.split(" ") : [];
},
},
});
// // Add event listeners to each "todo" element for drag and drop
// todos.forEach((todo) => {
// // When dragging starts, add a "dragging" class to the element
// todo.addEventListener("dragstart", () => {
// todo.classList.add("dragging");
// });
// // When dragging ends, remove the "dragging" class from the element
// todo.addEventListener("dragend", () => {
// todo.classList.remove("dragging");
// });
// });
// // Add a dragover event listener to the "todos" container
// todoContainer.addEventListener("dragover", (e) => {
// // Prevent the default behavior to allow dropping elements
// e.preventDefault();
// // Get the "todos" container again
// const todoContainer = document.querySelector(".todos");
// // Get the element below the current mouse position
// const afterElement = getDragAfterElement(todoContainer, e.clientY);
// // Get the element being dragged
// const draggable = document.querySelector(".dragging");
// // If there's no element below, append the dragged element to the end
// if (afterElement == null) {
// todoContainer.appendChild(draggable);
// } else {
// // Otherwise, insert the dragged element before the element below
// todoContainer.insertBefore(draggable, afterElement);
// }
// });
// // Function to find the element below the current mouse position
// function getDragAfterElement(container, y) {
// // Get all draggable elements inside the container (excluding the one being dragged)
// const draggableElements = [
// ...container.querySelectorAll(".todo:not(.dragging)"),
// ];
// // Find the closest element to the current mouse position
// return draggableElements.reduce(
// (closest, child) => {
// const box = child.getBoundingClientRect();
// const offset = y - box.top - box.height / 2;
// // If the element is closer to the mouse position, update the closest element
// if (offset < 0 && offset > closest.offset) {
// return { offset: offset, element: child };
// } else {
// return closest;
// }
// },
// { offset: Number.NEGATIVE_INFINITY }
// ).element;
// }
}
// Export the dragDrop function for use in other parts of the code
export { dragDrop };