Skip to content

Commit

Permalink
Merge pull request YaoApp#228 from trheyi/main
Browse files Browse the repository at this point in the history
[add] form widget bind.form & bind.table
  • Loading branch information
trheyi authored Oct 24, 2022
2 parents fb0ff4f + 0977059 commit 9547592
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 50 deletions.
2 changes: 1 addition & 1 deletion widgets/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func LoadAndExport(cfg config.Config) error {
func SelectTransform(name string) (*Transform, error) {
trans, has := Transforms[name]
if !has {
return nil, fmt.Errorf("%s does not found", name)
return nil, fmt.Errorf("SelectTransform %s does not found", name)
}
return trans, nil
}
Expand Down
52 changes: 52 additions & 0 deletions widgets/form/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/action"
"github.com/yaoapp/yao/widgets/hook"
"github.com/yaoapp/yao/widgets/table"
)

var processActionDefaults = map[string]*action.Process{
Expand Down Expand Up @@ -100,3 +102,53 @@ func (act *ActionDSL) BindModel(m *gou.Model) {
act.Find.Default[1] = act.Bind.Option
}
}

// BindForm bind form
func (act *ActionDSL) BindForm(form *DSL) error {

// Copy Hooks
hook.CopyBefore(act.BeforeFind, form.Action.BeforeFind)
hook.CopyBefore(act.BeforeSave, form.Action.BeforeSave)
hook.CopyBefore(act.BeforeCreate, form.Action.BeforeCreate)
hook.CopyBefore(act.BeforeUpdate, form.Action.BeforeUpdate)
hook.CopyBefore(act.BeforeDelete, form.Action.BeforeDelete)
hook.CopyAfter(act.AfterFind, form.Action.AfterFind)
hook.CopyAfter(act.AfterSave, form.Action.AfterSave)
hook.CopyAfter(act.AfterCreate, form.Action.AfterCreate)
hook.CopyAfter(act.AfterUpdate, form.Action.AfterUpdate)
hook.CopyAfter(act.AfterDelete, form.Action.AfterDelete)

// Merge Actions
act.Find.Merge(form.Action.Find)
act.Save.Merge(form.Action.Save)
act.Create.Merge(form.Action.Create)
act.Update.Merge(form.Action.Update)
act.Delete.Merge(form.Action.Delete)

return nil
}

// BindTable bind table
func (act *ActionDSL) BindTable(tab *table.DSL) error {

// Copy Hooks
hook.CopyBefore(act.BeforeFind, tab.Action.BeforeFind)
hook.CopyBefore(act.BeforeSave, tab.Action.BeforeSave)
hook.CopyBefore(act.BeforeCreate, tab.Action.BeforeCreate)
hook.CopyBefore(act.BeforeUpdate, tab.Action.BeforeUpdate)
hook.CopyBefore(act.BeforeDelete, tab.Action.BeforeDelete)
hook.CopyAfter(act.AfterFind, tab.Action.AfterFind)
hook.CopyAfter(act.AfterSave, tab.Action.AfterSave)
hook.CopyAfter(act.AfterCreate, tab.Action.AfterCreate)
hook.CopyAfter(act.AfterUpdate, tab.Action.AfterUpdate)
hook.CopyAfter(act.AfterDelete, tab.Action.AfterDelete)

// Merge Actions
act.Find.Merge(tab.Action.Find)
act.Save.Merge(tab.Action.Save)
act.Create.Merge(tab.Action.Create)
act.Update.Merge(tab.Action.Update)
act.Delete.Merge(tab.Action.Delete)

return nil
}
77 changes: 76 additions & 1 deletion widgets/form/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/table"
)

// Bind model / store / table / ...
Expand All @@ -17,6 +18,10 @@ func (dsl *DSL) Bind() error {
return dsl.bindModel()
}

if dsl.Action.Bind.Form != "" {
return dsl.bindForm()
}

if dsl.Action.Bind.Store != "" {
return dsl.bindStore()
}
Expand All @@ -42,9 +47,79 @@ func (dsl *DSL) bindModel() error {
return nil
}

func (dsl *DSL) bindForm() error {
id := dsl.Action.Bind.Form
if id == dsl.ID {
return fmt.Errorf("bind.form %s can't bind self form", id)
}

// Load form
if _, has := Forms[id]; !has {
if err := LoadID(id, dsl.Root); err != nil {
return err
}
}

form, err := Get(id)
if err != nil {
return err
}

// Bind Fields
err = dsl.Fields.BindForm(form)
if err != nil {
return err
}

// Bind Actions
err = dsl.Action.BindForm(form)
if err != nil {
return err
}

// Bind Layout
err = dsl.Layout.BindForm(form, dsl.Fields)
if err != nil {
return err
}

return nil
}

func (dsl *DSL) bindTable() error {
id := dsl.Action.Bind.Table
return fmt.Errorf("bind.table %s does not support yet", id)

// Load table
if _, has := table.Tables[id]; !has {
if err := table.LoadID(id, dsl.Root); err != nil {
return err
}
}

tab, err := table.Get(id)
if err != nil {
return err
}

// Bind Fields
err = dsl.Fields.BindTable(tab)
if err != nil {
return err
}

// Bind Actions
err = dsl.Action.BindTable(tab)
if err != nil {
return err
}

// Bind Layout
err = dsl.Layout.BindTable(tab, dsl.ID, dsl.Fields)
if err != nil {
return err
}

return nil
}

