Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that extendShinyjs does not overwrite other shinyjs functions #230

Merged
merged 4 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}