Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop Jstree confirmation

I developped a confirmation alert that appears do you want to move this file or folder ?

if i click no: it return to default folder.

if yes it wil go to the new folder.

So my problem is that when i drag and drop a parent folder the confirmation message appears and if i click no the movement didn't stop,that's mean the folder will move under new folder

For example i have:

  • ROOT1
    • folder1
      • Child1
  • ROOT2

In my jstree I have Root1 and Root2 nodes which are the parent nodes So when i move child1 to Root2 it works fine. but when i move ROOT2 to ROOT1 it didn't stop the movement when i click no

This is my code:

// html demo
$('#tree').jstree({
"core" : {
"animation" : 0,
"check_callback" : true,
"data" : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson11", "parent" : "#", "text" : "Simple root 1 node" },
{ "id" : "ajson12", "parent" : "#", "text" : "Simple root 2 node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1","type" : "file" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2","type" : "file" },
]
},
  "types" : {
    "#" : {
      "max_children" : 1, 
      "max_depth" : 4, 
      "valid_children" : ["root"]
    },
    "root" : {
      "icon" : "/static/3.1.1/assets/images/tree_icon.png",
      "valid_children" : ["default"]
    },
    "default" : {
      "valid_children" : ["default","file"]
    },
    "file" : {
      "icon" : "glyphicon glyphicon-file",
      "valid_children" : []
    }
  },
"plugins" : [
"contextmenu", "dnd", "types"
]
});


var Parent = 0;
var newParent = 0;
var Pos = 0;
var newPos = 0;

$(document).on('dnd_start.vakata', function (event, data) {
    sel = "li#"+ data.data.nodes[0] +".jstree-node";
    Parent = $('#tree').jstree(true).get_node(data.data.nodes[0]).parent;
    Pos = $(sel).index();
});

$(document).on('dnd_stop.vakata', function (event, data) {
    node = data.data.origin.get_node(data.data.nodes[0]);
    if (node.type == "root")
    {
        return false;
    }

    if (confirm("Voulez vous vraiment deplacer le fichier ou le dossier ?") === false)
    {
        $('#tree').jstree(true).move_node(node,Parent,Pos);
        return false;
    }
    sel = "li#"+ data.data.nodes[0] +".jstree-node";
    newPos =  $(sel).index();
    newParent = node.parent; 
});

and this is the example

like image 811
Najd Mrabet Avatar asked May 22 '15 10:05

Najd Mrabet


1 Answers

No need to use the dnd_* events, you'd be better off with the check_callback function:

"check_callback" :  function (op, node, par, pos, more) {
    if ((op === "move_node" || op === "copy_node") && node.type && node.type == "root") {
        return false;
    }
    if ((op === "move_node" || op === "copy_node") && more && more.core && !confirm('Are you sure ...')) {
        return false;
    }
    return true;
},

Here is your updated demo: http://jsfiddle.net/bq8xme0y/6/

You may also benefit from setting the dnd.is_draggable function - to prevent certain nodes from being dragged (as it seems to me this is something else you need).

like image 85
vakata Avatar answered Sep 25 '22 12:09

vakata