Skip to content

Commit

Permalink
support htmlDependency in extendShinyjs; related to #254
Browse files Browse the repository at this point in the history
  • Loading branch information
daattali committed Jan 16, 2023
1 parent c3562b8 commit b3fc4db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyjs
Title: Easily Improve the User Experience of Your Shiny Apps in Seconds
Version: 2.1.0.9001
Version: 2.1.0.9002
Authors@R: person("Dean", "Attali",
email = "[email protected]",
role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- 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)
- New feature: When using `extendShinyjs()`, the `path` can now be a `htmltools::htmlDependency()` (#254)

# shinyjs 2.1.0 (2021-12-20)

Expand Down
40 changes: 32 additions & 8 deletions R/extendShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#' or \href{https://deanattali.com/shinyjs/}{view the shinyjs webpage}
#' to learn more.
#'
#' @param script Path to a JavaScript file that contains all the functions.
#' Each function name must begin with "`shinyjs.`", for example
#' "`shinyjs.myfunc`". Note that the path to the file must be discoverable by the browser
#' (meaning that it needs to be in a "www/" directory or available via `addResourcePath()`).
#' @param script Either a path or an [`htmltools::htmlDependency()`] to a JavaScript file
#' that contains all the functions. Each function name must begin with "`shinyjs.`", for example
#' "`shinyjs.myfunc`". Note that if a path is provided, it must be discoverable by the browser
#' (ie. it needs to be in a "www/" directory or available via `addResourcePath()`).
#' See 'Basic Usage' below for more details.
#' @param text Inline JavaScript code to use instead of providing a file.
#' See 'Basic Usage' below.
Expand Down Expand Up @@ -242,9 +242,11 @@ extendShinyjs <- function(script, text, functions) {
if (missing(script) && missing(text)) {
errMsg("extendShinyjs: Either `script` or `text` need to be provided.")
}

if (!missing(script) && !missing(text)) {
errMsg("extendShinyjs: Either `script` or `text` need to be provided, but not both.")
}
if (missing(functions)) {
errMsg("extendShinyjs: `functions` argument must be provided. See the documentation for `?extendShinyjs` for more details.")
errMsg("extendShinyjs: `functions` argument must be provided.")
}

isShinyjsFunction <- functions %in% shinyjsFunctionNames("all")
Expand All @@ -264,8 +266,30 @@ extendShinyjs <- function(script, text, functions) {
assign(x, jsFunc, js)
})

# set up the message handlers for all functions
setupJS(jsFuncs, script, text)
jsCodeFuncs <- jsFuncTemplate(jsFuncs)

if (!missing(text)) {
shinyjsContent <- insertHead(
shiny::tags$script(shiny::HTML(text)),
shiny::tags$script(shiny::HTML(jsCodeFuncs))
)
} else if (!missing(script)) {
if (is.character(script)) {
shinyjsContent <- insertHead(
shiny::tags$script(src = script),
shiny::tags$script(shiny::HTML(jsCodeFuncs))
)
} else if (inherits(script, "html_dependency")) {
shinyjsContent <- insertHead(
script,
shiny::tags$script(shiny::HTML(jsCodeFuncs))
)
}
}

shiny::tagList(
shinyjsContent
)
}


Expand Down
22 changes: 13 additions & 9 deletions R/useShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) {
jsFuncs <- shinyjsFunctionNames("core")
jsCodeFuncs <- jsFuncTemplate(jsFuncs)

# JavaScript to include to turn debug mode on/off (used for seeing more messages)
if (debug) {
initJS <- "shinyjs.debug = true;"
jsCodeVersion <- paste0("shinyjs.version = '", as.character(utils::packageVersion("shinyjs")), "';")
if (identical(getOption("shinyjs.debug", FALSE), TRUE)) {
jsCodeDebug <- "shinyjs.debug = true;"
} else {
initJS <- "shinyjs.debug = false;"
jsCodeDebug <- "shinyjs.debug = false;"
}

# Add the shinyjs package version as a debugging tool
initJS <- paste0(initJS, "shinyjs.version = '", as.character(utils::packageVersion("shinyjs")), "';")
jsCode <- paste(
jsCodeVersion,
jsCodeDebug,
jsCodeFuncs,
sep = "\n"
)

# include CSS for hiding elements
initCSS <- ".shinyjs-hide { display: none !important; }"
cssCode <- ".shinyjs-hide { display: none !important; }"

shinyjsContent <- htmltools::htmlDependency(
name = "shinyjs-binding",
Expand All @@ -85,8 +89,8 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) {
src = "srcjs",
script = "shinyjs-default-funcs.js",
head = paste0(
"<script>", paste(initJS, jsCodeFuncs, sep = "\n"), "</script>",
"<style>", initCSS, "</style>"
"<script>", jsCode, "</script>",
"<style>", cssCode, "</style>"
)
)

Expand Down
6 changes: 5 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ insertHead <- function(...) {
shinyjsInlcudeScript <- function(script) {
if (missing(script) || is.null(script)) {
return(NULL)
} else {
} else if (is.character(script)) {
shiny::tags$script(src = script)
} else if (inherits(script, "html_dependency")) {
script
} else {
stop("`script` must be either a URL or an `htmltools::htmlDependency()`", call. = FALSE)
}
}

Expand Down
8 changes: 4 additions & 4 deletions man/extendShinyjs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3fc4db

Please sign in to comment.