forked from fragmenta/view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.go
executable file
·266 lines (205 loc) · 6.43 KB
/
forms.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package helpers
import (
"fmt"
got "html/template"
"reflect"
"strconv"
"strings"
"time"
)
// FORMS
// These should probably use templates from or from lib, so that users can change what form fields get generated
// and use templ rather than fmt.Sprintf
// We need to set this token in the session on the get request for the form
// CSRF generates an input field tag containing a CSRF token
func CSRF() got.HTML {
token := "my_csrf_token" // instead of generating this here, should we instead get router or app to generate and put into the context?
output := fmt.Sprintf("<input type='hidden' name='csrf' value='%s'>", token)
return got.HTML(output)
}
// Field accepts name string, value interface{}, fieldType string, args ...string
func Field(label string, name string, v interface{}, args ...string) got.HTML {
attributes := ""
if len(args) > 0 {
attributes = strings.Join(args, " ")
}
// If no type, add it to attributes
if !strings.Contains(attributes, "type=") {
attributes = attributes + " type=\"text\""
}
tmpl :=
`<div class="field">
<label>%s</label>
<input name="%s" value="%s" %s>
</div>`
if label == "" {
tmpl = `%s<input name="%s" value="%s" %s>`
}
output := fmt.Sprintf(tmpl, Escape(label), Escape(name), Escape(fmt.Sprintf("%v", v)), attributes)
return got.HTML(output)
}
// DateField sets up a date field with a data-date attribute storing the real date
func DateField(label string, name string, t time.Time, args ...string) got.HTML {
// NB we use text type for date fields because of inconsistent browser behaviour
// and to support our own date picker popups
tmpl :=
`<div class="field">
<label>%s</label>
<input name="%s" id="%s" class="date_field" type="text" value="%s" data-date="%s" %s autocomplete="off">
</div>`
attributes := ""
if len(args) > 0 {
attributes = strings.Join(args, " ")
}
output := fmt.Sprintf(tmpl, Escape(label), Escape(name), Escape(name), Date(t), Date(t, "2006-01-02"), attributes)
return got.HTML(output)
}
// TextArea returns a field div containing a textarea
func TextArea(label string, name string, v interface{}, args ...string) got.HTML {
attributes := ""
if len(args) > 0 {
attributes = strings.Join(args, " ")
}
fieldTemplate :=
`<div class="field">
<label>%s</label>
<textarea name="%s" %s>%v</textarea>
</div>`
output := fmt.Sprintf(fieldTemplate,
Escape(label),
Escape(name),
attributes, // NB we do not escape attributes, which may contain HTML
v) // NB value may contain HTML
return got.HTML(output)
}
// TODO flip the select helpers to use Selectable all the time?
// Redefine concrete type Option as a Selectable and this should be doable?
// Selectable provides an interface for options in a select
type Selectable interface {
SelectName() string
SelectValue() string
}
// SelectableOption provides a concrete implementation of Selectable - this should be called string option or similar
type SelectableOption struct {
Name string
Value string
}
// SelectName returns the public name for this select option
func (o SelectableOption) SelectName() string {
return o.Name
}
// SelectValue returns the value for this select option
func (o SelectableOption) SelectValue() string {
return o.Value
}
// StringOptions creates an array of selectables from strings
func StringOptions(args ...string) []SelectableOption {
var options []SelectableOption
// Construct a slice of options from these strings
for _, s := range args {
options = append(options, SelectableOption{s, s})
}
return options
}
// NumberOptions creates an array of selectables, with an optional min and max value supplied as arguments
func NumberOptions(args ...int64) []SelectableOption {
min := int64(0)
max := int64(50)
if len(args) > 0 {
min = args[0]
}
if len(args) > 1 {
max = args[1]
}
var options []SelectableOption
for i := min; i <= max; i++ {
v := strconv.Itoa(int(i))
n := v
options = append(options, SelectableOption{n, v})
}
return options
}
// Better to use an interface and not reflect here - Would rather avoid use of reflect...
// OptionsForSelect creates a select field given an array of keys and values in order
func OptionsForSelect(value interface{}, options interface{}) got.HTML {
stringValue := fmt.Sprintf("%v", value)
output := ""
switch reflect.TypeOf(options).Kind() {
case reflect.Slice:
s := reflect.ValueOf(options)
for i := 0; i < s.Len(); i++ {
o := s.Index(i).Interface().(Selectable)
sel := ""
if o.SelectValue() == stringValue {
sel = "selected"
}
output += fmt.Sprintf(`<option value="%s" %s>%s</option>
`, o.SelectValue(), sel, Escape(o.SelectName()))
}
}
return got.HTML(output)
}
// SelectArray creates a select field given an array of keys and values in order
func SelectArray(label string, name string, value interface{}, options interface{}) got.HTML {
stringValue := fmt.Sprintf("%v", value)
tmpl :=
`<div class="field">
<label>%s</label>
<select type="select" name="%s" id="%s">
%s
</select>
</div>`
if label == "" {
tmpl = `%s<select type="select" name="%s" id="%s">
%s
</select>`
}
opts := ""
switch reflect.TypeOf(options).Kind() {
case reflect.Slice:
s := reflect.ValueOf(options)
for i := 0; i < s.Len(); i++ {
o := s.Index(i).Interface().(Selectable)
sel := ""
if o.SelectValue() == stringValue {
sel = "selected"
}
opts += fmt.Sprintf(`<option value="%s" %s>%s</option>
`, o.SelectValue(), sel, Escape(o.SelectName()))
}
}
output := fmt.Sprintf(tmpl, Escape(label), Escape(name), Escape(name), opts)
return got.HTML(output)
}
// FIXME - make Option conform to Selectable interface and use that instead of concrete type below
// Option type contains number and string
type Option struct {
Id int64
Name string
}
// Select creates a select field given an array of keys and values in order
func Select(label string, name string, value int64, options []Option) got.HTML {
tmpl :=
`<div class="field">
<label>%s</label>
<select type="select" name="%s">
%s
</select>
</div>`
if label == "" {
tmpl = `%s<select type="select" name="%s">
%s
</select>`
}
opts := ""
for _, o := range options {
s := ""
if o.Id == value {
s = "selected"
}
opts += fmt.Sprintf(`<option value="%d" %s>%s</option>
`, o.Id, s, Escape(o.Name))
}
output := fmt.Sprintf(tmpl, Escape(label), Escape(name), opts)
return got.HTML(output)
}