diff --git a/R/save-state.R b/R/save-state.R index 9eb82f917..6ac9ef381 100644 --- a/R/save-state.R +++ b/R/save-state.R @@ -342,16 +342,16 @@ urlModal <- function(url, title = "Share link", subtitle = NULL) { #' #' @param eventExpr An expression to listen for, similar to #' \code{\link{observeEvent}}. -#' @param enable If \code{TRUE} (the default), enable bookmarking for this app. -#' @param type Either \code{"save"}, which saves to disk, or \code{"encode"}, -#' which encodes all of the relevant values in a URL. +#' @param type Either \code{"save"}, which saves to disk, \code{"encode"}, which +#' encodes all of the relevant values in a URL, or \code{"disable"}, which +#' disables any previously-enabled bookmarking. #' @param exclude Input values to exclude from bookmarking. #' @param onBookmarked A callback function to invoke after the bookmarking has #' been done. #' @param session A Shiny session object. #' @export -configureBookmarking <- function(eventExpr, enable = TRUE, - type = c("save", "encode"), exclude = NULL, +configureBookmarking <- function(eventExpr, + type = c("save", "encode", "disable"), exclude = NULL, onSave = NULL, onRestore = NULL, onBookmarked = NULL, session = getDefaultReactiveDomain()) { @@ -359,22 +359,6 @@ configureBookmarking <- function(eventExpr, enable = TRUE, eventExpr <- substitute(eventExpr) type <- match.arg(type) - # If no onBookmarked function is provided, use one of these defaults. - if (is.null(onBookmarked)) { - - if (type == "save") { - onBookmarked <- function(url) { - showModal(urlModal(url, subtitle = "The state of this application has been saved.")) - } - } else { - onBookmarked <- function(url) { - showModal(urlModal(url)) - } - } - } else if (!is.function(onBookmarked)) { - stop("onBookmarked must be a function.") - } - # If there's an existing onBookmarked observer, destroy it before creating a # new one. if (!is.null(session$bookmarkConfig$onBookmarkedObserver)) { @@ -382,40 +366,57 @@ configureBookmarking <- function(eventExpr, enable = TRUE, session$bookmarkConfig$onBookmarkedObserver <- NULL } - if (enable) { - session$bookmarkConfig$onBookmarkedObserver <- observeEvent( - eventExpr, - event.env = parent.frame(), - event.quoted = TRUE, - { - values <- NULL - if (!is.null(onSave)) - values <- onSave() - if (!is.null(values) && !is.list(values)) - stop("The value returned by onSave() must be NULL or a list.") - - if (type == "save") { - url <- saveStateQueryString(session$input, exclude, values) - } else { - url <- encodeStateQueryString(session$input, exclude, values) - } - - clientData <- session$clientData - url <- paste0( - clientData$url_protocol, "//", - clientData$url_hostname, - if (nzchar(clientData$url_port)) paste0(":", clientData$url_port), - clientData$url_pathname, - "?", url - ) - - onBookmarked(url) - } - ) - - # Run the onRestore function immediately - onRestore(getCurrentRestoreContext()) + if (type == "disable") { + return(invisible()) } + # If no onBookmarked function is provided, use one of these defaults. + if (is.null(onBookmarked)) { + if (type == "save") { + onBookmarked <- function(url) { + showModal(urlModal(url, subtitle = "The state of this application has been saved.")) + } + } else if (type == "encode") { + onBookmarked <- function(url) { + showModal(urlModal(url)) + } + } + } else if (!is.function(onBookmarked)) { + stop("onBookmarked must be a function.") + } + + session$bookmarkConfig$onBookmarkedObserver <- observeEvent( + eventExpr, + event.env = parent.frame(), + event.quoted = TRUE, + { + values <- NULL + if (!is.null(onSave)) + values <- onSave() + if (!is.null(values) && !is.list(values)) + stop("The value returned by onSave() must be NULL or a list.") + + if (type == "save") { + url <- saveStateQueryString(session$input, exclude, values) + } else { + url <- encodeStateQueryString(session$input, exclude, values) + } + + clientData <- session$clientData + url <- paste0( + clientData$url_protocol, "//", + clientData$url_hostname, + if (nzchar(clientData$url_port)) paste0(":", clientData$url_port), + clientData$url_pathname, + "?", url + ) + + onBookmarked(url) + } + ) + + # Run the onRestore function immediately + onRestore(getCurrentRestoreContext()) + invisible() } diff --git a/man/configureBookmarking.Rd b/man/configureBookmarking.Rd index e9a1668b8..da4cc06b8 100644 --- a/man/configureBookmarking.Rd +++ b/man/configureBookmarking.Rd @@ -4,7 +4,7 @@ \alias{configureBookmarking} \title{Configure bookmarking for the current session} \usage{ -configureBookmarking(eventExpr, enable = TRUE, type = c("save", "encode"), +configureBookmarking(eventExpr, type = c("save", "encode", "disable"), exclude = NULL, onSave = NULL, onRestore = NULL, onBookmarked = NULL, session = getDefaultReactiveDomain()) } @@ -12,10 +12,9 @@ configureBookmarking(eventExpr, enable = TRUE, type = c("save", "encode"), \item{eventExpr}{An expression to listen for, similar to \code{\link{observeEvent}}.} -\item{enable}{If \code{TRUE} (the default), enable bookmarking for this app.} - -\item{type}{Either \code{"save"}, which saves to disk, or \code{"encode"}, -which encodes all of the relevant values in a URL.} +\item{type}{Either \code{"save"}, which saves to disk, \code{"encode"}, which +encodes all of the relevant values in a URL, or \code{"disable"}, which +disables any previously-enabled bookmarking.} \item{exclude}{Input values to exclude from bookmarking.}