Yang-Tang/shinyjqui

Regression bug: `dragcreate` event callback is not called when draggable is created dynamically

daattali opened this issue · 12 comments

This broke in commit 9f1e613fef43809d2e85253771c11747ccee6345 (it worked up until the commit before that one)

Example: here is a simple app that start off with a draggable element and every time you click the "Add" button it adds another draggable element. Every time a draggable is created, you should see an alert message. This works for draggable elements that are created on page load, but it does not work for draggables added with insertUI. It did work up until the commit mentioned above.

library(shiny)

ui <- fluidPage(
  tags$script("$(document).on('dragcreate', '.ui-draggable', function(e, ui) { alert('test'); });"),
  shinyjqui::jqui_draggable(div("test")),
  actionButton("add", "add")
)

server <- function(input, output, session) {
  observeEvent(input$add, {
    insertUI(
      "#add", "afterEnd", 
      shinyjqui::jqui_draggable(div(paste("test", input$add)))
    )
  })
}

shinyApp(ui, server)

This problem is happening because you changed from inserting the javascript to the head, to just inserting javascript code at a random location. This works for Rmd, but breaks in shiny apps. Here's a bug report about this rstudio/shiny#1545

I would wager that shiny is a more common usecase than Rmd so it would be worthwhile to revert this. You could also have some logic that conditionally adds to the head tag or not depending on whether it's a shiny app or not.

Could #18 be related aswell? It's a closed issue, that should be reopened I guess. At least both given examples dont work for me.

The issue I'm describing is happening only since February 2020 because of the issue I linked to above. Your issue is from 2 years ago, before this change happened, so I don't think it's related. But I haven't actually looked into your issue so I can't say for sure.

The original issue is from 2 years ago, I encountered a similar problem yesterday and added my example there.

You'll have to debug it to see if it's the same code that breaks it. The way I debugged this issue is that I kept going backwards in git commits until I found a commit where the code worked, and then I looked at exactly what changed in the commit and tried slowly adding those changes to the code until it broke. That's how I pinpointed the problem. I'm sorry but I dont have the bandwidth to troubleshoot your issue, from first glance it doesnt seem to be the same, but maybe it is if you look deeper.

@Yang-Tang Would you accept a PR that fixes this issue, if it means Rmd won't work? This is a tradeoff between rmd and shiny.

hi, @Yang-Tang is there hope to fix this regression? best regards

Hi @daattali @trafficonese @smouksassi , I finally got time to look into this. Apologize for such a long time delay in response.
As Dean mentioned, this is related to rstudio/shiny#1545 (comment), so I used the onload trick mentioned by bborgesr to get around. Now, with this commit af36f95, both the current issue and #18 should have been resolved.

@smouksassi you might be interested in this :)

absolutely looking forward to make gradientinput work again in ggquikeda CRAN ! awaiting merge to main thanks @Yang-Tang

Hi @smouksassi I have submitted the new shinyjqui to CRAN. Hope it works in you package. I'll close this issue now.

thanks a million ! have a great day !