-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.go
644 lines (514 loc) · 14.5 KB
/
types.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright 2019 GoAdmin. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package types
import (
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/modules/menu"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules"
form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
"github.com/GoAdminGroup/go-admin/template/types/form"
"html/template"
"strings"
)
// Attribute is the component interface of template. Every component of
// template should implement it.
type Attribute struct {
TemplateList map[string]string
}
// Page used in the template as a top variable.
type Page struct {
// User is the login user.
User models.UserModel
// Menu is the left side menu of the template.
Menu menu.Menu
// Panel is the main content of template.
Panel Panel
// System contains some system info.
System SystemInfo
// UrlPrefix is the prefix of url.
UrlPrefix string
// Title is the title of the web page.
Title string
// Logo is the logo of the template.
Logo template.HTML
// MiniLogo is the downsizing logo of the template.
MiniLogo template.HTML
// ColorScheme is the color scheme of the template.
ColorScheme string
// IndexUrl is the home page url of the site.
IndexUrl string
// AssetUrl is the cdn link of assets
CdnUrl string
// Custom html in the tag head.
CustomHeadHtml template.HTML
// Custom html after body.
CustomFootHtml template.HTML
}
func NewPage(user models.UserModel, menu menu.Menu, panel Panel, cfg config.Config) Page {
return Page{
User: user,
Menu: menu,
Panel: panel,
System: SystemInfo{
Version: system.Version,
},
UrlPrefix: cfg.Prefix(),
Title: cfg.Title,
Logo: cfg.Logo,
MiniLogo: cfg.MiniLogo,
ColorScheme: cfg.ColorScheme,
IndexUrl: cfg.GetIndexUrl(),
CdnUrl: cfg.AssetUrl,
CustomHeadHtml: cfg.CustomHeadHtml,
CustomFootHtml: cfg.CustomFootHtml,
}
}
// SystemInfo contains basic info of system.
type SystemInfo struct {
Version string
}
// Panel contains the main content of the template which used as pjax.
type Panel struct {
Content template.HTML
Title string
Description string
Url string
}
type GetPanel func(ctx interface{}) (Panel, error)
// FieldModel is the single query result.
type FieldModel struct {
// The primaryKey of the table.
ID string
// The value of the single query result.
Value string
// The current row data.
Row map[string]interface{}
}
// PostFieldModel contains ID and value of the single query result and the current row data.
type PostFieldModel struct {
ID string
Value FieldModelValue
Row map[string]interface{}
}
type FieldModelValue []string
func (r FieldModelValue) Value() string {
return r.First()
}
func (r FieldModelValue) First() string {
return r[0]
}
// FieldDisplay is filter function of data.
type FieldFilterFn func(value FieldModel) interface{}
// PostFieldFilterFn is filter function of data.
type PostFieldFilterFn func(value PostFieldModel) string
type DisplayProcessFn func(string) string
type DisplayProcessFnChains []DisplayProcessFn
func (d DisplayProcessFnChains) Valid() bool {
return len(d) > 0
}
func (d DisplayProcessFnChains) Add(f DisplayProcessFn) DisplayProcessFnChains {
return append(d, f)
}
type FieldDisplay struct {
Display FieldFilterFn
DisplayProcessChains DisplayProcessFnChains
}
func (f FieldDisplay) ToDisplay(value FieldModel) interface{} {
val := f.Display(value)
if valStr, ok := val.(string); ok {
for _, process := range f.DisplayProcessChains {
valStr = process(valStr)
}
return valStr
}
return val
}
func (f FieldDisplay) AddLimit(limit int) DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
if limit > len(value) {
return value
} else if limit < 0 {
return ""
} else {
return value[:limit]
}
})
}
func (f FieldDisplay) AddTrimSpace() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.TrimSpace(value)
})
}
func (f FieldDisplay) AddSubstr(start int, end int) DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
if start > end || start > len(value) || end < 0 {
return ""
}
if start < 0 {
start = 0
}
if end > len(value) {
end = len(value)
}
return value[start:end]
})
}
func (f FieldDisplay) AddToTitle() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.Title(value)
})
}
func (f FieldDisplay) AddToUpper() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.ToUpper(value)
})
}
func (f FieldDisplay) AddToLower() DisplayProcessFnChains {
return f.DisplayProcessChains.Add(func(value string) string {
return strings.ToLower(value)
})
}
// Field is the table field.
type Field struct {
Head string
Field string
TypeName db.DatabaseType
Join Join
Width int
Sortable bool
Fixed bool
Filterable bool
Hide bool
FieldDisplay
}
type Join struct {
Table string
Field string
JoinField string
}
func (j Join) Valid() bool {
return j.Table != "" && j.Field != "" && j.JoinField != ""
}
type TabGroups [][]string
func (t TabGroups) Valid() bool {
return len(t) > 0
}
func NewTabGroups(items ...string) TabGroups {
var t = make(TabGroups, 0)
return append(t, items)
}
func (t TabGroups) AddGroup(items ...string) TabGroups {
return append(t, items)
}
type TabHeaders []string
func (t TabHeaders) Add(header string) TabHeaders {
return append(t, header)
}
// InfoPanel
type InfoPanel struct {
FieldList []Field
curFieldListIndex int
Table string
Title string
Description string
// Warn: may be deprecated future.
TabGroups TabGroups
TabHeaders TabHeaders
Sort Sort
Action template.HTML
HeaderHtml template.HTML
FooterHtml template.HTML
}
func NewInfoPanel() *InfoPanel {
return &InfoPanel{curFieldListIndex: -1}
}
func (i *InfoPanel) AddField(head, field string, typeName db.DatabaseType) *InfoPanel {
i.FieldList = append(i.FieldList, Field{
Head: head,
Field: field,
TypeName: typeName,
Sortable: false,
FieldDisplay: FieldDisplay{
Display: func(value FieldModel) interface{} {
return value.Value
},
DisplayProcessChains: make(DisplayProcessFnChains, 0),
},
})
i.curFieldListIndex++
return i
}
// Field attribute setting functions
// ====================================================
func (i *InfoPanel) FieldDisplay(filter FieldFilterFn) *InfoPanel {
i.FieldList[i.curFieldListIndex].Display = filter
return i
}
func (i *InfoPanel) FieldWidth(width int) *InfoPanel {
i.FieldList[i.curFieldListIndex].Width = width
return i
}
func (i *InfoPanel) FieldSortable(sort bool) *InfoPanel {
i.FieldList[i.curFieldListIndex].Sortable = sort
return i
}
func (i *InfoPanel) FieldFixed(fixed bool) *InfoPanel {
i.FieldList[i.curFieldListIndex].Fixed = fixed
return i
}
func (i *InfoPanel) FieldFilterable(filter bool) *InfoPanel {
i.FieldList[i.curFieldListIndex].Filterable = filter
return i
}
func (i *InfoPanel) FieldHide(hide bool) *InfoPanel {
i.FieldList[i.curFieldListIndex].Hide = hide
return i
}
func (i *InfoPanel) FieldJoin(join Join) *InfoPanel {
i.FieldList[i.curFieldListIndex].Join = join
return i
}
func (i *InfoPanel) FieldLimit(limit int) *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddLimit(limit)
return i
}
func (i *InfoPanel) FieldTrimSpace() *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddTrimSpace()
return i
}
func (i *InfoPanel) FieldSubstr(start int, end int) *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddSubstr(start, end)
return i
}
func (i *InfoPanel) FieldToTitle() *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddToTitle()
return i
}
func (i *InfoPanel) FieldToUpper() *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddToUpper()
return i
}
func (i *InfoPanel) FieldToLower() *InfoPanel {
i.FieldList[i.curFieldListIndex].DisplayProcessChains = i.FieldList[i.curFieldListIndex].AddToLower()
return i
}
// InfoPanel attribute setting functions
// ====================================================
func (i *InfoPanel) SetTable(table string) *InfoPanel {
i.Table = table
return i
}
func (i *InfoPanel) SetTitle(title string) *InfoPanel {
i.Title = title
return i
}
func (i *InfoPanel) SetTabGroups(groups TabGroups) *InfoPanel {
i.TabGroups = groups
return i
}
func (i *InfoPanel) SetTabHeaders(headers ...string) *InfoPanel {
i.TabHeaders = headers
return i
}
func (i *InfoPanel) SetDescription(desc string) *InfoPanel {
i.Description = desc
return i
}
func (i *InfoPanel) SetSort(sort Sort) *InfoPanel {
i.Sort = sort
return i
}
func (i *InfoPanel) SetAction(action template.HTML) *InfoPanel {
i.Action = action
return i
}
func (i *InfoPanel) SetHeaderHtml(header template.HTML) *InfoPanel {
i.HeaderHtml = header
return i
}
func (i *InfoPanel) SetFooterHtml(footer template.HTML) *InfoPanel {
i.FooterHtml = footer
return i
}
type Sort uint8
const (
SortDesc Sort = iota
SortAsc
)
// FormField is the form field with different options.
type FormField struct {
Field string
TypeName db.DatabaseType
Head string
FormType form.Type
Default string
Value string
Options []map[string]string
DefaultOptionDelimiter string
Editable bool
NotAllowAdd bool
Must bool
FieldDisplay
PostFilterFn PostFieldFilterFn
}
// FormPanel
type FormPanel struct {
FieldList FormFields
curFieldListIndex int
// Warn: may be deprecated future.
TabGroups TabGroups
TabHeaders TabHeaders
Table string
Title string
Description string
Validator FormValidator
PostHook FormPostHookFn
HeaderHtml template.HTML
FooterHtml template.HTML
}
func NewFormPanel() *FormPanel {
return &FormPanel{curFieldListIndex: -1}
}
func (f *FormPanel) AddField(head, field string, filedType db.DatabaseType, formType form.Type) *FormPanel {
f.FieldList = append(f.FieldList, FormField{
Head: head,
Field: field,
TypeName: filedType,
Editable: true,
FormType: formType,
FieldDisplay: FieldDisplay{
Display: func(value FieldModel) interface{} {
return value.Value
},
DisplayProcessChains: make(DisplayProcessFnChains, 0),
},
})
f.curFieldListIndex++
return f
}
// Field attribute setting functions
// ====================================================
func (f *FormPanel) FieldDisplay(filter FieldFilterFn) *FormPanel {
f.FieldList[f.curFieldListIndex].Display = filter
return f
}
func (f *FormPanel) SetTable(table string) *FormPanel {
f.Table = table
return f
}
func (f *FormPanel) FieldMust(must bool) *FormPanel {
f.FieldList[f.curFieldListIndex].Must = must
return f
}
func (f *FormPanel) FieldEditable(edit bool) *FormPanel {
f.FieldList[f.curFieldListIndex].Editable = edit
return f
}
func (f *FormPanel) FieldNotAllowAdd(add bool) *FormPanel {
f.FieldList[f.curFieldListIndex].NotAllowAdd = add
return f
}
func (f *FormPanel) FieldFormType(formType form.Type) *FormPanel {
f.FieldList[f.curFieldListIndex].FormType = formType
return f
}
func (f *FormPanel) FieldValue(value string) *FormPanel {
f.FieldList[f.curFieldListIndex].Value = value
return f
}
func (f *FormPanel) FieldOptions(options []map[string]string) *FormPanel {
f.FieldList[f.curFieldListIndex].Options = options
return f
}
func (f *FormPanel) FieldDefaultOptionDelimiter(delimiter string) *FormPanel {
f.FieldList[f.curFieldListIndex].DefaultOptionDelimiter = delimiter
return f
}
func (f *FormPanel) FieldPostFilterFn(post PostFieldFilterFn) *FormPanel {
f.FieldList[f.curFieldListIndex].PostFilterFn = post
return f
}
func (f *FormPanel) FieldLimit(limit int) *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddLimit(limit)
return f
}
func (f *FormPanel) FieldTrimSpace() *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddTrimSpace()
return f
}
func (f *FormPanel) FieldSubstr(start int, end int) *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddSubstr(start, end)
return f
}
func (f *FormPanel) FieldToTitle() *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddToTitle()
return f
}
func (f *FormPanel) FieldToUpper() *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddToUpper()
return f
}
func (f *FormPanel) FieldToLower() *FormPanel {
f.FieldList[f.curFieldListIndex].DisplayProcessChains = f.FieldList[f.curFieldListIndex].AddToLower()
return f
}
// FormPanel attribute setting functions
// ====================================================
func (f *FormPanel) SetTitle(title string) *FormPanel {
f.Title = title
return f
}
func (f *FormPanel) SetTabGroups(groups TabGroups) *FormPanel {
f.TabGroups = groups
return f
}
func (f *FormPanel) SetTabHeaders(headers ...string) *FormPanel {
f.TabHeaders = headers
return f
}
func (f *FormPanel) SetDescription(desc string) *FormPanel {
f.Description = desc
return f
}
func (f *FormPanel) SetHeaderHtml(header template.HTML) *FormPanel {
f.HeaderHtml = header
return f
}
func (f *FormPanel) SetFooterHtml(footer template.HTML) *FormPanel {
f.FooterHtml = footer
return f
}
func (f *FormPanel) SetPostValidator(va FormValidator) *FormPanel {
f.Validator = va
return f
}
func (f *FormPanel) SetPostHook(po FormPostHookFn) *FormPanel {
f.PostHook = po
return f
}
type FormValidator func(values form2.Values) error
type FormPostHookFn func(values form2.Values)
type FormFields []FormField
func (f FormFields) Copy() FormFields {
formList := make(FormFields, len(f))
copy(formList, f)
for i := 0; i < len(formList); i++ {
formList[i].Options = make([]map[string]string, len(f[i].Options))
for j := 0; j < len(f[i].Options); j++ {
formList[i].Options[j] = modules.CopyMap(f[i].Options[j])
}
}
return formList
}
func (f FormFields) FindByFieldName(field string) FormField {
for i := 0; i < len(f); i++ {
if f[i].Field == field {
return f[i]
}
}
return FormField{}
}