|
| 1 | +#' @title Add, remove or toggle CSS classes on girafe elements |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' These functions allow programmatic manipulation of CSS classes |
| 5 | +#' on SVG elements within a girafe output in Shiny applications. |
| 6 | +#' Elements are targeted by their `data-id`, `key-id`, or `theme-id` |
| 7 | +#' attributes. |
| 8 | +#' |
| 9 | +#' @param session The Shiny session object. |
| 10 | +#' @param id The output id of the girafe element |
| 11 | +#' (the `outputId` used in [girafeOutput()]). |
| 12 | +#' @param class A non-empty character string of CSS class names to |
| 13 | +#' add, remove, or toggle. |
| 14 | +#' @param data_id A character vector of `data-id` values identifying |
| 15 | +#' the target elements. |
| 16 | +#' @param key_id A character vector of `key-id` values identifying |
| 17 | +#' the target elements. |
| 18 | +#' @param theme_id A character vector of `theme-id` values identifying |
| 19 | +#' the target elements. |
| 20 | +#' |
| 21 | +#' @details |
| 22 | +#' At least one of `data_id`, `key_id`, or `theme_id` must be provided. |
| 23 | +#' |
| 24 | +#' These functions send a custom message to the JavaScript side, |
| 25 | +#' which applies the CSS class operation to all matching SVG elements |
| 26 | +#' within the girafe root node. |
| 27 | +#' |
| 28 | +#' @examples |
| 29 | +#' \dontrun{ |
| 30 | +#' # In a Shiny server function: |
| 31 | +#' girafe_class_add(session, "plot", "highlighted", data_id = "some_id") |
| 32 | +#' girafe_class_remove(session, "plot", "highlighted", data_id = "some_id") |
| 33 | +#' girafe_class_toggle(session, "plot", "highlighted", data_id = "some_id") |
| 34 | +#' } |
| 35 | +#' @name girafe_class |
| 36 | +NULL |
| 37 | + |
| 38 | +#' @rdname girafe_class |
| 39 | +#' @export |
| 40 | +girafe_class_add <- function(session, id, class, data_id = NULL, key_id = NULL, theme_id = NULL) { |
| 41 | + girafe_class_action(session, id, class, action = "add", data_id = data_id, key_id = key_id, theme_id = theme_id) |
| 42 | +} |
| 43 | + |
| 44 | +#' @rdname girafe_class |
| 45 | +#' @export |
| 46 | +girafe_class_remove <- function(session, id, class, data_id = NULL, key_id = NULL, theme_id = NULL) { |
| 47 | + girafe_class_action(session, id, class, action = "remove", data_id = data_id, key_id = key_id, theme_id = theme_id) |
| 48 | +} |
| 49 | + |
| 50 | +#' @rdname girafe_class |
| 51 | +#' @export |
| 52 | +girafe_class_toggle <- function(session, id, class, data_id = NULL, key_id = NULL, theme_id = NULL) { |
| 53 | + girafe_class_action(session, id, class, action = "toggle", data_id = data_id, key_id = key_id, theme_id = theme_id) |
| 54 | +} |
| 55 | + |
| 56 | +girafe_class_action <- function(session, id, class, action, data_id = NULL, key_id = NULL, theme_id = NULL) { |
| 57 | + if (!is.character(class) || length(class) != 1 || !nzchar(class)) { |
| 58 | + stop("`class` must be a non-empty string.", call. = FALSE) |
| 59 | + } |
| 60 | + if (is.null(data_id) && is.null(key_id) && is.null(theme_id)) { |
| 61 | + stop("At least one of `data_id`, `key_id`, or `theme_id` must be provided.", call. = FALSE) |
| 62 | + } |
| 63 | + |
| 64 | + message <- list(action = action, cls = class) |
| 65 | + if (!is.null(data_id)) message$data_id <- as.character(data_id) |
| 66 | + if (!is.null(key_id)) message$key_id <- as.character(key_id) |
| 67 | + if (!is.null(theme_id)) message$theme_id <- as.character(theme_id) |
| 68 | + |
| 69 | + session$sendCustomMessage( |
| 70 | + type = paste0(id, "_class"), |
| 71 | + message = message |
| 72 | + ) |
| 73 | +} |
0 commit comments