diff --git a/R/config.R b/R/config.R index cab87fc1..36901572 100644 --- a/R/config.R +++ b/R/config.R @@ -74,7 +74,7 @@ get_html_content <- function(survey_file) { } extract_html_pages <- function( - html_content, required_questions, all_questions_required, show_if + html_content, required_questions, all_questions_required, show_if ) { pages <- html_content |> rvest::html_elements(".sd-page") |> @@ -108,7 +108,7 @@ extract_html_pages <- function( if (!is.null(show_if)) { if (question_id %in% show_if$targets) { current_style <- xml2::xml_attr(container, "style") - new_style <- gsub("display:\\s*block;", "display: none;", current_style, ignore.case = TRUE) + new_style <- paste(current_style, "display: none;", sep = " ") xml2::xml_attr(container, "style") <- new_style } } diff --git a/R/server.R b/R/server.R index e37de435..711ac2dd 100644 --- a/R/server.R +++ b/R/server.R @@ -139,6 +139,9 @@ sd_server <- function( # Set up show_if conditions ---- + # Track if show_if conditions should be re-evaluated + show_if_trigger <- shiny::reactiveVal(0) + # Reactive values storing status of show_if conditions show_if_results <- set_show_if_conditions(show_if) @@ -149,6 +152,9 @@ sd_server <- function( # Observer to hide/show based on show_if condition results shiny::observe({ + + # Run if the trigger or condition results change + show_if_trigger() results <- show_if_results() # Update question visibility based on show_if results @@ -161,9 +167,9 @@ sd_server <- function( # Show or hide question for (target in names(results)) { if (results[[target]]) { - shinyjs::show(target) + shinyjs::show(paste0('container-', target)) } else { - shinyjs::hide(target) + shinyjs::hide(paste0('container-', target)) } } }) @@ -272,26 +278,30 @@ sd_server <- function( local_ts_id <- question_ts_ids[index] observeEvent(input[[local_id]], { + # Tag event time + timestamp <- get_utc_timestamp() + # Update question value formatted_value <- format_question_value(input[[local_id]]) all_data[[local_id]] <- formatted_value - # Update tracker of which fields changed - changed_fields(c(changed_fields(), local_id)) + # Trigger show_if evaluation + show_if_trigger(show_if_trigger() + 1) # Update timestamp and progress if interacted + changed <- local_id if (!is.null(input[[paste0(local_id, "_interacted")]])) { - all_data[[local_ts_id]] <- get_utc_timestamp() - changed_fields(c(changed_fields(), local_ts_id)) + all_data[[local_ts_id]] <- timestamp + changed <- c(changed, local_ts_id) update_progress_bar(index) } + # Update tracker of which fields changed + changed_fields(c(changed_fields(), changed)) + # Make value accessible in the UI output[[paste0(local_id, "_value")]] <- renderText({ formatted_value }) - # Trigger show_if evaluation - show_if_results() - # Update data after a short delay shiny::invalidateLater(100) }, ignoreNULL = FALSE, ignoreInit = TRUE)