forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_collection.go
173 lines (147 loc) · 4.36 KB
/
field_collection.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
package editor
import (
"bytes"
"encoding/json"
"fmt"
"github.com/fanky5g/ponzu/content"
"reflect"
)
func FieldCollection(fieldName, label string, p interface{}, types map[string]func(interface{}, *FieldArgs, ...Field) []byte) []byte {
scope := TagNameFromStructField(fieldName, p, nil)
tmpl := `
<div class="input-field col s12 clearfix __ponzu-field-collection ` + scope + `">
<label class="active">` + label + `</label>
`
value := ValueByName(fieldName, p, nil)
fieldCollections, ok := value.Interface().(content.FieldCollections)
if !ok {
panic(fmt.Sprintf("Ponzu: '%s' is not a valid FieldCollections type", value.Type()))
}
positionalPlaceHolder := "%pos%"
parentFieldPath := fmt.Sprintf("%s.%s.Value", fieldName, positionalPlaceHolder)
typeTemplateMap := make(map[string]string)
for typeName := range fieldCollections.AllowedTypes() {
var emptyType interface{}
emptyType, err := makeTypeWithEmptyAllowedTypes(p, fieldName, typeName)
if err != nil {
panic(err)
}
fieldCollectionTemplate := types[typeName](
emptyType,
&FieldArgs{
Parent: parentFieldPath,
TypeName: typeName,
},
Field{
View: []byte(
fmt.Sprintf(
`<input name="%s" type="hidden" value="%s" />`,
fmt.Sprintf("%s.%s.type", scope, positionalPlaceHolder),
typeName,
),
),
},
)
typeTemplateMap[typeName] = string(fieldCollectionTemplate)
}
templatesBytes, err := json.Marshal(typeTemplateMap)
if err != nil {
panic(fmt.Sprintf("Failed to parse templates: %s", err))
}
data := fieldCollections.Data()
for i := 0; i < len(data); i++ {
typeName := data[i].Type
fieldCollectionTemplate := types[typeName](
p,
&FieldArgs{
Parent: fmt.Sprintf("%s.%d.Value", fieldName, i),
TypeName: typeName,
},
Field{
View: []byte(
fmt.Sprintf(
`<input name="%s" type="hidden" value="%s" />`,
fmt.Sprintf("%s.%d.type", scope, i),
typeName,
),
),
})
tmpl += string(fieldCollectionTemplate)
}
blockSelector := "block-select"
tmpl += string(getBlockSelector(blockSelector, types))
tmpl += `</div>`
script := &bytes.Buffer{}
scriptTmpl := makeScript("field_collection")
if err = scriptTmpl.Execute(script, struct {
Scope string
BlockSelector string
FieldCollectionTemplates string
PositionalPlaceholder string
FieldCollectionInputTypeName string
NumItems int
}{
Scope: scope,
BlockSelector: blockSelector,
FieldCollectionTemplates: string(templatesBytes),
PositionalPlaceholder: positionalPlaceHolder,
NumItems: len(data),
FieldCollectionInputTypeName: fmt.Sprintf("%s.%s.type", scope, positionalPlaceHolder),
}); err != nil {
panic(err)
}
return append([]byte(tmpl), script.Bytes()...)
}
func makeTypeWithEmptyAllowedTypes(p interface{}, fieldName, typeName string) (interface{}, error) {
pType := reflect.TypeOf(p)
if pType.Kind() == reflect.Pointer {
pType = pType.Elem()
}
emptyType := reflect.New(pType).Interface()
value := ValueByName(fieldName, emptyType, nil)
iface := reflect.New(value.Type().Elem()).Interface()
fieldCollections, ok := (iface).(content.FieldCollections)
if !ok {
return nil, fmt.Errorf("ponzu: '%s' is not a valid FieldCollections type", value.Type())
}
allowedTypes := fieldCollections.AllowedTypes()
t, ok := allowedTypes[typeName]
if !ok {
return nil, fmt.Errorf("invalid type %s", typeName)
}
fieldCollections.Add(content.FieldCollection{
Type: typeName,
Value: t(),
})
value.Set(reflect.ValueOf(iface))
return emptyType, nil
}
func getBlockSelector(
selectorName string,
types map[string]func(interface{}, *FieldArgs, ...Field) []byte) []byte {
var opts []*Element
sel := &Element{
TagName: "select",
Attrs: map[string]string{
"class": fmt.Sprintf("browser-default %s", selectorName),
},
Label: "Blocks",
ViewBuf: &bytes.Buffer{},
}
cta := &Element{
TagName: "option",
Attrs: map[string]string{"disabled": "true", "selected": "true", "value": ""},
Data: "Select a block...",
ViewBuf: &bytes.Buffer{},
}
opts = append(opts, cta)
for k := range types {
opts = append(opts, &Element{
TagName: "option",
Attrs: map[string]string{"value": k},
Data: k,
ViewBuf: &bytes.Buffer{},
})
}
return DOMElementWithChildrenSelect(sel, opts)
}