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

Improve UI Feedback For Disabled Cells #3

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 32 additions & 8 deletions R/30_mod_scenario_fct.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ js <- function(ns) {
disable_specific_rows_edit_js <- function(rows_to_disable) {
glue('function (td, cellData, rowData, row, col) {{
if ([{paste(rows_to_disable, collapse = ", ")}].includes(row)) {{
// Add cursor style to indicate disabled state
$(td).css("cursor", "not-allowed");

$(td).on("dblclick", function(e) {{
e.stopPropagation();
$(td).addClass("disabled-cell");
setTimeout(function() {{
$(td).removeClass("disabled-cell");
}}, 250);
}});
}}
}}')
Expand All @@ -69,14 +68,39 @@ disable_all_rows_edit_js <- function() {
'function (td, cellData, rowData, row, col) {
$(td).on("dblclick", function(e) {
e.stopPropagation();
$(td).addClass("disabled-cell");
setTimeout(function() {
$(td).removeClass("disabled-cell");
}, 250);
});
}'
}

# Disable editing and add "not-allowed" cursor to the entire column
disable_and_add_cursor_js <- function() {
'function (td, cellData, rowData, row, col) {
// Disable double-click editing for all cells
$(td).on("dblclick", function(e) {
e.stopPropagation();
});

// Add "not-allowed" cursor style to all cells
$(td).css("cursor", "not-allowed");
}'
}

# Disable editing for all column cells and conditionally add "not-allowed" cursor
disable_with_conditional_cursor_js <- function(rows_to_highlight) {
glue('function (td, cellData, rowData, row, col) {{
// Always disable editing
$(td).off("dblclick").on("dblclick", function(e) {{
e.stopPropagation();
}});

// Check if the cell row index is in the rows_to_highlight array
if ([{paste(rows_to_highlight, collapse = ", ")}].includes(row)) {{
// Add "not-allowed" cursor for these specific rows
$(td).css("cursor", "not-allowed");
}}
}}')
}

# Define a function to click on elements with a specific prefix in their ID
click_by_prefix <- function(prefix) {
js_code <- sprintf("
Expand Down
25 changes: 16 additions & 9 deletions R/30_mod_scenario_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,6 @@ scenario_server <- function(
h2("Choose a Fertilizer", class = "mb-3"),
shinyWidgets::pickerInput(
ns("fertilizer_code"),
"Fertilizer",
choices = setNames(
lkp_orgfertilizer()$fertilizer_code,
lkp_orgfertilizer()$fertilizer_desc
Expand Down Expand Up @@ -1429,6 +1428,21 @@ scenario_server <- function(
createdCell = JS(disable_specific_rows_edit_js(rows_not_contains_rice)),
searchable = FALSE
),
list(
targets = get_column_indices(
feedtype_dt, c("water_regime", "ecosystem_type", "organic_amendment")
) - 1,
createdCell = JS(disable_with_conditional_cursor_js(rows_not_contains_rice)),
searchable = FALSE
),
list(
targets = get_column_indices(
feedtype_dt,
c("landcover_c_factor", "slope_p_factor", "grassman_change_factor")
) - 1,
createdCell = JS(disable_and_add_cursor_js()),
searchable = FALSE
),
list(
targets = get_column_indices(
feedtype_dt,
Expand All @@ -1439,14 +1453,7 @@ scenario_server <- function(
"land_cover_desc",
"slope_desc",
"grassman_desc",
"water_regime",
"ecosystem_type",
"organic_amendment",
"category",
"landcover_c_factor",
"slope_p_factor",
"grassman_change_factor"
)
"category")
) - 1, # - 1 Added because rownames = FALSE
createdCell = JS(disable_all_rows_edit_js()),
searchable = FALSE
Expand Down
8 changes: 8 additions & 0 deletions R/40_mod_params_db_fct.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ checkbox_link_multi <- function(id, ns, table_name) {
)
)
}

# Add "cursor: not-allowed" to an entire disabled column
add_cursor_to_disabled_column_js <- function() {
'function (td, cellData, rowData, row, col) {
// Add "not-allowed" cursor style
$(td).css("cursor", "not-allowed");
}'
}
11 changes: 10 additions & 1 deletion R/40_mod_params_db_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ params_db_server <- function(
"livetype_desc", "ipcc_meth_ef_t1", "ipcc_meth_ef_t2",
"ipcc_meth_man", "ipcc_meth_exc")
) - 1,
createdCell = JS(disable_all_rows_edit_js()),
createdCell = JS(disable_and_add_cursor_js()),
searchable = FALSE
)
))
Expand All @@ -580,6 +580,15 @@ params_db_server <- function(
"lkp_grassinputlevel", "lkp_landcover",
"lkp_slope")) {
editablity <- FALSE

column_defs <- list(
list(
targets = "_all", # Apply to all cells
createdCell = JS(add_cursor_to_disabled_column_js()),
searchable = FALSE
)
)

# Disable add and delete rows buttons
shinyjs::disable(id = paste0("add_rows_", name))
shinyjs::disable(id = paste0("delete_rows_", name))
Expand Down