-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_select_many.go
67 lines (59 loc) · 2.55 KB
/
meta_select_many.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
package admin
import (
"errors"
"html/template"
"reflect"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
)
// SelectManyConfig meta configuration used for select many
type SelectManyConfig struct {
Collection interface{} // []string, [][]string, func(interface{}, *qor.Context) [][]string, func(interface{}, *admin.Context) [][]string
DefaultCreating bool
Placeholder string
SelectionTemplate string
SelectMode string // select, select_async, bottom_sheet
Select2ResultTemplate template.JS
Select2SelectionTemplate template.JS
RemoteDataResource *Resource
PrimaryField string
SelectOneConfig
}
// GetTemplate get template for selection template
func (selectManyConfig SelectManyConfig) GetTemplate(context *Context, metaType string) ([]byte, error) {
if metaType == "form" && selectManyConfig.SelectionTemplate != "" {
return context.Asset(selectManyConfig.SelectionTemplate)
}
return nil, errors.New("not implemented")
}
// ConfigureQorMeta configure select many meta
func (selectManyConfig *SelectManyConfig) ConfigureQorMeta(metaor resource.Metaor) {
if meta, ok := metaor.(*Meta); ok {
selectManyConfig.SelectOneConfig.Collection = selectManyConfig.Collection
selectManyConfig.SelectOneConfig.SelectMode = selectManyConfig.SelectMode
selectManyConfig.SelectOneConfig.DefaultCreating = selectManyConfig.DefaultCreating
selectManyConfig.SelectOneConfig.Placeholder = selectManyConfig.Placeholder
selectManyConfig.SelectOneConfig.RemoteDataResource = selectManyConfig.RemoteDataResource
selectManyConfig.SelectOneConfig.PrimaryField = selectManyConfig.PrimaryField
selectManyConfig.SelectOneConfig.ConfigureQorMeta(meta)
selectManyConfig.RemoteDataResource = selectManyConfig.SelectOneConfig.RemoteDataResource
selectManyConfig.SelectMode = selectManyConfig.SelectOneConfig.SelectMode
selectManyConfig.DefaultCreating = selectManyConfig.SelectOneConfig.DefaultCreating
selectManyConfig.PrimaryField = selectManyConfig.SelectOneConfig.PrimaryField
meta.Type = "select_many"
// Set FormattedValuer
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(record interface{}, context *qor.Context) interface{} {
reflectValues := reflect.Indirect(reflect.ValueOf(meta.GetValuer()(record, context)))
var results []string
if reflectValues.IsValid() {
for i := 0; i < reflectValues.Len(); i++ {
results = append(results, utils.Stringify(reflectValues.Index(i).Interface()))
}
}
return results
})
}
}
}