-
Notifications
You must be signed in to change notification settings - Fork 2
/
accordions.R
232 lines (226 loc) · 6.16 KB
/
accordions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#' Generate an accordion
#'
#' Wrap one or more accordion items into a container
#'
#' @param ... The accordionItem elements to include within the body of the particular accordion
#' @param id The identification of the accordion
#' @param style Style of the accordion, use "default", "1", "2", "3", "4"
#' @param uncollapsed Indicate by number which accordionItem should not be collapsed
#' @return HTML of a container with a class called accordion
#' @examples
#' \donttest{
#' if(interactive()){
#'
#' # Example 1
#'
#' library(shiny)
#' library(nextGenShinyApps)
#'
#' shiny::shinyApp(
#' ui = fluidPage(
#' style = "8",
#' custom.bg.color = "#d9d9d9",
#' sidebar = NULL,
#' header = titlePanel(left="Sample Ex1"),
#' accordion(
#' id = "accordion5",
#' style = "4",
#' accordionItem(
#' title = "Accordion 1",
#' "Massa sed elementum sus"
#' ),
#' accordionItem(
#' title = "Accordion 2",
#' "Auctor neque etiam non."
#' )
#' )
#' ),
#' server = function(input, output) {
#' }
#' )
#'
#'
#' # Example 2
#'
#' library(shiny)
#' library(nextGenShinyApps)
#'
#' shiny::shinyApp(
#' ui = fluidPage(
#' style = "8",
#' custom.bg.color = "#f7c4bb",
#' sidebar = NULL,
#' header = titlePanel(left="Sample Ex2"),
#' accordion(
#' id = "accordion5",
#' style = "1",
#' accordionItem(
#' title = "Accordion A",
#' icon = shiny::icon("trash"),
#' "Massa sed elementum sus"
#' ),
#' accordionItem(
#' title = "Accordion B",
#' icon = shiny::icon("edit"),
#' "Auctor neque etiam non."
#' )
#' )
#' ),
#' server = function(input, output) {
#' }
#' )
#'
#'
#' }
#'
#' }
#'
#' @export
#'
accordion <- function(..., id, style = c("default", "1", "2", "3", "4"), uncollapsed = 1) {
accordian.parent <- paste0("id", id, rand.num(1))
accordian.parent <- gsub("[^[:alnum:] ]", "", accordian.parent)
# get the type of tabset
style <- match.arg(style)
# insert class based on the tabset
top.side <- ""
right.side <- ""
hover <- FALSE
switch(style,
"default" = {
left.side <- ""
},
"1" = {
left.side <- tags$span(
class = "ml-auto",
tags$span(
class = "collapsed-reveal",
tags$i(class = "fa fa-minus-circle text-danger fs-xl")
),
tags$span(
class = "collapsed-hidden",
tags$i(class = "fa fa-plus-circle text-success fs-xl")
)
)
},
"2" = {
left.side <- tags$span(
class = "ml-auto",
tags$span(
class = "collapsed-reveal",
tags$i(class = "fa fa-minus fs-xl")
),
tags$span(
class = "collapsed-hidden",
tags$i(class = "fa fa-plus fs-xl")
)
)
top.side <- "accordion-outline"
},
"3" = {
left.side <- ""
right.side <- tags$span(
class = "mr-2",
tags$span(
class = "collapsed-reveal",
tags$i(class = "fa fa-minus fs-xl")
),
tags$span(
class = "collapsed-hidden",
tags$i(class = "fa fa-plus fs-xl")
)
)
top.side <- "accordion-clean"
},
"4" = {
left.side <- tags$span(
class = "ml-auto",
tags$span(
class = "collapsed-reveal",
tags$i(class = "fa fa-chevron-up fs-xl")
),
tags$span(
class = "collapsed-hidden",
tags$i(class = "fa fa-chevron-down fs-xl")
)
)
hover <- TRUE
}
)
tabItems <- list(...)
count.pre <- as.integer(Sys.time()) - rand.num(1)
count.title <- 0
part.content <- ""
collapsed.num <- 1
# loop through the items of the accordian
for (tab in tabItems) {
count.title <- count.title + 1
activeOrNot <- ifelse(count.title == 1, " active", "")
collapsed.1 <- ifelse(as.integer(uncollapsed) == collapsed.num, TRUE, FALSE)
# for content
part.content <- paste0(
part.content,
div(
class = "card", div(
class = "card-header",
class = paste0("bg-", tab$status),
tags$a(
href = "javascript:void(0);", class = "card-title", class = ifelse(!collapsed.1, "collapsed", ""), `data-toggle` = "collapse", `data-target` = paste0("#", tab$id), `aria-expanded` = tolower(as.character(collapsed.1)),
if (style == "3") {
right.side
} else {
if (typeof(tab$icon) == "list") {
tags$span(class = "width-2 fs-xl", list(tab$icon))
} else {
""
}
},
tab$title,
left.side
)
),
div(
id = tab$id, class = "collapse", class = ifelse(collapsed.1, "show", ""), `data-parent` = paste0("#", accordian.parent),
div(
class = "card-body",
tab$content
)
)
)
)
collapsed.num <- 1 + collapsed.num
}
div(
class = "accordion",
class = top.side,
class = ifelse(hover, "accordion-hover", ""),
id = accordian.parent,
HTML(part.content)
)
}
#' Generate an accordion item
#'
#' Embed an accordion item within an accordion
#'
#' @param ... The elements to include within the body of the particular accordion
#' @param title The title of the accordion item
#' @param status Set the header background using either of "default", "primary", "secondary", "info", "success", "danger", "warning"
#' @param icon Include an icon to the left of the title for the accordion item
#' @return A list of properties for an accordion item
#' @examples
#' if (interactive()) {
#' accordionItem(
#' title = "Accordion 2",
#' icon = shiny::icon("cog"),
#' "Auctor neque etiam non."
#' )
#' }
#'
#' @export
#'
#'
accordionItem <- function(..., title = "A title", status = c("default", "primary", "secondary", "info", "success", "danger", "warning"), icon = NULL) {
status <- match.arg(status)
accordianid <- paste0("accordion1a", rand.num(1))
list(title = title, id = accordianid, status = status, content = div(...), icon = icon)
}