diff --git a/DESCRIPTION b/DESCRIPTION index 0747a22..95c7294 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: shinyjs Title: Easily Improve the User Experience of Your Shiny Apps in Seconds -Version: 2.1.0.9000 +Version: 2.1.0.9001 Authors@R: person("Dean", "Attali", email = "daattali@gmail.com", role = c("aut", "cre"), @@ -21,7 +21,7 @@ Imports: shiny (>= 1.0.0), utils Suggests: - htmltools (>= 0.2.9), + htmltools (>= 0.3.5), knitr (>= 1.7), rmarkdown, shinyAce, @@ -31,5 +31,5 @@ Suggests: License: MIT + file LICENSE VignetteBuilder: knitr Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.1 +RoxygenNote: 7.2.3 Encoding: UTF-8 diff --git a/NEWS.md b/NEWS.md index 110fb30..2b0d1ec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ - Replace `list(...)` with `rlang::list2(...)` which is more flexible and allows you to use trailing commas without error - Added tests using {shinytest2} - Officially removed all `colourInput()` related functions, after 5 years of being defunct (they exist in the {colourpicker} package) +- Replaced `addResourcePath()` with `htmlDependency()` which is more robust and useful for package developers (#260) # shinyjs 2.1.0 (2021-12-20) diff --git a/R/useShinyjs.R b/R/useShinyjs.R index 24dee5c..41c4ab8 100644 --- a/R/useShinyjs.R +++ b/R/useShinyjs.R @@ -22,9 +22,7 @@ #' \href{https://github.com/daattali/shinyjs}{README} online to learn #' how to use shinyjs in these apps. #' @return Scripts that \code{shinyjs} requires that are automatically inserted -#' to the app's \code{
} tag. A side effect of calling this function is that -#' a \code{shinyjs} directory is added as a resource path using -#' [shiny::addResourcePath()]. +#' to the app's \code{} tag. #' @examples #' if (interactive()) { #' library(shiny) @@ -65,10 +63,7 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) { # all the default shinyjs methods that should be forwarded to javascript jsFuncs <- shinyjsFunctionNames("core") - - # grab the file with all the default shinyjs javascript functions - shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs")) - jsFile <- file.path("shinyjs", "shinyjs-default-funcs.js") + jsCodeFuncs <- jsFuncTemplate(jsFuncs) # JavaScript to include to turn debug mode on/off (used for seeing more messages) if (debug) { @@ -81,8 +76,21 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) { initJS <- paste0(initJS, "shinyjs.version = '", as.character(utils::packageVersion("shinyjs")), "';") # include CSS for hiding elements - initCSS <- inlineCSS(".shinyjs-hide { display: none !important; }") + initCSS <- ".shinyjs-hide { display: none !important; }" + + shinyjsContent <- htmltools::htmlDependency( + name = "shinyjs-binding", + version = as.character(utils::packageVersion("shinyjs")), + package = "shinyjs", + src = "srcjs", + script = "shinyjs-default-funcs.js", + head = paste0( + "", + "" + ) + ) - # set up the message handlers and add some initial JS and CSS - setupJS(jsFuncs, jsFile, initJS, initCSS) + shiny::tagList( + shinyjsContent + ) } diff --git a/R/utils.R b/R/utils.R index d56858f..2a96636 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,6 +17,18 @@ getSession <- function() { session } +jsFuncTemplate <- function(funcs) { + tpl <- paste0( + "Shiny.addCustomMessageHandler('shinyjs-%s', function(params) {", + " shinyjs.debugMessage('shinyjs: calling function \"%s\" with parameters:');", + " shinyjs.debugMessage(params);", + " shinyjs.%s(params);", + "});") + controllers <- lapply(funcs, function(x) { sprintf(tpl, x, x, x) }) + controllers <- paste(controllers, collapse = "\n") + controllers +} + # set up some javascript functions to work with shinyjs and any other resources setupJS <- function(jsFuncs, script, text, ...) { # add a shiny message handler binding for each supported method diff --git a/man/useShinyjs.Rd b/man/useShinyjs.Rd index a735de9..b10a67a 100644 --- a/man/useShinyjs.Rd +++ b/man/useShinyjs.Rd @@ -24,9 +24,7 @@ how to use shinyjs in these apps.} } \value{ Scripts that \code{shinyjs} requires that are automatically inserted -to the app's \code{} tag. A side effect of calling this function is that -a \code{shinyjs} directory is added as a resource path using -\code{\link[shiny:resourcePaths]{shiny::addResourcePath()}}. +to the app's \code{} tag. } \description{ This function must be called from a Shiny app's UI in order for all other