| 1 |
#' Panel with absolute positioning |
| 2 |
#' |
| 3 |
#' Creates a panel whose contents are absolutely positioned. |
| 4 |
#' |
| 5 |
#' The `absolutePanel` function creates a `<div>` tag whose CSS |
| 6 |
#' position is set to `absolute` (or fixed if `fixed = TRUE`). The way |
| 7 |
#' absolute positioning works in HTML is that absolute coordinates are specified |
| 8 |
#' relative to its nearest parent element whose position is not set to |
| 9 |
#' `static` (which is the default), and if no such parent is found, then |
| 10 |
#' relative to the page borders. If you're not sure what that means, just keep |
| 11 |
#' in mind that you may get strange results if you use `absolutePanel` from |
| 12 |
#' inside of certain types of panels. |
| 13 |
#' |
| 14 |
#' The `fixedPanel` function is the same as `absolutePanel` with |
| 15 |
#' `fixed = TRUE`. |
| 16 |
#' |
| 17 |
#' The position (`top`, `left`, `right`, `bottom`) and size |
| 18 |
#' (`width`, `height`) parameters are all optional, but you should |
| 19 |
#' specify exactly two of `top`, `bottom`, and `height` and |
| 20 |
#' exactly two of `left`, `right`, and `width` for predictable |
| 21 |
#' results. |
| 22 |
#' |
| 23 |
#' Like most other distance parameters in Shiny, the position and size |
| 24 |
#' parameters take a number (interpreted as pixels) or a valid CSS size string, |
| 25 |
#' such as `"100px"` (100 pixels) or `"25%"`. |
| 26 |
#' |
| 27 |
#' For arcane HTML reasons, to have the panel fill the page or parent you should |
| 28 |
#' specify `0` for `top`, `left`, `right`, and `bottom` |
| 29 |
#' rather than the more obvious `width = "100%"` and `height = |
| 30 |
#' "100%"`. |
| 31 |
#' |
| 32 |
#' @param ... Attributes (named arguments) or children (unnamed arguments) that |
| 33 |
#' should be included in the panel. |
| 34 |
#' |
| 35 |
#' @param top Distance between the top of the panel, and the top of the page or |
| 36 |
#' parent container. |
| 37 |
#' @param left Distance between the left side of the panel, and the left of the |
| 38 |
#' page or parent container. |
| 39 |
#' @param right Distance between the right side of the panel, and the right of |
| 40 |
#' the page or parent container. |
| 41 |
#' @param bottom Distance between the bottom of the panel, and the bottom of the |
| 42 |
#' page or parent container. |
| 43 |
#' @param width Width of the panel. |
| 44 |
#' @param height Height of the panel. |
| 45 |
#' @param draggable If `TRUE`, allows the user to move the panel by |
| 46 |
#' clicking and dragging. |
| 47 |
#' @param fixed Positions the panel relative to the browser window and prevents |
| 48 |
#' it from being scrolled with the rest of the page. |
| 49 |
#' @param cursor The type of cursor that should appear when the user mouses over |
| 50 |
#' the panel. Use `"move"` for a north-east-south-west icon, |
| 51 |
#' `"default"` for the usual cursor arrow, or `"inherit"` for the |
| 52 |
#' usual cursor behavior (including changing to an I-beam when the cursor is |
| 53 |
#' over text). The default is `"auto"`, which is equivalent to |
| 54 |
#' `ifelse(draggable, "move", "inherit")`. |
| 55 |
#' @return An HTML element or list of elements. |
| 56 |
#' @export |
| 57 |
absolutePanel <- function(..., |
| 58 |
top = NULL, left = NULL, right = NULL, bottom = NULL, |
| 59 |
width = NULL, height = NULL, |
| 60 |
draggable = FALSE, fixed = FALSE, |
| 61 |
cursor = c('auto', 'move', 'default', 'inherit')) { |
| 62 |
cssProps <- list( |
| 63 |
top = top, |
| 64 |
left = left, |
| 65 |
right = right, |
| 66 |
bottom = bottom, |
| 67 |
width = width, |
| 68 |
height = height |
| 69 |
) |
| 70 |
cssProps <- cssProps[!sapply(cssProps, is.null)] |
| 71 |
cssProps <- sapply(cssProps, validateCssUnit) |
| 72 |
cssProps[['position']] <- ifelse(fixed, 'fixed', 'absolute') |
| 73 |
cssProps[['cursor']] <- match.arg(cursor) |
| 74 |
if (identical(cssProps[['cursor']], 'auto')) |
| 75 |
cssProps[['cursor']] <- ifelse(draggable, 'move', 'inherit') |
| 76 |
|
| 77 |
style <- paste(paste(names(cssProps), cssProps, sep = ':', collapse = ';'), ';', sep='') |
| 78 |
divTag <- tags$div(style=style, ...) |
| 79 |
|
| 80 |
if (identical(draggable, FALSE)) { |
| 81 |
return(divTag) |
| 82 |
} |
| 83 |
|
| 84 |
# Add Shiny inputs and htmlwidgets to 'non-draggable' elements |
| 85 |
# Cf. https://api.jqueryui.com/draggable/#option-cancel |
| 86 |
dragOpts <- '{cancel: ".shiny-input-container,.html-widget,input,textarea,button,select,option"}' |
| 87 |
dragJS <- sprintf('$(".draggable").draggable(%s);', dragOpts) |
| 88 |
tagList( |
| 89 |
tagAppendAttributes(divTag, class='draggable'), |
| 90 |
jqueryuiDependency(), |
| 91 |
tags$script(HTML(dragJS)) |
| 92 |
) |
| 93 |
} |
| 94 |
|
| 95 |
#' @rdname absolutePanel |
| 96 |
#' @export |
| 97 |
fixedPanel <- function(..., |
| 98 |
top = NULL, left = NULL, right = NULL, bottom = NULL, |
| 99 |
width = NULL, height = NULL, |
| 100 |
draggable = FALSE, |
| 101 |
cursor = c('auto', 'move', 'default', 'inherit')) { |
| 102 |
absolutePanel(..., top=top, left=left, right=right, bottom=bottom, |
| 103 |
width=width, height=height, draggable=draggable, cursor=match.arg(cursor), |
| 104 |
fixed=TRUE) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
jqueryuiDependency <- function() { |
| 109 |
htmlDependency( |
| 110 |
"jqueryui", |
| 111 |
version_jqueryui, |
| 112 |
src = "www/shared/jqueryui", |
| 113 |
package = "shiny", |
| 114 |
script = "jquery-ui.min.js" |
| 115 |
) |
| 116 |
} |