Search code examples
jqueryjquery-uijquery-ui-draggablejquery-ui-droppable

Disable parent droppable box while dragging from parent


http://jsfiddle.net/xSQBw/ demonstrates my problem. Notice that when you drag a box out of its parent and bring it back into the parent, then parent will accept the element. I need for the parent's droppable'ness to be disabled while the child is being dragged. Then I need the droppable'ness to be re-enabled after the child is dropped.

I tried using the start: and stop: methods for droppable, however, getting the correct element to disable/re-enable was tricky, kludgy, and didn't work anyway.

Here's an example of what I am attempting with the droppable handler:

$('<fieldset />',
{
    id: login,
    level: self.levelValue,
    class: 'member ' + employeeClass,
}).appendTo('#columnWrapDiv')
  .droppable(
    {
        hoverClass: 'hovered',
        drop: function(event,ui) { /* removed */});

And my draggable:

 $('.class')
  .draggable(
  {
      containment: '#ttkanbanDiv',
      cursor: 'crosshairs',
      revert: true,
      opacity: 0.4,
      start: 
        function(ui)
        {
            var id = $($($(ui).attr('srcElement'))
                        .parents()
                        .find('.ui-droppable')[0]).attr('id');
            $('#' + id).removeClass('ui-droppable');
        },
      stop:
        function(ui)
        {
            $($($(ui).attr('srcElement'))
                .parents()
                .find('.ui-droppable')[0])
                .addClass('ui-droppable');
        },
      zindex: 'auto',
  });

Solution

  • And the fix: http://jsfiddle.net/xSQBw/1/

    The specific code that makes it work (Thanks to Shoky in irc.freenode.net #jquery):

    $('#unassignedDiv,.member').droppable({
        hoverClass: 'hovered',
        accept: function(draggable) {
            draggable = draggable[0];
            var droppable = this;
            return !$.contains(droppable, draggable);
        },
        drop: ...