diff --git a/utils/editform/editform.go b/utils/editform/editform.go index 1a05598d..49fe976c 100644 --- a/utils/editform/editform.go +++ b/utils/editform/editform.go @@ -115,76 +115,7 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i // String if fieldType == "string" { - idType := field.Tag.Get("idType") - - // Try to infer the ID type by the field name - if idType == "" { - switch field.Name { - case "AnimeID": - idType = "Anime" - - case "CharacterID": - idType = "Character" - } - } - - showPreview := idType != "" && fieldValue.String() != "" - - if showPreview { - b.WriteString("
") - } - - // Input field - 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"))) - } else if field.Tag.Get("type") == "upload" { - 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)) - } else { - b.WriteString(components.InputText(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip"))) - } - - if showPreview { - b.WriteString("
") - } - - // 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)) - } - - case "Character": - characterID := fieldValue.String() - character, err := arn.GetCharacter(characterID) - - if err == nil { - b.WriteString(components.EditFormImagePreview(character.Link(), character.ImageLink("medium"), false)) - } - - case "": - break - - default: - fmt.Println("Error: Unknown idType tag: " + idType) - } - - // Close preview tags - if showPreview { - b.WriteString("
") - } - + renderStringField(b, v, field, idPrefix, fieldValue) return } @@ -218,46 +149,7 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i // Any kind of array if strings.HasPrefix(fieldType, "[]") { - b.WriteString(`
`) - b.WriteString(`

`) - b.WriteString(field.Name) - b.WriteString(`

`) - - for sliceIndex := 0; sliceIndex < fieldValue.Len(); sliceIndex++ { - b.WriteString(`
`) - - b.WriteString(`
`) - - // Title - b.WriteString(strconv.Itoa(sliceIndex+1) + ". " + field.Name) - b.WriteString(`
`) - - // Remove button - b.WriteString(``) - - b.WriteString(`
`) - - arrayObj := fieldValue.Index(sliceIndex).Interface() - arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex) - RenderObject(b, arrayObj, arrayIDPrefix) - - // Preview - // elementValue := fieldValue.Index(sliceIndex) - // RenderArrayElement(b, &elementValue) - if fieldType == "[]*arn.ExternalMedia" { - b.WriteString(components.ExternalMedia(fieldValue.Index(sliceIndex).Interface().(*arn.ExternalMedia))) - } - - b.WriteString(`
`) - } - - b.WriteString(`
`) - b.WriteString(``) - b.WriteString(`
`) - - b.WriteString(`
`) + renderSliceField(b, v, field, idPrefix, fieldType, fieldValue) return } @@ -273,6 +165,123 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i b.WriteString(``) } +// 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 + if idType == "" { + switch field.Name { + case "AnimeID": + idType = "Anime" + + case "CharacterID": + idType = "Character" + } + } + + showPreview := idType != "" && fieldValue.String() != "" + + if showPreview { + b.WriteString("
") + } + + // Input field + 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"))) + } else if field.Tag.Get("type") == "upload" { + 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)) + } else { + b.WriteString(components.InputText(idPrefix+field.Name, fieldValue.String(), field.Name, field.Tag.Get("tooltip"))) + } + + if showPreview { + b.WriteString("
") + } + + // 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)) + } + + case "Character": + characterID := fieldValue.String() + character, err := arn.GetCharacter(characterID) + + if err == nil { + b.WriteString(components.EditFormImagePreview(character.Link(), character.ImageLink("medium"), false)) + } + + case "": + break + + default: + fmt.Println("Error: Unknown idType tag: " + idType) + } + + // Close preview tags + if showPreview { + b.WriteString("
") + } +} + +// Slice field +func renderSliceField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, idPrefix string, fieldType string, fieldValue reflect.Value) { + b.WriteString(`
`) + b.WriteString(`

`) + b.WriteString(field.Name) + b.WriteString(`

`) + + for sliceIndex := 0; sliceIndex < fieldValue.Len(); sliceIndex++ { + b.WriteString(`
`) + + b.WriteString(`
`) + + // Title + b.WriteString(strconv.Itoa(sliceIndex+1) + ". " + field.Name) + b.WriteString(`
`) + + // Remove button + b.WriteString(``) + + b.WriteString(`
`) + + arrayObj := fieldValue.Index(sliceIndex).Interface() + arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex) + RenderObject(b, arrayObj, arrayIDPrefix) + + // Preview + // elementValue := fieldValue.Index(sliceIndex) + // RenderArrayElement(b, &elementValue) + if fieldType == "[]*arn.ExternalMedia" { + b.WriteString(components.ExternalMedia(fieldValue.Index(sliceIndex).Interface().(*arn.ExternalMedia))) + } + + b.WriteString(`
`) + } + + b.WriteString(`
`) + b.WriteString(``) + b.WriteString(`
`) + + b.WriteString(`
`) +} + // findMainID finds the main ID of the object. func findMainID(t reflect.Type, v reflect.Value) reflect.Value { idField := v.FieldByName("ID")