From 8309152c8ec703c6c42c94411841f47c0e08cedc Mon Sep 17 00:00:00 2001 From: pvictor Date: Tue, 22 Feb 2022 20:03:05 +0100 Subject: [PATCH] added config_noty() --- NAMESPACE | 1 + R/notify.R | 120 ++++++++++++++++++++++++++++++--- examples/config_notify.R | 24 +++++++ man/config_notify.Rd | 140 +++++++++++++++++++++++++++++++++++++++ man/notify.Rd | 11 ++- 5 files changed, 279 insertions(+), 17 deletions(-) create mode 100644 examples/config_notify.R create mode 100644 man/config_notify.Rd diff --git a/NAMESPACE b/NAMESPACE index f42bd33..ee6928b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(add_busy_gif) export(add_busy_spinner) export(add_loading_state) export(busy_start_up) +export(config_notify) export(config_report) export(hide_spinner) export(html_dependency_epic) diff --git a/R/notify.R b/R/notify.R index f35a932..a9efb08 100644 --- a/R/notify.R +++ b/R/notify.R @@ -4,8 +4,7 @@ #' @description Send notifications to the user. #' #' @param text Text to be displayed. -#' @param ... Options passed to JavaScript method, see -#' [https://notiflix.github.io/documentation](https://notiflix.github.io/documentation). +#' @param ... Options passed to JavaScript method, see [config_notify()]. #' @param timeout The delay in milliseconds to hide and remove the notifications. #' @param position Position where to display the notification. #' @param type Type of notification: `success`, `failure`, `info` or `warning`. @@ -33,6 +32,9 @@ notify <- function(text, session = shiny::getDefaultReactiveDomain()) { type <- match.arg(type) position <- match.arg(position) + config <- list(timeout = timeout, position = position, ...) + idx <- vapply(X = config, FUN = inherits, what = "config_notify", FUN.VALUE = logical(1)) + config <- c(config[!idx], unlist(config[idx], recursive = FALSE)) insertUI( selector = "html", ui = tagList(html_dependency_notify()), @@ -44,7 +46,7 @@ notify <- function(text, type = paste0("shinybusy-notify-", type), message = list( text = text, - config = list(timeout = timeout, position = position, ...) + config = config ) ) } @@ -54,9 +56,9 @@ notify <- function(text, #' #' @rdname notify notify_success <- function(text, + ..., timeout = 3000, - position = "right-top", - ...) { + position = "right-top") { notify( text = text, timeout = timeout, @@ -71,9 +73,9 @@ notify_success <- function(text, #' #' @rdname notify notify_failure <- function(text, + ..., timeout = 3000, - position = "right-top", - ...) { + position = "right-top") { notify( text = text, timeout = timeout, @@ -87,9 +89,9 @@ notify_failure <- function(text, #' #' @rdname notify notify_info <- function(text, + ..., timeout = 3000, - position = "right-top", - ...) { + position = "right-top") { notify( text = text, timeout = timeout, @@ -103,9 +105,9 @@ notify_info <- function(text, #' #' @rdname notify notify_warning <- function(text, + ..., timeout = 3000, - position = "right-top", - ...) { + position = "right-top") { notify( text = text, timeout = timeout, @@ -115,3 +117,99 @@ notify_warning <- function(text, ) } + + +#' @title Configure options for [notify()] and others +#' +#' @description Options for [notify()] functions, see +#' [online documentation](https://notiflix.github.io/documentation) +#' for default values and examples. +#' +#' @param background Changes the background color. +#' @param textColor Changes the text color. +#' @param childClassName Changes the class name. +#' @param notiflixIconColor Changes the SVG icon color. +#' @param fontAwesomeClassName Changes the FontAwesome icon class name (FontAwesome has to be added to the project separately.) +#' @param fontAwesomeIconColor Changes the FontAwesome icon color. +#' @param backOverlayColor Changes the color of the background overlay. +#' @param width Changes the width of the notifications. +#' @param distance The distance between positioned notifications and the body element. +#' @param opacity Changes the opacity. (Between 0 and 1) +#' @param borderRadius Changes the radius of the notifications corners. +#' @param rtl Specifies the text direction to "right-to-left". +#' @param messageMaxLength The maximum length of the notifications message text. +#' @param backOverlay Adds a background overlay to the notifications. +#' @param plainText Strips all HTML tags. +#' @param showOnlyTheLastOne Auto-removes all the notifications except for the last one. +#' @param clickToClose Removes the notification when it has been clicked without waiting for the delay. +#' @param pauseOnHover Auto-remove functionality will be paused for each notification element when the pointer(mouse) enters on it. +#' @param ID Changes the ID (attribute) of the notifications. +#' @param className Changes the class name (attribute) of the notifications. +#' @param zindex Changes the z-index of the notifications. +#' @param fontFamily Changes the font-family of the notifications message text. +#' @param fontSize Changes the font-size of the notifications message text. +#' @param cssAnimation Enables/disables CSS animations to show/hide the notifications. +#' @param cssAnimationDuration Changes the CSS animations duration as milliseconds. +#' @param cssAnimationStyle 6 types of styles can be used: fade zoom from-right from-top from-bottom from-left +#' @param closeButton Adds a close button/icon to the notifications. (Notifications with a close button won't disappear until they were clicked.) +#' @param useIcon Allows using built-in SVG or external FontAwesome icons in the notifications. (By default, built-in SVG icons have been defined.) +#' @param useFontAwesome Ignores built-in SVG icons and allows to use of external FontAwesome icons. +#' @param fontAwesomeIconStyle 2 types of styles can be used: basic shadow +#' @param fontAwesomeIconSize Changes the font-size of the FontAwesome icons +#' @param ... Other potential arguments. +#' +#' @return A config `list` that can be used in [notify()] and other `notify_*` functions. +#' @export +#' +#' @example examples/config_notify.R +config_notify <- function(background = NULL, + textColor = NULL, + childClassName = NULL, + notiflixIconColor = NULL, + fontAwesomeClassName = NULL, + fontAwesomeIconColor = NULL, + backOverlayColor = NULL, + width = NULL, + distance = NULL, + opacity = NULL, + borderRadius = NULL, + rtl = NULL, + messageMaxLength = NULL, + backOverlay = NULL, + plainText = NULL, + showOnlyTheLastOne = NULL, + clickToClose = NULL, + pauseOnHover = NULL, + ID = NULL, + className = NULL, + zindex = NULL, + fontFamily = NULL, + fontSize = NULL, + cssAnimation = NULL, + cssAnimationDuration = NULL, + cssAnimationStyle = NULL, + closeButton = NULL, + useIcon = NULL, + useFontAwesome = NULL, + fontAwesomeIconStyle = NULL, + fontAwesomeIconSize = NULL, + ...) { + config <- c(as.list(environment()), list(...)) + config$success <- config$failure <- dropNulls(config[1:7]) + config$info <- config$warning <- dropNulls(config[1:7]) + config[1:6] <- NULL + config <- dropNulls(config) + class(config) <- c(class(config), "config_notify") + return(config) +} + + +# library(rvest) +# docs <- html_table(read_html("https://notiflix.github.io/documentation")) +# +# docs[[1]] +# cat(paste(docs[[2]]$Option, collapse = " = NULL,\n")) +# +# cat( +# paste("#' @param", docs[[1]]$Option, docs[[1]]$Description, "\n") +# ) diff --git a/examples/config_notify.R b/examples/config_notify.R new file mode 100644 index 0000000..78c057f --- /dev/null +++ b/examples/config_notify.R @@ -0,0 +1,24 @@ +library(shiny) +library(shinybusy) + +ui <- fluidPage( + tags$h2("config for notify examples"), + actionButton("success", "Success") +) + +server <- function(input, output, session) { + + observeEvent(input$success, { + notify_success( + "Well done!", + config_notify( + background = "#0431B4", + notiflixIconColor = "#FFF" + ) + ) + }) + +} + +if (interactive()) + shinyApp(ui, server) diff --git a/man/config_notify.Rd b/man/config_notify.Rd new file mode 100644 index 0000000..8b90268 --- /dev/null +++ b/man/config_notify.Rd @@ -0,0 +1,140 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/notify.R +\name{config_notify} +\alias{config_notify} +\title{Configure options for \code{\link[=notify]{notify()}} and others} +\usage{ +config_notify( + background = NULL, + textColor = NULL, + childClassName = NULL, + notiflixIconColor = NULL, + fontAwesomeClassName = NULL, + fontAwesomeIconColor = NULL, + backOverlayColor = NULL, + width = NULL, + distance = NULL, + opacity = NULL, + borderRadius = NULL, + rtl = NULL, + messageMaxLength = NULL, + backOverlay = NULL, + plainText = NULL, + showOnlyTheLastOne = NULL, + clickToClose = NULL, + pauseOnHover = NULL, + ID = NULL, + className = NULL, + zindex = NULL, + fontFamily = NULL, + fontSize = NULL, + cssAnimation = NULL, + cssAnimationDuration = NULL, + cssAnimationStyle = NULL, + closeButton = NULL, + useIcon = NULL, + useFontAwesome = NULL, + fontAwesomeIconStyle = NULL, + fontAwesomeIconSize = NULL, + ... +) +} +\arguments{ +\item{background}{Changes the background color.} + +\item{textColor}{Changes the text color.} + +\item{childClassName}{Changes the class name.} + +\item{notiflixIconColor}{Changes the SVG icon color.} + +\item{fontAwesomeClassName}{Changes the FontAwesome icon class name (FontAwesome has to be added to the project separately.)} + +\item{fontAwesomeIconColor}{Changes the FontAwesome icon color.} + +\item{backOverlayColor}{Changes the color of the background overlay.} + +\item{width}{Changes the width of the notifications.} + +\item{distance}{The distance between positioned notifications and the body element.} + +\item{opacity}{Changes the opacity. (Between 0 and 1)} + +\item{borderRadius}{Changes the radius of the notifications corners.} + +\item{rtl}{Specifies the text direction to "right-to-left".} + +\item{messageMaxLength}{The maximum length of the notifications message text.} + +\item{backOverlay}{Adds a background overlay to the notifications.} + +\item{plainText}{Strips all HTML tags.} + +\item{showOnlyTheLastOne}{Auto-removes all the notifications except for the last one.} + +\item{clickToClose}{Removes the notification when it has been clicked without waiting for the delay.} + +\item{pauseOnHover}{Auto-remove functionality will be paused for each notification element when the pointer(mouse) enters on it.} + +\item{ID}{Changes the ID (attribute) of the notifications.} + +\item{className}{Changes the class name (attribute) of the notifications.} + +\item{zindex}{Changes the z-index of the notifications.} + +\item{fontFamily}{Changes the font-family of the notifications message text.} + +\item{fontSize}{Changes the font-size of the notifications message text.} + +\item{cssAnimation}{Enables/disables CSS animations to show/hide the notifications.} + +\item{cssAnimationDuration}{Changes the CSS animations duration as milliseconds.} + +\item{cssAnimationStyle}{6 types of styles can be used: fade zoom from-right from-top from-bottom from-left} + +\item{closeButton}{Adds a close button/icon to the notifications. (Notifications with a close button won't disappear until they were clicked.)} + +\item{useIcon}{Allows using built-in SVG or external FontAwesome icons in the notifications. (By default, built-in SVG icons have been defined.)} + +\item{useFontAwesome}{Ignores built-in SVG icons and allows to use of external FontAwesome icons.} + +\item{fontAwesomeIconStyle}{2 types of styles can be used: basic shadow} + +\item{fontAwesomeIconSize}{Changes the font-size of the FontAwesome icons} + +\item{...}{Other potential arguments.} +} +\value{ +A config \code{list} that can be used in \code{\link[=notify]{notify()}} and other \verb{notify_*} functions. +} +\description{ +Options for \code{\link[=notify]{notify()}} functions, see +\href{https://notiflix.github.io/documentation}{online documentation} +for default values and examples. +} +\examples{ +library(shiny) +library(shinybusy) + +ui <- fluidPage( + tags$h2("config for notify examples"), + actionButton("success", "Success") +) + +server <- function(input, output, session) { + + observeEvent(input$success, { + notify_success( + "Well done!", + config_notify( + background = "#0431B4", + notiflixIconColor = "#FFF" + ) + ) + }) + +} + +if (interactive()) + shinyApp(ui, server) +} diff --git a/man/notify.Rd b/man/notify.Rd index 2294b71..949afca 100644 --- a/man/notify.Rd +++ b/man/notify.Rd @@ -18,19 +18,18 @@ notify( session = shiny::getDefaultReactiveDomain() ) -notify_success(text, timeout = 3000, position = "right-top", ...) +notify_success(text, ..., timeout = 3000, position = "right-top") -notify_failure(text, timeout = 3000, position = "right-top", ...) +notify_failure(text, ..., timeout = 3000, position = "right-top") -notify_info(text, timeout = 3000, position = "right-top", ...) +notify_info(text, ..., timeout = 3000, position = "right-top") -notify_warning(text, timeout = 3000, position = "right-top", ...) +notify_warning(text, ..., timeout = 3000, position = "right-top") } \arguments{ \item{text}{Text to be displayed.} -\item{...}{Options passed to JavaScript method, see -\url{https://notiflix.github.io/documentation}.} +\item{...}{Options passed to JavaScript method, see \code{\link[=config_notify]{config_notify()}}.} \item{timeout}{The delay in milliseconds to hide and remove the notifications.}