Script.aculo.us Drag 'n' Drop - Revert onEnd condition

416 Views Asked by At

I'm trying to revert a draggable if a condition returns false. So for instance, I'd like to do the following:

new Draggable('myelement', {
    onStart: function() {
      // do something
    },
    onEnd: function() {
      var condition = getConditionVal();
      if (!condition) revert to original position
      else {
         // do something else
      }
    }
});

Would this be possible? Not sure if "droppables" would work in this case since the droppable area changes dynamically.

2

There are 2 best solutions below

0
On

Scriptaculous's drag/drop code wasn't designed to have conditional revert. You can have revert or no revert. That's all, sadly.

This feature has been requested many times but scripty/prototype has waned in popularity over the years, so it's doubtful this feature will ever be added.

0
On

Scriptaculous drag/drop is designed to have all kinds of fancy stuff easily added. Of course you can edit the revert option any time.

To change the value of the revert-option of an draggable, just reset the revert-option:

var myDraggable = new Draggable('myelement', {
    onStart: function() {
      // do something
    },
    onEnd: function() {
      var condition = getConditionVal();
      if (!condition){
          myDraggable.options.revert = true;
      }
      else {
          myDraggable.options.revert = false;
          // do something else
      }
    };
});

Scriptaculous does the revert right after the onEnd event call, which gives us the possibility of changing it before it will be executed.