|
| 1 | +library(jsTreeR) |
| 2 | + |
| 3 | +nodes <- list( |
| 4 | + list( |
| 5 | + text = "Menu", |
| 6 | + state = list(opened = TRUE), |
| 7 | + children = list( |
| 8 | + list( |
| 9 | + text = "A", |
| 10 | + type = "moveable", |
| 11 | + state = list(disabled = TRUE) |
| 12 | + ), |
| 13 | + list( |
| 14 | + text = "B", |
| 15 | + type = "moveable", |
| 16 | + state = list(disabled = TRUE) |
| 17 | + ), |
| 18 | + list( |
| 19 | + text = "C", |
| 20 | + type = "moveable", |
| 21 | + state = list(disabled = TRUE) |
| 22 | + ), |
| 23 | + list( |
| 24 | + text = "D", |
| 25 | + type = "moveable", |
| 26 | + state = list(disabled = TRUE) |
| 27 | + ) |
| 28 | + ) |
| 29 | + ), |
| 30 | + list( |
| 31 | + text = "Drag here:", |
| 32 | + type = "target", |
| 33 | + state = list(opened = TRUE) |
| 34 | + ) |
| 35 | +) |
| 36 | + |
| 37 | +checkCallback <- JS( |
| 38 | + "function(operation, node, parent, position, more) { console.log(node);", |
| 39 | + " if(operation === 'copy_node') {", |
| 40 | + " if(parent.id === '#' || node.parent !== 'j1_1' || parent.type !== 'target') {", |
| 41 | + " return false;", # prevent moving an item above or below the root |
| 42 | + " }", # and moving inside an item except a 'target' item |
| 43 | + " }", |
| 44 | + " return true;", # allow everything else |
| 45 | + "}" |
| 46 | +) |
| 47 | + |
| 48 | +dnd <- list( |
| 49 | + always_copy = TRUE, |
| 50 | + is_draggable = JS( |
| 51 | + "function(node) {", |
| 52 | + " return node[0].type === 'moveable';", |
| 53 | + "}" |
| 54 | + ) |
| 55 | +) |
| 56 | + |
| 57 | +customMenu <- JS( |
| 58 | + "function customMenu(node) {", |
| 59 | + " var tree = $('#mytree').jstree(true);", # 'mytree' is the Shiny id or the elementId |
| 60 | + " var items = {", |
| 61 | + " 'delete' : {", |
| 62 | + " 'label' : 'Delete',", |
| 63 | + " 'action' : function (obj) { tree.delete_node(node); },", |
| 64 | + " 'icon' : 'glyphicon glyphicon-trash'", |
| 65 | + " }", |
| 66 | + " }", |
| 67 | + " return items;", |
| 68 | + "}") |
| 69 | + |
| 70 | + |
| 71 | +jstree( |
| 72 | + nodes, dragAndDrop = TRUE, dnd = dnd, checkCallback = checkCallback, |
| 73 | + types = list(moveable = list(), target = list()), |
| 74 | + contextMenu = list(items = customMenu), |
| 75 | + elementId = "mytree" # don't use elementId in Shiny! use the Shiny id |
| 76 | +) |
0 commit comments