-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabs-fieldset.go
227 lines (184 loc) · 4.63 KB
/
tabs-fieldset.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package goschemaform
import (
"bytes"
"log"
"text/template"
)
// NewTabFieldset returns an initialized TabFieldset
func NewTabFieldset() *TabFieldset {
return &TabFieldset{
title: "",
elem: []Tab{},
condition: "",
condFlip: false,
}
}
// TabFieldset represents the fieldset that holds multiple tabs
type TabFieldset struct {
title string
elem []Tab
condition string
condFlip bool
}
// Form returns the "form" section of the JSON Schema Form definition
func (f *TabFieldset) Form() string {
// Compile the template for generating the form section
var tmplForm = template.Must(template.New("form").Parse(tmplTabFieldsetForm))
var cCheck = false
if f.condition != "" {
cCheck = true
}
tmplData := struct {
Title string
Elem []element
ConditionCheck bool
Condition string
ConditionFlip bool
}{
Title: f.title,
Elem: []element{},
ConditionCheck: cCheck,
Condition: f.condition,
ConditionFlip: f.condFlip,
}
for i := range f.elem {
e := element{
Form: f.elem[i].Form(),
}
tmplData.Elem = append(tmplData.Elem, e)
}
form := bytes.NewBuffer([]byte{})
err := tmplForm.Execute(form, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return form.String()
}
// Schema returns the "schema" section of the JSON Schema Form definition
func (f *TabFieldset) Schema() string {
// Compile the template for generating the Schema section
var tmplSchema = template.Must(template.New("schema").Parse(tmplTabFieldsetSchema))
tmplData := struct {
Title string
Elem []element
}{
Title: f.title,
Elem: []element{},
}
for i := range f.elem {
e := element{
Schema: f.elem[i].Schema(),
}
tmplData.Elem = append(tmplData.Elem, e)
}
schema := bytes.NewBuffer([]byte{})
err := tmplSchema.Execute(schema, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return schema.String()
}
// Inputs returns all Inputs from the underlying tabs of the fieldset.
func (f *TabFieldset) Inputs() []Input {
var inputs []Input
for _, v := range f.elem {
inputs = append(inputs, v.Inputs()...)
}
return inputs
}
// SetTitle sets the fieldset title
func (f *TabFieldset) SetTitle(title string) {
f.title = title
}
// SetCondition will set whether this item displays on the form based on if the provided
// key has a value or not. You can reverse the behaivor with the defaultHide switch. False
// for this option is the default and will make something only appear if the condition is
// is set, while true flips this and shows the control until the condition is met.
func (f *TabFieldset) SetCondition(text string, defaultHide bool) {
f.condition = text
f.condFlip = defaultHide
}
// AddTab adds a tab to the fieldset
func (f *TabFieldset) AddTab(tab *Tab) {
f.elem = append(f.elem, *tab)
}
// NewTab returns an initialized Tab
func NewTab() *Tab {
return &Tab{
title: "",
elem: []Element{},
}
}
// Tab represents an individual Tab in a Fieldset
type Tab struct {
title string
elem []Element
}
// Form returns the "form" section of the JSON Schema Form definition
func (t *Tab) Form() string {
// Compile the template for generating the form section
var tmplForm = template.Must(template.New("form").Parse(tmplTabForm))
tmplData := struct {
Title string
Elem []element
}{
Title: t.title,
Elem: []element{},
}
for i := range t.elem {
e := element{
Form: t.elem[i].Form(),
}
tmplData.Elem = append(tmplData.Elem, e)
}
form := bytes.NewBuffer([]byte{})
err := tmplForm.Execute(form, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return form.String()
}
// Schema returns the "schema" section of the JSON Schema Form definition
func (t *Tab) Schema() string {
// Compile the template for generating the Schema section
var tmplSchema = template.Must(template.New("schema").Parse(tmplTabSchema))
tmplData := struct {
Title string
Elem []element
}{
Title: t.title,
Elem: []element{},
}
for i := range t.elem {
e := element{
Schema: t.elem[i].Schema(),
}
tmplData.Elem = append(tmplData.Elem, e)
}
schema := bytes.NewBuffer([]byte{})
err := tmplSchema.Execute(schema, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return schema.String()
}
// Inputs returns all Inputs added to this Tab
func (t *Tab) Inputs() []Input {
var inputs []Input
for _, v := range t.elem {
inputs = append(inputs, v.Inputs()...)
}
return inputs
}
// SetTitle sets the Tab title text
func (t *Tab) SetTitle(title string) {
t.title = title
}
// AddElement adds a form Input to the Tab
func (t *Tab) AddElement(el Element) {
t.elem = append(t.elem, el)
}