-
Notifications
You must be signed in to change notification settings - Fork 11
/
setting.go
375 lines (332 loc) · 11.1 KB
/
setting.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
372
373
374
375
package widget
import (
"fmt"
"html/template"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
"github.com/qor/serializable_meta"
)
// QorWidgetSettingInterface qor widget setting interface
type QorWidgetSettingInterface interface {
GetWidgetName() string
SetWidgetName(string)
GetGroupName() string
SetGroupName(string)
GetScope() string
SetScope(string)
GetTemplate() string
SetTemplate(string)
GetSourceType() string
SetSourceType(string)
GetSourceID() string
SetSourceID(string)
GetShared() bool
SetShared(bool)
serializable_meta.SerializableMetaInterface
}
// QorWidgetSetting default qor widget setting struct
type QorWidgetSetting struct {
Name string `gorm:"primary_key;size:100"`
Scope string `gorm:"primary_key;size:128;default:'default'"`
SourceType string `gorm:"primary_key;default:'';size:100"`
SourceID string `gorm:"primary_key;default:'';size:100"`
Description string
Shared bool
WidgetType string
GroupName string
Template string
serializable_meta.SerializableMeta
CreatedAt time.Time
UpdatedAt time.Time
}
// ResourceName get widget setting's resource name
func (widgetSetting *QorWidgetSetting) ResourceName() string {
return "Widget Content"
}
// GetSerializableArgumentKind get serializable kind
func (widgetSetting *QorWidgetSetting) GetSerializableArgumentKind() string {
if widgetSetting.WidgetType != "" {
return widgetSetting.WidgetType
}
return widgetSetting.Kind
}
// SetSerializableArgumentKind set serializable kind
func (widgetSetting *QorWidgetSetting) SetSerializableArgumentKind(name string) {
widgetSetting.WidgetType = name
widgetSetting.Kind = name
}
// GetWidgetName get widget setting's group name
func (widgetSetting QorWidgetSetting) GetWidgetName() string {
return widgetSetting.Name
}
// SetWidgetName set widget setting's group name
func (widgetSetting *QorWidgetSetting) SetWidgetName(name string) {
widgetSetting.Name = name
}
// GetGroupName get widget setting's group name
func (widgetSetting QorWidgetSetting) GetGroupName() string {
return widgetSetting.GroupName
}
// SetGroupName set widget setting's group name
func (widgetSetting *QorWidgetSetting) SetGroupName(groupName string) {
widgetSetting.GroupName = groupName
}
// GetScope get widget's scope
func (widgetSetting QorWidgetSetting) GetScope() string {
return widgetSetting.Scope
}
// SetScope set widget setting's scope
func (widgetSetting *QorWidgetSetting) SetScope(scope string) {
widgetSetting.Scope = scope
}
// GetSourceType get widget's source type
func (widgetSetting QorWidgetSetting) GetSourceType() string {
return widgetSetting.SourceType
}
// SetSourceType set widget setting's souce type
func (widgetSetting *QorWidgetSetting) SetSourceType(sourceType string) {
widgetSetting.SourceType = sourceType
}
// GetSourceID get widget's source ID
func (widgetSetting QorWidgetSetting) GetSourceID() string {
return widgetSetting.SourceID
}
// SetSourceID set widget setting's source id
func (widgetSetting *QorWidgetSetting) SetSourceID(sourceID string) {
widgetSetting.SourceID = sourceID
}
// GetShared get widget's source ID
func (widgetSetting QorWidgetSetting) GetShared() bool {
return widgetSetting.Shared
}
// SetShared set widget setting's source id
func (widgetSetting *QorWidgetSetting) SetShared(shared bool) {
widgetSetting.Shared = shared
}
// GetTemplate get used widget template
func (widgetSetting QorWidgetSetting) GetTemplate() string {
if widget := GetWidget(widgetSetting.GetSerializableArgumentKind()); widget != nil {
for _, value := range widget.Templates {
if value == widgetSetting.Template {
return value
}
}
// return first value of defined widget templates
for _, value := range widget.Templates {
return value
}
}
return ""
}
// SetTemplate set used widget's template
func (widgetSetting *QorWidgetSetting) SetTemplate(template string) {
widgetSetting.Template = template
}
// GetSerializableArgumentResource get setting's argument's resource
func (widgetSetting *QorWidgetSetting) GetSerializableArgumentResource() *admin.Resource {
widget := GetWidget(widgetSetting.GetSerializableArgumentKind())
if widget != nil {
return widget.Setting
}
return nil
}
// ConfigureQorResource a method used to config Widget for qor admin
func (widgetSetting *QorWidgetSetting) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
res.Meta(&admin.Meta{Name: "PreviewIcon", Valuer: func(result interface{}, context *qor.Context) interface{} {
if setting, ok := result.(QorWidgetSettingInterface); ok {
if widget := GetWidget(setting.GetSerializableArgumentKind()); widget != nil {
return template.HTML(fmt.Sprintf("<img class='qor-preview-icon' src='%v'/>", widget.PreviewIcon))
}
}
return ""
}})
res.Meta(&admin.Meta{Name: "Name", Type: "string"})
res.Meta(&admin.Meta{Name: "DisplayName", Label: "Name", Type: "readonly", FieldName: "Name"})
res.Meta(&admin.Meta{Name: "Description", Type: "string"})
res.Meta(&admin.Meta{
Name: "Scope",
Type: "hidden",
Valuer: func(result interface{}, context *qor.Context) interface{} {
if scope := context.Request.URL.Query().Get("widget_scope"); scope != "" {
return scope
}
if setting, ok := result.(QorWidgetSettingInterface); ok {
if scope := setting.GetScope(); scope != "" {
return scope
}
}
return "default"
},
Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
setting.SetScope(utils.ToString(metaValue.Value))
}
},
})
res.Meta(&admin.Meta{
Name: "SourceType",
Type: "hidden",
Valuer: func(result interface{}, context *qor.Context) interface{} {
if sourceType := context.Request.URL.Query().Get("widget_source_type"); sourceType != "" {
return sourceType
}
if setting, ok := result.(QorWidgetSettingInterface); ok {
if sourceType := setting.GetSourceType(); sourceType != "" {
return sourceType
}
}
return ""
},
Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
setting.SetSourceType(utils.ToString(metaValue.Value))
}
},
})
res.Meta(&admin.Meta{
Name: "SourceID",
Type: "hidden",
Valuer: func(result interface{}, context *qor.Context) interface{} {
if sourceID := context.Request.URL.Query().Get("widget_source_id"); sourceID != "" {
return sourceID
}
if setting, ok := result.(QorWidgetSettingInterface); ok {
if sourceID := setting.GetSourceID(); sourceID != "" {
return sourceID
}
}
return ""
},
Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
setting.SetSourceID(utils.ToString(metaValue.Value))
}
},
})
res.Meta(&admin.Meta{
Name: "Widgets",
Type: "select_one",
Valuer: func(result interface{}, context *qor.Context) interface{} {
if typ := context.Request.URL.Query().Get("widget_type"); typ != "" {
return typ
}
if setting, ok := result.(QorWidgetSettingInterface); ok {
widget := GetWidget(setting.GetSerializableArgumentKind())
if widget == nil {
return ""
}
return widget.Name
}
return ""
},
Collection: func(result interface{}, context *qor.Context) (results [][]string) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
if setting.GetWidgetName() == "" {
for _, widget := range registeredWidgets {
results = append(results, []string{widget.Name, widget.Name})
}
} else {
groupName := setting.GetGroupName()
for _, group := range registeredWidgetsGroup {
if group.Name == groupName {
for _, widget := range group.Widgets {
results = append(results, []string{widget, widget})
}
}
}
}
if len(results) == 0 {
results = append(results, []string{setting.GetSerializableArgumentKind(), setting.GetSerializableArgumentKind()})
}
}
return
},
Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
setting.SetSerializableArgumentKind(utils.ToString(metaValue.Value))
}
},
})
res.Meta(&admin.Meta{
Name: "Template",
Type: "select_one",
Valuer: func(result interface{}, context *qor.Context) interface{} {
if setting, ok := result.(QorWidgetSettingInterface); ok {
return setting.GetTemplate()
}
return ""
},
Collection: func(result interface{}, context *qor.Context) (results [][]string) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
if widget := GetWidget(setting.GetSerializableArgumentKind()); widget != nil {
for _, value := range widget.Templates {
results = append(results, []string{value, value})
}
}
}
return
},
Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if setting, ok := result.(QorWidgetSettingInterface); ok {
setting.SetTemplate(utils.ToString(metaValue.Value))
}
},
})
res.Meta(&admin.Meta{
Name: "Shared",
Label: "Add to Container Library (can be reused on other pages)",
})
res.Action(&admin.Action{
Name: "Preview",
URL: func(record interface{}, context *admin.Context) string {
return fmt.Sprintf("%v/%v/%v/!preview", context.Admin.GetRouter().Prefix, res.ToParam(), record.(QorWidgetSettingInterface).GetWidgetName())
},
Modes: []string{"edit", "menu_item"},
})
res.AddProcessor(&resource.Processor{
Name: "widget-default-value",
Handler: func(value interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
if widgetSetting, ok := value.(QorWidgetSettingInterface); ok {
if widgetSetting.GetShared() {
widgetSetting.SetSourceType("")
widgetSetting.SetSourceID("")
}
}
return nil
},
})
res.UseTheme("widget")
res.IndexAttrs("PreviewIcon", "Name", "Description", "CreatedAt", "UpdatedAt")
res.OverrideShowAttrs(func() {
res.ShowAttrs("PreviewIcon", "Name", "Scope", "WidgetType", "Template", "Description", "Value", "CreatedAt", "UpdatedAt")
})
res.EditAttrs(
"DisplayName", "Description", "Scope", "Widgets", "Template",
&admin.Section{
Title: "Settings",
Rows: [][]string{{"Kind"}, {"SerializableMeta"}},
},
"Shared", "SourceType", "SourceID",
)
res.NewAttrs("Name", "Description", "Scope", "Widgets", "Template",
&admin.Section{
Title: "Settings",
Rows: [][]string{{"Kind"}, {"SerializableMeta"}},
},
"Shared", "SourceType", "SourceID",
)
searchHandler := res.SearchHandler
res.SearchHandler = func(keyword string, context *qor.Context) *gorm.DB {
// don't include widgets have source_type in index page
if context.ResourceID == "" {
context.SetDB(context.GetDB().Where("source_type = ?", ""))
}
return searchHandler(keyword, context)
}
}
}