func (dsl *DSL) bindStore() error {
Expand Down
53 changes: 52 additions & 1 deletion widgets/form/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/yaoapp/gou"
"github.com/yaoapp/yao/widgets/field"
"github.com/yaoapp/yao/widgets/table"
)

// BindModel bind model
Expand All @@ -26,7 +27,7 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
}

// append columns
if _, has := fields.formMap[formField.Key]; !has {
if _, has := fields.Form[formField.Key]; !has {
fields.Form[formField.Key] = *formField
fields.formMap[col.Name] = fields.Form[formField.Key]
}
Expand All @@ -35,6 +36,56 @@ func (fields *FieldsDSL) BindModel(m *gou.Model) error {
return nil
}

// BindForm bind form
func (fields *FieldsDSL) BindForm(form *DSL) error {
// Bind Form
if fields.Form == nil || len(fields.Form) == 0 {
fields.Form = form.Fields.Form
} else if form.Fields.Form != nil {
for key, form := range form.Fields.Form {
if _, has := fields.Form[key]; !has {
fields.Form[key] = form
}
}
}
return nil
}

// BindTable bind table
func (fields *FieldsDSL) BindTable(tab *table.DSL) error {

// Bind tab
if fields.Form == nil || len(fields.Form) == 0 {
fields.Form = field.Columns{}
fields.formMap = map[string]field.ColumnDSL{}
}

if tab.Fields.Table != nil {
for key, form := range tab.Fields.Table {
if form.Edit == nil {
continue
}

if _, has := fields.Form[key]; !has {
edit := *form.Edit
fields.Form[key] = field.ColumnDSL{Key: key, Bind: form.Bind, Edit: &edit}
}
}

mapping := tab.Fields.TableMap()
for name, form := range mapping {
if _, has := fields.formMap[name]; !has {
if form.Edit == nil {
continue
}
fields.formMap[name] = fields.Form[name]
}
}

}
return nil
}

// Xgen trans to xgen setting
func (fields *FieldsDSL) Xgen(layout *LayoutDSL) (map[string]interface{}, error) {
res := map[string]interface{}{}
Expand Down
103 changes: 66 additions & 37 deletions widgets/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/widgets/component"
"github.com/yaoapp/yao/widgets/environment"
"github.com/yaoapp/yao/widgets/field"
)

Expand Down Expand Up @@ -65,7 +67,7 @@ func New(id string) *DSL {
func LoadAndExport(cfg config.Config) error {
err := Load(cfg)
if err != nil {
return err
log.Error(err.Error())
}
return Export()
}
Expand All @@ -86,56 +88,83 @@ func LoadFrom(dir string, prefix string) error {
messages := []string{}
err := share.Walk(dir, ".json", func(root, filename string) {
id := prefix + share.ID(root, filename)
data := share.ReadFile(filename)
dsl := New(id)
err := jsoniter.Unmarshal(data, dsl)
data, err := environment.ReadFile(filename)
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
messages = append(messages, err.Error())
return
}

if dsl.Action == nil {
dsl.Action = &ActionDSL{}
err = LoadData(data, id, filepath.Dir(dir))
if err != nil {
messages = append(messages, err.Error())
}
dsl.Action.SetDefaultProcess()
})

if dsl.Layout == nil {
dsl.Layout = &LayoutDSL{}
}
if len(messages) > 0 {
return fmt.Errorf(strings.Join(messages, ";\n"))
}

if dsl.Fields == nil {
dsl.Fields = &FieldsDSL{}
}
return err
}

// Bind model / store / table / ...
err = dsl.Bind()
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
return
}
// LoadID load via id
func LoadID(id string, root string) error {
dirs := strings.Split(id, ".")
name := fmt.Sprintf("%s.form.json", dirs[len(dirs)-1])
elems := []string{root}
elems = append(elems, dirs[0:len(dirs)-1]...)
elems = append(elems, "forms", name)
filename := filepath.Join(elems...)
data, err := environment.ReadFile(filename)
if err != nil {
return fmt.Errorf("[Form] LoadID %s root=%s %s", id, root, err.Error())
}
return LoadData(data, id, root)
}

// Parse
err = dsl.Parse()
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
return
}
// LoadData load via data
func LoadData(data []byte, id string, root string) error {
dsl := New(id)
dsl.Root = root

// Validate
err = dsl.Validate()
if err != nil {
messages = append(messages, fmt.Sprintf("[%s] %s", id, err.Error()))
return
}
err := jsoniter.Unmarshal(data, dsl)
if err != nil {
return fmt.Errorf("[Form] LoadData %s %s", id, err.Error())
}

Forms[id] = dsl
})
if dsl.Action == nil {
dsl.Action = &ActionDSL{}
}
dsl.Action.SetDefaultProcess()

if len(messages) > 0 {
return fmt.Errorf(strings.Join(messages, ";"))
if dsl.Layout == nil {
dsl.Layout = &LayoutDSL{}
}

return err
if dsl.Fields == nil {
dsl.Fields = &FieldsDSL{}
}

// Bind model / store / table / ...
err = dsl.Bind()
if err != nil {
return fmt.Errorf("[Form] LoadData Bind %s %s", id, err.Error())
}

// Parse
err = dsl.Parse()
if err != nil {
return fmt.Errorf("[Form] LoadData Parse %s %s", id, err.Error())
}

// Validate
err = dsl.Validate()
if err != nil {
return fmt.Errorf("[Form] LoadData Validate %s %s", id, err.Error())
}

Forms[id] = dsl
return nil
}

// Get form via process or id
Expand Down
Loading

0 comments on commit 9547592

Please sign in to comment.