340 lines
9.4 KiB
Go
Raw Normal View History

2017-10-17 21:17:04 +00:00
package editform
import (
"fmt"
2019-06-07 01:03:16 +00:00
"io"
2017-10-17 21:17:04 +00:00
"reflect"
"strconv"
"strings"
"github.com/aerogo/api"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2019-08-31 07:52:42 +00:00
"github.com/animenotifier/notify.moe/arn/limits"
2017-10-17 21:17:04 +00:00
"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()
2018-03-09 15:08:35 +00:00
id := findMainID(t, v)
2017-10-17 21:17:04 +00:00
lowerCaseTypeName := strings.ToLower(t.Name())
endpoint := `/api/` + lowerCaseTypeName + `/` + id.String()
2019-04-30 14:59:53 +00:00
var b strings.Builder
2017-10-17 21:17:04 +00:00
b.WriteString(`<div class="widget-form">`)
b.WriteString(`<div class="widget" data-api="` + endpoint + `">`)
2017-11-27 16:31:02 +00:00
// Title
b.WriteString(`<h1 class="mountable">`)
2017-10-17 21:17:04 +00:00
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
2018-03-21 02:42:18 +00:00
// Publish button
2017-11-19 01:04:37 +00:00
_, ok := t.FieldByName("IsDraft")
if ok {
isDraft := v.FieldByName("IsDraft").Interface().(bool)
if isDraft {
2018-03-15 16:01:00 +00:00
b.WriteString(`<div class="buttons"><button class="mountable action" data-action="publish" data-trigger="click">` + utils.Icon("share-alt") + `Publish</button></div>`)
2017-11-19 01:04:37 +00:00
}
}
2017-10-28 15:18:27 +00:00
// Redownload button
track, isSoundTrack := obj.(*arn.SoundTrack)
if isSoundTrack && !track.IsDraft && track.HasMediaByService("Youtube") && track.File == "" && (user.Role == "editor" || user.Role == "admin") {
b.WriteString(`<button class="mountable action" data-action="downloadSoundTrackFile" data-trigger="click" data-id="` + track.ID + `">` + utils.Icon("refresh") + `Redownload</button>`)
}
2018-03-21 02:42:18 +00:00
// Delete button
_, isDeletable := obj.(api.Deletable)
if isDeletable && (user.Role == "editor" || user.Role == "admin") {
2018-03-21 02:42:18 +00:00
returnPath := ""
switch lowerCaseTypeName {
2018-03-26 19:45:36 +00:00
case "anime":
returnPath = "/explore"
2018-03-21 02:42:18 +00:00
case "company":
returnPath = "/companies"
default:
returnPath = "/" + lowerCaseTypeName + "s"
}
b.WriteString(`<button class="mountable action" data-action="deleteObject" data-trigger="click" data-return-path="` + returnPath + `" data-confirm-type="` + lowerCaseTypeName + `">` + utils.Icon("trash") + `Delete</button>`)
2017-10-28 15:18:27 +00:00
}
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.
2019-04-30 14:59:53 +00:00
func RenderObject(b *strings.Builder, 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 ...
2019-04-30 14:59:53 +00:00
func RenderField(b *strings.Builder, v *reflect.Value, field reflect.StructField, idPrefix string) {
fieldValue := reflect.Indirect(v.FieldByName(field.Name))
// Embedded fields
if field.Anonymous {
2019-05-27 09:08:42 +00:00
// If it's exported, render it normally.
if field.PkgPath == "" {
RenderObject(b, fieldValue.Interface(), idPrefix)
}
// If it's an unexported struct, we try to access the field names
// given to us from the anonymous struct on the actual object.
if field.Type.Kind() == reflect.Struct {
for i := 0; i < field.Type.NumField(); i++ {
f := field.Type.Field(i)
RenderField(b, v, f, idPrefix)
}
}
return
}
if field.Tag.Get("editable") != "true" {
2017-10-17 21:17:04 +00:00
return
}
b.WriteString("<div class='mountable'>")
defer b.WriteString("</div>")
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" {
2018-04-26 10:28:58 +00:00
renderStringField(b, v, field, idPrefix, fieldValue)
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
b.WriteString(components.InputBool(idPrefix+field.Name, fieldValue.Bool(), field.Name, field.Tag.Get("tooltip")))
2017-11-27 17:04:28 +00:00
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, "[]") {
2019-06-05 07:18:04 +00:00
renderSliceField(b, field, idPrefix, fieldType, fieldValue)
2018-04-26 10:28:58 +00:00
return
}
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +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>`)
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
// Indent the fields
b.WriteString(`<div class="indent">`)
RenderObject(b, fieldValue.Interface(), field.Name+".")
b.WriteString(`</div>`)
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
b.WriteString(`</div>`)
}
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
// String field
2019-06-07 01:05:28 +00:00
// nolint:errcheck
2019-06-07 01:03:16 +00:00
func renderStringField(b io.StringWriter, v *reflect.Value, field reflect.StructField, idPrefix string, fieldValue reflect.Value) {
2018-04-26 10:28:58 +00:00
idType := field.Tag.Get("idType")
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
// Try to infer the ID type by the field name
if idType == "" {
switch field.Name {
case "AnimeID":
idType = "Anime"
2017-10-17 21:17:04 +00:00
2018-04-26 10:28:58 +00:00
case "CharacterID":
idType = "Character"
}
}
2017-10-17 21:17:04 +00:00
2018-04-26 10:28:58 +00:00
showPreview := idType != "" && fieldValue.String() != ""
if showPreview {
b.WriteString("<div class='widget-section-with-preview'>")
}
2019-06-07 01:03:16 +00:00
switch {
case field.Tag.Get("datalist") != "":
2018-04-26 10:28:58 +00:00
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))
2019-06-07 01:03:16 +00:00
case field.Tag.Get("type") == "textarea":
2019-08-31 07:52:42 +00:00
maxLength, err := strconv.Atoi(field.Tag.Get("maxLength"))
if err != nil {
maxLength = limits.DefaultTextAreaMaxLength
}
b.WriteString(components.InputTextArea(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip"), maxLength))
2019-06-07 01:03:16 +00:00
case field.Tag.Get("type") == "upload":
2018-04-26 10:28:58 +00:00
endpoint := field.Tag.Get("endpoint")
id := v.FieldByName("ID").String()
endpoint = strings.Replace(endpoint, ":id", id, 1)
b.WriteString(components.InputFileUpload(idPrefix+field.Name, field.Name, field.Tag.Get("filetype"), endpoint))
2019-06-07 01:03:16 +00:00
default:
2019-08-31 07:52:42 +00:00
maxLength, err := strconv.Atoi(field.Tag.Get("maxLength"))
if err != nil {
maxLength = limits.DefaultTextMaxLength
}
b.WriteString(components.InputText(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip"), maxLength))
2018-04-26 10:28:58 +00:00
}
2017-10-17 21:17:04 +00:00
2018-04-26 10:28:58 +00:00
if showPreview {
b.WriteString("<div class='widget-section-preview'>")
}
// Preview
switch idType {
case "Anime":
animeID := fieldValue.String()
anime, err := arn.GetAnime(animeID)
if err == nil {
b.WriteString(components.EditFormImagePreview(anime.Link(), anime.ImageLink("small"), true, anime.Title.ByUser(nil)))
2017-10-17 21:17:04 +00:00
}
2018-04-26 10:28:58 +00:00
case "Character":
characterID := fieldValue.String()
character, err := arn.GetCharacter(characterID)
2017-11-19 00:42:52 +00:00
2018-04-26 10:28:58 +00:00
if err == nil {
b.WriteString(components.EditFormImagePreview(character.Link(), character.ImageLink("medium"), true, character.Name.ByUser(nil)))
2018-04-26 10:28:58 +00:00
}
case "":
break
default:
fmt.Println("Error: Unknown idType tag: " + idType)
2017-10-17 21:17:04 +00:00
}
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
// Close preview tags
if showPreview {
b.WriteString("</div></div>")
}
}
// Slice field
2019-06-05 07:18:04 +00:00
func renderSliceField(b *strings.Builder, field reflect.StructField, idPrefix string, fieldType string, fieldValue reflect.Value) {
2017-11-27 17:04:28 +00:00
b.WriteString(`<div class="widget-section">`)
2018-04-26 10:28:58 +00:00
b.WriteString(`<h3 class="widget-title">`)
b.WriteString(field.Name)
b.WriteString(`</h3>`)
2017-11-27 17:04:28 +00:00
2018-04-26 10:28:58 +00:00
for sliceIndex := 0; sliceIndex < fieldValue.Len(); sliceIndex++ {
b.WriteString(`<div class="widget-section">`)
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" title="Delete this ` + field.Name + `" 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>`)
arrayObj := fieldValue.Index(sliceIndex).Interface()
arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex)
2019-06-07 01:31:21 +00:00
RenderObject(b, arrayObj, idPrefix+arrayIDPrefix)
2018-04-26 10:28:58 +00:00
// Preview
// elementValue := fieldValue.Index(sliceIndex)
// RenderArrayElement(b, &elementValue)
if fieldType == "[]*arn.ExternalMedia" {
b.WriteString(components.ExternalMedia(fieldValue.Index(sliceIndex).Interface().(*arn.ExternalMedia)))
}
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>`)
2017-11-27 17:04:28 +00:00
b.WriteString(`</div>`)
b.WriteString(`</div>`)
2017-10-17 21:17:04 +00:00
}
2018-03-09 15:08:35 +00:00
// findMainID finds the main ID of the object.
func findMainID(t reflect.Type, v reflect.Value) reflect.Value {
idField := v.FieldByName("ID")
if idField.IsValid() {
return reflect.Indirect(idField)
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Tag.Get("mainID") == "true" {
return reflect.Indirect(v.Field(i))
}
}
panic("Type " + t.Name() + " doesn't have a main ID!")
}