-
Notifications
You must be signed in to change notification settings - Fork 6
/
checkbox.go
125 lines (109 loc) · 3.16 KB
/
checkbox.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
package control
import (
"context"
"fmt"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/html5tag"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
"io"
)
type Checkbox struct {
control.Checkbox
inline bool
}
func NewCheckbox(parent page.ControlI, id string) *Checkbox {
c := &Checkbox{}
c.Self = c
c.Init(parent, id)
config.LoadBootstrap(c.ParentForm())
return c
}
func (c *Checkbox) SetInline(v bool) *Checkbox {
c.inline = v
return c
}
func (c *Checkbox) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := c.Checkbox.DrawingAttributes(ctx)
a.AddClass("form-check-input")
a.SetData("grctl", "bs-checkbox")
if c.Text() == "" {
a.AddClass("position-static")
}
return a
}
func (c *Checkbox) GetDrawingLabelAttributes() html5tag.Attributes {
a := c.Checkbox.GetDrawingLabelAttributes()
a.AddClass("form-check-label")
return a
}
func (c *Checkbox) 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")
}
if _, err := fmt.Fprint(w, "<div ", checkWrapperAttributes.String(), ">\n"); err != nil {panic(err)}
c.Checkbox.DrawTag(ctx, w)
if _, err := io.WriteString(w, "\n</div>"); err != nil {panic(err)}
}
func (c *Checkbox) Serialize(e page.Encoder) {
c.Checkbox.Serialize(e)
if err := e.Encode(c.inline); err != nil {
panic(err)
}
}
func (c *Checkbox) Deserialize(d page.Decoder) {
c.Checkbox.Deserialize(d)
if err := d.Decode(&c.inline); err != nil {
panic(err)
}
return
}
type CheckboxCreator 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
// Set inline when drawing this checkbox inline or wrapped by 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 CheckboxCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewCheckbox(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)
}
ctrl.ApplyOptions(ctx, c.ControlOptions)
if c.SaveState {
ctrl.SaveState(ctx, c.SaveState)
}
if c.Inline {
ctrl.SetInline(c.Inline)
}
return ctrl
}
// GetCheckbox is a convenience method to return the checkbox with the given id from the page.
func GetCheckbox(c page.ControlI, id string) *Checkbox {
return c.Page().GetControl(id).(*Checkbox)
}
func init() {
page.RegisterControl(&Checkbox{})
}