-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilePicker.go
371 lines (323 loc) · 10.4 KB
/
filePicker.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
361
362
363
364
365
366
367
368
369
370
371
package rui
import (
"encoding/base64"
"strconv"
"strings"
"time"
)
// Constants for [FilePicker] specific properties and events
const (
// FileSelectedEvent is the constant for "file-selected-event" property tag.
//
// Used by FilePicker.
// Fired when user selects file(s).
//
// General listener format:
// func(picker rui.FilePicker, files []rui.FileInfo).
//
// where:
// picker - Interface of a file picker which generated this event,
// files - Array of description of selected files.
//
// Allowed listener formats:
// func(picker rui.FilePicker)
// func(files []rui.FileInfo)
// func()
FileSelectedEvent PropertyName = "file-selected-event"
// Accept is the constant for "accept" property tag.
//
// Used by FilePicker.
// Set the list of allowed file extensions or MIME types.
//
// Supported types: string, []string.
//
// Internal type is string, other types converted to it during assignment.
//
// Conversion rules:
// - string - may contain single value of multiple separated by comma(,).
// - []string - an array of acceptable file extensions or MIME types.
Accept PropertyName = "accept"
// Multiple is the constant for "multiple" property tag.
//
// Used by FilePicker.
// Controls whether multiple files can be selected.
//
// Supported types: bool, int, string.
//
// Values:
// - true, 1, "true", "yes", "on", "1" - Several files can be selected.
// - false, 0, "false", "no", "off", "0" - Only one file can be selected.
Multiple PropertyName = "multiple"
)
// FileInfo describes a file which selected in the FilePicker view
type FileInfo struct {
// Name - the file's name.
Name string
// LastModified specifying the date and time at which the file was last modified
LastModified time.Time
// Size - the size of the file in bytes.
Size int64
// MimeType - the file's MIME type.
MimeType string
}
// FilePicker represents the FilePicker view
type FilePicker interface {
View
// Files returns the list of selected files.
// If there are no files selected then an empty slice is returned (the result is always not nil)
Files() []FileInfo
// LoadFile loads the content of the selected file. This function is asynchronous.
// The "result" function will be called after loading the data.
LoadFile(file FileInfo, result func(FileInfo, []byte))
}
type filePickerData struct {
viewData
files []FileInfo
loader map[int]func(FileInfo, []byte)
}
func (file *FileInfo) initBy(node DataValue) {
if obj := node.Object(); obj != nil {
file.Name, _ = obj.PropertyValue("name")
file.MimeType, _ = obj.PropertyValue("mime-type")
if size, ok := obj.PropertyValue("size"); ok {
if n, err := strconv.ParseInt(size, 10, 64); err == nil {
file.Size = n
}
}
if value, ok := obj.PropertyValue("last-modified"); ok {
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
file.LastModified = time.UnixMilli(n)
}
}
}
}
// NewFilePicker create new FilePicker object and return it
func NewFilePicker(session Session, params Params) FilePicker {
view := new(filePickerData)
view.init(session)
setInitParams(view, params)
return view
}
func newFilePicker(session Session) View {
return new(filePickerData) // NewFilePicker(session, nil)
}
func (picker *filePickerData) init(session Session) {
picker.viewData.init(session)
picker.tag = "FilePicker"
picker.hasHtmlDisabled = true
picker.files = []FileInfo{}
picker.loader = map[int]func(FileInfo, []byte){}
picker.set = picker.setFunc
picker.changed = picker.propertyChanged
}
func (picker *filePickerData) Focusable() bool {
return true
}
func (picker *filePickerData) Files() []FileInfo {
return picker.files
}
func (picker *filePickerData) LoadFile(file FileInfo, result func(FileInfo, []byte)) {
if result == nil {
return
}
for i, info := range picker.files {
if info.Name == file.Name && info.Size == file.Size && info.LastModified == file.LastModified {
picker.loader[i] = result
picker.Session().callFunc("loadSelectedFile", picker.htmlID(), i)
return
}
}
}
func (picker *filePickerData) setFunc(tag PropertyName, value any) []PropertyName {
switch tag {
case FileSelectedEvent:
return setOneArgEventListener[FilePicker, []FileInfo](picker, tag, value)
case Accept:
switch value := value.(type) {
case string:
return setStringPropertyValue(picker, Accept, strings.Trim(value, " \t\n"))
case []string:
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for _, val := range value {
val = strings.Trim(val, " \t\n")
if val != "" {
if buffer.Len() > 0 {
buffer.WriteRune(',')
}
buffer.WriteString(val)
}
}
return setStringPropertyValue(picker, Accept, buffer.String())
}
notCompatibleType(tag, value)
return nil
}
return picker.viewData.setFunc(tag, value)
}
func (picker *filePickerData) propertyChanged(tag PropertyName) {
switch tag {
case Accept:
session := picker.Session()
if css := acceptPropertyCSS(picker); css != "" {
session.updateProperty(picker.htmlID(), "accept", css)
} else {
session.removeProperty(picker.htmlID(), "accept")
}
default:
picker.viewData.propertyChanged(tag)
}
}
func (picker *filePickerData) htmlTag() string {
return "input"
}
func acceptPropertyCSS(view View) string {
accept, ok := stringProperty(view, Accept, view.Session())
if !ok {
if value := valueFromStyle(view, Accept); value != nil {
accept, ok = value.(string)
}
}
if ok {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
for _, value := range strings.Split(accept, ",") {
if value = strings.Trim(value, " \t\n"); value != "" {
if buffer.Len() > 0 {
buffer.WriteString(", ")
}
if value[0] != '.' && !strings.Contains(value, "/") {
buffer.WriteRune('.')
}
buffer.WriteString(value)
}
}
return buffer.String()
}
return ""
}
func (picker *filePickerData) htmlProperties(self View, buffer *strings.Builder) {
picker.viewData.htmlProperties(self, buffer)
if accept := acceptPropertyCSS(picker); accept != "" {
buffer.WriteString(` accept="`)
buffer.WriteString(accept)
buffer.WriteRune('"')
}
buffer.WriteString(` type="file"`)
if IsMultipleFilePicker(picker) {
buffer.WriteString(` multiple`)
}
buffer.WriteString(` oninput="fileSelectedEvent(this)"`)
if picker.getRaw(ClickEvent) == nil {
buffer.WriteString(` onclick="stopEventPropagation(this, event)"`)
}
}
func (picker *filePickerData) handleCommand(self View, command PropertyName, data DataObject) bool {
switch command {
case "fileSelected":
if node := data.PropertyByTag("files"); node != nil && node.Type() == ArrayNode {
count := node.ArraySize()
files := make([]FileInfo, count)
for i := 0; i < count; i++ {
if value := node.ArrayElement(i); value != nil {
files[i].initBy(value)
}
}
picker.files = files
for _, listener := range GetFileSelectedListeners(picker) {
listener(picker, files)
}
}
return true
case "fileLoaded":
if index, ok := dataIntProperty(data, "index"); ok {
if result, ok := picker.loader[index]; ok {
var file FileInfo
file.initBy(data)
var fileData []byte = nil
if base64Data, ok := data.PropertyValue("data"); ok {
if index := strings.LastIndex(base64Data, ","); index >= 0 {
base64Data = base64Data[index+1:]
}
decode, err := base64.StdEncoding.DecodeString(base64Data)
if err == nil {
fileData = decode
} else {
ErrorLog(err.Error())
}
}
result(file, fileData)
delete(picker.loader, index)
}
}
return true
case "fileLoadingError":
if error, ok := data.PropertyValue("error"); ok {
ErrorLog(error)
}
if index, ok := dataIntProperty(data, "index"); ok {
if result, ok := picker.loader[index]; ok {
if index >= 0 && index < len(picker.files) {
result(picker.files[index], nil)
} else {
result(FileInfo{}, nil)
}
delete(picker.loader, index)
}
}
return true
}
return picker.viewData.handleCommand(self, command, data)
}
// GetFilePickerFiles returns the list of FilePicker selected files
// If there are no files selected then an empty slice is returned (the result is always not nil)
// If the second argument (subviewID) is not specified or it is "" then selected files of the first argument (view) is returned
func GetFilePickerFiles(view View, subviewID ...string) []FileInfo {
subview := ""
if len(subviewID) > 0 {
subview = subviewID[0]
}
if picker := FilePickerByID(view, subview); picker != nil {
return picker.Files()
}
return []FileInfo{}
}
// LoadFilePickerFile loads the content of the selected file. This function is asynchronous.
// The "result" function will be called after loading the data.
// If the second argument (subviewID) is "" then the file from the first argument (view) is loaded
func LoadFilePickerFile(view View, subviewID string, file FileInfo, result func(FileInfo, []byte)) {
if picker := FilePickerByID(view, subviewID); picker != nil {
picker.LoadFile(file, result)
}
}
// IsMultipleFilePicker returns "true" if multiple files can be selected in the FilePicker, "false" otherwise.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func IsMultipleFilePicker(view View, subviewID ...string) bool {
return boolStyledProperty(view, subviewID, Multiple, false)
}
// GetFilePickerAccept returns sets the list of allowed file extensions or MIME types.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetFilePickerAccept(view View, subviewID ...string) []string {
if view = getSubview(view, subviewID); view != nil {
accept, ok := stringProperty(view, Accept, view.Session())
if !ok {
if value := valueFromStyle(view, Accept); value != nil {
accept, ok = value.(string)
}
}
if ok {
result := strings.Split(accept, ",")
for i := 0; i < len(result); i++ {
result[i] = strings.Trim(result[i], " \t\n")
}
return result
}
}
return []string{}
}
// GetFileSelectedListeners returns the "file-selected-event" listener list.
// If there are no listeners then the empty list is returned.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetFileSelectedListeners(view View, subviewID ...string) []func(FilePicker, []FileInfo) {
return getOneArgEventListeners[FilePicker, []FileInfo](view, subviewID, FileSelectedEvent)
}