-
Notifications
You must be signed in to change notification settings - Fork 6
/
image.go
207 lines (178 loc) · 4.8 KB
/
image.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
package control
import (
"context"
"encoding/base64"
"strconv"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/html5tag"
)
type ImageI interface {
page.ControlI
SetSrc(src string) ImageI
SetAlt(alt string) ImageI
SetWidth(width int) ImageI
SetHeight(height int) ImageI
SetMimeType(typ string) ImageI
SetData(data []byte) ImageI
}
// Image is an img tag. You can display either a URL, or direct image information by setting the Src or the Data values.
type Image struct {
page.ControlBase
data []byte // slice of data itself
typ string // the image MIME type (jpeg, gif, etc.) for data. Default is jpeg.
}
// NewImage creates a new image.
func NewImage(parent page.ControlI, id string) *Image {
i := new(Image)
i.Init(i, parent, id)
return i
}
// Init is called by subclasses. Normally you will not call this directly.
func (i *Image) Init(self any, parent page.ControlI, id string) {
i.ControlBase.Init(self, parent, id)
i.Tag = "img"
i.IsVoidTag = true
i.typ = "jpeg"
}
func (i *Image) this() ImageI {
return i.Self().(ImageI)
}
// Src returns the src attribute
func (i *Image) Src() string {
return i.Attribute("src")
}
// SetSrc sets the src attribute.
func (i *Image) SetSrc(src string) ImageI {
i.SetAttribute("src", src)
return i.this()
}
// Data returns the data of the image if provided.
func (i *Image) Data() []byte {
return i.data
}
// SetData sets the raw data of the image.
func (i *Image) SetData(data []byte) ImageI {
i.data = data
return i.this()
}
// Set the MIME type for the data, (jpeg, gif, png, etc.)
func (i *Image) SetMimeType(typ string) ImageI {
i.typ = typ
return i.this()
}
// Alt returns the text that will be used for the alt tag.
func (i *Image) Alt() string {
return i.Attribute("alt")
}
// SetAlt will set the alt tag. The html standard requires the alt tag. Alt tags are used to display
// a descirption of an image when the browser cannot display an image, and is very important
// for assistive technologies.
func (i *Image) SetAlt(alt string) ImageI {
i.SetAttribute("alt", alt)
return i.this()
}
// Width returns the number that will be used as the width of the image
func (i *Image) Width() int {
w := i.Attribute("width")
if i, err := strconv.Atoi(w); err != nil {
return 0
} else {
return i
}
}
// SetWidth sets the width attribute.
func (i *Image) SetWidth(width int) ImageI {
i.SetAttribute("width", strconv.Itoa(width))
i.Refresh()
return i.this()
}
// Height returns the number that will be used in the height attribute.
func (i *Image) Height() int {
w := i.Attribute("height")
if i, err := strconv.Atoi(w); err != nil {
return 0
} else {
return i
}
}
// SetHeight sets the height attribute.
func (i *Image) SetHeight(height int) ImageI {
i.SetAttribute("width", strconv.Itoa(height))
i.Refresh()
return i.this()
}
// DrawingAttributes is called by the framework.
func (i *Image) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := i.ControlBase.DrawingAttributes(ctx)
if i.data != nil {
// Turn the data into a source attribute
d := base64.StdEncoding.EncodeToString(i.data)
d = "data:image/" + i.typ + ";base64," + d
a.Set("src", d)
}
return a
}
func (i *Image) Serialize(e page.Encoder) {
i.ControlBase.Serialize(e)
if err := e.Encode(i.data); err != nil {
panic(err)
}
if err := e.Encode(i.typ); err != nil {
panic(err)
}
}
func (i *Image) Deserialize(dec page.Decoder) {
i.ControlBase.Deserialize(dec)
if err := dec.Decode(&i.data); err != nil {
panic(err)
}
if err := dec.Decode(&i.typ); err != nil {
panic(err)
}
}
// ImageCreator is the initialization structure for declarative creation of buttons
type ImageCreator struct {
// ID is the control id
ID string
// Src is the content of the source attribute, usually a url
Src string
// Alt is the text displayed for screen readers
Alt string
MimeType string
Width int
Height int
Data []byte
page.ControlOptions
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c ImageCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewImage(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by implementations of Images to initialize a control with the creator.
func (c ImageCreator) Init(ctx context.Context, ctrl ImageI) {
ctrl.SetSrc(c.Src)
ctrl.SetAlt(c.Alt)
if c.MimeType != "" {
ctrl.SetMimeType(c.MimeType)
}
if c.Width != 0 {
ctrl.SetWidth(c.Width)
}
if c.Height != 0 {
ctrl.SetHeight(c.Height)
}
if c.Data != nil {
ctrl.SetData(c.Data)
}
ctrl.ApplyOptions(ctx, c.ControlOptions)
}
// GetImage is a convenience method to return the button with the given id from the page.
func GetImage(c page.ControlI, id string) *Image {
return c.Page().GetControl(id).(*Image)
}
func init() {
page.RegisterControl(&Image{})
}