2

I am trying to define the new parent id for a node after a dragdrop event.

This function works fine assuming you drag a node to any level other than root

dragDrop: function(node, data) {
                /** This function MUST be defined to enable dropping of items on
                 *  the tree.
                 */
                data.otherNode.moveTo(node, data.hitMode);
                // Set Parent ID with id of parent
                data.otherNode.data.parent_id = node.data.id;
            }

However when dragging a node to the root it takes on an id from somewhere else (I haven't figured out the pattern yet).

I've been monitoring the fancytree node.key (which shows the key of the parent node) and it always outputs key_2 and never root_1 which is the value of root when I output the tree using the tree.toDict(true); function.

What is the proper way to find out if a node has been dragged to the root?

Tim
  • 3,091
  • 9
  • 48
  • 64
  • just to clarify, you want to know if the node you're dragging it to has no parent or is the top-level root node? `getLevel()` = 0 is the root – Chase Rocker Oct 03 '14 at 01:29
  • @ChaseRocker Unfortunately when I look at node.getLevel() I get level 1 for root and first level folders, it works correctly for levels 2, 3 and so on.. – Tim Oct 03 '14 at 05:50

2 Answers2

4

As mar10 mentioned, you will never get the invisible system root node as target node of a dragDrop event. Thus you'll never reach level 0.

Nodes are always dropped "over", "before" or "after" an other node, what is represented by node.hitMode.

If you just want to prevent to drop a node into the first level (I think that's what you call root level) and only "into" folders, restrict the hitMode like:

dragEnter: function(node, data) {
        if(node.folder == false) {
               return false;
        }
        return 'over';
}

If you want to check the first (root) level in the dragDrop event, you could do something like:

dragDrop: function(node, data) {
        if (node.getLevel() == 1 && (data.hitMode == 'after' || data.hitMode == 'before')) {
            console.log('dropped into first level');
        }
}

If your question targets something else, please edit your question or leave a comment.

joergwork
  • 301
  • 3
  • 6
0

This is the expected behvior:

var tree = $(":ui-fancytree").fancytree("getTree");
tree.rootNode.getLevel() // == 0 (invisble system root)
tree.rootNode.getFirstChild().getLevel() // == 1 (visible top level node)

You can drop before or over (i.a. as child of) another node. (You never get the root node as target, since you don't see it). If you log node.title, node.hitMode, and node.getLevel() things may become clear...

mar10
  • 14,320
  • 5
  • 39
  • 64