Added array operations

This commit is contained in:
Eduard Urbach 2017-10-12 12:07:17 +02:00
parent 77d458eb13
commit 6d0b2ccdf6
3 changed files with 29 additions and 10 deletions

View File

@ -22,11 +22,11 @@ component InputTags(id string, value []string, label string)
.widget-section .widget-section
label(for=id)= label + ":" label(for=id)= label + ":"
.tags(id=id) .tags(id=id)
each tag in value for index, tag := range value
.tag .tag
span.tag-title= tag span.tag-title= tag
.tag-remove.action(data-action="removeTag", data-trigger="click", data-tag=tag) x .tag-remove.action(data-action="arrayRemove", data-trigger="click", data-field=id, data-index=index) x
button.tag-add button.tag-add.action(data-action="arrayAppend", data-trigger="click", data-field=id)
RawIcon("plus") RawIcon("plus")

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
"strconv"
"strings" "strings"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
@ -88,9 +89,14 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
arrayObj := fieldValue.Index(sliceIndex).Interface() arrayObj := fieldValue.Index(sliceIndex).Interface()
arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex) arrayIDPrefix := fmt.Sprintf("%s[%d].", field.Name, sliceIndex)
RenderObject(b, arrayObj, arrayIDPrefix) RenderObject(b, arrayObj, arrayIDPrefix)
// Remove button
b.WriteString(`<div class="buttons"><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></div>`)
} }
b.WriteString(`<div class="buttons"><button>` + utils.Icon("plus") + `Add ` + field.Name + `</button></div>`) b.WriteString(`<div class="buttons"><button class="action" data-action="arrayAppend" data-trigger="click" data-field="` + field.Name + `">` + utils.Icon("plus") + `Add ` + field.Name + `</button></div>`)
default: default:
panic("No edit form implementation for " + idPrefix + field.Name + " with type " + field.Type.String()) panic("No edit form implementation for " + idPrefix + field.Name + " with type " + field.Type.String())
} }

View File

@ -365,13 +365,26 @@ export function buyItem(arn: AnimeNotifier, button: HTMLElement) {
.then(() => arn.loading(false)) .then(() => arn.loading(false))
} }
// Remove tag // Append new element to array
export function removeTag(arn: AnimeNotifier, element: HTMLElement) { export function arrayAppend(arn: AnimeNotifier, element: HTMLElement) {
let tag = element.dataset.tag let field = element.dataset.field
let object = element.dataset.object ? JSON.parse(element.dataset.object) : {}
// arn.loading(true) let apiEndpoint = arn.findAPIEndpoint(element)
alert("Remove " + tag) arn.post(apiEndpoint + "/field/" + field + "/append", object)
.then(() => arn.reloadContent())
.catch(err => arn.statusMessage.showError(err))
}
// Remove element from array
export function arrayRemove(arn: AnimeNotifier, element: HTMLElement) {
let field = element.dataset.field
let index = element.dataset.index
let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint + "/field/" + field + "/remove/" + index, "")
.then(() => arn.reloadContent())
.catch(err => arn.statusMessage.showError(err))
} }
// Chrome extension installation // Chrome extension installation