-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_configors.go
145 lines (132 loc) · 3.46 KB
/
meta_configors.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
package admin
import (
"errors"
"time"
"github.com/qor/qor"
"github.com/qor/qor/utils"
)
// metaConfig meta config
type metaConfig struct {
}
// GetTemplate get customized template for meta
func (metaConfig) GetTemplate(context *Context, metaType string) ([]byte, error) {
return nil, errors.New("not implemented")
}
var defaultMetaConfigorMaps = map[string]func(*Meta){
"date": func(meta *Meta) {
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
switch date := meta.GetValuer()(value, context).(type) {
case *time.Time:
if date == nil {
return ""
}
if date.IsZero() {
return ""
}
return utils.FormatTime(*date, "2006-01-02", context)
case time.Time:
if date.IsZero() {
return ""
}
return utils.FormatTime(date, "2006-01-02", context)
default:
return date
}
})
}
},
"datetime": func(meta *Meta) {
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
switch date := meta.GetValuer()(value, context).(type) {
case *time.Time:
if date == nil {
return ""
}
if date.IsZero() {
return ""
}
return utils.FormatTime(*date, "2006-01-02 15:04", context)
case time.Time:
if date.IsZero() {
return ""
}
return utils.FormatTime(date, "2006-01-02 15:04", context)
default:
return date
}
})
}
},
"string": func(meta *Meta) {
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
switch str := meta.GetValuer()(value, context).(type) {
case *string:
if str != nil {
return *str
}
return ""
case string:
return str
default:
return str
}
})
}
},
"text": func(meta *Meta) {
if meta.FormattedValuer == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
switch str := meta.GetValuer()(value, context).(type) {
case *string:
if str != nil {
return *str
}
return ""
case string:
return str
default:
return str
}
})
}
},
"select_one": func(meta *Meta) {
if metaConfig, ok := meta.Config.(*SelectOneConfig); !ok || metaConfig == nil {
meta.Config = &SelectOneConfig{Collection: meta.Collection}
meta.Config.ConfigureQorMeta(meta)
} else if meta.Collection != nil {
metaConfig.Collection = meta.Collection
meta.Config.ConfigureQorMeta(meta)
}
},
"select_many": func(meta *Meta) {
if metaConfig, ok := meta.Config.(*SelectManyConfig); !ok || metaConfig == nil {
meta.Config = &SelectManyConfig{Collection: meta.Collection}
meta.Config.ConfigureQorMeta(meta)
} else if meta.Collection != nil {
metaConfig.Collection = meta.Collection
meta.Config.ConfigureQorMeta(meta)
}
},
"single_edit": func(meta *Meta) {
if _, ok := meta.Config.(*SingleEditConfig); !ok || meta.Config == nil {
meta.Config = &SingleEditConfig{}
meta.Config.ConfigureQorMeta(meta)
}
},
"collection_edit": func(meta *Meta) {
if _, ok := meta.Config.(*CollectionEditConfig); !ok || meta.Config == nil {
meta.Config = &CollectionEditConfig{}
meta.Config.ConfigureQorMeta(meta)
}
},
"rich_editor": func(meta *Meta) {
if _, ok := meta.Config.(*RichEditorConfig); !ok || meta.Config == nil {
meta.Config = &RichEditorConfig{}
meta.Config.ConfigureQorMeta(meta)
}
},
}