Skip to content

Commit

Permalink
simplify the verbatim pop up (#127)
Browse files Browse the repository at this point in the history
closes #102

I simplify the code by placing the observe one module deeper.

Signed-off-by: Maciej Nasinski <nasinski.maciej@gmail.com>
  • Loading branch information
Polkas authored Dec 9, 2022
1 parent a08873a commit 552365a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 61 deletions.
54 changes: 24 additions & 30 deletions R/verbatim_popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ verbatim_popup_ui <- function(id, button_label, type = c("button", "link"), ...)
ns <- shiny::NS(id)
ui_args <- list(
inputId = ns("button"),
label = div(
button_label,
shiny::uiOutput(ns("disable_controller"), inline = TRUE) # this is dummy output - see the comment in srv
)
label = button_label
)


shiny::tagList(
shiny::singleton(
shiny::tags$head(shiny::includeScript(system.file("js/verbatim_popup.js", package = "teal.widgets")))
),
shinyjs::useShinyjs(),
do.call(ui_function, c(ui_args, list(...)))
)
}
Expand All @@ -61,7 +58,7 @@ verbatim_popup_ui <- function(id, button_label, type = c("button", "link"), ...)
#' @param style (`logical(1)`) whether to style the `verbatim_content` using `styler::style_text`.
#' If `verbatim_content` is a `condition` or `reactive` holding `condition` then this argument is ignored
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled
#' when the flag is `TRUE` and enabled otherwise
#' when the flag is `TRUE` and enabled otherwise.
#'
verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disabled = shiny::reactiveVal(FALSE)) {
checkmate::assert_string(id)
Expand All @@ -70,39 +67,18 @@ verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disab
checkmate::assert_class(disabled, classes = "reactive")
moduleServer(id, function(input, output, session) {
ns <- session$ns
# In normal case we could enable/disable using observeEvent(disabled_flag(), ...)
# but observeEvent doesn't care whether output is displayed or not
# This means that if we want to prevent calulation of disabled_flag for hidden
# output, we need to use renderUI which triggers when the output is shown.
output$disable_controller <- shiny::renderUI(disable_button(disabled, "button"))
modal_content <- format_content(verbatim_content, style)
button_click_observer(
click_event = shiny::reactive(input$button),
copy_button_id = ns("copy_button"),
copied_area_id = ns("verbatim_content"),
modal_title = title,
modal_content = modal_content
modal_content = modal_content,
disabled = disabled
)
})
}

#' Disables a button
#'
#' @details
#' When the flag is `TRUE` the button to open the popup is disabled; it is enabled otherwise.
#'
#' @keywords internal
#' @param disabled_flag (`reactive`) containing the flag
#' @param button_id (`character(1)`) the id of the controlled button
disable_button <- function(disabled_flag, button_id) {
if (disabled_flag()) {
shinyjs::disable(button_id)
} else {
shinyjs::enable(button_id)
}
NULL
}

#' Creates a `shiny` observer handling button clicks.
#'
#' @description
Expand All @@ -115,7 +91,25 @@ disable_button <- function(disabled_flag, button_id) {
#' @param copied_area_id (`character(1)`) the id of the element which contents are copied
#' @param modal_title (`character(1)`) the title of the modal window
#' @param modal_content (`reactive`) the content of the modal window
button_click_observer <- function(click_event, copy_button_id, copied_area_id, modal_title, modal_content) {
#' @param disabled (`reactive(1)`) the `shiny` reactive value holding a `logical`. The popup button is disabled
#' when the flag is `TRUE` and enabled otherwise.
button_click_observer <- function(click_event,
copy_button_id,
copied_area_id,
modal_title,
modal_content,
disabled) {
shiny::observeEvent(
disabled(),
handlerExpr = {
if (disabled()) {
shinyjs::disable("button")
} else {
shinyjs::enable("button")
}
}
)

shiny::observeEvent(
click_event(),
handlerExpr = {
Expand Down
6 changes: 5 additions & 1 deletion man/button_click_observer.Rd

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

20 changes: 0 additions & 20 deletions man/disable_button.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/verbatim_popup.Rd

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

9 changes: 0 additions & 9 deletions tests/testthat/test-verbatim_popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ testthat::test_that(
}
)

testthat::test_that("disable_button returns NULL", {
shiny::withReactiveDomain(
domain = shiny::MockShinySession$new(),
expr = testthat::expect_null(
isolate(disable_button(shiny::reactive(FALSE), "test_id"))
)
)
})

testthat::test_that("button_click_observer returns an observer", {
testthat::expect_true(inherits(button_click_observer(
click_event = shiny::reactive(0),
Expand Down

0 comments on commit 552365a

Please sign in to comment.