github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/table/table.go (about) 1 package table 2 3 import ( 4 "html/template" 5 "sync" 6 "sync/atomic" 7 8 "github.com/kotovmak/go-admin/context" 9 "github.com/kotovmak/go-admin/modules/db" 10 "github.com/kotovmak/go-admin/modules/service" 11 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 12 "github.com/kotovmak/go-admin/plugins/admin/modules/paginator" 13 "github.com/kotovmak/go-admin/plugins/admin/modules/parameter" 14 "github.com/kotovmak/go-admin/template/types" 15 ) 16 17 type Generator func(ctx *context.Context) Table 18 19 type GeneratorList map[string]Generator 20 21 func (g GeneratorList) Add(key string, gen Generator) { 22 g[key] = gen 23 } 24 25 func (g GeneratorList) Combine(list GeneratorList) GeneratorList { 26 for key, gen := range list { 27 if _, ok := g[key]; !ok { 28 g[key] = gen 29 } 30 } 31 return g 32 } 33 34 func (g GeneratorList) CombineAll(gens []GeneratorList) GeneratorList { 35 for _, list := range gens { 36 for key, gen := range list { 37 if _, ok := g[key]; !ok { 38 g[key] = gen 39 } 40 } 41 } 42 return g 43 } 44 45 type Table interface { 46 GetInfo() *types.InfoPanel 47 GetDetail() *types.InfoPanel 48 GetDetailFromInfo() *types.InfoPanel 49 GetForm() *types.FormPanel 50 GetNewForm() *types.FormPanel 51 GetActualNewForm() *types.FormPanel 52 53 GetCanAdd() bool 54 GetEditable() bool 55 GetDeletable() bool 56 GetExportable() bool 57 58 GetPrimaryKey() PrimaryKey 59 60 GetData(params parameter.Parameters) (PanelInfo, error) 61 GetDataWithIds(params parameter.Parameters) (PanelInfo, error) 62 GetDataWithId(params parameter.Parameters) (FormInfo, error) 63 UpdateData(dataList form.Values) error 64 InsertData(dataList form.Values) error 65 DeleteData(pk string) error 66 67 GetNewFormInfo() FormInfo 68 69 GetOnlyInfo() bool 70 GetOnlyDetail() bool 71 GetOnlyNewForm() bool 72 GetOnlyUpdateForm() bool 73 74 Copy() Table 75 } 76 77 type BaseTable struct { 78 Info *types.InfoPanel 79 Form *types.FormPanel 80 NewForm *types.FormPanel 81 Detail *types.InfoPanel 82 CanAdd bool 83 Editable bool 84 Deletable bool 85 Exportable bool 86 OnlyInfo bool 87 OnlyDetail bool 88 OnlyNewForm bool 89 OnlyUpdateForm bool 90 PrimaryKey PrimaryKey 91 } 92 93 func (base *BaseTable) GetInfo() *types.InfoPanel { 94 return base.Info.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type) 95 } 96 97 func (base *BaseTable) GetDetail() *types.InfoPanel { 98 return base.Detail.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type) 99 } 100 101 func (base *BaseTable) GetDetailFromInfo() *types.InfoPanel { 102 detail := base.GetDetail() 103 detail.FieldList = make(types.FieldList, len(base.Info.FieldList)) 104 copy(detail.FieldList, base.Info.FieldList) 105 return detail 106 } 107 108 func (base *BaseTable) GetForm() *types.FormPanel { 109 return base.Form.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type) 110 } 111 112 func (base *BaseTable) GetNewForm() *types.FormPanel { 113 return base.NewForm.SetPrimaryKey(base.PrimaryKey.Name, base.PrimaryKey.Type) 114 } 115 116 func (base *BaseTable) GetActualNewForm() *types.FormPanel { 117 if len(base.NewForm.FieldList) == 0 { 118 return base.Form 119 } 120 return base.NewForm 121 } 122 123 func (base *BaseTable) GetCanAdd() bool { 124 return base.CanAdd 125 } 126 127 func (base *BaseTable) GetPrimaryKey() PrimaryKey { return base.PrimaryKey } 128 func (base *BaseTable) GetEditable() bool { return base.Editable } 129 func (base *BaseTable) GetDeletable() bool { return base.Deletable } 130 func (base *BaseTable) GetExportable() bool { return base.Exportable } 131 func (base *BaseTable) GetOnlyInfo() bool { return base.OnlyInfo } 132 func (base *BaseTable) GetOnlyDetail() bool { return base.OnlyDetail } 133 func (base *BaseTable) GetOnlyNewForm() bool { return base.OnlyNewForm } 134 func (base *BaseTable) GetOnlyUpdateForm() bool { return base.OnlyUpdateForm } 135 136 func (base *BaseTable) GetPaginator(size int, params parameter.Parameters, extraHtml ...template.HTML) types.PaginatorAttribute { 137 138 var eh template.HTML 139 140 if len(extraHtml) > 0 { 141 eh = extraHtml[0] 142 } 143 144 return paginator.Get(paginator.Config{ 145 Size: size, 146 Param: params, 147 PageSizeList: base.Info.GetPageSizeList(), 148 }).SetExtraInfo(eh) 149 } 150 151 type PanelInfo struct { 152 Thead types.Thead `json:"thead"` 153 InfoList types.InfoList `json:"info_list"` 154 FilterFormData types.FormFields `json:"filter_form_data"` 155 Paginator types.PaginatorAttribute `json:"-"` 156 Title string `json:"title"` 157 Description string `json:"description"` 158 } 159 160 type FormInfo struct { 161 FieldList types.FormFields `json:"field_list"` 162 GroupFieldList types.GroupFormFields `json:"group_field_list"` 163 GroupFieldHeaders types.GroupFieldHeaders `json:"group_field_headers"` 164 Title string `json:"title"` 165 Description string `json:"description"` 166 } 167 168 type PrimaryKey struct { 169 Type db.DatabaseType 170 Name string 171 } 172 173 const ( 174 DefaultPrimaryKeyName = "id" 175 DefaultConnectionName = "default" 176 ) 177 178 var ( 179 services service.List 180 count uint32 181 lock sync.Mutex 182 ) 183 184 func SetServices(srv service.List) { 185 lock.Lock() 186 defer lock.Unlock() 187 188 if atomic.LoadUint32(&count) != 0 { 189 panic("can not initialize twice") 190 } 191 192 services = srv 193 }