Fancytree (Javascript) Toggle ON/OFF dnd5

158 views Asked by At

I'm trying to toggle ON/OFF the drag and drop feature. I'm attempting to do this by toggling "dnd5" from the extensions property, but it is not working.

I want to initialize the tree without drag and drop. Then the user can click a button to toggle the ability to drag and drop.

*Note: "dnd5" is the only extension I am using.

The tree initializes as planned, but the Drag and Drop feature never activates when I click the button. What am I doing wrong?

var $tree = $("#tree-drag");

function toggleDnD(isOn) {
   if (isOn) {
     $mainTree.extensions.push("dnd5");
   }  
   else {
      if ($mainTree.extensions.length > 0)
         $mainTree.extensions.pop();
   }
}

$("#btn-dnd-on").on("click", function() { toggleDnD(true); });
$("#btn-dnd-off").on("click", function() { toggleDnD(false); });

var $mainTree = $tree.fancytree({
            extensions: [],
            autoScroll: true,
            init: function (event, data) {

            },
            dnd5: {
                dragStart: function (node, data) {
                    data.effectAllowed = "all";
                    data.dropEffect = data.dropEffectSuggested;
                    return true;
                },
                dragEnter: function (node, data) {
                    return true;
                },
                dragOver: function (node, data) {
                    data.dropEffect = data.dropEffectSuggested;
                },
                dragDrop: function (node, data) {
                    var newNode,
                    transfer = data.dataTransfer,
                    sourceNodes = data.otherNodeList,
                    mode = data.dropEffect;

                    data.originalEvent.preventDefault();

                    if (data.hitMode === "after") {
                        sourceNodes.reverse();
                    }
                    if (data.otherNode) {
                        var sameTree = data.otherNode.tree === data.tree;

                        if (mode === "move") {
                            console.log("moved");                                
                            var _node = data.otherNode;

                            var childId = data.otherNode.data.id;
                            var parentId = data.node.data.id;

                            $(_node).attr({
                                "data-child": childId,
                                "data-parent": parentId
                            });
                            data.otherNode.moveTo(node, data.hitMode);
                            
                            //Assign the parent ID to the object. This will tell the system that a category has changed its home and needs its parent to be updated.
                            $("ul.ui-fancytree-source").find("li[data-id='" + childId + "']").attr("data-parent", parentId);


                        }
                        else {
                            newNode = data.otherNode.copyTo(node, data.hitMode);
                            if (mode === "link") {
                                newNode.setTitle("Link to " + newNode.title);
                            }
                            else {
                                newNode.setTitle("Copy of " + newNode.title);
                            }
                        }
                    }
                    else if (data.otherNodeData) {
                        node.addChild(data.otherNodeData, data.hitMode);
                    }
                    else if (data.files.length) {
                        for (var i = 0; i < data.files.length; i++) {
                            var file = data.files[i];
                            node.addNode({ title: "'" + file.name + "' (" + file.size + " bytes)" }, data.hitMode);
                        }
                    }
                    else {
                        node.addNode({ title: transfer.getData("text"), }, data.hitMode);
                    }
                    node.setExpanded();
                    $(".fancytree-statusnode-nodata").parent("li[role='treeitem']").remove();
                }
            },
            dblclick: function (event, data) {
                // Get the next table
                var id = data.node.data.id;
                getTable(id);
                setTreeNode(id);
                node.setExpanded(true);
                node.setActive(true);
            }

           });
1

There are 1 answers

1
mar10 On BEST ANSWER

Modifying the extensions list will not work after initialization.

Instead you could maintain a global flag and return false in the dragStart event:

  dnd5: {
    dragStart: function (node, data) {
      if( dndDisabled ) { return false; }
      data.effectAllowed = "all";
      data.dropEffect = data.dropEffectSuggested;
      return true;
    },

See also the docs.