forked from bakape/shamichan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
248 lines (217 loc) · 4.61 KB
/
input.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
// Form input field rendering
package templates
import (
"html"
"strconv"
"strings"
"meguca/lang"
"github.com/valyala/quicktemplate"
)
// Input field types
type inputType uint8
const (
_bool inputType = iota
_number
_string
_select
_textarea
_password
_map
_image
_shortcut
)
// Spec of an option passed into the rendering function
type inputSpec struct {
Type inputType
Required, Placeholder, NoID bool
Min, Max, MaxLength, Rows int
ID, Pattern, Autocomplete string
Options []string
Val interface{}
}
// For constructing various HTML input forms
type formWriter struct {
quicktemplate.Writer
lang lang.Pack
}
// Write an element attribute to the buffer
func (w *formWriter) attr(key, val string) {
w.N().S(` `)
w.N().S(key)
if val != "" {
w.N().S(`="`)
w.N().S(val)
w.N().S(`"`)
}
}
func (w *formWriter) typ(val string) {
w.attr("type", val)
}
// Write an input element from the spec to the buffer
func (w *formWriter) input(spec inputSpec) {
cont := false
switch spec.Type {
case _select:
w.sel(spec)
case _textarea:
w.textArea(spec)
case _map:
streamrenderMap(&w.Writer, spec, w.lang)
case _shortcut:
w.N().S("Alt+")
cont = true
default:
cont = true
}
if !cont {
return
}
w.tag("input", spec)
switch spec.Type {
case _bool:
w.typ("checkbox")
if spec.Val != nil && spec.Val.(bool) {
w.attr("checked", "")
}
case _number:
w.typ("number")
if spec.Val != nil {
cast := uint64(spec.Val.(uint))
w.attr("value", strconv.FormatUint(cast, 10))
}
w.attr("min", strconv.Itoa(spec.Min))
if spec.Max != 0 {
w.attr("max", strconv.Itoa(spec.Max))
}
case _password, _string:
if spec.Type == _string {
w.typ("text")
} else {
w.typ("password")
}
if spec.Val != nil {
w.attr("value", html.EscapeString(spec.Val.(string)))
}
if spec.Pattern != "" {
w.attr("pattern", spec.Pattern)
}
if spec.MaxLength != 0 {
w.attr("maxlength", strconv.Itoa(spec.MaxLength))
}
if spec.Autocomplete != "" {
w.attr("autocomplete", spec.Autocomplete)
}
case _image:
w.typ("file")
w.attr("accept", "image/png,image/gif,image/jpeg")
case _shortcut:
w.attr("maxlength", "1")
w.attr("class", "shortcut")
}
w.N().S(`>`)
}
// Write the element tag and the common parts of all input element types to
// buffer
func (w *formWriter) tag(tag string, spec inputSpec) {
w.N().S(`<`)
w.N().S(tag)
w.attr("name", spec.ID)
if !spec.NoID { // To not conflict with non-unique labels
w.attr("id", spec.ID)
}
w.attr("title", w.lang.Forms[spec.ID][1])
if spec.Placeholder {
w.attr("placeholder", w.lang.Forms[spec.ID][0])
}
if spec.Required {
w.attr("required", "")
}
}
// Write a select element to buffer
func (w *formWriter) sel(spec inputSpec) {
w.tag("select", spec)
w.N().S(`>`)
var val string
if spec.Val != nil {
val = spec.Val.(string)
}
for _, o := range spec.Options {
w.N().S("<option")
w.attr("value", o)
if o == val {
w.attr("selected", "selected")
}
w.N().S(`>`)
label, ok := w.lang.Options[spec.ID]
if !ok {
label = o
}
w.N().S(label)
w.N().S("</option>")
}
w.N().S("</select>")
}
// Render a text area input element
func (w *formWriter) textArea(spec inputSpec) {
w.tag("textarea", spec)
if spec.MaxLength != 0 {
w.attr("maxlength", strconv.Itoa(spec.MaxLength))
}
w.attr("rows", strconv.Itoa(spec.Rows))
w.N().S(`>`)
switch spec.Val.(type) {
case string:
w.E().S(spec.Val.(string))
case []string:
w.E().S(strings.Join(spec.Val.([]string), "\n"))
}
w.N().S("</textarea>")
}
// Write an input element label from the spec to the buffer
func (w *formWriter) label(spec inputSpec) {
ln := w.lang.Forms[spec.ID]
w.N().S("<label")
if !spec.NoID {
w.attr("for", spec.ID)
}
w.attr("title", ln[1])
w.N().S(`>`)
w.N().S(ln[0])
w.N().S("</label>")
}
// Render a table containing {label input_element} pairs
func streamtable(qw *quicktemplate.Writer, specs []inputSpec, lang lang.Pack) {
w := formWriter{
Writer: *qw,
lang: lang,
}
w.N().S("<table>")
for _, spec := range specs {
w.N().S("<tr><td>")
w.label(spec)
w.N().S("</td><td>")
w.input(spec)
w.N().S("</td></tr>")
}
w.N().S("</table>")
}
// Render a single input element
func streaminput(qw *quicktemplate.Writer, spec inputSpec, lang lang.Pack) {
w := formWriter{
Writer: *qw,
lang: lang,
}
w.input(spec)
}
// Render the options inputs of an options panel
func streamoptions(qw *quicktemplate.Writer, specs []inputSpec, ln lang.Pack) {
w := formWriter{
Writer: *qw,
lang: ln,
}
for _, s := range specs {
w.input(s)
w.label(s)
w.N().S(`<br>`)
}
}