189 lines
5.5 KiB
Go
Raw Normal View History

2017-10-17 21:17:04 +00:00
package editform
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2017-11-27 16:31:02 +00:00
// Render renders a generic editing UI for any kind of datatype that has an ID.
2017-10-17 21:17:04 +00:00
func Render(obj interface{}, title string, user *arn.User) string {
t := reflect.TypeOf(obj).Elem()
v := reflect.ValueOf(obj).Elem()
id := reflect.Indirect(v.FieldByName("ID"))
lowerCaseTypeName := strings.ToLower(t.Name())
endpoint := `/api/` + lowerCaseTypeName + `/` + id.String()
var b bytes.Buffer
b.WriteString(`<div class="widget-form">`)
b.WriteString(`<div class="widget" data-api="` + endpoint + `">`)
2017-11-27 16:31:02 +00:00
// Title
2017-10-17 21:17:04 +00:00
b.WriteString(`<h1>`)
b.WriteString(title)
b.WriteString(`</h1>`)
2017-11-27 16:31:02 +00:00
// Render the object with its fields
2017-10-17 21:17:04 +00:00
RenderObject(&b, obj, "")
2017-11-27 16:31:02 +00:00
// Additional buttons when logged in
2017-10-28 15:18:27 +00:00
if user != nil {
2017-10-17 21:17:04 +00:00
b.WriteString(`<div class="buttons">`)
2017-11-19 01:04:37 +00:00
_, ok := t.FieldByName("IsDraft")
if ok {
isDraft := v.FieldByName("IsDraft").Interface().(bool)
if isDraft {
b.WriteString(`<div class="buttons"><button class="action" data-action="publish" data-trigger="click">` + utils.Icon("share-alt") + `Publish</button></div>`)
}
}
2017-10-28 15:18:27 +00:00
if user.Role == "editor" || user.Role == "admin" {
b.WriteString(`<button class="action" data-action="deleteObject" data-trigger="click" data-return-path="/` + lowerCaseTypeName + "s" + `" data-confirm-type="` + lowerCaseTypeName + `">` + utils.Icon("trash") + `Delete</button>`)
}
2017-10-17 21:17:04 +00:00
b.WriteString(`</div>`)
}
b.WriteString("</div>")
b.WriteString("</div>")
return b.String()
}
2017-11-27 17:04:28 +00:00
// RenderObject renders the UI for the object into the bytes buffer and appends an ID prefix for all API requests.
// The ID prefix should either be empty or end with a dot character.
2017-10-17 21:17:04 +00:00
func RenderObject(b *bytes.Buffer, obj interface{}, idPrefix string) {
2017-11-27 17:04:28 +00:00
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
2017-10-17 21:17:04 +00:00
// Fields
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
RenderField(b, &v, field, idPrefix)
}
}
// RenderField ...
func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, idPrefix string) {
if field.Anonymous || field.Tag.Get("editable") != "true" {
return
}
fieldValue := reflect.Indirect(v.FieldByName(field.Name))
2017-11-27 17:04:28 +00:00
fieldType := field.Type.String()
2017-10-17 21:17:04 +00:00
2017-11-27 17:04:28 +00:00
// String
if fieldType == "string" {
2017-10-21 22:17:35 +00:00
if field.Tag.Get("datalist") != "" {
dataList := field.Tag.Get("datalist")
values := arn.DataLists[dataList]
b.WriteString(components.InputSelection(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip"), values))
} else if field.Tag.Get("type") == "textarea" {
b.WriteString(components.InputTextArea(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip")))
2017-10-18 19:18:10 +00:00
} else {
2017-10-21 22:17:35 +00:00
b.WriteString(components.InputText(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip")))
2017-10-18 19:18:10 +00:00
}
2017-11-27 17:04:28 +00:00
return
}
2017-11-28 15:32:23 +00:00
// Int
if fieldType == "int" {
b.WriteString(components.InputNumber(idPrefix+field.Name, float64(fieldValue.Int()), field.Name, field.Tag.Get("tooltip"), "", "", "1"))
return
}
2017-11-27 17:34:34 +00:00
// Float
if fieldType == "float64" {
b.WriteString(components.InputNumber(idPrefix+field.Name, fieldValue.Float(), field.Name, field.Tag.Get("tooltip"), "", "", ""))
return
}
2017-11-27 17:04:28 +00:00
// Bool
if fieldType == "bool" {
2017-10-17 21:17:04 +00:00
if field.Name == "IsDraft" {
return
}
2017-11-27 17:04:28 +00:00
// TODO: Render bool type
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, "[]") {
b.WriteString(`<div class="widget-section">`)
b.WriteString(`<h3 class="widget-title">`)
b.WriteString(field.Name)
b.WriteString(`</h3>`)
2017-10-17 21:17:04 +00:00
for sliceIndex := 0; sliceIndex < fieldValue.Len(); sliceIndex++ {
b.WriteString(`<div class="widget-section">`)
2017-11-27 17:04:28 +00:00
b.WriteString(`<div class="widget-title">`)
// Title
b.WriteString(strconv.Itoa(sliceIndex+1) + ". " + field.Name)
b.WriteString(`<div class="spacer"></div>`)
// Remove button
b.WriteString(`<button class="action" data-action="arrayRemove" data-trigger="click" data-field="` + field.Name + `" data-index="`)
b.WriteString(strconv.Itoa(sliceIndex))
b.WriteString(`">` + utils.RawIcon("trash") + `</button>`)
b.WriteString(`</div>`)
2017-10-17 21:17:04 +00:00
arrayObj := fieldValue.Index(sliceIndex).Interface()
arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex)
RenderObject(b, arrayObj, arrayIDPrefix)
// Preview
2017-11-27 16:31:02 +00:00
// elementValue := fieldValue.Index(sliceIndex)
// RenderArrayElement(b, &elementValue)
2017-11-27 17:04:28 +00:00
if fieldType == "[]*arn.ExternalMedia" {
2017-11-27 16:31:02 +00:00
b.WriteString(components.ExternalMedia(fieldValue.Index(sliceIndex).Interface().(*arn.ExternalMedia)))
}
2017-10-17 21:17:04 +00:00
b.WriteString(`</div>`)
}
b.WriteString(`<div class="buttons">`)
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>`)
2017-11-19 00:42:52 +00:00
b.WriteString(`</div>`)
2017-11-27 17:04:28 +00:00
return
2017-10-17 21:17:04 +00:00
}
2017-11-27 17:04:28 +00:00
// 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>`)
2017-10-17 21:17:04 +00:00
}