Skip to content

Commit

Permalink
Ensure that extendShinyjs does not overwrite other shinyjs functions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBarke committed Jan 8, 2021
1 parent 72ab5d9 commit 82e5696
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
9 changes: 9 additions & 0 deletions R/extendShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ extendShinyjs <- function(script, text, functions) {
errMsg("extendShinyjs: `functions` argument must be provided. See the documentation for `?extendShinyjs` for more details.")
}

isShinyjsFunction <- functions %in% shinyjsFunctionNames("all")
if (any(isShinyjsFunction)) {
errMsg(paste0(
"extendShinyjs: `functions` argument must not contain any of the ",
"following function names:\n",
paste(functions[isShinyjsFunction], collapse = ", ")
))
}

jsFuncs <- functions

# add all the given functions to the shinyjs namespace so that they can be
Expand Down
4 changes: 1 addition & 3 deletions R/useShinyjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ useShinyjs <- function(rmd = FALSE, debug = FALSE, html = FALSE) {
.globals$inject <- html

# all the default shinyjs methods that should be forwarded to javascript
jsFuncs <- c("show", "hide", "toggle", "enable", "disable", "toggleState",
"addClass", "removeClass", "toggleClass", "html", "onevent",
"alert", "logjs", "runjs", "reset", "delay", "click", "refresh")
jsFuncs <- shinyjsFunctionNames("core")

# grab the file with all the default shinyjs javascript functions
shiny::addResourcePath("shinyjs", system.file("srcjs", package = "shinyjs"))
Expand Down
22 changes: 22 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,25 @@ shinyjsInlineScript <- function(text) {
shiny::tags$script(shiny::HTML(text))
}
}

# names of JS functions defined by shinyjs
shinyjsFunctionNames <- function(type = c("all", "core")) {
type <- match.arg(type)

# core functions are defined in JS and exported in R
jsFuncs <- c(
"show", "hide", "toggle", "enable", "disable", "toggleState",
"addClass", "removeClass", "toggleClass", "html", "onevent",
"alert", "logjs", "runjs", "reset", "delay", "click", "refresh"
)

if (type == "all") {
jsFuncs <- c(
jsFuncs,
# additional functions which are only defined on the JS side
"debug", "debugMessage", "getParams", "initShinyjs"
)
}

jsFuncs
}

0 comments on commit 82e5696

Please sign in to comment.