[add] widget.fields & widget.filters
This commit is contained in:
parent
72b9a8b62d
commit
bab61e8cbd
6 changed files with 188 additions and 1 deletions
|
|
@ -55,7 +55,7 @@ func widgetActions(actions map[string]interface{}, widget WidgetAction, widgetID
|
||||||
items := widget.Actions()
|
items := widget.Actions()
|
||||||
if len(items) > 0 {
|
if len(items) > 0 {
|
||||||
actions[dsl] = map[string]interface{}{
|
actions[dsl] = map[string]interface{}{
|
||||||
"items": widget.Actions(),
|
"items": items,
|
||||||
"DSL": dsl,
|
"DSL": dsl,
|
||||||
"ID": widgetID,
|
"ID": widgetID,
|
||||||
"name": name,
|
"name": name,
|
||||||
|
|
|
||||||
145
widgets/field.go
Normal file
145
widgets/field.go
Normal file
|
|
@ -0,0 +1,145 @@
|
||||||
|
package widgets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yaoapp/yao/widgets/chart"
|
||||||
|
"github.com/yaoapp/yao/widgets/field"
|
||||||
|
"github.com/yaoapp/yao/widgets/form"
|
||||||
|
"github.com/yaoapp/yao/widgets/list"
|
||||||
|
"github.com/yaoapp/yao/widgets/table"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Fields return loaded widgets fields
|
||||||
|
func Fields() []Item {
|
||||||
|
|
||||||
|
fields := map[string]interface{}{}
|
||||||
|
tableFields(fields)
|
||||||
|
formFields(fields)
|
||||||
|
listFields(fields)
|
||||||
|
chartFields(fields)
|
||||||
|
|
||||||
|
grouping := Grouping(fields)
|
||||||
|
items := Array(grouping, []Item{})
|
||||||
|
Sort(items, []string{"tables", "forms", "lists", "charts"})
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filters return loaded widgets filters
|
||||||
|
func Filters() []Item {
|
||||||
|
filters := map[string]interface{}{}
|
||||||
|
tableFilters(filters)
|
||||||
|
chartFilters(filters)
|
||||||
|
|
||||||
|
grouping := Grouping(filters)
|
||||||
|
items := Array(grouping, []Item{})
|
||||||
|
Sort(items, []string{"tables", "forms", "lists", "charts"})
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableFields(fields map[string]interface{}) {
|
||||||
|
for id, widget := range table.Tables {
|
||||||
|
dsl := fmt.Sprintf("tables%s%s.tab.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFields(fields, widget.Fields.Table, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formFields(fields map[string]interface{}) {
|
||||||
|
for id, widget := range form.Forms {
|
||||||
|
dsl := fmt.Sprintf("forms%s%s.form.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFields(fields, widget.Fields.Form, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func chartFields(fields map[string]interface{}) {
|
||||||
|
for id, widget := range chart.Charts {
|
||||||
|
dsl := fmt.Sprintf("charts%s%s.chart.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFields(fields, widget.Fields.Chart, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func listFields(fields map[string]interface{}) {
|
||||||
|
for id, widget := range list.Lists {
|
||||||
|
dsl := fmt.Sprintf("lists%s%s.list.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFields(fields, widget.Fields.List, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableFilters(filters map[string]interface{}) {
|
||||||
|
for id, widget := range table.Tables {
|
||||||
|
dsl := fmt.Sprintf("tables%s%s.tab.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFilters(filters, widget.Fields.Filter, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func chartFilters(filters map[string]interface{}) {
|
||||||
|
for id, widget := range chart.Charts {
|
||||||
|
dsl := fmt.Sprintf("charts%s%s.chart.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
|
||||||
|
widgetFilters(filters, widget.Fields.Filter, id, dsl, widget.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func widgetFields(items map[string]interface{}, fields map[string]field.ColumnDSL, widgetID string, dsl string, name string) map[string]interface{} {
|
||||||
|
|
||||||
|
fieldlist := []map[string]string{}
|
||||||
|
if fields != nil {
|
||||||
|
names := []string{}
|
||||||
|
mapping := map[string]string{}
|
||||||
|
for name, field := range fields {
|
||||||
|
if field.ID != "" {
|
||||||
|
mapping[name] = field.ID
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
for _, name := range names {
|
||||||
|
fieldlist = append(fieldlist, map[string]string{
|
||||||
|
"name": name,
|
||||||
|
"id": mapping[name],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
items[dsl] = map[string]interface{}{
|
||||||
|
"items": fieldlist,
|
||||||
|
"DSL": dsl,
|
||||||
|
"ID": widgetID,
|
||||||
|
"name": name,
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func widgetFilters(items map[string]interface{}, fields map[string]field.FilterDSL, widgetID string, dsl string, name string) map[string]interface{} {
|
||||||
|
|
||||||
|
fieldlist := []map[string]string{}
|
||||||
|
if fields != nil {
|
||||||
|
names := []string{}
|
||||||
|
mapping := map[string]string{}
|
||||||
|
for name, field := range fields {
|
||||||
|
if field.ID != "" {
|
||||||
|
mapping[name] = field.ID
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
for _, name := range names {
|
||||||
|
fieldlist = append(fieldlist, map[string]string{
|
||||||
|
"name": name,
|
||||||
|
"id": mapping[name],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
items[dsl] = map[string]interface{}{
|
||||||
|
"items": fieldlist,
|
||||||
|
"DSL": dsl,
|
||||||
|
"ID": widgetID,
|
||||||
|
"name": name,
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -63,6 +63,11 @@ func (column ColumnDSL) Replace(data map[string]interface{}) (*ColumnDSL, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new.ID, err = column.Hash()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &new, nil
|
return &new, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,11 @@ func (filter FilterDSL) Replace(data map[string]interface{}) (*FilterDSL, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new.ID, err = filter.Hash()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &new, nil
|
return &new, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ var WidgetHandlers = map[string]gou.ProcessHandler{
|
||||||
"apis": processApis,
|
"apis": processApis,
|
||||||
"actions": processActions,
|
"actions": processActions,
|
||||||
"models": processModels,
|
"models": processModels,
|
||||||
|
"fields": processFields,
|
||||||
|
"filters": processFilters,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -30,6 +32,16 @@ func processModels(process *gou.Process) interface{} {
|
||||||
return Models()
|
return Models()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the loaded Fields
|
||||||
|
func processFields(process *gou.Process) interface{} {
|
||||||
|
return Fields()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the loaded Filters
|
||||||
|
func processFilters(process *gou.Process) interface{} {
|
||||||
|
return Filters()
|
||||||
|
}
|
||||||
|
|
||||||
// Get the loaded flows
|
// Get the loaded flows
|
||||||
func processFlows() {}
|
func processFlows() {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,26 @@ func TestProcessModels(t *testing.T) {
|
||||||
assert.Greater(t, len(res.([]Item)), 0)
|
assert.Greater(t, len(res.([]Item)), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProcessFields(t *testing.T) {
|
||||||
|
testData(t)
|
||||||
|
args := []interface{}{}
|
||||||
|
res, err := gou.NewProcess("widget.fields", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Greater(t, len(res.([]Item)), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessFilters(t *testing.T) {
|
||||||
|
testData(t)
|
||||||
|
args := []interface{}{}
|
||||||
|
res, err := gou.NewProcess("widget.filters", args...).Exec()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Greater(t, len(res.([]Item)), 0)
|
||||||
|
}
|
||||||
|
|
||||||
func testData(t *testing.T) {
|
func testData(t *testing.T) {
|
||||||
prepare(t)
|
prepare(t)
|
||||||
err := Load(config.Conf)
|
err := Load(config.Conf)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue