2022-04-15 14:24:30 +00:00
|
|
|
import { Directive } from 'vue'
|
2022-04-30 13:20:47 +00:00
|
|
|
import { $ } from '@/utils'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
export const droppable: Directive = {
|
2022-04-30 13:20:47 +00:00
|
|
|
created: (el: HTMLElement, binding) => {
|
|
|
|
const acceptsDrops = binding.arg === undefined || Boolean(binding.arg)
|
|
|
|
|
|
|
|
if (!acceptsDrops) {
|
|
|
|
return
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 16:38:33 +00:00
|
|
|
el.addEventListener('dragenter', (event: DragEvent) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
event.preventDefault()
|
|
|
|
$.addClass(el, 'droppable')
|
|
|
|
event.dataTransfer!.dropEffect = 'move'
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2022-04-30 13:20:47 +00:00
|
|
|
el.addEventListener('dragover', (event: DragEvent) => event.preventDefault())
|
2022-04-25 16:38:33 +00:00
|
|
|
el.addEventListener('dragleave', () => $.removeClass(el, 'droppable'))
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-25 16:38:33 +00:00
|
|
|
el.addEventListener('drop', (event: DragEvent) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
$.removeClass(el, 'droppable')
|
2022-04-30 13:20:47 +00:00
|
|
|
binding.value(event)
|
2022-04-15 14:24:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|