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
|
// Package editor enables users to create edit views from their content
// structs so that admins can manage content
package editor
import (
"bytes"
)
// Editable ensures data is editable
type Editable interface {
SetContentID(id int)
ContentID() int
ContentName() string
SetSlug(slug string)
Editor() *Editor
MarshalEditor() ([]byte, 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 := post.Editor()
editor.ViewBuf = &bytes.Buffer{}
editor.ViewBuf.Write([]byte(`<table><tbody class="row"><tr class="col s8"><td>`))
for _, f := range fields {
addFieldToEditorView(editor, f)
}
editor.ViewBuf.Write([]byte(`</td></tr>`))
// content items with Item embedded have some default fields we need to render
editor.ViewBuf.Write([]byte(`<tr class="col s4 default-fields"><td>`))
publishTime := `
<div class="row">
<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">
<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>
`
editor.ViewBuf.Write([]byte(publishTime))
addPostDefaultFieldsToEditorView(post, editor)
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>
<script>
$(function() {
var form = $('form'),
del = form.find('button.delete-post'),
id = form.find('input[name=id]');
// hide delete button if this is a new post, or a non-post editor page
if (id.val() === '-1' || form.attr('action') !== '/admin/edit') {
del.hide();
}
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();
}
});
});
</script>
`
editor.ViewBuf.Write([]byte(submit + `</td></tr></tbody></table>`))
return editor.ViewBuf.Bytes(), nil
}
func addFieldToEditorView(e *Editor, f Field) {
e.ViewBuf.Write(f.View)
}
func addPostDefaultFieldsToEditorView(p Editable, e *Editor) {
defaults := []Field{
Field{
View: Input("Slug", p, map[string]string{
"label": "URL Slug",
"type": "text",
"disabled": "true",
"placeholder": "Will be set automatically",
}),
},
Field{
View: Timestamp("Timestamp", p, map[string]string{
"type": "hidden",
"class": "timestamp __ponzu",
}),
},
Field{
View: Timestamp("Updated", p, map[string]string{
"type": "hidden",
"class": "updated __ponzu",
}),
},
}
for _, f := range defaults {
addFieldToEditorView(e, f)
}
}
|