Made editform less complex

This commit is contained in:
Eduard Urbach 2018-04-26 12:28:58 +02:00
parent 30955b041c
commit e178e84f8b

View File

@ -115,6 +115,58 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
// String
if fieldType == "string" {
renderStringField(b, v, field, idPrefix, fieldValue)
return
}
// Int
if fieldType == "int" {
b.WriteString(components.InputNumber(idPrefix+field.Name, float64(fieldValue.Int()), field.Name, field.Tag.Get("tooltip"), "", "", "1"))
return
}
// Float
if fieldType == "float64" {
b.WriteString(components.InputNumber(idPrefix+field.Name, fieldValue.Float(), field.Name, field.Tag.Get("tooltip"), "", "", ""))
return
}
// Bool
if fieldType == "bool" {
if field.Name == "IsDraft" {
return
}
b.WriteString(components.InputBool(idPrefix+field.Name, fieldValue.Bool(), field.Name, field.Tag.Get("tooltip")))
return
}
// Array of strings
if fieldType == "[]string" {
b.WriteString(components.InputTags(idPrefix+field.Name, fieldValue.Interface().([]string), field.Name, field.Tag.Get("tooltip")))
return
}
// Any kind of array
if strings.HasPrefix(fieldType, "[]") {
renderSliceField(b, v, field, idPrefix, fieldType, fieldValue)
return
}
// Any custom field type will be recursively rendered via another RenderObject call
b.WriteString(`<div class="widget-section">`)
b.WriteString(`<h3 class="widget-title">` + field.Name + `</h3>`)
// Indent the fields
b.WriteString(`<div class="indent">`)
RenderObject(b, fieldValue.Interface(), field.Name+".")
b.WriteString(`</div>`)
b.WriteString(`</div>`)
}
// String field
func renderStringField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, idPrefix string, fieldValue reflect.Value) {
idType := field.Tag.Get("idType")
// Try to infer the ID type by the field name
@ -184,40 +236,10 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
if showPreview {
b.WriteString("</div></div>")
}
}
return
}
// Int
if fieldType == "int" {
b.WriteString(components.InputNumber(idPrefix+field.Name, float64(fieldValue.Int()), field.Name, field.Tag.Get("tooltip"), "", "", "1"))
return
}
// Float
if fieldType == "float64" {
b.WriteString(components.InputNumber(idPrefix+field.Name, fieldValue.Float(), field.Name, field.Tag.Get("tooltip"), "", "", ""))
return
}
// Bool
if fieldType == "bool" {
if field.Name == "IsDraft" {
return
}
b.WriteString(components.InputBool(idPrefix+field.Name, fieldValue.Bool(), field.Name, field.Tag.Get("tooltip")))
return
}
// Array of strings
if fieldType == "[]string" {
b.WriteString(components.InputTags(idPrefix+field.Name, fieldValue.Interface().([]string), field.Name, field.Tag.Get("tooltip")))
return
}
// Any kind of array
if strings.HasPrefix(fieldType, "[]") {
// Slice field
func renderSliceField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, idPrefix string, fieldType string, fieldValue reflect.Value) {
b.WriteString(`<div class="widget-section">`)
b.WriteString(`<h3 class="widget-title">`)
b.WriteString(field.Name)
@ -257,19 +279,6 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
b.WriteString(`<button class="action" data-action="arrayAppend" data-trigger="click" data-field="` + field.Name + `">` + utils.Icon("plus") + `Add ` + field.Name + `</button>`)
b.WriteString(`</div>`)
b.WriteString(`</div>`)
return
}
// Any custom field type will be recursively rendered via another RenderObject call
b.WriteString(`<div class="widget-section">`)
b.WriteString(`<h3 class="widget-title">` + field.Name + `</h3>`)
// Indent the fields
b.WriteString(`<div class="indent">`)
RenderObject(b, fieldValue.Interface(), field.Name+".")
b.WriteString(`</div>`)
b.WriteString(`</div>`)
}