-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelement.go
360 lines (302 loc) · 7.7 KB
/
element.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package goform
import (
"errors"
"fmt"
"github.com/vincent-petithory/dataurl"
"io"
"io/ioutil"
"mime/multipart"
"os"
"strings"
)
type ElementType string
const (
ElementTypeText ElementType = "text"
ElementTypeTextarea ElementType = "textarea"
ElementTypeSelect ElementType = "select"
ElementTypeRadio ElementType = "radio"
ElementTypeCheckbox ElementType = "checkbox"
ElementTypeMultiCheckbox ElementType = "multicheckbox"
ElementTypeHidden ElementType = "hidden"
ElementTypePassword ElementType = "password"
ElementTypeEmail ElementType = "email"
ElementTypeNumber ElementType = "number"
ElementTypeSearch ElementType = "search"
ElementTypeTel ElementType = "tel"
ElementTypeFile ElementType = "file"
ElementTypeButton ElementType = "button"
ElementTypeSubmit ElementType = "submit"
ElementTypeImage ElementType = "image"
ElementTypeCaptcha ElementType = "captcha"
)
var (
ErrAttributeNotFound = errors.New("Element attribute not found")
)
type ElementInterface interface {
AddAttribute(attribute *Attribute)
HasAttribute(key string) bool
GetAttribute(key string) (*Attribute, error)
SetAttribute(key string, value string)
GetType() ElementType
GetName() string
SetName(string)
GetLabel() string
SetValue(string)
SetValues([]string)
GetValue() string
GetValues() []string
SetFile(file *File)
GetFile() *File
GetDeletionUrl() string
SetDeletionUrl(string)
AddValueOption(valueOption *ValueOption)
ClearValueOptions()
IsChecked() bool
IsCheckedInValues(string) bool
IsValid() bool
AddValidator(validator ValidatorInterface)
GetValidators() []ValidatorInterface
ClearValidators()
GetFilters() []FilterInterface
ApplyFilters()
GetErrors() []Message
AddError(string, []interface{})
Render() string
GetTheme() Theme
SetTheme(Theme)
GetTemplateFunctions() map[string]interface{}
SetTemplateFunctions(map[string]interface{})
}
type File struct {
Headers map[string][]string
Name string
Extension string
Location string
Binary multipart.File
}
func (file *File) ToString() string {
if file == nil {
return ""
}
b, err := ioutil.ReadAll(file.Binary)
if err != nil {
fmt.Println("File reader error", err)
}
dataUrl := dataurl.New(b, strings.Join(file.Headers["Content-Type"], ";"))
return string(dataUrl.String())
}
func (file *File) SaveTo(path string) error {
pathInfo := strings.Split(path, "/")
dir := strings.Join(pathInfo[:len(pathInfo)-1], "/")
os.MkdirAll(dir, 0775)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0775)
if err != nil {
fmt.Println(err)
return err
}
defer f.Close()
io.Copy(f, file.Binary)
return nil
}
func (file *File) Save() error {
file.Binary.Seek(0, 0)
pathInfo := strings.Split(file.Location, "/")
dir := strings.Join(pathInfo[:len(pathInfo)-1], "/")
os.MkdirAll(dir, 0775)
f, err := os.OpenFile(file.Location+"."+file.Extension, os.O_WRONLY|os.O_CREATE, 0775)
if err != nil {
fmt.Println(err)
return err
}
defer f.Close()
io.Copy(f, file.Binary)
return nil
}
type Attribute struct {
Key string
Value string
}
type ValueOption struct {
Value string
Label string
Selected bool
Disabled bool
}
type Element struct {
Type ElementType
Label string
Name string
Attributes []*Attribute
Value string
Values []string
ValueOptions []*ValueOption
Validators []ValidatorInterface
Filters []FilterInterface
Errors []Message
File *File
deletionUrl string
theme Theme
templateFunctions map[string]interface{}
}
func (element *Element) GetType() ElementType {
return element.Type
}
func (element *Element) GetName() string {
return element.Name
}
func (element *Element) SetName(n string) {
element.Name = n
}
func (element *Element) GetLabel() string {
return element.Label
}
func (element *Element) SetValue(s string) {
element.Value = s
for _, v := range element.Validators {
v.SetValue(s)
}
for _, f := range element.Filters {
f.SetValue(s)
}
if element.ValueOptions != nil {
for _, valueOption := range element.ValueOptions {
if valueOption.Value == s {
valueOption.Selected = true
break
}
}
}
}
func (element *Element) SetValues(s []string) {
element.Values = s
for _, v := range element.Validators {
v.SetValues(s)
}
for _, f := range element.Filters {
f.SetValues(s)
}
}
func (element *Element) GetValue() string {
return element.Value
}
func (element *Element) GetValues() []string {
return element.Values
}
func (element *Element) SetFile(f *File) {
element.File = f
for _, v := range element.Validators {
v.SetFile(f)
}
for _, fi := range element.Filters {
fi.SetFile(f)
}
}
func (element *Element) GetFile() *File {
return element.File
}
func (element *Element) BinaryToString() string {
var s string
return s
}
func (element *Element) GetDeletionUrl() string {
return element.deletionUrl
}
func (element *Element) SetDeletionUrl(n string) {
element.deletionUrl = n
}
func (element *Element) AddValueOption(valueOption *ValueOption) {
element.ValueOptions = append(element.ValueOptions, valueOption)
}
func (element *Element) ClearValueOptions() {
element.ValueOptions = nil
}
func (element *Element) IsChecked() bool {
return element.Value != "" && element.Value != "false"
}
func (element *Element) IsValid() bool {
var errs []Message
for _, v := range element.Validators {
if !v.IsValid() {
errs = append(errs, v.GetMessages()...)
}
}
if len(errs) > 0 {
element.Errors = errs
return false
}
return true
}
func (element *Element) ApplyFilters() {
for _, f := range element.Filters {
f.Apply()
}
}
func (element *Element) GetFilters() []FilterInterface {
return element.Filters
}
func (element *Element) IsCheckedInValues(s string) bool {
for _, val := range element.Values {
if s == val {
return true
}
}
return false
}
func (element *Element) AddAttribute(attribute *Attribute) {
element.Attributes = append(element.Attributes, attribute)
}
func (element *Element) HasAttribute(key string) bool {
for _, atr := range element.Attributes {
if key == atr.Key {
return true
}
}
return false
}
func (element *Element) GetAttribute(key string) (*Attribute, error) {
for _, attr := range element.Attributes {
if key == attr.Key {
return attr, nil
}
}
return nil, ErrAttributeNotFound
}
func (element *Element) SetAttribute(key string, value string) {
for idx, attribute := range element.Attributes {
if attribute.Key == key {
element.Attributes[idx] = &Attribute{Key: key, Value: value}
return
}
}
element.AddAttribute(&Attribute{Key: key, Value: value})
}
func (element *Element) AddValidator(validator ValidatorInterface) {
validator.SetValue(element.GetValue())
validator.SetValues(element.GetValues())
validator.SetFile(element.GetFile())
element.Validators = append(element.Validators, validator)
}
func (element *Element) GetValidators() []ValidatorInterface {
return element.Validators
}
func (element *Element) ClearValidators() {
element.Validators = []ValidatorInterface{}
}
func (element *Element) GetErrors() []Message {
return element.Errors
}
func (element *Element) AddError(s string, args []interface{}) {
element.Errors = append(element.Errors, Message{Message: s, Args: args})
}
func (element *Element) GetTheme() Theme {
return element.theme
}
func (element *Element) SetTheme(theme Theme) {
element.theme = theme
}
func (element *Element) SetTemplateFunctions(templateFunctions map[string]interface{}) {
element.templateFunctions = templateFunctions
}
func (element *Element) GetTemplateFunctions() map[string]interface{} {
return element.templateFunctions
}