Skip to content

Commit

Permalink
add bookmark exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksander Chlebowski committed Mar 26, 2024
1 parent 0cc4df5 commit 247aa8a
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 229 deletions.
215 changes: 110 additions & 105 deletions R/AddCardModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,126 +88,131 @@ add_card_button_srv <- function(id, reporter, card_fun) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_subset(names(formals(card_fun)), c("card", "comment", "label"), empty.ok = TRUE)

shiny::moduleServer(
id,
function(input, output, session) {
setBookmarkExclude(c("add_report_card_button", "add_card_ok"))
ns <- session$ns
add_modal <- function() {
shiny::modalDialog(
easyClose = TRUE,
shiny::tags$h3("Add a Card to the Report"),
shiny::tags$hr(),
shiny::textInput(
ns("label"),
"Card Name",
value = "",
placeholder = "Add the card title here",
width = "100%"
),
shiny::textAreaInput(
ns("comment"),
"Comment",
value = "",
placeholder = "Add a comment here...",
width = "100%"
),
shiny::tags$script(
shiny::HTML(
sprintf(
"
shiny::moduleServer(id, function(input, output, session) {

shiny::setBookmarkExclude(c(
"add_report_card_button", "download_button", "reset_reporter",
"add_card_ok", "download_data", "reset_reporter_ok",
"label", "comment"
))

ns <- session$ns

add_modal <- function() {
shiny::modalDialog(
easyClose = TRUE,
shiny::tags$h3("Add a Card to the Report"),
shiny::tags$hr(),
shiny::textInput(
ns("label"),
"Card Name",
value = "",
placeholder = "Add the card title here",
width = "100%"
),
shiny::textAreaInput(
ns("comment"),
"Comment",
value = "",
placeholder = "Add a comment here...",
width = "100%"
),
shiny::tags$script(
shiny::HTML(
sprintf(
"
$('#shiny-modal').on('shown.bs.modal', () => {
$('#%s').focus()
})
",
ns("label")
)
ns("label")
)
)
),
footer = shiny::div(
shiny::tags$button(
type = "button",
class = "btn btn-secondary",
`data-dismiss` = "modal",
`data-bs-dismiss` = "modal",
NULL,
"Cancel"
),
footer = shiny::div(
shiny::tags$button(
type = "button",
class = "btn btn-secondary",
`data-dismiss` = "modal",
`data-bs-dismiss` = "modal",
NULL,
"Cancel"
),
shiny::tags$button(
id = ns("add_card_ok"),
type = "button",
class = "btn btn-primary action-button",
`data-val` = shiny::restoreInput(id = ns("add_card_ok"), default = NULL),
NULL,
"Add Card"
)
shiny::tags$button(
id = ns("add_card_ok"),
type = "button",
class = "btn btn-primary action-button",
`data-val` = shiny::restoreInput(id = ns("add_card_ok"), default = NULL),
NULL,
"Add Card"
)
)
}
)
}

shiny::observeEvent(input$add_report_card_button, {
shiny::showModal(add_modal())
})
shiny::observeEvent(input$add_report_card_button, {
shiny::showModal(add_modal())
})

# the add card button is disabled when clicked to prevent multi-clicks
# please check the ui part for more information
shiny::observeEvent(input$add_card_ok, {
card_fun_args_nams <- names(formals(card_fun))
has_card_arg <- "card" %in% card_fun_args_nams
has_comment_arg <- "comment" %in% card_fun_args_nams
has_label_arg <- "label" %in% card_fun_args_nams
# the add card button is disabled when clicked to prevent multi-clicks
# please check the ui part for more information
shiny::observeEvent(input$add_card_ok, {
card_fun_args_nams <- names(formals(card_fun))
has_card_arg <- "card" %in% card_fun_args_nams
has_comment_arg <- "comment" %in% card_fun_args_nams
has_label_arg <- "label" %in% card_fun_args_nams

arg_list <- list()
arg_list <- list()

if (has_comment_arg) {
arg_list <- c(arg_list, list(comment = input$comment))
}
if (has_label_arg) {
arg_list <- c(arg_list, list(label = input$label))
}
if (has_comment_arg) {
arg_list <- c(arg_list, list(comment = input$comment))
}
if (has_label_arg) {
arg_list <- c(arg_list, list(label = input$label))
}

if (has_card_arg) {
# The default_card is defined here because formals() returns a pairedlist object
# of formal parameter names and their default values. The values are missing
# if not defined and the missing check does not work if supplied formals(card_fun)[[1]]
default_card <- formals(card_fun)$card
card <- `if`(
missing(default_card),
ReportCard$new(),
eval(default_card, envir = environment(card_fun))
)
arg_list <- c(arg_list, list(card = card))
}
if (has_card_arg) {
# The default_card is defined here because formals() returns a pairedlist object
# of formal parameter names and their default values. The values are missing
# if not defined and the missing check does not work if supplied formals(card_fun)[[1]]
default_card <- formals(card_fun)$card
card <- `if`(
missing(default_card),
ReportCard$new(),
eval(default_card, envir = environment(card_fun))
)
arg_list <- c(arg_list, list(card = card))
}

card <- try(do.call(card_fun, arg_list))
card <- try(do.call(card_fun, arg_list))

if (inherits(card, "try-error")) {
msg <- paste0(
"The card could not be added to the report. ",
"Have the outputs for the report been created yet? If not please try again when they ",
"are ready. Otherwise contact your application developer"
)
warning(msg)
shiny::showNotification(
msg,
type = "error"
)
} else {
checkmate::assert_class(card, "ReportCard")
if (!has_comment_arg && length(input$comment) > 0 && input$comment != "") {
card$append_text("Comment", "header3")
card$append_text(input$comment)
}

if (!has_label_arg && length(input$label) == 1 && input$label != "") {
card$set_name(input$label)
}

reporter$append_cards(list(card))
shiny::showNotification(sprintf("The card added successfully."), type = "message")
shiny::removeModal()
if (inherits(card, "try-error")) {
msg <- paste0(
"The card could not be added to the report. ",
"Have the outputs for the report been created yet? If not please try again when they ",
"are ready. Otherwise contact your application developer"
)
warning(msg)
shiny::showNotification(
msg,
type = "error"
)
} else {
checkmate::assert_class(card, "ReportCard")
if (!has_comment_arg && length(input$comment) > 0 && input$comment != "") {
card$append_text("Comment", "header3")
card$append_text(input$comment)
}
})
}

if (!has_label_arg && length(input$label) == 1 && input$label != "") {
card$set_name(input$label)
}

reporter$append_cards(list(card))
shiny::showNotification(sprintf("The card added successfully."), type = "message")
shiny::removeModal()
}
})
}
)
}
Loading

0 comments on commit 247aa8a

Please sign in to comment.