[add] bind model

This commit is contained in:
Max 2022-10-24 12:51:27 +08:00
parent 53c11fb256
commit 086bf244e5
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.Fields.BindModel(m)
// dsl.Layout.BindModel(m)
dsl.Layout.BindModel(m, dsl.ID, dsl.Fields, dsl.Action.Bind.Option)
return nil
}

View file

@ -1,26 +1,67 @@
package form
import (
jsoniter "github.com/json-iterator/go"
"fmt"
"strings"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/field"
)
// 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
func (fields *FieldsDSL) Xgen() (map[string]interface{}, error) {
func (fields *FieldsDSL) Xgen(layout *LayoutDSL) (map[string]interface{}, error) {
res := map[string]interface{}{}
data, err := jsoniter.Marshal(fields)
if err != nil {
return nil, err
forms := map[string]interface{}{}
messages := []string{}
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 err != nil {
return nil, err
if len(messages) > 0 {
return nil, fmt.Errorf(strings.Join(messages, ";\n"))
}
res["form"] = forms
return res, nil
}

View file

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

View file

@ -1,13 +1,98 @@
package form
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/component"
)
// BindModel bind model
func (layout *LayoutDSL) BindModel(m *gou.Model) {
layout.Primary = m.PrimaryKey
func (layout *LayoutDSL) BindModel(m *gou.Model, formID string, fields *FieldsDSL, option map[string]interface{}) {
if layout.Primary == "" {
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

View file

@ -67,7 +67,8 @@ type OperationLayoutDSL struct {
// FieldsDSL the form fields DSL
type FieldsDSL struct {
Form field.Columns `json:"form,omitempty"`
Form field.Columns `json:"form,omitempty"`
formMap map[string]field.ColumnDSL
}
// ViewLayoutDSL layout.form
@ -78,13 +79,13 @@ type ViewLayoutDSL struct {
// SectionDSL layout.form.sections[*]
type SectionDSL struct {
Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"`
Columns []Columns `json:"columns,omitempty"`
Title string `json:"title,omitempty"`
Desc string `json:"desc,omitempty"`
Columns []Column `json:"columns,omitempty"`
}
// Columns table columns
type Columns struct {
// Column table columns
type Column struct {
Tabs []SectionDSL `json:"tabs,omitempty"`
component.InstanceDSL
}