-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
flow.go
209 lines (170 loc) · 3.83 KB
/
flow.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
package app
import (
"strconv"
"time"
"github.com/google/uuid"
"github.com/maxence-charriere/go-app/v8/pkg/errors"
)
const (
flowItemBaseWidth = 300
flowResizeSizeDelay = time.Millisecond * 100
)
// UIFlow is the interface that describes a container that displays its items as
// a flow.
//
// EXPERIMENTAL WIDGET.
type UIFlow interface {
UI
// Class adds a CSS class to the flow root HTML element class property.
Class(v string) UIFlow
// Content sets the content with the given UI elements.
Content(elems ...UI) UIFlow
// ID sets the flow root HTML element id property.
ID(v string) UIFlow
// ItemsBaseWidth sets the items base width in px. Items size is adjusted to
// fit the space in the container. Default is 300px.
ItemsBaseWidth(px int) UIFlow
// StrechtOnSingleRow makes the items to occupy all the available space when
// the flow spreads on a single row.
StrechtOnSingleRow() UIFlow
}
// Flow creates a container that displays its items as a flow.
//
// EXPERIMENTAL WIDGET.
func Flow() UIFlow {
return &flow{
IitemsBaseWitdh: flowItemBaseWidth,
id: "goapp-flow-" + uuid.New().String(),
}
}
type flow struct {
Compo
IitemsBaseWitdh int
Iclass string
Iid string
Icontent []UI
IstrechOnSingleRow bool
id string
contentLen int
width int
itemWidth int
adjustSizeTimer *time.Timer
}
func (f *flow) Class(v string) UIFlow {
if v == "" {
return f
}
if f.Iclass != "" {
f.Iclass += " "
}
f.Iclass += v
return f
}
func (f *flow) Content(elems ...UI) UIFlow {
f.Icontent = FilterUIElems(elems...)
return f
}
func (f *flow) ID(v string) UIFlow {
f.Iid = v
return f
}
func (f *flow) ItemsBaseWidth(px int) UIFlow {
if px > 0 {
f.IitemsBaseWitdh = px
}
return f
}
func (f *flow) StrechtOnSingleRow() UIFlow {
f.IstrechOnSingleRow = true
return f
}
func (f *flow) OnMount(ctx Context) {
if f.requiresLayoutUpdate() {
f.refreshLayout(ctx)
}
}
func (f *flow) OnNav(ctx Context) {
if f.requiresLayoutUpdate() {
f.refreshLayout(ctx)
}
}
func (f *flow) OnResize(ctx Context) {
f.refreshLayout(ctx)
}
func (f *flow) OnDismount() {
f.cancelAdjustItemSizes()
}
func (f *flow) Render() UI {
if f.requiresLayoutUpdate() {
f.Defer(f.refreshLayout)
}
return Div().
ID(f.id).
Class("goapp-flow").
Class(f.Iclass).
Body(
Range(f.Icontent).Slice(func(i int) UI {
itemWidth := strconv.Itoa(f.itemWidth) + "px"
return Div().
Class("goapp-flow-item").
Style("max-width", itemWidth).
Style("width", itemWidth).
Body(f.Icontent[i])
}),
)
}
func (f *flow) requiresLayoutUpdate() bool {
return (f.Iid != "" && f.Iid != f.id) ||
len(f.Icontent) != f.contentLen
}
func (f *flow) refreshLayout(ctx Context) {
if f.Iid != "" && f.Iid != f.id {
f.id = f.Iid
f.Update()
return
}
f.contentLen = len(f.Icontent)
if IsServer {
return
}
f.cancelAdjustItemSizes()
if f.adjustSizeTimer != nil {
f.adjustSizeTimer.Reset(flowResizeSizeDelay)
return
}
f.adjustSizeTimer = time.AfterFunc(flowResizeSizeDelay, func() {
f.Defer(f.adjustItemSizes)
})
}
func (f *flow) adjustItemSizes(ctx Context) {
if f.IitemsBaseWitdh == 0 || len(f.Icontent) == 0 {
return
}
elem := Window().GetElementByID(f.id)
if !elem.Truthy() {
Log(errors.New("flow root element found").Tag("id", f.id))
return
}
width := elem.Get("clientWidth").Int()
if width == 0 {
return
}
defer f.ResizeContent()
defer f.Update()
itemWidth := f.IitemsBaseWitdh
itemsPerRow := width / itemWidth
if itemsPerRow <= 1 {
f.itemWidth = width
return
}
itemWidth = width / itemsPerRow
if l := len(f.Icontent); l <= itemsPerRow && f.IstrechOnSingleRow {
itemWidth = width / l
}
f.itemWidth = itemWidth
}
func (f *flow) cancelAdjustItemSizes() {
if f.adjustSizeTimer != nil {
f.adjustSizeTimer.Stop()
}
}