Skip to content

Commit

Permalink
dispatch sortenter (#494)
Browse files Browse the repository at this point in the history
* dispatches sortenter event first time dragged item enters a container;
makes sure original target is sent with the sortenter and sortstart event;

* fix styling issues
  • Loading branch information
christophe-g authored and lukasoppermann committed Mar 3, 2019
1 parent b9ff912 commit 728eece
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/html5sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ let originIndex
let originElementIndex
let originItemsBeforeUpdate

// Previous Sortable Container - we dispatch as sortenter event when a
// dragged item enters a sortableContainer for the first time
let previousContainer

// Destination List - data from before any item was changed
let destinationItemsBeforeUpdate

Expand Down Expand Up @@ -320,7 +324,8 @@ export default function sortable (sortableElements, options: object|string|undef
index: originIndex,
container: originContainer
},
item: dragging
item: dragging,
originalTarget: target
}
}))
})
Expand All @@ -330,13 +335,32 @@ export default function sortable (sortableElements, options: object|string|undef
*/
_on(sortableElement, 'dragenter', (e) => {
const target = getEventTarget(e)
if (target.isSortable === true) {
return
}
const sortableContainer = findSortable(target, e)
destinationItemsBeforeUpdate = _filter(sortableContainer.children, _data(sortableContainer, 'items'))
.filter(item => item !== store(sortableElement).placeholder)

if (sortableContainer && sortableContainer !== previousContainer) {
destinationItemsBeforeUpdate = _filter(sortableContainer.children, _data(sortableContainer, 'items'))
.filter(item => item !== store(sortableElement).placeholder)

sortableContainer.dispatchEvent(new CustomEvent('sortenter', {
detail: {
origin: {
elementIndex: originElementIndex,
index: originIndex,
container: originContainer
},
destination: {
container: sortableContainer,
itemsBeforeUpdate: destinationItemsBeforeUpdate
},
item: dragging,
originalTarget: target
}
}))
}

previousContainer = sortableContainer
})

/*
* Dragend Event - https://developer.mozilla.org/en-US/docs/Web/Events/dragend
* Fires each time dragEvent end, or ESC pressed
Expand Down Expand Up @@ -377,6 +401,7 @@ export default function sortable (sortableElements, options: object|string|undef
}
}))

previousContainer = null
dragging = null
draggingHeight = null
})
Expand Down

0 comments on commit 728eece

Please sign in to comment.