-
Notifications
You must be signed in to change notification settings - Fork 6
/
radioButton.go
139 lines (120 loc) · 3.46 KB
/
radioButton.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
package control
import (
"context"
"fmt"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control/button"
"github.com/goradd/html5tag"
"io"
)
type RadioButtonI interface {
button.RadioButtonI
}
type RadioButton struct {
button.RadioButton
inline bool
}
func NewRadioButton(parent page.ControlI, id string) *RadioButton {
c := &RadioButton{}
c.Self = c
c.Init(parent, id)
config.LoadBootstrap(c.ParentForm())
return c
}
func (c *RadioButton) this() RadioButtonI {
return c.Self.(RadioButtonI)
}
func (c *RadioButton) SetInline(v bool) *RadioButton {
c.inline = v
return c
}
func (c *RadioButton) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := c.RadioButton.DrawingAttributes(ctx)
a.SetData("grctl", "bs-radio")
a.AddClass("form-check-input")
if c.Text() == "" {
a.AddClass("position-static")
}
return a
}
func (c *RadioButton) GetDrawingLabelAttributes() html5tag.Attributes {
a := c.RadioButton.GetDrawingLabelAttributes()
a.AddClass("form-check-label")
return a
}
func (c *RadioButton) DrawTag(ctx context.Context, w io.Writer) {
checkWrapperAttributes := html5tag.NewAttributes().
AddClass("form-check").
SetData("grel", c.ID()) // make sure the entire control gets removed
if c.inline {
checkWrapperAttributes.AddClass("form-check-inline")
}
_, _ = fmt.Fprint(w, "<div ", checkWrapperAttributes.String(), ">\n")
c.RadioButton.DrawTag(ctx, w)
_, _ = io.WriteString(w, "\n</div>")
}
func (c *RadioButton) Serialize(e page.Encoder) {
c.RadioButton.Serialize(e)
if err := e.Encode(c.inline); err != nil {
panic(err)
}
}
func (c *RadioButton) Deserialize(d page.Decoder) {
c.RadioButton.Deserialize(d)
if err := d.Decode(&c.inline); err != nil {
panic(err)
}
}
type RadioButtonCreator struct {
// ID is the id of the control
ID string
// Text is the text of the label displayed right next to the checkbox.
Text string
// Checked will initialize the checkbox in its checked state.
Checked bool
// LabelMode specifies how the label is drawn with the checkbox.
LabelMode html5tag.LabelDrawingMode
// LabelAttributes are additional attributes placed on the label tag.
LabelAttributes html5tag.Attributes
// SaveState will save the value of the checkbox and restore it when the page is reentered.
SaveState bool
// Group is the name of the group that the button belongs to
Group string
// Inline draws the radio group inline. Specify this when drawing the
// radio button inline or in an inline FormGroup.
Inline bool
page.ControlOptions
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c RadioButtonCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewRadioButton(parent, c.ID)
if c.Text != "" {
ctrl.SetText(c.Text)
}
if c.LabelMode != html5tag.LabelDefault {
ctrl.LabelMode = c.LabelMode
}
if c.LabelAttributes != nil {
ctrl.LabelAttributes().Merge(c.LabelAttributes)
}
if c.Group != "" {
ctrl.SetGroup(c.Group)
}
ctrl.ApplyOptions(ctx, c.ControlOptions)
if c.SaveState {
ctrl.SaveState(ctx, c.SaveState)
}
if c.Inline {
ctrl.SetInline(c.Inline)
}
return ctrl
}
// GetRadioButton is a convenience method to return the radio button with the given id from the page.
func GetRadioButton(c page.ControlI, id string) *RadioButton {
return c.Page().GetControl(id).(*RadioButton)
}
func init() {
page.RegisterControl(&RadioButton{})
}