/
f7Dialog.R
167 lines (162 loc) · 3.98 KB
/
f7Dialog.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
#' Framework7 dialog window
#'
#' \code{f7Dialog} generates a modal window.
#'
#' @param id Input associated to the alert. Works when type is one of
#' "confirm", "prompt" or "login".
#' @param title Dialog title
#' @param text Dialog text.
#' @param type Dialog type: \code{c("alert", "confirm", "prompt", "login")}.
#' @param session shiny session.
#'
#' @export
#'
#' @examples
#' # simple alert
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "Simple Dialog",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Dialog"),
#' f7Button(inputId = "goButton", "Go!")
#' )
#' ),
#' server = function(input, output, session) {
#' observeEvent(input$goButton,{
#' f7Dialog(
#' title = "Dialog title",
#' text = "This is an alert dialog"
#' )
#' })
#' }
#' )
#' }
#' # confirm alert
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "Confirm Dialog",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Dialog"),
#' f7Button(inputId = "goButton", "Go!")
#' )
#' ),
#' server = function(input, output, session) {
#'
#' observeEvent(input$goButton,{
#' f7Dialog(
#' id = "test",
#' title = "Dialog title",
#' type = "confirm",
#' text = "This is an alert dialog"
#' )
#' })
#'
#' observeEvent(input$test, {
#' f7Toast(text = paste("Alert input is:", input$test))
#' })
#'
#' }
#' )
#' }
#' # prompt dialog
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "Prompt Dialog",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Dialog"),
#' f7Button(inputId = "goButton", "Go!"),
#' uiOutput("res")
#' )
#' ),
#' server = function(input, output, session) {
#'
#' observe({
#' print(input$prompt)
#' })
#'
#' observeEvent(input$goButton,{
#' f7Dialog(
#' id = "prompt",
#' title = "Dialog title",
#' type = "prompt",
#' text = "This is a prompt dialog"
#' )
#' })
#'
#' output$res <- renderUI(f7BlockTitle(title = input$prompt, size = "large"))
#' }
#' )
#' }
#'
#' # login dialog
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "Login Dialog",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Dialog"),
#' f7Button(inputId = "goButton", "Go!"),
#' uiOutput("ui")
#' )
#' ),
#' server = function(input, output, session) {
#'
#' observe({
#' print(input$login)
#' })
#'
#' observeEvent(input$goButton,{
#' f7Dialog(
#' id = "login",
#' title = "Dialog title",
#' type = "login",
#' text = "This is an login dialog"
#' )
#' })
#'
#' output$ui <- renderUI({
#' req(input$login$user == "David" & input$login$password == "prout")
#' img(src = "https://media2.giphy.com/media/12gfL8Xxrhv7C1fXiV/giphy.gif")
#' })
#' }
#' )
#' }
f7Dialog <- function(id = NULL, title = NULL, text,
type = c("alert", "confirm", "prompt", "login"),
session = shiny::getDefaultReactiveDomain()) {
type <- match.arg(type)
if (is.null(id) && type %in% c("confirm", "prompt", "login")) {
stop("Missing id.")
}
# force to render shiny.tag and convert it to character
# since text does not accept anything else
text <- if (class(text) %in% c("shiny.tag" , "shiny.tag.list")) as.character(force(text)) else text
message <- dropNulls(
list(
id = id,
title = title,
text = text,
type = type
)
)
# see my-app.js function
session$sendCustomMessage(
type = "dialog",
message = jsonlite::toJSON(
message,
auto_unbox = TRUE,
json_verbatim = TRUE
)
)
}