rui_orig/filePicker.go

387 lines
11 KiB
Go
Raw Permalink Normal View History

2021-11-04 14:59:25 +03:00
package rui
import (
2025-06-11 14:20:41 +03:00
"fmt"
2021-11-04 14:59:25 +03:00
"strconv"
"strings"
"time"
)
// Constants for [FilePicker] specific properties and events
2021-11-04 14:59:25 +03:00
const (
// FileSelectedEvent is the constant for "file-selected-event" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by FilePicker.
// Fired when user selects file(s).
//
// General listener format:
2024-12-05 20:15:39 +03:00
// 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:
2024-12-05 20:15:39 +03:00
// func(picker rui.FilePicker)
// func(files []rui.FileInfo)
// func()
2024-11-13 12:56:39 +03:00
FileSelectedEvent PropertyName = "file-selected-event"
2021-11-04 14:59:25 +03:00
// Accept is the constant for "accept" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by FilePicker.
// Set the list of allowed file extensions or MIME types.
//
2024-12-05 20:15:39 +03:00
// Supported types: string, []string.
//
2024-12-05 20:15:39 +03:00
// Internal type is string, other types converted to it during assignment.
//
// Conversion rules:
2024-12-05 20:15:39 +03:00
// - string - may contain single value of multiple separated by comma(,).
// - []string - an array of acceptable file extensions or MIME types.
2024-11-13 12:56:39 +03:00
Accept PropertyName = "accept"
2021-11-04 14:59:25 +03:00
// Multiple is the constant for "multiple" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by FilePicker.
// Controls whether multiple files can be selected.
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - true, 1, "true", "yes", "on", "1" - Several files can be selected.
// - false, 0, "false", "no", "off", "0" - Only one file can be selected.
2024-11-13 12:56:39 +03:00
Multiple PropertyName = "multiple"
2021-11-04 14:59:25 +03:00
)
// FileInfo describes a file which selected in the FilePicker view
type FileInfo struct {
// Name - the file's name.
Name string
2021-11-04 14:59:25 +03:00
// LastModified specifying the date and time at which the file was last modified
LastModified time.Time
2021-11-04 14:59:25 +03:00
// Size - the size of the file in bytes.
Size int64
2021-11-04 14:59:25 +03:00
// MimeType - the file's MIME type.
MimeType string
2025-06-11 14:20:41 +03:00
data []byte
2021-11-04 14:59:25 +03:00
}
// FilePicker represents the FilePicker view
2021-11-04 14:59:25 +03:00
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
2025-06-11 14:20:41 +03:00
files []FileInfo
//loader map[int]func(FileInfo, []byte)
2021-11-04 14:59:25 +03:00
}
func dataToFileInfo(obj DataObject) FileInfo {
2025-06-11 14:20:41 +03:00
var file FileInfo
file.Name, _ = obj.PropertyText("name")
file.MimeType, _ = obj.PropertyText("mime-type")
2021-11-04 14:59:25 +03:00
if size, ok := obj.PropertyText("size"); ok {
if n, err := strconv.ParseInt(size, 10, 64); err == nil {
file.Size = n
2021-11-04 14:59:25 +03:00
}
}
2021-11-04 14:59:25 +03:00
if value, ok := obj.PropertyText("last-modified"); ok {
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
file.LastModified = time.UnixMilli(n)
2021-11-04 14:59:25 +03:00
}
}
2025-06-11 14:20:41 +03:00
return file
}
func (file FileInfo) key() string {
return fmt.Sprintf("%s:%d", file.Name, int(file.Size))
2021-11-04 14:59:25 +03:00
}
// NewFilePicker create new FilePicker object and return it
func NewFilePicker(session Session, params Params) FilePicker {
2026-11-05 13:31:38 +03:00
if session == nil {
ErrorLog(`the Session argument cannot be nil`)
return nil
}
2021-11-04 14:59:25 +03:00
view := new(filePickerData)
view.init(session)
2021-11-04 14:59:25 +03:00
setInitParams(view, params)
return view
}
func newFilePicker(session Session) View {
2024-11-13 12:56:39 +03:00
return new(filePickerData) // NewFilePicker(session, nil)
2021-11-04 14:59:25 +03:00
}
func (picker *filePickerData) init(session Session) {
picker.viewData.init(session)
2021-11-04 14:59:25 +03:00
picker.tag = "FilePicker"
picker.files = []FileInfo{}
2025-06-11 14:20:41 +03:00
//picker.loader = map[int]func(FileInfo, []byte){}
2025-07-10 12:54:04 +03:00
picker.get = picker.getFunc
picker.set = picker.setFunc
picker.changed = picker.propertyChanged
2021-11-04 14:59:25 +03:00
2022-05-22 12:54:02 +03:00
}
func (picker *filePickerData) Focusable() bool {
return true
}
2021-11-04 14:59:25 +03:00
func (picker *filePickerData) Files() []FileInfo {
return picker.files
}
func (picker *filePickerData) LoadFile(file FileInfo, result func(FileInfo, []byte)) {
2025-06-11 14:20:41 +03:00
if result != nil {
for i, info := range picker.files {
2025-06-20 14:56:42 +03:00
if info.Name == file.Name && info.Size == file.Size && info.LastModified.Equal(file.LastModified) {
2025-06-11 14:20:41 +03:00
if info.data != nil {
result(info, info.data)
} else {
picker.fileLoader[info.key()] = func(file FileInfo, data []byte) {
picker.files[i].data = data
result(file, data)
}
picker.Session().callFunc("loadSelectedFile", picker.htmlID(), i)
}
return
}
2021-11-04 14:59:25 +03:00
}
2025-06-11 14:20:41 +03:00
picker.viewData.LoadFile(file, result)
2021-11-04 14:59:25 +03:00
}
}
2025-07-10 12:54:04 +03:00
func (picker *filePickerData) getFunc(tag PropertyName) any {
switch tag {
case FileSelectedEvent:
if listeners := getOneArgEventRawListeners[FilePicker, []FileInfo](picker, nil, tag); len(listeners) > 0 {
return listeners
}
return nil
}
return picker.viewData.getFunc(tag)
}
func (picker *filePickerData) setFunc(tag PropertyName, value any) []PropertyName {
2021-11-04 14:59:25 +03:00
switch tag {
case FileSelectedEvent:
return setOneArgEventListener[FilePicker, []FileInfo](picker, tag, value)
2021-11-04 14:59:25 +03:00
case Accept:
switch value := value.(type) {
case string:
return setStringPropertyValue(picker, Accept, strings.Trim(value, " \t\n"))
2021-11-04 14:59:25 +03:00
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())
2021-11-04 14:59:25 +03:00
}
2024-11-13 12:56:39 +03:00
notCompatibleType(tag, value)
return nil
}
return picker.viewData.setFunc(tag, value)
2024-11-13 12:56:39 +03:00
}
func (picker *filePickerData) propertyChanged(tag PropertyName) {
2024-11-13 12:56:39 +03:00
switch tag {
case Accept:
session := picker.Session()
if css := acceptPropertyCSS(picker); css != "" {
session.updateProperty(picker.htmlID(), "accept", css)
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(picker.htmlID(), "accept")
}
2021-11-04 14:59:25 +03:00
default:
picker.viewData.propertyChanged(tag)
2021-11-04 14:59:25 +03:00
}
}
func (picker *filePickerData) htmlTag() string {
return "input"
}
2024-11-13 12:56:39 +03:00
func acceptPropertyCSS(view View) string {
accept, ok := stringProperty(view, Accept, view.Session())
2021-11-04 14:59:25 +03:00
if !ok {
2024-11-13 12:56:39 +03:00
if value := valueFromStyle(view, Accept); value != nil {
2022-05-23 15:22:14 +03:00
accept, ok = value.(string)
}
2021-11-04 14:59:25 +03:00
}
2022-05-23 15:22:14 +03:00
2021-11-04 14:59:25 +03:00
if ok {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
2026-06-08 17:24:25 +03:00
for value := range strings.SplitSeq(accept, ",") {
2021-11-04 14:59:25 +03:00
if value = strings.Trim(value, " \t\n"); value != "" {
if buffer.Len() > 0 {
buffer.WriteString(", ")
}
2025-07-06 19:11:34 +03:00
if value[0] != '.' && !strings.ContainsRune(value, '/') {
2021-11-04 14:59:25 +03:00
buffer.WriteRune('.')
}
buffer.WriteString(value)
}
}
return buffer.String()
}
return ""
}
func (picker *filePickerData) htmlProperties(self View, buffer *strings.Builder) {
picker.viewData.htmlProperties(self, buffer)
2024-11-13 12:56:39 +03:00
if accept := acceptPropertyCSS(picker); accept != "" {
2026-09-03 12:39:07 +03:00
writeStrings(buffer, ` accept="`, accept, `"`)
2021-11-04 14:59:25 +03:00
}
buffer.WriteString(` type="file"`)
if IsMultipleFilePicker(picker) {
2021-11-04 14:59:25 +03:00
buffer.WriteString(` multiple`)
}
buffer.WriteString(` oninput="fileSelectedEvent(this)"`)
2022-04-15 15:41:44 +03:00
if picker.getRaw(ClickEvent) == nil {
buffer.WriteString(` onclick="stopEventPropagation(this, event)"`)
}
2021-11-04 14:59:25 +03:00
}
2025-06-11 14:20:41 +03:00
func parseFilesTag(data DataObject) []FileInfo {
if node := data.Property("files"); node != nil {
switch node.Type() {
case ArrayDataValue:
if array := node.Array(); array != nil {
files := make([]FileInfo, 0, array.Size())
for value := range array.Elements() {
if obj := value.Object(); obj != nil {
files = append(files, dataToFileInfo(obj))
}
}
return files
}
case ObjectDataValue:
if obj := node.Object(); obj != nil {
return []FileInfo{dataToFileInfo(obj)}
2025-06-11 14:20:41 +03:00
}
}
}
return nil
}
2024-11-13 12:56:39 +03:00
func (picker *filePickerData) handleCommand(self View, command PropertyName, data DataObject) bool {
2021-11-04 14:59:25 +03:00
switch command {
case "fileSelected":
2025-06-11 14:20:41 +03:00
if files := parseFilesTag(data); files != nil {
2021-11-04 14:59:25 +03:00
picker.files = files
2025-06-17 21:08:16 +03:00
for _, listener := range getOneArgEventListeners[FilePicker, []FileInfo](picker, nil, FileSelectedEvent) {
listener.Run(picker, files)
2021-11-04 14:59:25 +03:00
}
}
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 {
2021-11-04 14:59:25 +03:00
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.
2025-06-17 21:08:16 +03:00
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func IsMultipleFilePicker(view View, subviewID ...string) bool {
2022-07-28 12:11:27 +03:00
return boolStyledProperty(view, subviewID, Multiple, false)
2021-11-04 14:59:25 +03:00
}
// GetFilePickerAccept returns sets the list of allowed file extensions or MIME types.
2025-06-17 21:08:16 +03:00
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func GetFilePickerAccept(view View, subviewID ...string) []string {
2024-11-24 16:43:31 +03:00
if view = getSubview(view, subviewID); view != nil {
2021-11-04 14:59:25 +03:00
accept, ok := stringProperty(view, Accept, view.Session())
if !ok {
2022-05-23 15:22:14 +03:00
if value := valueFromStyle(view, Accept); value != nil {
accept, ok = value.(string)
}
2021-11-04 14:59:25 +03:00
}
if ok {
result := strings.Split(accept, ",")
2025-06-11 14:20:41 +03:00
for i := range len(result) {
2021-11-04 14:59:25 +03:00
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.
2025-06-17 21:08:16 +03:00
//
// Result elements can be of the following types:
// - func(rui.View, []rui.FileInfo),
// - func(rui.View),
// - func([]rui.FileInfo),
// - func(),
// - string.
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func GetFileSelectedListeners(view View, subviewID ...string) []any {
return getOneArgEventRawListeners[FilePicker, []FileInfo](view, subviewID, FileSelectedEvent)
2021-11-04 14:59:25 +03:00
}