Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsTreeCopied Event not recorded #28

Closed
Giatomo opened this issue Feb 7, 2024 · 4 comments
Closed

jsTreeCopied Event not recorded #28

Giatomo opened this issue Feb 7, 2024 · 4 comments

Comments

@Giatomo
Copy link

Giatomo commented Feb 7, 2024

I need to record events occurring when copying nodes from a tree A to an empty tree B (only a root node) and when deleting them from B.
Everything seems to work fine until I want to add back a previously deleted node from B. There no jsTreeCopied event are detected.

Tree A -> Copy A1 -> Tree B (jsTreeCopied detected)
TreeB -> Delete A1 (jsTreeDeleted detected)
Tree A -> Copy A1 -> Tree B (jsTreeCopied NOT detected)

Exemple :

library(shiny)
library(jsTreeR)

visibleLayersTreeCheckCallback <- JS("function visibleLayersTreeCheckCallback (operation, node, parent, position, more) {
  if(operation === 'move_node' || operation === 'copy_node') {
    if(parent.id === '#' || parent.type === 'child') {
      return false; // prevent moving a child above or below the root
    }               // and moving inside a child
  }
  return true; // allow everything else
}")

sourceLayersTreeCheckCallback <- JS("function sourceLayersTreeCheckCallback (operation, node, parent, position, more) {
  if(operation === 'move_node' || operation === 'copy_node') {
      return false; // prevent moving a child above or below the root            # and moving inside a child
  }
  return true; // allow everything else
}")

visibleLayersTreeMenu<-JS("function visibleLayersTreeMenu(node){
  var tree = $('#visibleLayersTree').jstree(true);
  var items = {
    'rename' : {
      'label' : 'Rename',
      'action' : function (obj) { tree.edit(node); },
      'icon': 'glyphicon glyphicon-edit'
    },
    'delete' : {
      'label' : 'Delete',
      'action' : function (obj) { tree.delete_node(node); },
      'icon' : 'glyphicon glyphicon-trash'
    },
    'create' : {
      'label' : 'Create',
      'action' : function (obj) { tree.create_node(node); },
      'icon': 'glyphicon glyphicon-plus'
    }
  }
  return items;
}")

sourceLayersTreeMenu<-JS("function sourceLayersTreeMenu(node)
{
  var tree = $('#sourceLayersTree').jstree(true);
  var items = {
    'rename' : {
      'label' : 'Rename',
      'action' : function (obj) { tree.edit(node); },
      'icon': 'glyphicon glyphicon-edit'
    },
    'delete' : {
      'label' : 'Delete',
      'action' : function (obj) { tree.delete_node(node); },
      'icon' : 'glyphicon glyphicon-trash'
    },
    'create' : {
      'label' : 'Create',
      'action' : function (obj) { tree.create_node(node); },
      'icon': 'glyphicon glyphicon-plus'
    }
  }
  return items;
}")
dnd_list <- list(
    is_draggable = JS(
        "function(node) {",
        "  return node[0].type === 'child';",
        "}"
    ),
    always_copy = TRUE
)

dnd_visible <- list(
    is_draggable = JS(
        "function(node) {",
        "  return node[0].type === 'child';",
        "}"
    ),
    large_drop_target = TRUE
)

nodes_test <- list(
    list(
        text = "A",
        type = "root",
        children = list(
            list(
            text = "A1",
            type = "child"
            ),
            list(
            text = "A2",
            type = "child"
            )
        )
    ),
    list(
        text = "B",
        type = "root",
        children = list(
            list(
                text = "B1",
                type = "child"
            ),
            list(
                text = "B2",
                type = "child"
            )
        )
    )
)
ui <- shinyUI(
    fluidPage(
        fluidRow(
            column(
                width = 3,
                jstreeOutput("sourceLayersTree"),
            ),
            column(
                width = 3,
                jstreeOutput("visibleLayersTree")
            )
        ),
    )
)

server <- shinyServer(
    function(input, output, session){
        
        output[["sourceLayersTree"]] <- renderJstree({
            jstree(
                nodes_test,
                checkCallback = sourceLayersTreeCheckCallback,
                dragAndDrop = TRUE, dnd = dnd_list,
                selectLeavesOnly = TRUE,
                types = types,
                search = TRUE,
                unique = TRUE,
                sort = TRUE,
                wholerow = TRUE,
                contextMenu = list(items = sourceLayersTreeMenu)
            )
        })
        
        output[["visibleLayersTree"]] <- renderJstree({
            jstree(
                nodes = list(list(text = "Visible Layers", type = "root", state = list(opened = TRUE))),
                checkCallback = visibleLayersTreeCheckCallback,
                dragAndDrop = TRUE,dnd = dnd_visible,
                selectLeavesOnly = TRUE,
                types = types,
                search = TRUE,
                unique = TRUE,
                wholerow = TRUE,
                contextMenu = list(items = visibleLayersTreeMenu)
            )
        })
        observeEvent(input[["jsTreeCopied"]],
            {
                node_path <- input[["jsTreeCopied"]][["from"]][["path"]]
                cat("Copied\n")
            })

        observeEvent(input[["jsTreeDeleted"]],
            {
            cat("Deleted\n")
            })
    }
)

shinyApp(ui, server)

Expected output :
Copied Deleted Copied
Output :
Copied Deleted
Is there a way to make jsTreeCopied consistent ?

Best regards,
Thanks

@stla
Copy link
Owner

stla commented Feb 7, 2024

Hello,
I'm lost. Where does the Shiny value input[["jsTreeCopied"]] come from? I never defined that.

@stla
Copy link
Owner

stla commented Feb 7, 2024

Indeed, input[["jsTreeDeleted"]] is detected. I don't remember I implemented that... But with your app, I'm not able to copy a node.

@stla
Copy link
Owner

stla commented Feb 7, 2024

types is not defined in your code. I've defined it and now that works (I don't understand why that doesn't work otherwise....).

Ok, I've looked at my code and I've found the problem.

@stla stla closed this as completed in 33ec525 Feb 7, 2024
@stla
Copy link
Owner

stla commented Feb 7, 2024

This is fixed now. Thanks for the report 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants