-
Notifications
You must be signed in to change notification settings - Fork 1
/
metroWindow.R
144 lines (138 loc) · 4.81 KB
/
metroWindow.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' Create a Metro 4 window
#'
#' Build a Metro window
#'
#' @param ... Any UI element.
#' @param title Window title.
#' @param icon Window icon.
#' @param shadow Whether to display a shadow around the window. TRUE by default.
#' @param header_color Header background color.
#' @param content_color Content background color.
#' @param content_text_color Content text color.
#' @param minimize Whether the window can be minimized. TRUE by default.
#' @param closable Whether the window can be closed. TRUE by default.
#' @param draggable Whether the window can be dragged. FALSE by default.
#' @param resizable Whether the window can be resized. FALSE by default.
#' @param width Window width.
#' @param height Window height.
#'
#' @note It is better to use only 2 windows per page and embed them as in the
#' example below.
#'
#' @examples
#' if(interactive()){
#' library(shiny)
#' library(shinyMetroUi)
#'
#' shiny::shinyApp(
#' ui = metroPage(
#' allow_loading = TRUE,
#' fluidRow(
#' metroCell(
#' metroWindow(
#' title = "Window",
#' resizable = TRUE,
#' width = "100%",
#' header_color = "red",
#' content_color = "black",
#' content_text_color = "yellow",
#' # content
#' metroTilesGrid(
#' metroTile(
#' size = "small",
#' color = "indigo",
#' icon = "github",
#' url = "https://github.com/olton/Metro-UI-CSS"),
#' metroTile(size = "small", color = "green", icon = "envelop"),
#' metroTile(size = "small", color = "blue", col_position = 1, row_position = 2),
#' metroTile(size = "small", color = "orange", col_position = 2, row_position = 2),
#' metroTile(
#' size = "wide",
#' color = "pink",
#' sliderInput("obs", "Number of observations:",
#' min = 0, max = 1000, value = 500
#' )
#' ),
#' metroTile(
#' size = "large",
#' color = "green",
#' selected = TRUE,
#' plotOutput("distPlot")
#' )
#' )
#' )
#' ),
#' metroCell(
#' metroWindow(
#' title = "Window",
#' resizable = TRUE,
#' width = "100%",
#' header_color = "red",
#' content_color = "black",
#' content_text_color = "yellow",
#' # content
#' metroTilesGrid(
#' metroTile(size = "large",color = "brown", icon = "windows"),
#' metroTile(size = "wide", color = "crimson", icon = "envelop"),
#' metroTile(size = "small", color = "cobalt", col_position = 1, row_position = 2),
#' metroTile(size = "small", color = "olive", col_position = 2, row_position = 2)
#' )
#' )
#' )
#' )
#' ),
#' server = function(input, output) {
#' output$distPlot <- renderPlot({
#' hist(rnorm(input$obs))
#' })
#' }
#' )
#' }
#'
#' @author David Granjon, \email{dgranjon@@ymail.com}
#'
#' @export
metroWindow <- function(..., title = NULL, icon = NULL, shadow = TRUE,
header_color = NULL, content_color = NULL, content_text_color = NULL,
minimize = TRUE, closable = TRUE,
draggable = FALSE, resizable = FALSE, width = NULL,
height = NULL){
windowClass <- "p-2"
whites <- c("light", "white", "grayWhite")
blacks <- c("black", "dark")
if (!is.null(content_color) && content_color %in% whites && content_text_color %in% whites) content_text_color <- "black"
if (!is.null(content_color) && content_color %in% blacks && content_text_color %in% blacks) content_text_color <- "white"
windowProps <- dropNulls(
list(
`data-role` = "window",
`data-title` = title,
`data-icon` = paste0("<span class='mif-", icon, "'></span>"),
`data-btn-close` = closable,
`data-btn-min` = minimize,
`data-btn-max` = FALSE,
`data-draggable` = draggable,
`data-resizable` = resizable,
`data-shadow` = shadow,
`data-cls-caption` = if (!is.null(header_color)) paste0("bg-", header_color) else NULL,
`data-cls-content` = if (!is.null(content_color) && !is.null(content_text_color)) {
paste0("bg-", content_color, " fg-", content_text_color)
} else {
NULL
},
`data-width` = width,
`data-height` = height
)
)
windowProps <- lapply(windowProps, function(x) {
if (identical(x, TRUE))
"true"
else if (identical(x, FALSE))
"false"
else x
})
div <- shiny::tags$div
windowTag <- do.call(div, windowProps)
windowTag <- shiny::tagAppendChild(windowTag, ...)
windowTag <- shiny::tagAppendAttributes(windowTag, class = windowClass)
windowTag
}