-
Notifications
You must be signed in to change notification settings - Fork 664
/
field.go
145 lines (124 loc) · 3.89 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
}