-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
273 lines (242 loc) · 6.43 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package forms
import (
"fmt"
"html/template"
"log"
"sort"
"strconv"
"strings"
"golang.org/x/exp/maps"
)
var (
debugLogList = false
)
type List[T comparable] struct {
RenderableImpl[List[T]]
Name string
Template string
TemplateStyle
Identity
Styles []*Style
WrapperTag TagOpts
*Binding[[]T]
MinCount int
MaxCount int
UseIndicesAsKeys bool
NewItem func(name, typ string, index int) (T, bool)
DeleteItem func(item T)
ItemName func(item T, index int) string
ItemType func(item T, index int) string
RenderItem func(item T, index int) *Group
RenderItemPtr func(item *T, index int) *Group
Sort func(items []T)
Empty Child
TopArea func() Children
BottomArea func() Children
ItemActions map[string]func(item T, index int, action *ItemAction)
children Children
childFlags []ChildFlags
items []T
InnerHTML template.HTML
}
type ItemAction struct {
ItemName string
Key string
Remove bool
Save bool
}
func (*List[T]) DefaultTemplate() string { return "list" }
func (list *List[T]) Finalize(state *State) {
state.PushName(list.Name)
state.PushStyles(list.Styles)
state.AssignIdentity(&list.Identity)
if list.FullName == "" {
panic(fmt.Errorf("%T must have a name", list))
}
if list.Template == "" {
list.Template = list.DefaultTemplate()
}
if list.children != nil {
return
}
if list.UseIndicesAsKeys {
if list.NewItem == nil {
list.NewItem = func(name, typ string, index int) (T, bool) {
var zero T
return zero, true
}
}
if list.ItemName == nil {
list.ItemName = func(item T, index int) string {
return strconv.Itoa(index)
}
}
}
if list.RenderItem == nil && list.RenderItemPtr == nil {
panic("need either RenderItem or RenderItemPtr")
}
existingItems := list.Binding.Value()
var items []T
newlyAddedItems := make(map[T]struct{})
if state.Data == nil {
items = existingItems
} else {
newItemNameSet, action := list.computeListItemNames(state.Data)
if debugLogList {
log.Printf("forms: %T: newItemNameSet = %v", list, maps.Keys(newItemNameSet))
}
items = make([]T, 0, len(existingItems)+10)
for i, item := range existingItems {
itemName := list.FullItemName(item, i)
if action != nil && action.ItemName == itemName {
list.ItemActions[action.Key](item, i, action)
if action.Remove {
delete(newItemNameSet, itemName)
continue
}
}
if _, found := newItemNameSet[itemName]; found {
items = append(items, item)
delete(newItemNameSet, itemName)
} else {
if list.DeleteItem != nil {
list.DeleteItem(item)
}
}
}
if list.NewItem != nil {
addedItemNames := maps.Keys(newItemNameSet)
sort.Strings(addedItemNames)
for _, itemName := range addedItemNames {
proposedName, typ := list.SplitFullItemName(itemName)
if item, ok := list.NewItem(proposedName, typ, len(items)); ok {
items = append(items, item)
if debugLogList {
log.Printf("forms: %T: added %q %q (full %q)", list, typ, proposedName, itemName)
}
} else {
if debugLogList {
log.Printf("forms: %T: failed to add %q %q", list, typ, proposedName)
}
}
}
addName := JoinNames(list.Identity.FullName, "add")
if state.Data.Action == addName {
if item, ok := list.NewItem("", "", len(items)); ok {
items = append(items, item)
newlyAddedItems[item] = struct{}{}
}
} else if suffix, ok := strings.CutPrefix(state.Data.Action, addName); ok {
comps := SplitName(suffix)
if len(comps) == 1 {
typ := comps[0]
if item, ok := list.NewItem("", typ, len(items)); ok {
items = append(items, item)
newlyAddedItems[item] = struct{}{}
}
}
}
}
}
if list.Sort != nil {
list.Sort(items)
}
list.items = items
children := make(Children, 0, len(items)+10)
childFlags := make([]ChildFlags, 0, len(items)+10)
if list.TopArea != nil {
for _, child := range list.TopArea() {
children = append(children, child)
childFlags = append(childFlags, 0)
}
}
for i, item := range items {
var group *Group
if list.RenderItem != nil {
group = list.RenderItem(item, i)
} else if list.RenderItemPtr != nil {
group = list.RenderItemPtr(&items[i], i)
}
group.Name = list.FullItemName(item, i)
var flags ChildFlags
if _, found := newlyAddedItems[item]; found {
flags = ChildFlagSkipProcessing
}
children = append(children, group)
childFlags = append(childFlags, flags)
}
if list.BottomArea != nil {
for _, child := range list.BottomArea() {
children = append(children, child)
childFlags = append(childFlags, 0)
}
}
list.children = children
list.childFlags = childFlags
}
func (list *List[T]) computeListItemNames(data *FormData) (map[string]struct{}, *ItemAction) {
fullListName := list.Identity.FullName
result := make(map[string]struct{})
prefix := fullListName + "["
var action *ItemAction
for k := range data.Values {
if suffix, ok := strings.CutPrefix(k, prefix); ok {
if name, _, ok := strings.Cut(suffix, "]"); ok {
removeAction := JoinNames(fullListName, name, "remove")
isRemoving := (data.Action == removeAction)
if !isRemoving {
result[name] = struct{}{}
for actionKey := range list.ItemActions {
if data.Action == JoinNames(fullListName, name, actionKey) {
action = &ItemAction{
ItemName: name,
Key: actionKey,
}
break
}
}
}
}
}
}
return result, action
}
func (list *List[T]) FullItemName(item T, index int) string {
name := list.ItemName(item, index)
if list.ItemType == nil {
return name
} else {
return list.ItemType(item, index) + ":" + name
}
}
func (list *List[T]) SplitFullItemName(fullName string) (name, typ string) {
if list.ItemType == nil {
return fullName, ""
} else {
typ, name, ok := strings.Cut(fullName, ":")
if !ok {
typ = ""
name = ""
}
return name, typ
}
}
func (*List[T]) EnumFields(f func(name string, field *Field)) {}
func (list *List[T]) EnumChildren(f func(Child, ChildFlags)) {
for i, child := range list.children {
f(child, list.childFlags[i])
}
}
func (list *List[T]) ShouldProcessChild(data *FormData, child Child) bool {
return false
}
func (list *List[T]) Process(fd *FormData) {
if list.Sort != nil {
list.Sort(list.items)
}
list.Binding.Set(list.items)
}
func (list *List[T]) RenderInto(buf *strings.Builder, r *Renderer) {
list.InnerHTML = r.Render(list.children)
r.RenderWrapperTemplateInto(buf, list.Template, list, list.InnerHTML)
}