Skip to content

Commit

Permalink
add 'asis' parameter to allow forcing namespaces to not be used
Browse files Browse the repository at this point in the history
  • Loading branch information
daattali committed Apr 16, 2018
1 parent 8627e85 commit f5f57c6
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 50 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: 1.0.1.9002
Version: 1.0.1.9003
Authors@R: person("Dean", "Attali", email = "[email protected]",
role = c("aut", "cre"))
Description: Perform common useful JavaScript operations in Shiny apps that will
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- New feature: `onevent()` returns the `offsetX` and `offsetY` event properties
- New feature: `onevent()` accepts a `properties` parameter that allows the user to retrieve additional properties that are not whitelisted by default; fixes #159
- Fix bug: shinyjs functions used inside a module with a `selector` argument instead of an `id` argument didn't work
- Add `asis` parameter to any function that takes an ID. When `asis=TRUE`, the ID will not be namespaced when inside a module (#118)

# shinyjs 1.0

Expand Down
4 changes: 3 additions & 1 deletion R/jsFunc-aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ jsFuncHelper <- function(fxn, params) {
# respect Shiny modules/namespaces
if (inherits(session , "session_proxy")) {
if ("id" %in% names(params) && !is.null(params[['id']])) {
params[['id']] <- session$ns(params[['id']])
if (!"asis" %in% names(params) || !params[['asis']]) {
params[['id']] <- session$ns(params[['id']])
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions R/jsFunc-classFuncs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#' @param selector JQuery selector of the elements to target. Ignored if the \code{id}
#' argument is given. For example, to add a certain class to all inputs with class x,
#' use \code{selector = "input.x"}
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}},
#' \code{\link[shinyjs]{inlineCSS}},
Expand Down Expand Up @@ -83,9 +85,9 @@ NULL

#' @export
#' @rdname classFuncs
addClass <- function(id = NULL, class = NULL, selector = NULL) {
addClass <- function(id = NULL, class = NULL, selector = NULL, asis = FALSE) {
fxn <- "addClass"
params <- list(id = id, class = class, selector = selector)
params <- list(id = id, class = class, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}
#' @export
Expand All @@ -94,9 +96,9 @@ addCssClass <- addClass

#' @export
#' @rdname classFuncs
removeClass <- function(id = NULL, class = NULL, selector = NULL) {
removeClass <- function(id = NULL, class = NULL, selector = NULL, asis = FALSE) {
fxn <- "removeClass"
params <- list(id = id, class = class, selector = selector)
params <- list(id = id, class = class, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}
#' @export
Expand All @@ -105,10 +107,10 @@ removeCssClass <- removeClass

#' @export
#' @rdname classFuncs
toggleClass <- function(id = NULL, class = NULL, condition = NULL, selector = NULL) {
toggleClass <- function(id = NULL, class = NULL, condition = NULL, selector = NULL, asis = FALSE) {
fxn <- "toggleClass"
params <- list(id = id, class = class, condition = condition,
selector = selector)
selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}
#' @export
Expand Down
6 changes: 4 additions & 2 deletions R/jsFunc-click.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' on a Shiny \code{actionButton()}.
#'
#' @param id The id of the button
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}}
#' @note \code{shinyjs} must be initialized with a call to \code{useShinyjs()}
Expand Down Expand Up @@ -31,8 +33,8 @@
#' )
#' }
#' @export
click <- function(id) {
click <- function(id, asis = FALSE) {
fxn <- "click"
params <- list(id = id)
params <- list(id = id, asis = asis)
jsFuncHelper(fxn, params)
}
6 changes: 4 additions & 2 deletions R/jsFunc-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' otherwise overwrite it.
#' @param selector JQuery selector of the elements to target. Ignored if the \code{id}
#' argument is given.
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}}
#' @note \code{shinyjs} must be initialized with a call to \code{useShinyjs()}
Expand Down Expand Up @@ -43,8 +45,8 @@
#' html(id = "element", add = TRUE, html = input$btn)
#' }
#' @export
html <- function(id = NULL, html = NULL, add = FALSE, selector = NULL) {
html <- function(id = NULL, html = NULL, add = FALSE, selector = NULL, asis = FALSE) {
fxn <- "html"
params <- list(id = id, html = html, add = add, selector = selector)
params <- list(id = id, html = html, add = add, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}
14 changes: 8 additions & 6 deletions R/jsFunc-stateFuncs.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' @param selector Query selector of the elements to target. Ignored if the \code{id}
#' argument is given. For example, to disable all text inputs, use
#' \code{selector = "input[type='text']"}
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}}
#' \code{\link[shinyjs]{disabled}}
Expand Down Expand Up @@ -75,24 +77,24 @@ NULL

#' @export
#' @rdname stateFuncs
enable <- function(id = NULL, selector = NULL) {
enable <- function(id = NULL, selector = NULL, asis = FALSE) {
fxn <- "enable"
params <- list(id = id, selector = selector)
params <- list(id = id, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}

#' @export
#' @rdname stateFuncs
disable <- function(id = NULL, selector = NULL) {
disable <- function(id = NULL, selector = NULL, asis = FALSE) {
fxn <- "disable"
params <- list(id = id, selector = selector)
params <- list(id = id, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}

#' @export
#' @rdname stateFuncs
toggleState <- function(id = NULL, condition = NULL, selector = NULL) {
toggleState <- function(id = NULL, condition = NULL, selector = NULL, asis = FALSE) {
fxn <- "toggleState"
params <- list(id = id, condition = condition, selector = selector)
params <- list(id = id, condition = condition, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}
14 changes: 8 additions & 6 deletions R/jsFunc-visibilityFuncs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#' \code{id} argument is given. For example, to select all span elements with
#' class x, use \code{selector = "span.x"}
#' @param condition An optional argument to \code{toggle}, see 'Details' below.
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}},
#' \code{\link[shinyjs]{hidden}},
Expand Down Expand Up @@ -89,10 +91,10 @@ NULL
#' @export
#' @rdname visibilityFuncs
show <- function(id = NULL, anim = FALSE, animType = "slide", time = 0.5,
selector = NULL) {
selector = NULL, asis = FALSE) {
fxn <- "show"
params <- list(id = id, anim = anim, animType = animType,
time = time, selector = selector)
time = time, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}

Expand All @@ -103,10 +105,10 @@ showElement <- show
#' @export
#' @rdname visibilityFuncs
hide <- function(id = NULL, anim = FALSE, animType = "slide", time = 0.5,
selector = NULL) {
selector = NULL, asis = FALSE) {
fxn <- "hide"
params <- list(id = id, anim = anim, animType = animType,
time = time, selector = selector)
time = time, selector = selector, asis = asis)
jsFuncHelper(fxn, params)
}

Expand All @@ -117,10 +119,10 @@ hideElement <- hide
#' @export
#' @rdname visibilityFuncs
toggle <- function(id = NULL, anim = FALSE, animType = "slide", time = 0.5,
selector = NULL, condition = NULL) {
selector = NULL, condition = NULL, asis = FALSE) {
fxn <- "toggle"
params <- list(id = id, anim = anim, animType = animType,
time = time, selector = selector, condition = condition)
time = time, selector = selector, condition = condition, asis = asis)
jsFuncHelper(fxn, params)
}

Expand Down
19 changes: 12 additions & 7 deletions R/onevent.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#' @param properties A list of JavaScript Event properties that should be available
#' to the argument of the \code{expr} function. See below for more information about
#' Event properties.
#' @param asis If \code{TRUE}, use the ID as-is even when inside a module
#' (instead of adding the namespace prefix to the ID).
#' @seealso \code{\link[shinyjs]{useShinyjs}},
#' \code{\link[shinyjs]{runExample}}
#' @note \code{shinyjs} must be initialized with a call to \code{useShinyjs()}
Expand Down Expand Up @@ -75,26 +77,29 @@

#' @rdname onevent
#' @export
onclick <- function(id, expr, add = FALSE) {
oneventHelper("click", id, substitute(expr), add, NULL)
onclick <- function(id, expr, add = FALSE, asis = FALSE) {
oneventHelper("click", id, substitute(expr), add = add,
properties = NULL, asis = asis)
}

#' @rdname onevent
#' @export
onevent <- function(event, id, expr, add = FALSE, properties = NULL) {
oneventHelper(event, id, substitute(expr), add, properties)
onevent <- function(event, id, expr, add = FALSE, properties = NULL, asis = FALSE) {
oneventHelper(event, id, substitute(expr), add, properties, asis = asis)
}

oneventHelper <- function(event, id, expr, add, properties) {
oneventHelper <- function(event, id, expr, add, properties, asis) {
# evaluate expressions in the caller's environment
parentFrame <- parent.frame(2)

# get the Shiny session
session <- getSession()

# Make sure reset works with namespaces (shiny modules)
# Make sure onevent works with namespaces (shiny modules)
if (inherits(session, "session_proxy")) {
id <- session$ns(id)
if (!asis) {
id <- session$ns(id)
}
}

# attach the event callback from JS to call this function to execute the
Expand Down
16 changes: 10 additions & 6 deletions man/classFuncs.Rd

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

5 changes: 4 additions & 1 deletion man/click.Rd

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

6 changes: 5 additions & 1 deletion man/html.Rd

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

7 changes: 5 additions & 2 deletions man/onevent.Rd

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

9 changes: 6 additions & 3 deletions man/stateFuncs.Rd

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

Loading

0 comments on commit f5f57c6

Please sign in to comment.