From 086bf244e5099f0aae3fb20ad051e8d7a7e36f83 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 24 Oct 2022 12:51:27 +0800 Subject: [PATCH] [add] bind model --- widgets/form/bind.go | 2 +- widgets/form/fields.go | 61 ++++++++++++++++++++++++----- widgets/form/form.go | 2 +- widgets/form/layout.go | 89 +++++++++++++++++++++++++++++++++++++++++- widgets/form/types.go | 13 +++--- 5 files changed, 147 insertions(+), 20 deletions(-) diff --git a/widgets/form/bind.go b/widgets/form/bind.go index bdc4f016..befb4f29 100644 --- a/widgets/form/bind.go +++ b/widgets/form/bind.go @@ -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 } diff --git a/widgets/form/fields.go b/widgets/form/fields.go index da223c8b..ed2fcaee 100644 --- a/widgets/form/fields.go +++ b/widgets/form/fields.go @@ -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 } diff --git a/widgets/form/form.go b/widgets/form/form.go index 43a510dd..e8fc0939 100644 --- a/widgets/form/form.go +++ b/widgets/form/form.go @@ -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 } diff --git a/widgets/form/layout.go b/widgets/form/layout.go index 94ad64e3..798478ae 100644 --- a/widgets/form/layout.go +++ b/widgets/form/layout.go @@ -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 diff --git a/widgets/form/types.go b/widgets/form/types.go index 0c52b1d2..66b78f0e 100644 --- a/widgets/form/types.go +++ b/widgets/form/types.go @@ -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 }