Skip to content

Commit

Permalink
Merge pull request #171 from trheyi/main
Browse files Browse the repository at this point in the history
[add] widget form
  • Loading branch information
trheyi authored Sep 27, 2022
2 parents 65bd03e + 1d289fe commit 622842d
Show file tree
Hide file tree
Showing 28 changed files with 1,711 additions and 327 deletions.
1 change: 1 addition & 0 deletions lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func init() {
lang.RegisterWidget("tables", "table")
lang.RegisterWidget("forms", "form")
lang.RegisterWidget("charts", "chart")
lang.RegisterWidget("kanban", "page")
lang.RegisterWidget("screen", "page")
Expand Down
148 changes: 148 additions & 0 deletions widgets/action/action.go
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
}
22 changes: 22 additions & 0 deletions widgets/action/types.go
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)
9 changes: 9 additions & 0 deletions widgets/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ import (
// Setting the application setting
var Setting *DSL

// LoadAndExport load app
func LoadAndExport(cfg config.Config) error {
err := Load(cfg)
if err != nil {
return err
}
return Export()
}

// Load the app DSL
func Load(cfg config.Config) error {

Expand Down
6 changes: 6 additions & 0 deletions widgets/component/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ type DSL struct {
Props PropsDSL `json:"props,omitempty"`
}

// Actions the actions
type Actions []ActionDSL

// Instances the Instances
type Instances []InstanceDSL

// InstanceDSL the component instance DSL
type InstanceDSL struct {
Name string `json:"name,omitempty"`
Expand Down
73 changes: 73 additions & 0 deletions widgets/field/field.go
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
}
}
30 changes: 30 additions & 0 deletions widgets/field/types.go
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"`
}
Loading

0 comments on commit 622842d

Please sign in to comment.