bevacqua / dragula

:ok_hand: Drag and drop so simple it hurts
https://bevacqua.github.io/dragula/
MIT License
22.01k stars 1.97k forks source link

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

Open maurice-bva opened 7 years ago

maurice-bva commented 7 years ago

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.

cmddavid commented 6 years ago

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.

pistou commented 3 years ago

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.