/
flow.go
199 lines (163 loc) · 4.58 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
package ui
// Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import (
"fmt"
"github.com/bhojpur/web/pkg/app"
"github.com/google/uuid"
)
// IFlow is the interface that describes a container that displays its items as
// a flow.
type IFlow interface {
app.UI
// Sets the ID.
ID(v string) IFlow
// Sets the class. Multiple classes can be defined by successive calls.
Class(v string) IFlow
// Sets the width in px for the content items.
// Default is 300px.
ItemWidth(px int) IFlow
// Sets the space between content elements in px.
Spacing(px int) IFlow
// Makes the items occupy all the available space when the content has only
// one row.
StretchItems() IFlow
// Sets the content.
Content(elems ...app.UI) IFlow
}
// Flow creates a container that displays its items as a flow.
func Flow() IFlow {
return &flow{
IitemWidth: 300,
id: "bhojpur-flow-" + uuid.NewString(),
itemsPerRow: 1,
}
}
type flow struct {
app.Compo
Iid string
Iclass string
IitemWidth int
Ispacing int
IstretchItems bool
Icontent []app.UI
id string
itemsPerRow int
itemWidth float64
}
func (f *flow) ID(v string) IFlow {
f.Iid = v
return f
}
func (f *flow) Class(v string) IFlow {
f.Iclass = app.AppendClass(f.Iclass, v)
return f
}
func (f *flow) ItemWidth(px int) IFlow {
if px > 0 {
f.IitemWidth = px
}
return f
}
func (f *flow) Spacing(px int) IFlow {
if px > 0 {
f.Ispacing = px
}
return f
}
func (f *flow) StretchItems() IFlow {
f.IstretchItems = true
return f
}
func (f *flow) Content(elems ...app.UI) IFlow {
f.Icontent = app.FilterUIElems(elems...)
return f
}
func (f *flow) OnPreRender(ctx app.Context) {
f.refresh(ctx)
}
func (f *flow) OnMount(ctx app.Context) {
f.refresh(ctx)
}
func (f *flow) OnResize(ctx app.Context) {
f.refresh(ctx)
}
func (f *flow) OnUpdate(ctx app.Context) {
f.refresh(ctx)
}
func (f *flow) Render() app.UI {
return app.Div().
DataSet("bhojpur-ui", "flow").
ID(f.Iid).
Class(f.Iclass).
Body(
app.Div().
ID(f.id).
Style("display", "flex").
Style("flex-wrap", "wrap").
Style("width", "100%").
Style("max-width", "100%").
Body(
app.Range(f.Icontent).Slice(func(i int) app.UI {
marginTop := "0"
if i >= f.itemsPerRow {
marginTop = pxToString(f.Ispacing)
}
marginLeft := "0"
if i%f.itemsPerRow != 0 {
marginLeft = pxToString(f.Ispacing)
}
width := fmt.Sprintf("%.6fpx", f.itemWidth)
return app.Div().
Style("position", "relative").
Style("flex-shrink", "0").
Style("flex-basis", width).
Style("max-width", width).
Style("margin-top", marginTop).
Style("margin-left", marginLeft).
Body(f.Icontent[i])
}),
),
)
}
func (f *flow) refresh(ctx app.Context) {
w, _ := f.layoutSize()
w += f.Ispacing
itemWidth := f.IitemWidth + f.Ispacing
itemsPerRow := w / itemWidth
if f.IstretchItems && len(f.Icontent) < itemsPerRow {
itemsPerRow = len(f.Icontent)
}
if itemsPerRow == 0 {
itemsPerRow = 1
}
itemWidthFloat := float64(w-f.Ispacing*itemsPerRow) / float64(itemsPerRow)
if itemsPerRow != f.itemsPerRow || itemWidthFloat != f.itemWidth {
f.itemsPerRow = itemsPerRow
f.itemWidth = itemWidthFloat
ctx.Defer(func(app.Context) {
f.ResizeContent()
})
}
}
func (f *flow) layoutSize() (int, int) {
layout := app.Window().GetElementByID(f.id)
if !layout.Truthy() {
return 320 - 24 - 24, 568
}
return layout.Get("clientWidth").Int(), layout.Get("clientHeight").Int()
}