Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FilteredData.R #57

Merged
merged 10 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

* Removed `CDISCFilteredDataset` class and functionality moved to `CDISCFilteredData`.
* Changed constructor of `FilteredData` to not require `TealData` object. See `help(init_filtered_data)` for more details.
* The filtered data is now stored in `FilteredData` not `FilteredDataset`.
* The filtered data is now stored in `FilteredData` not `FilteredDataset`.

# Bug fixes

* Fixed a bug when the filter panel overview would not refresh if the panel was hidden during a transition between active modules.

# Miscellaneous

* Enhanced the burger button so it is disabled when the filter panel is not used.
Polkas marked this conversation as resolved.
Show resolved Hide resolved

# Bug fixes

Expand Down
106 changes: 61 additions & 45 deletions R/FilteredData.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ FilteredData <- R6::R6Class( # nolint
ui_filter_panel = function(id) {
ns <- NS(id)
div(
id = ns("filter_panel_whole"), # used for hiding / showing
id = ns(NULL), # used for hiding / showing
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good move as we do not need additional layer here

include_css_files(pattern = "filter-panel"),
div(
id = ns("filters_overview"), # not used, can be used to customize CSS behavior
Expand Down Expand Up @@ -763,50 +763,7 @@ FilteredData <- R6::R6Class( # nolint
)
}
)

# rather than regenerating the UI dynamically for the dataset filtering,
# we instead choose to hide/show the elements
# the filters for this dataset are just hidden from the UI, but still applied
# optimization: we set `priority = 1` to execute it before the other
# observers (default priority 0), so that they are not computed if they are hidden anyways
observeEvent(active_datanames(),
priority = 1,
{
logger::log_trace(
"FilteredData$srv_filter_panel@1 active datanames: { paste(active_datanames(), collapse = \" \") }"
)
if (length(active_datanames()) == 0 || is.null(active_datanames())) {
# hide whole module UI when no datasets or when NULL
shinyjs::hide("filter_panel_whole")
shinyjs::runjs('$("#teal_secondary_col").hide();
$("#teal_primary_col").attr("class", "col-sm-12").resize();')
} else {
shinyjs::show("filter_panel_whole")
shinyjs::runjs('if (filter_open) {
$("#teal_primary_col").attr("class", "col-sm-9").resize();
$("#teal_secondary_col").show();}')

# selectively hide / show to only show `active_datanames` out of all datanames
lapply(
self$datanames(),
function(dataname) {
id_add_filter <- private$get_ui_add_filter_id(dataname)
id_filter_dataname <- private$get_ui_id(dataname)

if (dataname %in% active_datanames()) {
# shinyjs takes care of the namespace around the id
shinyjs::show(id_add_filter)
shinyjs::show(id_filter_dataname)
} else {
shinyjs::hide(id_add_filter)
shinyjs::hide(id_filter_dataname)
}
}
)
}
},
ignoreNULL = FALSE
)
private$active_datanames_observer(active_datanames, session$ns(NULL))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality transferred to private


observeEvent(input$remove_all_filters, {
logger::log_trace("FilteredData$srv_filter_panel@1 removing all filters")
Expand Down Expand Up @@ -898,6 +855,7 @@ FilteredData <- R6::R6Class( # nolint
logger::log_trace("FilteredData$srv_filter_overview@1 updated counts")
table_html
})

shiny::outputOptions(output, "table", suspendWhenHidden = FALSE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the actual fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will reduce the PR to only this line, if it is enough.

logger::log_trace("FilteredData$srv_filter_overview initialized")
NULL
Expand All @@ -908,6 +866,64 @@ FilteredData <- R6::R6Class( # nolint

## __Private Methods ====
private = list(
# Handles changes in `active_datanames`.
# @param active_datanames (`reactive(1)`) the observed reactive
# @param id (`character(1)`) the id of the filter panel
# @return a `shiny` observer
active_datanames_observer = function(active_datanames, id) {
# rather than regenerating the UI dynamically for the dataset filtering,
# we instead choose to hide/show the elements
# the filters for this dataset are just hidden from the UI, but still applied
# optimization: we set `priority = 1` to execute it before the other
# observers (default priority 0), so that they are not computed if they are hidden anyways
observeEvent(active_datanames(),
priority = 1,
{
logger::log_trace(
"FilteredData$srv_filter_panel@1 active datanames: { paste(active_datanames(), collapse = \" \") }"
)

private$hide_inactive_datasets(active_datanames)
if (length(active_datanames()) == 0 || is.null(active_datanames())) {
# The filter panel emits an event when there are no active datasets
# so the parent modules can hide the filter panel if needed.
script <- paste0(
"const noDatasetsEvent = new Event('noDatasetsEvent');",
"document.getElementById('%s').dispatchEvent(noDatasetsEvent);"
)
} else {
# so the parent modules can hide the filter panel if needed.
script <- paste0(
"const datasetsActive = new Event('datasetsActiveEvent');",
"document.getElementById('%s').dispatchEvent(datasetsActive);"
)
}
script <- sprintf(script, id)
shinyjs::runjs(script)
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
},
ignoreNULL = FALSE
)
},

# selectively hide / show to only show `active_datanames` out of all datanames
hide_inactive_datasets = function(active_datanames) {
lapply(
self$datanames(),
function(dataname) {
id_add_filter <- private$get_ui_add_filter_id(dataname)
id_filter_dataname <- private$get_ui_id(dataname)

if (dataname %in% active_datanames()) {
# shinyjs takes care of the namespace around the id
shinyjs::show(id_add_filter)
shinyjs::show(id_filter_dataname)
} else {
shinyjs::hide(id_add_filter)
shinyjs::hide(id_filter_dataname)
Comment on lines +885 to +890
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it comes from shinyjs:::jsFuncHelper where session$ns() is used around id.

}
}
)
},

# private attributes ----
filtered_datasets = list(),
Expand Down