forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
267 lines (233 loc) · 7.24 KB
/
editor.go
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Package editor enables users to create edit views from their content
// structs so that admins can manage content
package editor
import (
"bytes"
"log"
"net/http"
)
// Editable ensures data is editable
type Editable interface {
MarshalEditor() ([]byte, error)
}
// Mergeable allows external post content to be approved and published through
// the public-facing API
type Mergeable interface {
// Approve copies an external post to the internal collection and triggers
// a re-sort of its content type posts
Approve(http.ResponseWriter, *http.Request) error
}
// Editor is a view containing fields to manage content
type Editor struct {
ViewBuf *bytes.Buffer
}
// Field is used to create the editable view for a field
// within a particular content struct
type Field struct {
View []byte
}
// Form takes editable content and any number of Field funcs to describe the edit
// page for any content struct added by a user
func Form(post Editable, fields ...Field) ([]byte, error) {
editor := &Editor{}
editor.ViewBuf = &bytes.Buffer{}
_, err := editor.ViewBuf.WriteString(`<table><tbody class="row"><tr class="col s8 editor-fields"><td class="col s12">`)
if err != nil {
log.Println("Error writing HTML string to editor Form buffer")
return nil, err
}
for _, f := range fields {
addFieldToEditorView(editor, f)
}
_, err = editor.ViewBuf.WriteString(`</td></tr>`)
if err != nil {
log.Println("Error writing HTML string to editor Form buffer")
return nil, err
}
// content items with Item embedded have some default fields we need to render
_, err = editor.ViewBuf.WriteString(`<tr class="col s4 default-fields"><td class="col s12">`)
if err != nil {
log.Println("Error writing HTML string to editor Form buffer")
return nil, err
}
publishTime := `
<div class="row content-only __ponzu">
<div class="input-field col s6">
<label class="active">MM</label>
<select class="month __ponzu browser-default">
<option value="1">Jan - 01</option>
<option value="2">Feb - 02</option>
<option value="3">Mar - 03</option>
<option value="4">Apr - 04</option>
<option value="5">May - 05</option>
<option value="6">Jun - 06</option>
<option value="7">Jul - 07</option>
<option value="8">Aug - 08</option>
<option value="9">Sep - 09</option>
<option value="10">Oct - 10</option>
<option value="11">Nov - 11</option>
<option value="12">Dec - 12</option>
</select>
</div>
<div class="input-field col s2">
<label class="active">DD</label>
<input value="" class="day __ponzu" maxlength="2" type="text" placeholder="DD" />
</div>
<div class="input-field col s4">
<label class="active">YYYY</label>
<input value="" class="year __ponzu" maxlength="4" type="text" placeholder="YYYY" />
</div>
</div>
<div class="row content-only __ponzu">
<div class="input-field col s3">
<label class="active">HH</label>
<input value="" class="hour __ponzu" maxlength="2" type="text" placeholder="HH" />
</div>
<div class="col s1">:</div>
<div class="input-field col s3">
<label class="active">MM</label>
<input value="" class="minute __ponzu" maxlength="2" type="text" placeholder="MM" />
</div>
<div class="input-field col s4">
<label class="active">Period</label>
<select class="period __ponzu browser-default">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
</div>
</div>
`
_, err = editor.ViewBuf.WriteString(publishTime)
if err != nil {
log.Println("Error writing HTML string to editor Form buffer")
return nil, err
}
err = addPostDefaultFieldsToEditorView(post, editor)
if err != nil {
return nil, err
}
submit := `
<div class="input-field post-controls">
<button class="right waves-effect waves-light btn green save-post" type="submit">Save</button>
<button class="right waves-effect waves-light btn red delete-post" type="submit">Delete</button>
</div>
`
_, ok := post.(Mergeable)
if ok {
submit +=
`
<div class="row external post-controls">
<div class="col s12 input-field">
<button class="right waves-effect waves-light btn blue approve-post" type="submit">Approve</button>
<button class="right waves-effect waves-light btn grey darken-2 reject-post" type="submit">Reject</button>
</div>
<label class="approve-details right-align col s12">This content is pending approval. By clicking 'Approve', it will be immediately published. By clicking 'Reject', it will be deleted.</label>
</div>
`
}
script := `
<script>
$(function() {
var form = $('form'),
save = form.find('button.save-post'),
del = form.find('button.delete-post'),
external = form.find('.post-controls.external'),
id = form.find('input[name=id]'),
timestamp = $('.__ponzu.content-only'),
slug = $('input[name=slug]');
// hide if this is a new post, or a non-post editor page
if (id.val() === '-1' || form.attr('action') !== '/admin/edit') {
del.hide();
external.hide();
}
// hide approval if not on a pending content item
if (getParam('status') !== 'pending') {
external.hide();
}
// no timestamp, slug visible on addons
if (form.attr('action') === '/admin/addon') {
timestamp.hide();
slug.parent().hide();
}
save.on('click', function(e) {
e.preventDefault();
if (getParam('status') === 'pending') {
var action = form.attr('action');
form.attr('action', action + '?status=pending')
}
form.submit();
});
del.on('click', function(e) {
e.preventDefault();
var action = form.attr('action');
action = action + '/delete';
form.attr('action', action);
if (confirm("[Ponzu] Please confirm:\n\nAre you sure you want to delete this post?\nThis cannot be undone.")) {
form.submit();
}
});
external.find('button.approve-post').on('click', function(e) {
e.preventDefault();
var action = form.attr('action');
action = action + '/approve';
form.attr('action', action);
form.submit();
});
external.find('button.reject-post').on('click', function(e) {
e.preventDefault();
var action = form.attr('action');
action = action + '/delete?reject=true';
form.attr('action', action);
if (confirm("[Ponzu] Please confirm:\n\nAre you sure you want to reject this post?\nDoing so will delete it, and cannot be undone.")) {
form.submit();
}
});
});
</script>
`
_, err = editor.ViewBuf.WriteString(submit + script + `</td></tr></tbody></table>`)
if err != nil {
log.Println("Error writing HTML string to editor Form buffer")
return nil, err
}
return editor.ViewBuf.Bytes(), nil
}
func addFieldToEditorView(e *Editor, f Field) error {
_, err := e.ViewBuf.Write(f.View)
if err != nil {
log.Println("Error writing field view to editor view buffer")
return err
}
return nil
}
func addPostDefaultFieldsToEditorView(p Editable, e *Editor) error {
defaults := []Field{
{
View: Input("Slug", p, map[string]string{
"label": "URL Slug",
"type": "text",
"disabled": "true",
"placeholder": "Will be set automatically",
}),
},
{
View: Timestamp("Timestamp", p, map[string]string{
"type": "hidden",
"class": "timestamp __ponzu",
}),
},
{
View: Timestamp("Updated", p, map[string]string{
"type": "hidden",
"class": "updated __ponzu",
}),
},
}
for _, f := range defaults {
err := addFieldToEditorView(e, f)
if err != nil {
return err
}
}
return nil
}