Skip to content

Commit

Permalink
replace addResourcePath with htmlDependency, which is more robust and…
Browse files Browse the repository at this point in the history
… useful for package developers and alos works better in Rmd; fixes #260
  • Loading branch information
daattali committed Jan 16, 2023
1 parent dc6654b commit c3562b8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
6 changes: 3 additions & 3 deletions 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.9000
Version: 2.1.0.9001
Authors@R: person("Dean", "Attali",
email = "[email protected]",
role = c("aut", "cre"),
Expand All @@ -21,7 +21,7 @@ Imports:
shiny (>= 1.0.0),
utils
Suggests:
htmltools (>= 0.2.9),
htmltools (>= 0.3.5),
knitr (>= 1.7),
rmarkdown,
shinyAce,
Expand All @@ -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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 18 additions & 10 deletions R/useShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{<head>} 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{<head>} tag.
#' @examples
#' if (interactive()) {
#' library(shiny)
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
"<script>", paste(initJS, jsCodeFuncs, sep = "\n"), "</script>",
"<style>", initCSS, "</style>"
)
)

# set up the message handlers and add some initial JS and CSS
setupJS(jsFuncs, jsFile, initJS, initCSS)
shiny::tagList(
shinyjsContent
)
}
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions man/useShinyjs.Rd

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

0 comments on commit c3562b8

Please sign in to comment.