-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
145 lines (122 loc) · 3.05 KB
/
views.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 model
import (
"github.com/sohaha/zlsgo/zarray"
"github.com/sohaha/zlsgo/ztype"
)
// type View struct {
// Disabled bool `json:"disabled"`
// Title string `json:"title"`
// Fields []string `json:"Fields"`
// Layouts ztype.Var `json:"layouts"`
// Filters []interface{} `json:"filters"`
// }
func (m *Model) GetViewFields(view string) []string {
v := m.model.Extend.Get("views").Get(view).Map()
if v.Get("disabled").Bool() {
return []string{}
}
fields := v.Get("fields").Slice().String()
if len(fields) == 0 {
switch view {
case "info":
fields = m.fullFields
default:
fields = m.GetFields()
}
}
return zarray.Unique(append(fields, IDKey))
}
func parseViewLists(m *Model) ztype.Map {
columns := make(map[string]ztype.Map, 0)
data := m.model.Extend.Get("views").Get("lists").Map()
if data.Get("disabled").Bool() {
return ztype.Map{}
}
fields := append([]string{IDKey}, data.Get("fields").Slice().String()...)
if len(fields) == 1 {
fields = append(fields, m.GetFields()...)
}
fields = zarray.Unique(fields)
for i := range fields {
name := fields[i]
column, ok := m.getField(name)
if !ok {
continue
}
layout := data.Get("layouts").Get(name).Map()
columns[name] = ztype.Map{
"title": column.Label,
"type": column.Type,
"ModelOptions": column.Options,
"layout": layout,
}
if m.model.Options.CryptID && name == IDKey {
columns[name]["type"] = "string"
}
}
title := data.Get("title").String()
if title == "" {
title = m.Name()
}
info := ztype.Map{
"title": title,
"columns": columns,
"fields": fields,
}
return info
}
func parseViewInfo(m *Model) ztype.Map {
info := ztype.Map{}
data := m.model.Extend.Get("views").Get("info").Map()
if data.Get("disabled").Bool() {
return ztype.Map{}
}
columns := make(map[string]ztype.Map, 0)
fields := append([]string{IDKey}, data.Get("fields").Slice().String()...)
if len(fields) == 1 {
fields = append(fields, m.fullFields...)
}
fields = zarray.Unique(fields)
layouts := data.Get("layouts").Map()
for i := range fields {
name := fields[i]
column, ok := m.getField(name)
if !ok {
continue
}
layout := layouts.Get(name).Map()
columns[name] = ztype.Map{
"label": column.Label,
"type": column.Type,
"readonly": column.Options.ReadOnly,
"size": column.Size,
"layout": layout,
"disabled": m.isInlayField(name),
"ModelOptions": column.Options,
}
if m.model.Options.CryptID && name == IDKey {
columns[name]["type"] = "string"
}
}
if m.model.Options.SoftDeletes {
delete(columns, DeletedAtKey)
fields = zarray.Filter(fields, func(_ int, v string) bool {
return v != DeletedAtKey
})
}
info["columns"] = columns
info["fields"] = fields
if !layouts.IsEmpty() {
info["layouts"] = layouts
}
return info
}
func parseViews(m *Model) ztype.Map {
views := ztype.Map{}
views["lists"] = parseViewLists(m)
views["info"] = parseViewInfo(m)
return views
}
func (m *Model) GetViews() ztype.Map {
return m.views
}