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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
package editor
import (
"bytes"
"fmt"
"reflect"
)
type element struct {
TagName string
Attrs map[string]string
Name string
label string
data string
viewBuf *bytes.Buffer
}
// Input returns the []byte of an <input> HTML element with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Input(fieldName string, p interface{}, attrs map[string]string) []byte {
e := newElement("input", attrs["label"], fieldName, p, attrs)
return domElementSelfClose(e)
}
// Textarea returns the []byte of a <textarea> HTML element with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Textarea(fieldName string, p interface{}, attrs map[string]string) []byte {
e := newElement("textarea", attrs["label"], fieldName, p, attrs)
return domElement(e)
}
// Select returns the []byte of a <select> HTML element plus internal <options> with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Select(fieldName string, p interface{}, attrs, options map[string]string) []byte {
// options are the value attr and the display value, i.e.
// <option value="{map key}">{map value}</option>
// find the field value in p to determine if an option is pre-selected
fieldVal := valueFromStructField(fieldName, p).String()
// may need to alloc a buffer, as we will probably loop through options
// and append the []byte from domElement() called for each option
sel := newElement("select", attrs["label"], fieldName, p, attrs)
var opts []*element
// provide a call to action for the select element
cta := &element{
TagName: "option",
Attrs: map[string]string{"disabled": "true", "selected": "true"},
data: "Select an option...",
viewBuf: &bytes.Buffer{},
}
// provide a selection reset (will store empty string in db)
reset := &element{
TagName: "option",
Attrs: map[string]string{"value": ""},
data: "None",
viewBuf: &bytes.Buffer{},
}
opts = append(opts, cta, reset)
var val string
for k, v := range options {
if k == fieldVal {
val = "true"
} else {
val = "false"
}
opt := &element{
TagName: "option",
Attrs: map[string]string{"value": k, "selected": val},
data: v,
viewBuf: &bytes.Buffer{},
}
// if val is false (unselected option), delete the attr for clarity
if val == "false" {
delete(opt.Attrs, "selected")
}
opts = append(opts, opt)
}
return domElementWithChildren(sel, opts)
}
// Checkbox returns the []byte of a set of <input type="checkbox"> HTML elements
// wrapped in a <div> with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Checkbox(fieldName string, p interface{}, attrs, options map[string]string) []byte {
// options are the value attr and the display value, i.e.
/*
<label>
{map value}
<input type="checkbox" name="{fieldName}" value="{map key}"/>
</label>
*/
div := newElement("div", attrs["label"], "", p, attrs)
var opts []*element
// get the pre-checked options if this is already an existing post
checkedVals := valueFromStructField(fieldName, p) // returns refelct.Value
checked := checkedVals.Slice(0, checkedVals.Len()).Interface().([]string) // casts reflect.Value to []string
i := 0
for k, v := range options {
// check if k is in the pre-checked values and set to checked
var val string
for _, x := range checked {
if k == x {
val = "true"
}
}
// create a *element manually using the maodified tagNameFromStructFieldMulti
// func since this is for a multi-value name
input := &element{
TagName: "input",
Attrs: map[string]string{
"type": "checkbox",
"checked": val,
"value": k,
},
Name: tagNameFromStructFieldMulti(fieldName, i, p),
label: v,
data: "",
viewBuf: &bytes.Buffer{},
}
// if checked == false, delete it from input.Attrs for clarity
if input.Attrs["checked"] == "" {
delete(input.Attrs, "checked")
}
opts = append(opts, input)
i++
}
return domElementWithChildrenCheckbox(div, opts)
}
// domElementSelfClose is a special DOM element which is parsed as a
// self-closing tag and thus needs to be created differently
func domElementSelfClose(e *element) []byte {
if e.label != "" {
e.viewBuf.Write([]byte(`<label>` + e.label))
}
e.viewBuf.Write([]byte(`<` + e.TagName + ` value="`))
e.viewBuf.Write([]byte(e.data + `" `))
for attr, value := range e.Attrs {
e.viewBuf.Write([]byte(attr + `="` + value + `" `))
}
e.viewBuf.Write([]byte(` name="` + e.Name + `"`))
e.viewBuf.Write([]byte(` />`))
if e.label != "" {
e.viewBuf.Write([]byte(`</label>`))
}
return e.viewBuf.Bytes()
}
// domElementCheckbox is a special DOM element which is parsed as a
// checkbox input tag and thus needs to be created differently
func domElementCheckbox(e *element) []byte {
if e.label != "" {
e.viewBuf.Write([]byte(`<label>`))
}
e.viewBuf.Write([]byte(`<` + e.TagName + ` `))
for attr, value := range e.Attrs {
e.viewBuf.Write([]byte(attr + `="` + value + `" `))
}
e.viewBuf.Write([]byte(` name="` + e.Name + `"`))
e.viewBuf.Write([]byte(` /> `))
if e.label != "" {
e.viewBuf.Write([]byte(e.label + `</label>`))
}
return e.viewBuf.Bytes()
}
// domElement creates a DOM element
func domElement(e *element) []byte {
if e.label != "" {
e.viewBuf.Write([]byte(`<label>` + e.label))
}
e.viewBuf.Write([]byte(`<` + e.TagName + ` `))
for attr, value := range e.Attrs {
e.viewBuf.Write([]byte(attr + `="` + string(value) + `" `))
}
e.viewBuf.Write([]byte(` name="` + e.Name + `"`))
e.viewBuf.Write([]byte(` >`))
e.viewBuf.Write([]byte(e.data))
e.viewBuf.Write([]byte(`</` + e.TagName + `>`))
if e.label != "" {
e.viewBuf.Write([]byte(`</label>`))
}
return e.viewBuf.Bytes()
}
func domElementWithChildren(e *element, children []*element) []byte {
if e.label != "" {
e.viewBuf.Write([]byte(`<label>` + e.label))
}
e.viewBuf.Write([]byte(`<` + e.TagName + ` `))
for attr, value := range e.Attrs {
e.viewBuf.Write([]byte(attr + `="` + string(value) + `" `))
}
e.viewBuf.Write([]byte(` name="` + e.Name + `"`))
e.viewBuf.Write([]byte(` >`))
// loop over children and create domElement for each child
for _, child := range children {
e.viewBuf.Write(domElement(child))
}
e.viewBuf.Write([]byte(`</` + e.TagName + `>`))
if e.label != "" {
e.viewBuf.Write([]byte(`</label>`))
}
return e.viewBuf.Bytes()
}
func domElementWithChildrenCheckbox(e *element, children []*element) []byte {
if e.label != "" {
e.viewBuf.Write([]byte(`<label>` + e.label))
}
e.viewBuf.Write([]byte(`<` + e.TagName + ` `))
for attr, value := range e.Attrs {
e.viewBuf.Write([]byte(attr + `="` + value + `" `))
}
e.viewBuf.Write([]byte(` name="` + e.Name + `"`))
e.viewBuf.Write([]byte(` >`))
// loop over children and create domElement for each child
for _, child := range children {
e.viewBuf.Write(domElementCheckbox(child))
}
e.viewBuf.Write([]byte(`</` + e.TagName + `>`))
if e.label != "" {
e.viewBuf.Write([]byte(`</label>`))
}
return e.viewBuf.Bytes()
}
func tagNameFromStructField(name string, post interface{}) string {
// sometimes elements in these environments will not have a name,
// and thus no tag name in the struct which correlates to it.
if name == "" {
return name
}
field, ok := reflect.TypeOf(post).Elem().FieldByName(name)
if !ok {
panic("Couldn't get struct field for: " + name + ". Make sure you pass the right field name to editor field elements.")
}
tag, ok := field.Tag.Lookup("json")
if !ok {
panic("Couldn't get json struct tag for: " + name + ". Struct fields for content types must have 'json' tags.")
}
return tag
}
// due to the format in which gorilla/schema expects form names to be when
// one is associated with multiple values, we need to output the name as such.
// Ex. 'category.0', 'category.1', 'category.2' and so on.
func tagNameFromStructFieldMulti(name string, i int, post interface{}) string {
tag := tagNameFromStructField(name, post)
return fmt.Sprintf("%s.%d", tag, i)
}
func valueFromStructField(name string, post interface{}) reflect.Value {
field := reflect.Indirect(reflect.ValueOf(post)).FieldByName(name)
return field
}
func newElement(tagName, label, fieldName string, p interface{}, attrs map[string]string) *element {
return &element{
TagName: tagName,
Attrs: attrs,
Name: tagNameFromStructField(fieldName, p),
label: label,
data: valueFromStructField(fieldName, p).String(),
viewBuf: &bytes.Buffer{},
}
}
|