Skip to content

Commit

Permalink
fix(targetting): converted to actions
Browse files Browse the repository at this point in the history
* The targetting feature is now better named `actions`. (#166)
  • Loading branch information
nteetor committed Nov 26, 2019
1 parent 6cc8bbf commit 5cb19c8
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 40 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,targeter)
export("%>%")
export(active)
export(affix)
Expand Down Expand Up @@ -43,6 +44,7 @@ export(groupTextInput)
export(height)
export(hideCollapsePane)
export(hideNavPane)
export(hideTarget)
export(img)
export(jumbotron)
export(linkInput)
Expand Down Expand Up @@ -70,6 +72,7 @@ export(showCollapsePane)
export(showModal)
export(showNavPane)
export(showPopover)
export(showTarget)
export(showToast)
export(switchInput)
export(textInput)
Expand Down
88 changes: 88 additions & 0 deletions R/actions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#' Input actions
#'
#' These functions are used in conjunction with the `targets` argument found
#' in all of yonder's reactive input functions.
#'
#' @param id A character string specifying the id of a [navPane()] or
#' [collapsePane()].
#'
#' @param ... Additional arguments, currently ignored.
#'
#' @name actions
NULL

#' @rdname actions
#' @export
showTarget <- function(id, ...) {
create_targeter("show", id)
}

#' @rdname actions
#' @export
hideTarget <- function(id, ...) {
create_targeter("hide", id)
}

create_targeter <- function(action, id) {
structure(
class = c("targeter", "list"),
list(
action = action,
target = id
)
)
}

is_targeter <- function(x) {
inherits(x, "targeter")
}

#' Explain a targetting action
#'
#' Printing a targetting action displays a message about the action performed.
#'
#' @param x An object with class `"targeter"`.
#'
#' @param ... Additional arguments passed on to other methods.
#'
#' @keywords internal
#' @export
print.targeter <- function(x, ...) {
msg <- "Selecting the input choice with the corresponding value _%s_ the element with id `%s`"
cat(sprintf(msg, x$action, x$id), "\n")
invisible(x)
}

construct_targets <- function(targets, values) {
if (is.null(targets)) {
return(targets)
}

if (length(targets) > 1) {
targets <- lapply(targets, function(target) {
if (is.character(target)) {
paste0("#", target, collapse = " ")
} else if (is_targeter(target)) {
stop("Not implemented")
}
})

if (all(names2(targets) == "")) {
names(targets) <- values
}

targets
} else if (is.character(targets)) {
targets
}
}

get_target <- function(targets, value) {
if (is.null(targets)) {
NULL
} else if (is.character(targets)) {
paste0("#", value)
} else {
targets[[value]]
}
}
12 changes: 6 additions & 6 deletions R/nav.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@
#'
navInput <- function(id, choices = NULL, values = choices,
selected = values[[1]], ..., appearance = "links",
fill = FALSE, targets = NULL) {
fill = FALSE, actions = NULL) {
assert_id()
assert_choices()
assert_selected(length = 1)
assert_possible(appearance, c("links", "pills", "tabs"))
assert_targets()
# assert_targets()

dep_attach({
items <- map_navitems(choices, values, selected, targets)
items <- map_navitems(choices, values, selected, actions)

tags$ul(
class = str_collate(
Expand All @@ -167,12 +167,12 @@ navInput <- function(id, choices = NULL, values = choices,
#' @export
updateNavInput <- function(id, choices = NULL, values = choices,
selected = NULL, enable = NULL, disable = NULL,
targets = NULL,
actions = NULL,
session = getDefaultReactiveDomain()) {
assert_id()
assert_choices()
assert_selected(length = 1)
assert_targets()
# assert_targets()
assert_session()

items <- map_navitems(choices, values, selected, targets)
Expand All @@ -196,7 +196,7 @@ map_navitems <- function(choices, values, selected, targets) {
}

selected <- values %in% selected
targets <- format_targets(targets, values)
targets <- construct_targets(targets, values)

Map(
choice = choices,
Expand Down
32 changes: 0 additions & 32 deletions R/utils-arguments.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,3 @@ coerce_invalid <- function(x) {
HTML(as.character(x))
}
}

format_targets <- function(targets, values) {
if (is.null(targets)) {
return(targets)
}

if (length(targets) > 1) {
targets <- lapply(targets, function(target) {
if (is.character(target)) {
paste0("#", target, collapse = " ")
}
})

if (all(names2(targets) == "")) {
names(targets) <- values
}

targets
} else if (is.character(targets)) {
targets
}
}

get_target <- function(targets, value) {
if (is.null(targets)) {
NULL
} else if (is.character(targets)) {
paste0("#", value)
} else {
targets[[value]]
}
}
22 changes: 22 additions & 0 deletions man/actions.Rd

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

4 changes: 2 additions & 2 deletions man/navInput.Rd

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

17 changes: 17 additions & 0 deletions man/print.targeter.Rd

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

0 comments on commit 5cb19c8

Please sign in to comment.