dragula icon indicating copy to clipboard operation
dragula copied to clipboard

Change 'revertOnSpill' and 'removeOnSpill' option to functions instead of a static variable

Open maurice-bva opened this issue 7 years ago • 2 comments

Feature request

I was looking for a way to set 'revertOnSpill' and 'removeOnSpill' for each container.

For example I have two containers. The right container holds a list of items. and the left container only holds items that are dragged there.

Function for the right list When I drag an item from the right list to the left it should be moved to the left. When I drag an item from the right list to an area that is not a container the item should return back to it's original position. Items may not be sort-able.

Function for the left list: When I drag an item from the left list to an area that is not a container the item should be removed ( drake.on('removed', ....) ). Also items in this list should be sort-able.

My proposal would be to change both 'revertOnSpill' and 'removeOnSpill' that they can be a function with the 'el' beeing dragged and 'source' the container it is dragged from. That way you can implement a separate behavior for each container. This way I can easily check the source and decide if it may remove or not, or reverted or not.

maurice-bva avatar Jul 27 '17 15:07 maurice-bva

I was kind of surprised it did not work this way already. As most other options allow us to use a function. It gives a lot more flexibility.

cmddavid avatar Jul 30 '18 14:07 cmddavid

At first, it sounded like a pretty simple change:

// In /dist/dragula.js
// line 75: if (o.removeOnSpill === void 0) { o.removeOnSpill = false; } 
if (o.removeOnSpill === void 0) { o.removeOnSpill = never; } // default value is now a function that returns false

// line 286:  else if (o.removeOnSpill)
else if (o.removeOnSpill()) // check the function's result instead of the variable ; add params if needed

I tried it myself but the events seem linked to the whole page (document.documentElement) hence in case of multiple instance of Dragula the removeOnSpill function is triggered multiple times. This is may be an issue, like in my case the confirm box would appear multiple times:

removeOnSpill: () => confirm("Are you sure you want to delete this element?")

This looks like it implies a more-than-expected rebuild of the way Dragula's events work.

pistou avatar Apr 21 '21 10:04 pistou