-
Notifications
You must be signed in to change notification settings - Fork 1
/
inputs.go
152 lines (136 loc) · 7.87 KB
/
inputs.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
package leap
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/golangdaddy/leap/models"
)
func getInputs(object *models.Object, field *models.Field) (string, error) {
var err error
var tmp *template.Template
var output string
if field.Element == nil {
for _, input := range object.GetInputs() {
s, err := getInputs(object, input)
if err != nil {
return "", err
}
output += s
}
return output, nil
}
switch strings.ToLower(field.Element.Name) {
case "select":
const s = `<Select id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } reference={ "{{.InputReference}}" } referenceParent={ subject } title="%s {{lowercase .Name}}" options={ {{json .InputOptions}} } placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "string", "date", "week", "month", "time", "phone", "email", "color":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "uint":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "int":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "float":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "text":
const s = `<Textarea id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "checkbox":
const s = `<Checkbox id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange}/><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "object":
const s = `<Object id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "arraystring":
const s = `<Object id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
default:
return "", fmt.Errorf("missing input for %s %s '%s':", object.Name, field.Name, field.Element.Name)
}
if err != nil {
return "", err
}
buf := bytes.NewBuffer(nil)
// Execute the template with the provided data
if err := tmp.Execute(buf, field); err != nil {
return "", err
}
return buf.String(), nil
}
func getEditInputs(object *models.Object, field *models.Field) (string, error) {
var err error
var tmp *template.Template
var output string
if field.Element == nil {
for _, input := range object.GetInputs() {
s, err := getEditInputs(object, input)
if err != nil {
return "", err
}
output += s
}
return output, nil
}
switch strings.ToLower(field.Element.Name) {
case "select":
const s = `<Select id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } reference={ "{{.InputReference}}" } referenceParent={ subject } title="%s {{lowercase .Name}}" options={ {{json .InputOptions}} } placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "string", "date", "week", "month", "time", "phone", "email", "color":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "uint":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "int":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "float":
const s = `<Input id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "text":
const s = `<Textarea id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "checkbox":
const s = `<Checkbox id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" placeholder="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "object":
const s = `<Object id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
case "arraystring":
const s = `<Object id="{{.ID}}" ftype={ {{json .Element}} } required={ {{.Required}} } title="%s {{lowercase .Name}}" inputChange={handleInputChange} value={ inputs["{{.ID}}"]?.value } /><Spacer/>`
output = fmt.Sprintf(s, object.Name)
tmp, err = template.New(object.Name + "_" + field.Name).Funcs(funcMap).Parse(output)
default:
return "", fmt.Errorf("missing input for %s", field.Element.Name)
}
if err != nil {
return "", err
}
buf := bytes.NewBuffer(nil)
// Execute the template with the provided data
if err := tmp.Execute(buf, field); err != nil {
return "", err
}
return buf.String(), nil
}