forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_repeater.go
50 lines (42 loc) · 1.23 KB
/
input_repeater.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
package editor
import (
"bytes"
"log"
"strings"
)
func InputRepeater(fieldName string, p interface{}, attrs map[string]string) []byte {
// find the field values in p to determine pre-filled inputs
fieldVals := ValueFromStructField(fieldName, p, nil).(string)
vals := strings.Split(fieldVals, "__ponzu")
scope := TagNameFromStructField(fieldName, p, nil)
html := bytes.Buffer{}
_, err := html.WriteString(`<span class="__ponzu-repeat ` + scope + `">`)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
for i, val := range vals {
el := &Element{
TagName: "input",
Attrs: attrs,
Name: TagNameFromStructFieldMulti(fieldName, i, p),
Data: val,
ViewBuf: &bytes.Buffer{},
}
// only add the label to the first input in repeated list
if i == 0 {
el.Label = attrs["label"]
}
_, err := html.Write(DOMElementSelfClose(el))
if err != nil {
log.Println("Error writing DOMElementSelfClose to InputRepeater buffer")
return nil
}
}
_, err = html.WriteString(`</span>`)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
return append(html.Bytes(), RepeatController(fieldName, p, "input", ".input-field")...)
}