-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from trheyi/main
[add] widget form
- Loading branch information
Showing
28 changed files
with
1,711 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package action | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/kun/any" | ||
"github.com/yaoapp/kun/exception" | ||
"github.com/yaoapp/yao/widgets/hook" | ||
) | ||
|
||
// Bind the process name | ||
func (p *Process) Bind(name string) { | ||
p.ProcessBind = name | ||
} | ||
|
||
// NewProcess create a new process | ||
func NewProcess(name string, p *Process) *Process { | ||
if p == nil { | ||
p = &Process{} | ||
} | ||
p.Name = name | ||
return p | ||
} | ||
|
||
// SetHandler set the handler | ||
func (p *Process) SetHandler(handler Handler) *Process { | ||
p.Handler = handler | ||
return p | ||
} | ||
|
||
// SetDefault set the default value | ||
func (p *Process) SetDefault(defaults map[string]*Process) *Process { | ||
|
||
if defaultProcess, has := defaults[p.Name]; has { | ||
|
||
p.Name = defaultProcess.Name | ||
|
||
if p.Process == "" { | ||
p.Process = defaultProcess.Process | ||
} | ||
|
||
if p.Guard == "" { | ||
p.Guard = defaultProcess.Guard | ||
} | ||
|
||
if p.Default == nil { | ||
p.Default = defaultProcess.Default | ||
} | ||
|
||
// format defaults | ||
if len(p.Default) != len(defaultProcess.Default) { | ||
defauts := defaultProcess.Default | ||
nums := len(p.Default) | ||
if nums > len(defaultProcess.Default) { | ||
nums = len(defaultProcess.Default) | ||
} | ||
for i := 0; i < nums; i++ { | ||
defauts[i] = p.Default[i] | ||
} | ||
p.Default = defauts | ||
} | ||
} | ||
|
||
return p | ||
} | ||
|
||
// WithBefore bind before hook | ||
func (p *Process) WithBefore(before *hook.Before) *Process { | ||
p.Before = before | ||
return p | ||
} | ||
|
||
// WithAfter bind after hook | ||
func (p *Process) WithAfter(after *hook.After) *Process { | ||
p.After = after | ||
return p | ||
} | ||
|
||
// Args get the process args | ||
func (p *Process) Args(process *gou.Process) []interface{} { | ||
process.ValidateArgNums(1) | ||
args := p.Default | ||
nums := len(process.Args[1:]) | ||
if nums > len(args) { | ||
nums = len(args) | ||
} | ||
|
||
for i := 0; i < nums; i++ { | ||
args[i] = p.deepMergeDefault(process.Args[i+1], args[i]) | ||
} | ||
return args | ||
} | ||
|
||
// Exec exec the process | ||
func (p *Process) Exec(process *gou.Process) (interface{}, error) { | ||
if p.Handler == nil { | ||
return nil, fmt.Errorf("%s handler does not set", p.Name) | ||
} | ||
return p.Handler(p, process) | ||
} | ||
|
||
// MustExec exec the process | ||
func (p *Process) MustExec(process *gou.Process) interface{} { | ||
res, err := p.Exec(process) | ||
if err != nil { | ||
exception.New(err.Error(), 500).Throw() | ||
} | ||
return res | ||
} | ||
|
||
// deepMergeDefault deep merge args | ||
func (p *Process) deepMergeDefault(value interface{}, defaults interface{}) interface{} { | ||
|
||
if value == nil { | ||
return defaults | ||
} | ||
|
||
switch defaults.(type) { | ||
|
||
case map[string]interface{}: | ||
defaultMap := defaults.(map[string]interface{}) | ||
valueMap := any.Of(value).MapStr() | ||
for key, v := range defaultMap { | ||
valueMap[key] = p.deepMergeDefault(valueMap[key], v) | ||
} | ||
return valueMap | ||
|
||
case []interface{}: | ||
defaultArr := defaults.([]interface{}) | ||
valueArr := any.Of(value).CArray() | ||
|
||
// pad | ||
nums := len(defaultArr) - len(valueArr) | ||
for i := 0; i < nums; i++ { | ||
valueArr = append(valueArr, nil) | ||
} | ||
|
||
// set default | ||
for idx, v := range defaultArr { | ||
valueArr[idx] = p.deepMergeDefault(valueArr[idx], v) | ||
} | ||
|
||
return valueArr | ||
} | ||
|
||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package action | ||
|
||
import ( | ||
"github.com/yaoapp/gou" | ||
"github.com/yaoapp/yao/widgets/hook" | ||
) | ||
|
||
// Process action.search ... | ||
type Process struct { | ||
Name string `json:"-"` | ||
Process string `json:"process,omitempty"` | ||
ProcessBind string `json:"bind,omitempty"` | ||
Guard string `json:"guard,omitempty"` | ||
Default []interface{} `json:"default,omitempty"` | ||
Disable bool `json:"disable,omitempty"` | ||
Before *hook.Before `json:"-"` | ||
After *hook.After `json:"-"` | ||
Handler Handler `json:"-"` | ||
} | ||
|
||
// Handler action handler | ||
type Handler func(p *Process, process *gou.Process) (interface{}, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package field | ||
|
||
import ( | ||
"github.com/yaoapp/yao/widgets/component" | ||
) | ||
|
||
// CPropsMerge merge the Filters cloud props | ||
func (filters Filters) CPropsMerge(cloudProps map[string]component.CloudPropsDSL, getXpath func(name string, filter FilterDSL) (xpath string)) error { | ||
|
||
for name, filter := range filters { | ||
if filter.Edit != nil && filter.Edit.Props != nil { | ||
xpath := getXpath(name, filter) | ||
cProps, err := filter.Edit.Props.CloudProps(xpath) | ||
if err != nil { | ||
return err | ||
} | ||
mergeCProps(cloudProps, cProps) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CPropsMerge merge the Columns cloud props | ||
func (columns Columns) CPropsMerge(cloudProps map[string]component.CloudPropsDSL, getXpath func(name string, kind string, column ColumnDSL) (xpath string)) error { | ||
|
||
for name, column := range columns { | ||
|
||
if column.Edit != nil && column.Edit.Props != nil { | ||
xpath := getXpath(name, "edit", column) | ||
cProps, err := column.Edit.Props.CloudProps(xpath) | ||
if err != nil { | ||
return err | ||
} | ||
mergeCProps(cloudProps, cProps) | ||
} | ||
|
||
if column.View != nil && column.View.Props != nil { | ||
xpath := getXpath(name, "view", column) | ||
cProps, err := column.View.Props.CloudProps(xpath) | ||
if err != nil { | ||
return err | ||
} | ||
mergeCProps(cloudProps, cProps) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ComputeFieldsMerge merge the compute fields | ||
func (columns Columns) ComputeFieldsMerge(computeInFields map[string]string, computeOutFields map[string]string) { | ||
for name, column := range columns { | ||
|
||
// Compute In | ||
if column.In != "" { | ||
computeInFields[column.Bind] = column.In | ||
computeInFields[name] = column.In | ||
} | ||
|
||
// Compute Out | ||
if column.Out != "" { | ||
computeOutFields[column.Bind] = column.Out | ||
computeOutFields[name] = column.Out | ||
} | ||
} | ||
} | ||
|
||
func mergeCProps(cloudProps map[string]component.CloudPropsDSL, cProps map[string]component.CloudPropsDSL) { | ||
for k, v := range cProps { | ||
cloudProps[k] = v | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package field | ||
|
||
import "github.com/yaoapp/yao/widgets/component" | ||
|
||
// Filters the filters DSL | ||
type Filters map[string]FilterDSL | ||
|
||
// Columns the columns DSL | ||
type Columns map[string]ColumnDSL | ||
|
||
// ComputeFields the Compute filelds | ||
type ComputeFields map[string]string | ||
|
||
// CloudProps the cloud props | ||
type CloudProps map[string]component.CloudPropsDSL | ||
|
||
// ColumnDSL the field column dsl | ||
type ColumnDSL struct { | ||
Bind string `json:"bind,omitempty"` | ||
In string `json:"in,omitempty"` | ||
Out string `json:"out,omitempty"` | ||
View *component.DSL `json:"view,omitempty"` | ||
Edit *component.DSL `json:"edit,omitempty"` | ||
} | ||
|
||
// FilterDSL the field filter dsl | ||
type FilterDSL struct { | ||
Bind string `json:"bind,omitempty"` | ||
Edit *component.DSL `json:"edit,omitempty"` | ||
} |
Oops, something went wrong.