-
Notifications
You must be signed in to change notification settings - Fork 6
/
button.go
194 lines (164 loc) · 4.94 KB
/
button.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
grctl "github.com/goradd/goradd/pkg/page/control"
"reflect"
)
type ButtonI interface {
grctl.ButtonI
SetButtonStyle(style ButtonStyle) ButtonI
SetButtonSize(size ButtonSize) ButtonI
SetIsPrimary(isPrimary bool) ButtonI
}
type Button struct {
grctl.Button
style ButtonStyle
size ButtonSize
}
const ButtonClass = "btn"
type ButtonStyle string
const (
ButtonStylePrimary ButtonStyle = "btn-primary"
ButtonStyleSecondary = "btn-secondary"
ButtonStyleSuccess = "btn-success"
ButtonStyleInfo = "btn-info"
ButtonStyleWarning = "btn-warning"
ButtonStyleDanger = "btn-danger"
ButtonStyleLight = "btn-light"
ButtonStyleDark = "btn-dark"
ButtonStyleLink = "btn-link"
ButtonStyleOutlinePrimary = "btn-outline-primary"
ButtonStyleOutlineSecondary = "btn-outline-secondary"
ButtonStyleOutlineSuccess = "btn-outline-success"
ButtonStyleOutlineInfo = "btn-outline-info"
ButtonStyleOutlineWarning = "btn-outline-warning"
ButtonStyleOutlineDanger = "btn-outline-danger"
ButtonStyleOutlineLight = "btn-outline-light"
ButtonStyleOutlineDark = "btn-outline-dark"
)
type ButtonSize string
const (
ButtonSizeLarge ButtonSize = "btn-lg"
ButtonSizeMedium = ""
ButtonSizeSmall = "btn-sm"
)
// Add ButtonBlock as a class to a button to make it span a full block
const ButtonBlock = "btn-block"
// Creates a new standard html button
func NewButton(parent page.ControlI, id string) *Button {
b := &Button{}
b.Init(b, parent, id)
return b
}
func (b *Button) Init(self page.ControlI, parent page.ControlI, id string) {
b.Button.Init(self, parent, id)
b.style = ButtonStyleSecondary // default
config.LoadBootstrap(b.ParentForm())
}
func (b *Button) this() ButtonI {
return b.Self.(ButtonI)
}
// SetButtonStyle will set the button's style to one of the predefined bootstrap styles.
func (b *Button) SetButtonStyle(style ButtonStyle) ButtonI {
b.style = style
return b.this()
}
// SetButtonsSize sets the size class of the button.
func (b *Button) SetButtonSize(size ButtonSize) ButtonI {
b.size = size
return b.this()
}
func (b *Button) ΩDrawingAttributes() html.Attributes {
a := b.Button.ΩDrawingAttributes()
a.AddClass(ButtonClass)
a.AddClass(string(b.style))
a.AddClass(string(b.size))
return a
}
func (b *Button) SetIsPrimary(isPrimary bool) ButtonI {
if isPrimary {
b.style = ButtonStylePrimary
} else {
b.style = ButtonStyleSecondary
}
return b.this()
}
func (b *Button) Serialize(e page.Encoder) (err error) {
if err = b.Button.Serialize(e); err != nil {
return
}
if err = e.Encode(b.style); err != nil {
return err
}
if err = e.Encode(b.size); err != nil {
return err
}
return
}
// ΩisSerializer is used by the automated control serializer to determine how far down the control chain the control
// has to go before just calling serialize and deserialize
func (b *Button) ΩisSerializer(i page.ControlI) bool {
return reflect.TypeOf(b) == reflect.TypeOf(i)
}
func (b *Button) Deserialize(d page.Decoder, p *page.Page) (err error) {
if err = b.Button.Deserialize(d, p); err != nil {
return
}
if err = d.Decode(&b.style); err != nil {
return
}
if err = d.Decode(&b.size); err != nil {
return
}
return
}
// ButtonCreator is the initialization structure for declarative creation of buttons
type ButtonCreator struct {
// ID is the control id
ID string
// Text is the text displayed in the button
Text string
// OnSubmit is the action to take when the button is submitted. Use this specifically
// for buttons that move to other pages or processes transactions, as it debounces the button
// and waits until all other actions complete
OnSubmit action.ActionI
// OnClick is an action to take when the button is pressed. Do not specify both
// a OnSubmit and OnClick.
OnClick action.ActionI
Style ButtonStyle
Size ButtonSize
IsPrimary 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 ButtonCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewButton(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by implementations of Buttons to initialize a control with the
// creator. You do not normally need to call this.
func (c ButtonCreator) Init(ctx context.Context, ctrl ButtonI) {
sub := grctl.ButtonCreator{
ID: c.ID,
Text: c.Text,
OnSubmit: c.OnSubmit,
OnClick: c.OnClick,
ControlOptions: c.ControlOptions,
}
sub.Init(ctx, ctrl)
if c.Style != "" {
ctrl.SetButtonStyle(c.Style)
}
if c.Size != "" {
ctrl.SetButtonSize(c.Size)
}
if c.IsPrimary {
ctrl.SetIsPrimary(true)
}
}