Skip to content

Commit

Permalink
address #105 add withWaiter
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Oct 3, 2021
1 parent da2f842 commit dcabd85
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ export(waiter_update)
export(waiter_use)
export(withProgressAttendant)
export(withProgressWaitress)
export(withWaiter)
import(shiny)
55 changes: 55 additions & 0 deletions R/waiter.R
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,58 @@ autoWaiter <- function(
HTML(sprintf("<script>%s</script>", script))
)
}

#' With Waiter
#'
#' Adds a waiter to a rective UI element.
#' Thew waiter is displayed when the element is
#' invalidated then is removed when the element
#' receives a new value.
#'
#' @inheritParams waiter
#' @param element A reactive element, e.g.: `uiOutput`,
#' or `plotOutput`.
#'
#' @export
withWaiter <- function(
element,
html = spin_1(),
color = "#333e48",
image = ""
){
if(missing(element))
stop("Missing `element`", call. = FALSE)

id <- element$attribs$id
html <- as.character(html)
html <- gsub("\n", "", html)

script <- paste0(
"$(document).on('shiny:outputinvalidated', function(event) {
if(event.target.id != '", id, "')
return;
waiter.show({
id: '", id, "',
html: '", html, "',
color: '", color, "',
image: '", image, "'
});
});
$(document).on('shiny:value shiny:error', function(event) {
if(event.target.id != '", id, "')
return;
waiter.hide('", id, "');
});"
)

tagList(
singleton(
HTML(
paste0("<script>", script, "</script>")
)
),
element
)
}
24 changes: 24 additions & 0 deletions man/withWaiter.Rd

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

0 comments on commit dcabd85

Please sign in to comment.