|
| 1 | + |
| 2 | +# * ui_edge_controls() builds an individual UI control element. These elements |
| 3 | +# are re-rendered whenever the tab is opened, so this function finds the |
| 4 | +# current value of the input and uses that instead of the value declared |
| 5 | +# in the definition in ui_edge_controls_row(). This function also isolates |
| 6 | +# the edge control UI from other changes in nodes, etc, because they happen |
| 7 | +# on different screens. |
| 8 | +ui_controls <- function(hash, inputFn, prefix_input, label, ..., input = NULL) { |
| 9 | + stopifnot(!is.null(input)) |
| 10 | + current_value_arg_name <- intersect(names(list(...)), c("selected", "value")) |
| 11 | + if (!length(current_value_arg_name)) { |
| 12 | + stop("Must specifiy `selected` or `value` when specifying edge UI controls") |
| 13 | + } |
| 14 | + input_name <- paste(prefix_input, hash, sep = "__") |
| 15 | + input_label <- label |
| 16 | + |
| 17 | + if (input_name %in% names(isolate(input))) { |
| 18 | + # Make sure current value doesn't change |
| 19 | + dots <- list(...) |
| 20 | + dots[current_value_arg_name] <- paste(isolate(input[[input_name]])) |
| 21 | + dots$inputId <- input_name |
| 22 | + dots$label <- HTML(input_label) |
| 23 | + do.call(inputFn, dots) |
| 24 | + } else { |
| 25 | + # Create new input |
| 26 | + inputFn(input_name, HTML(input_label), ...) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +get_hashed_input_with_prefix <- function(input, prefix, hash_sep = "__") { |
| 31 | + prefix <- glue::glue("^({prefix}){hash_sep}") |
| 32 | + |
| 33 | + tibble( |
| 34 | + inputId = grep(prefix, names(input), value = TRUE) |
| 35 | + ) %>% |
| 36 | + filter(!grepl("-selectized$", inputId)) %>% |
| 37 | + # get current value of input |
| 38 | + mutate(value = lapply(inputId, function(x) input[[x]])) %>% |
| 39 | + tidyr::separate(inputId, into = c("var", "hash"), sep = hash_sep) %>% |
| 40 | + tidyr::spread(var, value) %>% |
| 41 | + mutate_if(is.list, ~ purrr::map(.x, ~ if (is.null(.x)) NA else .x)) %>% |
| 42 | + tidyr::unnest() %>% |
| 43 | + split(.$hash) |
| 44 | +} |
| 45 | + |
| 46 | +# The input for angles (here for easy refactoring or future changes) |
| 47 | +selectDegree <- function(inputId, label = "Degree", min = -180, max = 180, by = 15, value = 0, ...) { |
| 48 | + sliderInput(inputId, label = label, min = min, max = max, value = value, step = by) |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +# Edge Aesthetic UI ------------------------------------------------------- |
| 53 | + |
| 54 | +# These helper functions build up the Edge UI elements. |
| 55 | +# |
| 56 | +# * ui_edge_controls_row() creates the entire row of UI elements for a given |
| 57 | +# edge. This function is where the UI inputs are initially defined. |
| 58 | + |
| 59 | +ui_edge_controls_row <- function(hash, from_name, to_name, ..., input = NULL) { |
| 60 | + stopifnot(!is.null(input)) |
| 61 | + |
| 62 | + extra <- list(...) |
| 63 | + |
| 64 | + col_4 <- function(x) { |
| 65 | + tags$div(class = "col-sm-6 col-md-3", style = "min-height: 80px", x) |
| 66 | + } |
| 67 | + title_row <- function(x) tags$div(class = "col-xs-12", tags$h3(x)) |
| 68 | + edge_label <- paste0(from_name, " → ", to_name) |
| 69 | + |
| 70 | + tagList( |
| 71 | + fluidRow( |
| 72 | + title_row(HTML(edge_label)) |
| 73 | + ), |
| 74 | + fluidRow( |
| 75 | + # Edge Curve Angle |
| 76 | + col_4(ui_controls( |
| 77 | + hash, |
| 78 | + inputFn = selectDegree, |
| 79 | + prefix_input = "angle", |
| 80 | + label = "Angle", |
| 81 | + value = extra[["angle"]] %||% 0, |
| 82 | + width = "95%", |
| 83 | + input = input |
| 84 | + )), |
| 85 | + # Edge Color |
| 86 | + col_4(ui_controls( |
| 87 | + hash, |
| 88 | + inputFn = xcolorPicker, |
| 89 | + prefix_input = "color", |
| 90 | + label = "Edge", |
| 91 | + selected = extra[["color"]] %||% "Black", |
| 92 | + width = "95%", |
| 93 | + input = input |
| 94 | + )), |
| 95 | + # Curve Angle |
| 96 | + col_4(ui_controls( |
| 97 | + hash, |
| 98 | + inputFn = selectInput, |
| 99 | + prefix_input = "lty", |
| 100 | + label = "Line Type", |
| 101 | + choices = c("solid", "dashed"), |
| 102 | + selected = extra[["lty"]] %||% "solid", |
| 103 | + width = "95%", |
| 104 | + input = input |
| 105 | + )), |
| 106 | + # Curve Angle |
| 107 | + col_4(ui_controls( |
| 108 | + hash, |
| 109 | + inputFn = selectInput, |
| 110 | + prefix_input = "lineT", |
| 111 | + label = "Line Thickness", |
| 112 | + choices = c("ultra thin", "very thin", "thin", "semithick", "thick", "very thick", "ultra thick"), |
| 113 | + selected = extra[["lineT"]] %||% "thin", |
| 114 | + width = "95%", |
| 115 | + input = input |
| 116 | + )) |
| 117 | + ) |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +# Node Aesthetic UI ------------------------------------------------------- |
| 123 | + |
| 124 | +ui_node_controls_row <- function(hash, name, adjusted, name_latex, ..., input = NULL) { |
| 125 | + stopifnot(!is.null(input)) |
| 126 | + |
| 127 | + extra <- list(...) |
| 128 | + |
| 129 | + col_4 <- function(x) { |
| 130 | + tags$div(class = "col-sm-6 col-md-3", style = "min-height: 80px", x) |
| 131 | + } |
| 132 | + title_row <- function(x) tags$div(class = "col-xs-12", tags$h3(x)) |
| 133 | + |
| 134 | + tagList( |
| 135 | + fluidRow( |
| 136 | + title_row(HTML(name)) |
| 137 | + ), |
| 138 | + fluidRow( |
| 139 | + # LaTeX version of node label |
| 140 | + col_4(ui_controls( |
| 141 | + hash, |
| 142 | + inputFn = textInput, |
| 143 | + prefix_input = "name_latex", |
| 144 | + label = "LaTeX Label", |
| 145 | + value = name_latex, |
| 146 | + width = "95%", |
| 147 | + input = input |
| 148 | + )), |
| 149 | + # Text Color |
| 150 | + col_4(ui_controls( |
| 151 | + hash, |
| 152 | + inputFn = xcolorPicker, |
| 153 | + prefix_input = "color_text", |
| 154 | + label = "Text", |
| 155 | + selected = extra[["color_text"]] %||% "Black", |
| 156 | + width = "95%", |
| 157 | + input = input |
| 158 | + )), |
| 159 | + # Fill Color |
| 160 | + col_4(ui_controls( |
| 161 | + hash, |
| 162 | + inputFn = xcolorPicker, |
| 163 | + prefix_input = "color_fill", |
| 164 | + label = "Fill", |
| 165 | + selected = extra[["color_fill"]] %||% "White", |
| 166 | + width = "95%", |
| 167 | + input = input |
| 168 | + )), |
| 169 | + # Box Color (if shown) |
| 170 | + if (adjusted) { |
| 171 | + col_4(ui_controls( |
| 172 | + hash, |
| 173 | + inputFn = xcolorPicker, |
| 174 | + prefix_input = "color_draw", |
| 175 | + label = "Border", |
| 176 | + selected = extra[["color_draw"]] %||% "Black", |
| 177 | + width = "95%", |
| 178 | + input = input |
| 179 | + )) |
| 180 | + } |
| 181 | + ) |
| 182 | + ) |
| 183 | +} |
0 commit comments