Skip to content

Instantly share code, notes, and snippets.

@trafficonese
Last active January 29, 2020 12:18
Show Gist options
  • Save trafficonese/d326a2953766a371d91a0fc581fd3e56 to your computer and use it in GitHub Desktop.
Save trafficonese/d326a2953766a371d91a0fc581fd3e56 to your computer and use it in GitHub Desktop.
shinytree custom contextmenu delete only last level
library(shiny)
library(shinyTree)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
actionButton("run", "Change Contextmenu"),
hr(),
shinyTree("tree", contextmenu = TRUE)
),
server = function(input, output, session) {
observeEvent(input$run, {
runjs(HTML(' $("#tree").jstree().settings.contextmenu.items =
function(e,t){
return{
create:{
separator_before:!1,
separator_after:!0,
_disabled:!1,
label:"Create",
action:function(e){
var i=$.jstree.reference(e.reference),
t=i.get_node(e.reference);
i.create_node(t,{},"last",function(t){try{i.edit(t)}catch(e){setTimeout(function(){i.edit(t)},0)}})
}},
rename:{
separator_before:!1,
separator_after:!1,
_disabled:!1,
label:"Rename",
action:function(e){
var t=$.jstree.reference(e.reference),
i=t.get_node(e.reference);
t.edit(i)
}},
remove:{
separator_before:!1,
icon:!1,
separator_after:!1,
_disabled:function(e){
return !e.reference.parent().hasClass("jstree-leaf");
},
label:"Delete",
action:function(e){
var t=$.jstree.reference(e.reference),
i=t.get_node(e.reference);
t.is_selected(i)?t.delete_node(t.get_selected()):t.delete_node(i)
}},
ccp:{
separator_before:!0,
icon:!1,
separator_after:!1,
label:"Edit",
action:!1,
submenu:{
cut:{
separator_before:!1,
separator_after:!1,
label:"Cut",
action:function(e){
var t=$.jstree.reference(e.reference),
i=t.get_node(e.reference);
t.is_selected(i)?t.cut(t.get_top_selected()):t.cut(i)
}},
copy:{
separator_before:!1,
icon:!1,
separator_after:!1,
label:"Copy",
action:function(e){
var t=$.jstree.reference(e.reference),
i=t.get_node(e.reference);
t.is_selected(i)?t.copy(t.get_top_selected()):t.copy(i)
}},
paste:{
separator_before:!1,
icon:!1,
_disabled:function(e){
return!$.jstree.reference(e.reference).can_paste()
},
separator_after:!1,
label:"Paste",
action:function(e){
var t=$.jstree.reference(e.reference),
i=t.get_node(e.reference);
t.paste(i)
}
}
}
}}}'))
})
output$tree <- renderTree({
list(
root1 = "",
root2 = list(
SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
SubListB = list(leafA = "", leafB = "")
),
root3 = list(
SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
SubListB = list(leafA = "", leafB = "")
)
)
})
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment