Skip to content

Commit

Permalink
Create working example with js
Browse files Browse the repository at this point in the history
Drag and drop with javascript
  • Loading branch information
SimonLab committed Oct 24, 2022
1 parent 15cd7a0 commit eeba5be
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// We import the CSS which is extracted to its own file by esbuild.
// Remove this line if you add a your own CSS build pipeline (e.g postcss).

// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// If you want to use Phoenix channels, run `mix help phx.gen.channeItem 2l`
// to get started and then uncomment the line below.
// import "./user_socket.js"

Expand Down Expand Up @@ -62,30 +62,35 @@ draggables.forEach(dragable => {

listItems.addEventListener('dragover', e => {
e.preventDefault()
const draggable = document.querySelector('.dragging')
const nextItem = getNextItem(e.clientY)
console.log(nextItem)
if (nextItem == null) {
listItems.appendChild(draggable)
const dragged = document.querySelector('.dragging')
const overItem = getOverItem(e.clientY)
const moving = direction(dragged, overItem)
if (moving == "down") {
listItems.insertBefore(dragged, overItem.nextSibling)
} else {
listItems.insertBefore(draggable, nextItem)
listItems.insertBefore(dragged, overItem)
}
})

function getNextItem(y) {
const draggables = [...document.querySelectorAll(".draggable:not(.dragging)")]
return draggables.reduce(function(nextItem, currentItem) {
const box = currentItem.getBoundingClientRect()
const offset = y - (box.y - (box.height / 2))

if (offset < 0 && offset > nextItem.offset) {
return {offset: offset, element: currentItem}
} else {
return nextItem
}
}, {offset: Number.NEGATIVE_INFINITY}).element

function getOverItem(y) {
const draggables = [...document.querySelectorAll(".draggable")]
return draggables.find( item => {
const box = item.getBoundingClientRect()
return y > box.top && y < box.bottom
})
}

function direction(dragged, overItem) {
const draggables = [...document.querySelectorAll(".draggable")]
if (draggables.indexOf(dragged) < draggables.indexOf(overItem)) {
return "down"
} else {
return "up"
}
}





Expand Down

0 comments on commit eeba5be

Please sign in to comment.