/
server.R
215 lines (196 loc) · 6.71 KB
/
server.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
library(shinyAce)
library(shiny)
library(knitr)
if(!require(RWordPress))
stop("Please install the RWordPress package first.")
## User login information
userInfo = reactiveValues(LoggedIn = FALSE, URL = '', userName = '', passWord = '')
uiControl = reactiveValues(showFileUploadDlg=FALSE, saveFileMsg="", publishMsg="")
wpPosts = reactiveValues(posts=NULL)
## Get all posts
getAllPosts <- function(){
print("getAllPosts function invoked")
.login = userInfo$passWord
names(.login) = userInfo$userName
.server = userInfo$URL
posts = try(getPosts(login=.login, .server=.server), silent = TRUE)
if(inherits(posts, "try-error")) return(NULL)
options(WordpressLogin = .login,
WordpressURL = .server)
return(posts)
}
## Judge whether the user login information is correct
loginSucceed <- function(){
print("loginSucceed function invoked")
wpPosts$posts <<- getAllPosts()
if(is.null(wpPosts$posts)) return(FALSE)
return(TRUE)
}
shinyServer(function(input, output, session){
## Login Dialog UI
output$loginDlg <- renderUI({
print("output$loginDlg is invoked")
if(!userInfo$LoggedIn){
print("output$login invoked")
fluidRow(
column(12,
fluidRow(
column(3, offset = 2, textInput("URL", "Blog URL:")),
column(3, textInput("userName", "User Name:")),
column(3, textInput("passWord", "Password:")),
column(1, actionButton("loginBtn", "Login"))
),
fluidRow(
column(12, offset = 2, htmlOutput("loginMessage"))
)
)
)
} else {
div(id="loginIcon", userInfo$userName)
}
})
output$logoutDlg <- renderUI({
print("output$logoutDlg is invoked")
if(userInfo$LoggedIn){
fluidRow(
column(6, actionButton("newPostBtn", "New Post")),
column(6, actionButton("logoutBtn", "Sign Out"))
)
}
})
output$loginMessage <- renderUI({
print("output$loginMessage invoked")
if(!userInfo$LoggedIn){
if(input$loginBtn > 0){
userInfo$URL = isolate(input$URL)
userInfo$userName = isolate(input$userName)
userInfo$passWord = isolate(input$passWord)
if(loginSucceed()){
userInfo$LoggedIn = TRUE
""
} else{
"Incorrect Blog address or password, please input again"
}
}
}
})
output$postsList <- renderDataTable({
if(userInfo$LoggedIn){
print("output$postsList is invoked")
wpPosts$posts
}
})
output$knitDoc <- renderUI({
print("output$knitDoc is invoked")
return(HTML(knit2html(text = input$editor, fragment.only = TRUE, quiet = TRUE)))
})
output$loadFileDlg <- renderUI({
if(uiControl$showFileUploadDlg){
div(class="modal-dialog", fileInput('rmdFile', 'Please choose Rmd file'),
actionButton("loadFileDlgCloseBtn", "Close"))
}
})
output$saveFileMsg <- renderUI({
div(class="status-message", p(uiControl$saveFileMsg))
})
output$publishMsg <- renderUI({
div(class="status-message", p(uiControl$publishMsg))
})
## Observe the New Post button click event
observe({
if(!is.null(input$newPostBtn) && input$newPostBtn > 0){
updateTabsetPanel(session, "pepTabSetPanel", selected = "Editor")
updateAceEditor(session, "editor", value=" ")
}
})
## Observe the Sign Out button click event
observe({
if(!is.null(input$logoutBtn) && input$logoutBtn > 0){
print("input$logoutBtn is invoked")
userInfo$LoggedIn = FALSE
updateTextInput(session, "URL", value=userInfo$URL)
updateTextInput(session, "userName", value=userInfo$userName)
updateTextInput(session, "passWord", value=userInfo$passWord)
}
})
## Observe the New file button click event
observe({
if(!is.null(input$newFileBtn) && input$newFileBtn > 0){
print("input$newFileBtn is invoked")
updateAceEditor(session, "editor", value=" ")
}
})
## Observe the Save file button click event
observe({
if(!is.null(input$saveFileBtn) && input$saveFileBtn > 0){
print("input$saveFileBtn is invoked")
title = isolate(input$title)
if(title == ""){
uiControl$saveFileMsg = "Please input the title."
} else
{
title = paste0(gsub(" ", "-", title), ".Rmd")
fPath = paste0(getwd(), "/", title)
writeLines(isolate(input$editor), con = fPath)
uiControl$saveFileMsg = paste0("The file has been succeeded saved to: ", fPath)
}
}
})
## Observe the Load file button click event
observe({
if(!is.null(input$loadFileBtn) && input$loadFileBtn > 0){
uiControl$showFileUploadDlg = TRUE
}
})
## Observe the Delete button click event
observe({
if(!is.null(input$deleteBtn) && input$deleteBtn > 0){
postid = isolate(input$postId)
status = try(deletePost(postid), silent = TRUE)
if(inherits(status, "try-error")){
uiControl$publishMsg = paste0("Failed to delete the post with postoid", postid)
} else{
wpPosts$posts = getAllPosts()
uiControl$publishMsg = paste0("Post with postid", postid, " has been deleted successfully!")
}
}
})
## Observe the Publish button click event
observe({
if(!is.null(input$publishBtn) && input$publishBtn > 0){
print("input$publishBtn invoked")
content = knit2html(text = isolate(input$editor), fragment.only = TRUE, quiet = TRUE)
if(isolate(input$shortcodeCBox)){
content = gsub("<pre><code class=\"([[:alpha:]]+)\">",
"[sourcecode language=\"\\1\"]", content)
content = gsub("<pre><code( class=\"no-highlight\"|)>",
"[sourcecode]", content)
content = gsub("</code></pre>", "[/sourcecode]", content)
}
title = isolate(input$title)
status = try(newPost(list(description = content, title = title)), silent = TRUE)
if(inherits(status, "try-error")){
uiControl$publishMsg = "Failed to publish this post to wordpress!"
} else{
wpPosts$posts = getAllPosts()
uiControl$publishMsg = paste0('"', title, '"', ' has been published to wordpress successfully!')
}
}
})
## Observe the load file dialog tutton click event
observe({
if(!is.null(input$loadFileDlgCloseBtn) && input$loadFileDlgCloseBtn > 0){
uiControl$showFileUploadDlg = FALSE
}
})
## Observe the file upload event and update the editor content
observe({
inFile = input$rmdFile
if (is.null(inFile))
return(NULL)
tmpFile = readLines(inFile$datapath)
updateAceEditor(session, "editor", value=paste(tmpFile, collapse="\n"))
Sys.sleep(0.5) ## To see the loading animation:D
uiControl$showFileUploadDlg = FALSE
})
})