-
Notifications
You must be signed in to change notification settings - Fork 0
/
booleanfield.go
79 lines (69 loc) · 1.89 KB
/
booleanfield.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
package fork
import (
"fmt"
"net/http"
"strings"
)
type booleanfield struct {
Selection *Selection
*named
Processor
}
func (b *booleanfield) New(i ...interface{}) Field {
var newfield booleanfield = *b
var newselection Selection = *b.Selection
newfield.named = b.named.Copy()
newfield.Selection = &newselection
newfield.SetValidateable(false)
return &newfield
}
func (b *booleanfield) Get() *Value {
return NewValue(b.Selection)
}
func (b *booleanfield) Set(r *http.Request) {
v := b.Filter(b.Name(), r)
if v.String() == b.Selection.Value {
b.Selection.Set = true
} else {
b.Selection.Set = false
}
b.SetValidateable(true)
}
func toggleWidget(input string, options ...string) Widget {
in := strings.Join([]string{
fmt.Sprintf(`<input type="%s" `, input),
`name="{{ .Name }}" `,
`value="{{ .Selection.Value }}"`,
`{{ if .Selection.Set }} checked{{ end }} %s>`,
`{{ .Selection.Label }}`,
}, "")
return NewWidget(WithOptions(in, options...))
}
func BooleanField(name string, label string, start bool, options ...string) Field {
return &booleanfield{
Selection: NewSelection(name, label, start),
named: newnamed(name),
Processor: NewProcessor(
toggleWidget("checkbox", options...),
nilValidater,
nilFilterer,
),
}
}
func ToggleInput(name string, label string, value string, widget Widget, checked bool) Field {
return &booleanfield{
Selection: NewSelection(value, label, checked),
named: newnamed(name),
Processor: NewProcessor(
widget,
nilValidater,
nilFilterer,
),
}
}
func RadioInput(name string, label string, value string, checked bool, options ...string) Field {
return ToggleInput(name, label, value, toggleWidget("radio", options...), checked)
}
func CheckboxInput(name string, label string, value string, checked bool, options ...string) Field {
return ToggleInput(name, label, value, toggleWidget("checkbox", options...), checked)
}