-
Notifications
You must be signed in to change notification settings - Fork 1
/
uiwrapcontainer.go
90 lines (80 loc) · 1.96 KB
/
uiwrapcontainer.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
package jaws
import (
"html/template"
"io"
"slices"
"strings"
"github.com/linkdata/deadlock"
)
type uiWrapContainer struct {
UiHtml
Container
mu deadlock.Mutex
contents []*Element
}
func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag string, params []interface{}) {
ui.parseGetter(e, ui.Container)
attrs := ui.parseParams(e, params)
b := e.jid.AppendStartTagAttr(nil, outerhtmltag)
for _, attr := range attrs {
b = append(b, ' ')
b = append(b, attr...)
}
b = append(b, '>')
_, err := w.Write(b)
if err == nil {
for _, cui := range ui.Container.JawsContains(e.Request) {
elem := e.Request.NewElement(cui)
ui.contents = append(ui.contents, elem)
elem.Render(w, nil)
}
b = b[:0]
b = append(b, "</"...)
b = append(b, outerhtmltag...)
b = append(b, '>')
_, err = w.Write(b)
}
maybePanic(err)
}
func (ui *uiWrapContainer) JawsUpdate(e *Element) {
var toRemove, toAppend []*Element
var orderData []Jid
oldMap := make(map[UI]*Element)
newMap := make(map[UI]struct{})
newContents := ui.Container.JawsContains(e.Request)
for _, t := range newContents {
newMap[t] = struct{}{}
}
ui.mu.Lock()
oldOrder := make([]Jid, len(ui.contents))
for i, elem := range ui.contents {
oldOrder[i] = elem.jid
oldMap[elem.ui] = elem
if _, ok := newMap[elem.ui]; !ok {
toRemove = append(toRemove, elem)
}
}
ui.contents = ui.contents[:0]
for _, cui := range newContents {
var elem *Element
if elem = oldMap[cui]; elem == nil {
elem = e.Request.NewElement(cui)
toAppend = append(toAppend, elem)
}
ui.contents = append(ui.contents, elem)
orderData = append(orderData, elem.jid)
}
ui.mu.Unlock()
for _, elem := range toRemove {
e.Remove(elem.jid.String())
e.deleteElement(elem)
}
for _, elem := range toAppend {
var sb strings.Builder
elem.ui.JawsRender(elem, &sb, nil)
e.Append(template.HTML(sb.String())) // #nosec G203
}
if !slices.Equal(oldOrder, orderData) {
e.Order(orderData)
}
}