Merge pull request #227 from trheyi/main

[add] bind model
This commit is contained in:
Max 2022-10-24 12:51:54 +08:00 committed by GitHub
commit fb0ff4f345
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 147 additions and 20 deletions

View file

@ -38,7 +38,7 @@ func (dsl *DSL) bindModel() error {
dsl.Action.BindModel(m) dsl.Action.BindModel(m)
dsl.Fields.BindModel(m) dsl.Fields.BindModel(m)
// dsl.Layout.BindModel(m) dsl.Layout.BindModel(m, dsl.ID, dsl.Fields, dsl.Action.Bind.Option)
return nil return nil
} }

View file

@ -1,26 +1,67 @@
package form package form
import ( import (
jsoniter "github.com/json-iterator/go" "fmt"
"strings"
"github.com/yaoapp/gou" "github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/field"
) )
// BindModel bind model // BindModel bind model
func (fields *FieldsDSL) BindModel(m *gou.Model) { func (fields *FieldsDSL) BindModel(m *gou.Model) error {
fields.formMap = map[string]field.ColumnDSL{}
trans, err := field.ModelTransform()
if err != nil {
return err
}
for _, col := range m.Columns {
data := col.Map()
formField, err := trans.Form(col.Type, data)
if err != nil {
return err
}
// append columns
if _, has := fields.formMap[formField.Key]; !has {
fields.Form[formField.Key] = *formField
fields.formMap[col.Name] = fields.Form[formField.Key]
}
}
return nil
} }
// Xgen trans to xgen setting // Xgen trans to xgen setting
func (fields *FieldsDSL) Xgen() (map[string]interface{}, error) { func (fields *FieldsDSL) Xgen(layout *LayoutDSL) (map[string]interface{}, error) {
res := map[string]interface{}{} res := map[string]interface{}{}
data, err := jsoniter.Marshal(fields) forms := map[string]interface{}{}
if err != nil { messages := []string{}
return nil, err if layout.Form != nil && layout.Form.Sections != nil {
layout.listColumns(func(path string, f Column) {
name := f.Name
field, has := fields.Form[name]
if !has {
if strings.HasPrefix(f.Name, "::") {
name = fmt.Sprintf("$L(%s)", strings.TrimPrefix(f.Name, "::"))
if field, has = fields.Form[name]; has {
forms[name] = field.Map()
return
}
}
messages = append(messages, fmt.Sprintf("fields.form.%s not found, checking %s", f.Name, path))
}
forms[name] = field.Map()
}, "", nil)
} }
err = jsoniter.Unmarshal(data, &res) if len(messages) > 0 {
if err != nil { return nil, fmt.Errorf(strings.Join(messages, ";\n"))
return nil, err
} }
res["form"] = forms
return res, nil return res, nil
} }

View file

@ -197,7 +197,7 @@ func (dsl *DSL) Xgen() (map[string]interface{}, error) {
return nil, err return nil, err
} }
fields, err := dsl.Fields.Xgen() fields, err := dsl.Fields.Xgen(dsl.Layout)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,15 +1,100 @@
package form package form
import ( import (
"fmt"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou" "github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/component"
) )
// BindModel bind model // BindModel bind model
func (layout *LayoutDSL) BindModel(m *gou.Model) { func (layout *LayoutDSL) BindModel(m *gou.Model, formID string, fields *FieldsDSL, option map[string]interface{}) {
if layout.Primary == "" {
layout.Primary = m.PrimaryKey layout.Primary = m.PrimaryKey
} }
if layout.Operation == nil {
layout.Operation = &OperationLayoutDSL{
Preset: map[string]map[string]interface{}{"save": {}, "back": {}},
Actions: []component.ActionDSL{
{
Title: "::Delete",
Icon: "icon-trash-2",
Style: "danger",
Action: map[string]component.ParamsDSL{
"Table.delete": {"model": formID},
},
Confirm: &component.ConfirmActionDSL{
Title: "::Confirm",
Desc: "::Please confirm, the data cannot be recovered",
},
},
},
}
}
if layout.Form == nil && len(fields.Form) > 0 {
layout.Form = &ViewLayoutDSL{
Props: component.PropsDSL{},
Sections: []SectionDSL{{Columns: []Column{}}},
}
columns := []Column{}
for _, namev := range m.ColumnNames {
name, ok := namev.(string)
if ok && name != "deleted_at" {
if col, has := fields.formMap[name]; has {
width := 12
// if c, has := m.Columns[name]; has {
// typ := strings.ToLower(c.Type)
// if typ == "id" || strings.Contains(typ, "integer") || strings.Contains(typ, "float") {
// width = 6
// }
// }
columns = append(columns, Column{InstanceDSL: component.InstanceDSL{Name: col.Key, Width: width}})
}
}
}
layout.Form.Sections = []SectionDSL{{Columns: columns}}
}
}
func (layout *LayoutDSL) listColumns(fn func(string, Column), path string, sections []SectionDSL) {
if layout.Form == nil || layout.Form.Sections == nil {
return
}
if sections == nil {
sections = layout.Form.Sections
path = "layout.sections"
}
for i := range sections {
if sections[i].Columns != nil {
for j := range sections[i].Columns {
if sections[i].Columns[j].Tabs != nil {
for k := range sections[i].Columns[j].Tabs {
layout.listColumns(
fn,
fmt.Sprintf("%s[%d].Columns[%d].tabs[%d]", path, i, j, k),
[]SectionDSL{sections[i].Columns[j].Tabs[k]},
)
}
continue
}
if path == "layout.sections" {
fn(fmt.Sprintf("%s[%d].Columns[%d]", path, i, j), sections[i].Columns[j])
} else {
fn(fmt.Sprintf("%s.Columns[%d]", path, j), sections[i].Columns[j])
}
}
}
}
}
// Xgen trans to Xgen setting // Xgen trans to Xgen setting
func (layout *LayoutDSL) Xgen() (map[string]interface{}, error) { func (layout *LayoutDSL) Xgen() (map[string]interface{}, error) {
res := map[string]interface{}{} res := map[string]interface{}{}

View file

@ -68,6 +68,7 @@ type OperationLayoutDSL struct {
// FieldsDSL the form fields DSL // FieldsDSL the form fields DSL
type FieldsDSL struct { type FieldsDSL struct {
Form field.Columns `json:"form,omitempty"` Form field.Columns `json:"form,omitempty"`
formMap map[string]field.ColumnDSL
} }
// ViewLayoutDSL layout.form // ViewLayoutDSL layout.form
@ -80,11 +81,11 @@ type ViewLayoutDSL struct {
type SectionDSL struct { type SectionDSL struct {
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"` Desc string `json:"desc,omitempty"`
Columns []Columns `json:"columns,omitempty"` Columns []Column `json:"columns,omitempty"`
} }
// Columns table columns // Column table columns
type Columns struct { type Column struct {
Tabs []SectionDSL `json:"tabs,omitempty"` Tabs []SectionDSL `json:"tabs,omitempty"`
component.InstanceDSL component.InstanceDSL
} }