Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

100 lines
3.0KB

  1. apply_tag <- function(ll, tag, class = NULL, extra_attributes = NULL) {
  2. lapply(ll, function(x) htmltools::tag(tag, c(x, class = class, extra_attributes)))
  3. }
  4. dots2list <- function(...) {
  5. # Convert dots to list, but return first element if the first element is a list
  6. # and the length of ... is 1
  7. x <- list(...)
  8. if (length(x) == 1 && is.list(x[[1]])) return(x[[1]])
  9. x
  10. }
  11. map_arg <- function(x, .f, .args = NULL) {
  12. mapply(.f, x, MoreArgs = .args, SIMPLIFY = FALSE, USE.NAMES = TRUE)
  13. }
  14. tag_function <- function(.tag = "div") {
  15. function(...) htmltools::tag(.tag, list(...))
  16. }
  17. validate_value <- function(value = NULL, choices, several.ok = TRUE, value_name = "") {
  18. if (!is.null(value) && length(value)) {
  19. value_name <- if (nchar(value_name) > 0) glue("`{value_name}` - ") else ""
  20. warnings <- c()
  21. not_in_choices <- setdiff(value, choices)
  22. if (length(not_in_choices)) {
  23. warnings <- glue("{value_name}Ignoring invalid choices: ",
  24. "\"{paste(not_in_choices, collapse = '\", \"')}\"")
  25. value <- intersect(value, choices)
  26. }
  27. if (!several.ok && length(value) > 1) {
  28. warnings <- c(
  29. warnings,
  30. glue("{value_name}Using the first of {length(value)} values: {value[1]}")
  31. )
  32. value <- value[1]
  33. }
  34. if (length(value)) {
  35. if (length(warnings)) {
  36. rlang::warn(paste(warnings, collapse = "\n"))
  37. }
  38. value
  39. } else {
  40. rlang::abort(glue("{value_name}Must be one of the following valid choices: ",
  41. "\"{paste(choices, collapse = '\", \"')}\""))
  42. }
  43. } else if (!is.null(value) && !length(value)) NULL
  44. }
  45. is_html <- function(x) inherits(class(x), "html")
  46. is_tagList <- function(x) inherits(class(x), "shiny.tag.list")
  47. is_tag <- function(x) inherits(class(x), "shiny.tag")
  48. is_htmlish <- function(x) is_html(x) | is_tag(x) | is_tagList(x)
  49. c_is <- function(x) {
  50. if (is.null(x)) return(NULL)
  51. str_trim(paste("is-", x, sep = "", collapse = " "))
  52. }
  53. c_has <- function(x) {
  54. if (is.null(x)) return(NULL)
  55. str_trim(paste("has-", x, sep = "", collapse = " "))
  56. }
  57. c_prefix <- function(x = NULL, prefix = NULL) {
  58. if (is.null(x)) return(NULL)
  59. paste0(prefix, x)
  60. }
  61. str_trim <- function(x) {
  62. x <- gsub("^\\s*|\\s*$", "", x)
  63. gsub("\\s+", " ", x)
  64. }
  65. #' Font Awesome Icon
  66. #'
  67. #' Create the correct Font Awesome class.
  68. #' @param name Name of the Font Awesome icon
  69. #' @param solid Should the solid or the regular icon be used?
  70. #' @param as_html If `FALSE` (default), only the icon class is returned.
  71. #' @examples
  72. #' fa_icon("github")
  73. #' fa_icon("star")
  74. #' fa_icon("star", FALSE)
  75. #'
  76. #' @references <https://fontawesome.com/icons>
  77. #' @export
  78. fa_icon <- function(name, solid = TRUE, as_html = FALSE) {
  79. iconClass <- if (name %in% font_awesome_brands) "fab" else {
  80. if (solid) "fas" else "far"
  81. }
  82. iconClass <- paste(iconClass, paste0("fa-", name))
  83. if (!as_html) return(iconClass)
  84. icon <- tag_function("i")(class = iconClass)
  85. htmltools::htmlDependencies(icon) <- rmarkdown::html_dependency_font_awesome()
  86. icon
  87. }