| 1 |
label_with_extras <- function(label, ...) { |
| 2 |
div( |
| 3 |
class = "d-inline-block w-100", |
| 4 |
span(label), |
| 5 |
span(class = "float-right", ...), |
| 6 |
singleton( |
| 7 |
tags$style(HTML(".shiny-input-container .control-label { width: 100%; }")) |
| 8 |
) |
| 9 |
) |
| 10 |
} |
| 11 |
|
| 12 |
ui_selextra <- function(id, label) { |
| 13 |
ns <- shiny::NS(id) |
| 14 |
|
| 15 |
selectizeInput( |
| 16 |
inputId = ns("selected"), |
| 17 |
choices = NULL, |
| 18 |
label_with_extras( |
| 19 |
label, |
| 20 |
actionLink( |
| 21 |
ns("shuffle"), |
| 22 |
bsicons::bs_icon("shuffle", title = paste("Random", label)) |
| 23 |
), |
| 24 |
actionLink( |
| 25 |
ns("prev"), |
| 26 |
bsicons::bs_icon("caret-left-fill", title = paste("Previous", label)) |
| 27 |
), |
| 28 |
actionLink( |
| 29 |
ns("next"), |
| 30 |
bsicons::bs_icon("caret-right-fill", title = paste("Next", label)) |
| 31 |
) |
| 32 |
) |
| 33 |
) |
| 34 |
} |
| 35 |
|
| 36 |
server_selextra <- function(input, output, session, choices) { |
| 37 |
ns <- session$ns |
| 38 |
|
| 39 |
# These are server-side selectize inputs, so we update them on initialization |
| 40 |
updateSelectizeInput(session, "selected", choices = choices, server = TRUE) |
| 41 |
|
| 42 |
trigger_shuffle <- reactiveVal(0) |
| 43 |
trigger_next <- reactiveVal(0) |
| 44 |
trigger_prev <- reactiveVal(0) |
| 45 |
|
| 46 |
observeEvent( |
| 47 |
input$shuffle, |
| 48 |
ignoreInit = TRUE, |
| 49 |
trigger_shuffle(trigger_shuffle() + 1) |
| 50 |
) |
| 51 |
|
| 52 |
observeEvent(trigger_shuffle(), { |
| 53 |
req(trigger_shuffle() > 0) |
| 54 |
|
| 55 |
updateSelectizeInput( |
| 56 |
session, |
| 57 |
"selected", |
| 58 |
selected = sample(unlist(choices), 1), |
| 59 |
choices = choices, |
| 60 |
server = TRUE |
| 61 |
) |
| 62 |
}) |
| 63 |
|
| 64 |
observeEvent(input[["next"]], move(1)) |
| 65 |
observeEvent(trigger_next(), move(1), ignoreInit = TRUE) |
| 66 |
|
| 67 |
observeEvent(input[["prev"]], move(-1)) |
| 68 |
observeEvent(trigger_prev(), move(-1), ignoreInit = TRUE) |
| 69 |
|
| 70 |
move <- reactiveVal(0) |
| 71 |
|
| 72 |
observeEvent(move(), { |
| 73 |
req(move() != 0) |
| 74 |
|
| 75 |
current <- input$selected |
| 76 |
choices_flat <- unlist(choices) |
| 77 |
|
| 78 |
idx <- which(choices_flat == current) + move() |
| 79 |
move(0) |
| 80 |
req(idx) |
| 81 |
|
| 82 |
if (idx > length(choices_flat)) { |
| 83 |
idx <- 1 |
| 84 |
} |
| 85 |
if (idx <= 0) { |
| 86 |
idx <- length(choices_flat) |
| 87 |
} |
| 88 |
|
| 89 |
updateSelectizeInput( |
| 90 |
session, |
| 91 |
"selected", |
| 92 |
selected = choices_flat[[idx]], |
| 93 |
choices = choices, |
| 94 |
server = TRUE |
| 95 |
) |
| 96 |
}) |
| 97 |
|
| 98 |
list( |
| 99 |
"value" = reactive(input$selected %||% ""), |
| 100 |
"shuffle" = function() trigger_shuffle(as.integer(Sys.time())), |
| 101 |
"next" = function() trigger_next(as.integer(Sys.time())), |
| 102 |
"prev" = function() trigger_prev(as.integer(Sys.time())), |
| 103 |
"choices" = choices, |
| 104 |
"set" = function(value) { |
| 105 |
updateSelectizeInput( |
| 106 |
session, |
| 107 |
"selected", |
| 108 |
selected = value, |
| 109 |
choices = choices, |
| 110 |
server = TRUE |
| 111 |
) |
| 112 |
} |
| 113 |
) |
| 114 |
} |
| 115 |
|
| 116 |
module_selextra <- function(id, choices) { |
| 117 |
callModule(server_selextra, id, choices = choices) |
| 118 |
} |