-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
194 lines (159 loc) · 5 KB
/
table.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
package table
import (
"html/template"
"sync"
"sync/atomic"
"github.com/MarErm27/go-admin/context"
"github.com/MarErm27/go-admin/modules/db"
"github.com/MarErm27/go-admin/modules/service"
"github.com/MarErm27/go-admin/plugins/admin/modules/form"
"github.com/MarErm27/go-admin/plugins/admin/modules/paginator"
"github.com/MarErm27/go-admin/plugins/admin/modules/parameter"
"github.com/MarErm27/go-admin/template/types"
)
type Generator func(ctx *context.Context) Table
type GeneratorList map[string]Generator
func (g GeneratorList) Add(key string, gen Generator) {
g[key] = gen
}
func (g GeneratorList) Combine(list GeneratorList) GeneratorList {
for key, gen := range list {
if _, ok := g[key]; !ok {
g[key] = gen
}
}
return g
}
func (g GeneratorList) CombineAll(gens []GeneratorList) GeneratorList {
for _, list := range gens {
for key, gen := range list {
if _, ok := g[key]; !ok {
g[key] = gen
}
}
}
return g
}
type Table interface {
GetInfo() *types.InfoPanel
GetDetail() *types.InfoPanel
GetDetailFromInfo() *types.InfoPanel
GetForm() *types.FormPanel
GetNewForm() *types.FormPanel
GetActualNewForm() *types.FormPanel
GetCanAdd() bool
GetEditable() bool
GetDeletable() bool
GetExportable() bool
GetPrimaryKey() PrimaryKey
GetData(params parameter.Parameters) (PanelInfo, error)
GetDataWithIds(params parameter.Parameters) (PanelInfo, error)
GetDataWithId(params parameter.Parameters) (FormInfo, error)
UpdateData(dataList form.Values) error
InsertData(dataList form.Values) error
DeleteData(pk string) error
GetNewFormInfo() FormInfo
GetOnlyInfo() bool
GetOnlyDetail() bool
GetOnlyNewForm() bool
GetOnlyUpdateForm() bool
Copy() Table
}
type BaseTable struct {
Info *types.InfoPanel
Form *types.FormPanel
NewForm *types.FormPanel
Detail *types.InfoPanel
CanAdd bool
Editable bool
Deletable bool
Exportable bool
OnlyInfo bool
OnlyDetail bool
OnlyNewForm bool
OnlyUpdateForm bool
PrimaryKey PrimaryKey
}
func (base *BaseTable) GetInfo() *types.InfoPanel {
return base.Info.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type)
}
func (base *BaseTable) GetDetail() *types.InfoPanel {
return base.Detail.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type)
}
func (base *BaseTable) GetDetailFromInfo() *types.InfoPanel {
detail := base.GetDetail()
detail.FieldList = make(types.FieldList, len(base.Info.FieldList))
copy(detail.FieldList, base.Info.FieldList)
return detail
}
func (base *BaseTable) GetForm() *types.FormPanel {
return base.Form.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type)
}
func (base *BaseTable) GetNewForm() *types.FormPanel {
return base.Form.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type)
}
func (base *BaseTable) GetActualNewForm() *types.FormPanel {
f := base.GetNewForm()
if len(f.FieldList) == 0 {
return base.GetForm()
}
return f
}
func (base *BaseTable) GetCanAdd() bool {
return base.CanAdd
}
func (base *BaseTable) GetPrimaryKey() PrimaryKey { return base.PrimaryKey }
func (base *BaseTable) GetEditable() bool { return base.Editable }
func (base *BaseTable) GetDeletable() bool { return base.Deletable }
func (base *BaseTable) GetExportable() bool { return base.Exportable }
func (base *BaseTable) GetOnlyInfo() bool { return base.OnlyInfo }
func (base *BaseTable) GetOnlyDetail() bool { return base.OnlyDetail }
func (base *BaseTable) GetOnlyNewForm() bool { return base.OnlyNewForm }
func (base *BaseTable) GetOnlyUpdateForm() bool { return base.OnlyUpdateForm }
func (base *BaseTable) GetPaginator(size int, params parameter.Parameters, extraHtml ...template.HTML) types.PaginatorAttribute {
var eh template.HTML
if len(extraHtml) > 0 {
eh = extraHtml[0]
}
return paginator.Get(paginator.Config{
Size: size,
Param: params,
PageSizeList: base.Info.GetPageSizeList(),
}).SetExtraInfo(eh)
}
type PanelInfo struct {
Thead types.Thead `json:"thead"`
InfoList types.InfoList `json:"info_list"`
FilterFormData types.FormFields `json:"filter_form_data"`
Paginator types.PaginatorAttribute `json:"-"`
Title string `json:"title"`
Description string `json:"description"`
}
type FormInfo struct {
FieldList types.FormFields `json:"field_list"`
GroupFieldList types.GroupFormFields `json:"group_field_list"`
GroupFieldHeaders types.GroupFieldHeaders `json:"group_field_headers"`
Title string `json:"title"`
Description string `json:"description"`
}
type PrimaryKey struct {
Type db.DatabaseType
Name string
}
const (
DefaultPrimaryKeyName = "id"
DefaultConnectionName = "default"
)
var (
services service.List
count uint32
lock sync.Mutex
)
func SetServices(srv service.List) {
lock.Lock()
defer lock.Unlock()
if atomic.LoadUint32(&count) != 0 {
panic("can not initialize twice")
}
services = srv
}