I have draggable li
elements nested in a ul
in turn nested in a div
, as seen below:
<div class='group'>
<div class='header'>
// some stuff here
</div>
<ul>
<li draggable='true'>
Stuff I want to drag and drop to another div.group
</li>
</ul>
</div>
There are multiple of these div
elements and I am trying to implement a drag & drop functionality to move the li
elements of one div
group to another.
I have hooked up the ondragenter
, ondragleave
callbacks here:
// controller using mithril.js
ctrl.onDragLeave = function () {
return function (event) {
var target;
// Using isDropzone to recursively search for the appropriate div.group
// parent element, as event.target is always the children inside it
if ((target = isDropzone(event.target)) != null) {
target.style.background = "";
}
}
};
ctrl.onDragEnter = function () {
return function (event) {
var target;
if ((target = isDropzone(event.target)) != null) {
target.style.background = "purple";
}
};
};
function isDropzone(elem){
if(elem == null){
return null;
}
return elem.className == 'group' ? elem: isDropzone(elem.parentNode)
}
The problem comes when the event.target
of the callbacks are always the nested child elements inside the div
, such as li
, and thus the callbacks are constantly fired. In this case I'm changing the color of the div.group
with my callbacks, resulting in the div.group
blinking undesirably.
Is there a way to delegate events and only allow the div
grand parent of li
to handle the events? Or any other way to work around this?
EDIT: Would still love to find out if there's a way to do this, but right now I'm using the workaround I found here.