-
Notifications
You must be signed in to change notification settings - Fork 1
/
elements.go
243 lines (216 loc) · 6.56 KB
/
elements.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
package jaws
import (
"html/template"
"strconv"
"strings"
"time"
)
type ClickFn func(rq *Request) error
type InputTextFn func(rq *Request, val string) error
type InputFloatFn func(rq *Request, val float64) error
type InputBoolFn func(rq *Request, val bool) error
type InputDateFn func(rq *Request, val time.Time) error
const ISO8601 = "2006-01-02"
func (rq *Request) inputHTML(id, typ, val, attrs string) template.HTML {
need := 11 + len(id) + 8 + len(typ) + 9 + len(val) + 1 + 1 + len(attrs) + 1
b := make([]byte, 0, need)
b = append(b, `<input id="`...)
b = append(b, id...)
b = append(b, `" type="`...)
b = append(b, typ...)
if val != "" {
b = append(b, `" value="`...)
b = append(b, val...)
}
b = append(b, '"')
if attrs != "" {
b = append(b, ' ')
b = append(b, attrs...)
}
b = append(b, '>')
return template.HTML(b) // #nosec G203
}
func (rq *Request) innerHTML(id, tag, typ, inner, attrs string) template.HTML {
need := 1 + len(tag)*2 + 5 + len(id) + 8 + len(typ) + 1 + 1 + len(attrs) + 1 + len(inner) + 2 + 1
b := make([]byte, 0, need)
b = append(b, '<')
b = append(b, tag...)
b = append(b, ` id="`...)
b = append(b, id...)
if typ != "" {
b = append(b, `" type="`...)
b = append(b, typ...)
}
b = append(b, '"')
if attrs != "" {
b = append(b, ' ')
b = append(b, attrs...)
}
b = append(b, '>')
b = append(b, inner...)
b = append(b, "</"...)
b = append(b, tag...)
b = append(b, '>')
return template.HTML(b) // #nosec G203
}
func (rq *Request) maybeClick(id string, fn ClickFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == "click" {
err = fn(rq)
}
return
}
}
return rq.RegisterEventFn(id, wf)
}
func (rq *Request) maybeInputText(id string, fn InputTextFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == "input" {
err = fn(rq, val)
}
return
}
}
return rq.RegisterEventFn(id, wf)
}
func (rq *Request) maybeInputFloat(id string, fn InputFloatFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == "input" {
var v float64
if v, err = strconv.ParseFloat(val, 64); err == nil {
err = fn(rq, v)
}
}
return
}
}
return rq.RegisterEventFn(id, wf)
}
func (rq *Request) maybeInputBool(id string, fn InputBoolFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == "input" {
var v bool
if v, err = strconv.ParseBool(val); err == nil {
err = fn(rq, v)
}
}
return
}
}
return rq.RegisterEventFn(id, wf)
}
func (rq *Request) maybeInputDate(id string, fn InputDateFn) string {
var wf EventFn
if fn != nil {
wf = func(rq *Request, id, evt, val string) (err error) {
if evt == "input" {
var v time.Time
if v, err = time.Parse(ISO8601, val); err == nil {
err = fn(rq, v)
}
}
return
}
}
return rq.RegisterEventFn(id, wf)
}
// OnInput registers a HTML id and a function to be called when it's input event fires.
// Returns a nil error so it can be used inside templates.
func (rq *Request) OnInput(id string, fn InputTextFn) error {
rq.maybeInputText(id, fn)
return nil
}
// OnClick registers a HTML id and a function to be called when it's click event fires.
// Returns a nil error so it can be used inside templates.
func (rq *Request) OnClick(id string, fn ClickFn) error {
rq.maybeClick(id, fn)
return nil
}
func (rq *Request) Div(id, inner string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "div", "", inner, attrs)
}
func (rq *Request) Span(id, inner string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "span", "", inner, attrs)
}
func (rq *Request) Li(id, inner string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "li", "", inner, attrs)
}
func (rq *Request) Td(id, inner string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "td", "", inner, attrs)
}
func (rq *Request) A(id, inner string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "a", "", inner, attrs)
}
func (rq *Request) Button(id, txt string, fn ClickFn, attrs string) template.HTML {
return rq.innerHTML(rq.maybeClick(id, fn), "button", "button", txt, attrs)
}
func (rq *Request) Text(id, val string, fn InputTextFn, attrs string) template.HTML {
return rq.inputHTML(rq.maybeInputText(id, fn), "text", val, attrs)
}
func (rq *Request) Password(id string, fn InputTextFn, attrs string) template.HTML {
return rq.inputHTML(rq.maybeInputText(id, fn), "password", "", attrs)
}
func (rq *Request) Number(id string, val float64, fn InputFloatFn, attrs string) template.HTML {
return rq.inputHTML(rq.maybeInputFloat(id, fn), "number", strconv.FormatFloat(val, 'f', -1, 64), attrs)
}
func (rq *Request) Range(id string, val float64, fn InputFloatFn, attrs string) template.HTML {
return rq.inputHTML(rq.maybeInputFloat(id, fn), "range", strconv.FormatFloat(val, 'f', -1, 64), attrs)
}
func (rq *Request) Checkbox(id string, val bool, fn InputBoolFn, attrs string) template.HTML {
if val {
attrs += " checked"
}
return rq.inputHTML(rq.maybeInputBool(id, fn), "checkbox", "", attrs)
}
func (rq *Request) Date(id string, val time.Time, fn InputDateFn, attrs string) template.HTML {
if val.IsZero() {
val = time.Now()
}
return rq.inputHTML(rq.maybeInputDate(id, fn), "date", val.Format(ISO8601), attrs)
}
func radioGroup(id string) string {
if slash := strings.IndexByte(id, '/'); slash != -1 {
return id[slash+1:]
}
panic("radio button ID's must be in the form 'buttonid/groupid'")
}
func (rq *Request) Radio(id string, val bool, fn InputBoolFn, attrs string) template.HTML {
attrs += " name=\"" + radioGroup(id) + "\""
if val {
attrs += " checked"
}
return rq.inputHTML(rq.maybeInputBool(id, fn), "radio", "", attrs)
}
func (rq *Request) Select(id string, val *NamedBoolArray, fn InputTextFn, attrs string) template.HTML {
id = rq.maybeInputText(id, fn)
b := make([]byte, 0, 256)
b = append(b, "<select id=\""...)
b = append(b, id...)
b = append(b, '"')
if attrs != "" {
b = append(b, ' ')
b = append(b, attrs...)
}
b = append(b, ">\n"...)
for _, nb := range *val {
b = append(b, "<option value=\""...)
b = append(b, (nb.Value)...)
if nb.Checked {
b = append(b, "\" selected>"...)
} else {
b = append(b, "\">"...)
}
b = append(b, (nb.Text)...)
b = append(b, "</option>\n"...)
}
b = append(b, "</select>\n"...)
return template.HTML(b) // #nosec G203
}