forked from icza/gowut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_buttons.go
416 lines (348 loc) · 11.5 KB
/
state_buttons.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright (C) 2013 Andras Belicza. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// State button interfaces and implementations
// (CheckBox, RadioButton, SwitchButton).
package gwu
import (
"net/http"
"strconv"
)
// StateButton interface defines a button which has a boolean state:
// true/false or selected/deselected.
type StateButton interface {
// stateButton is a button
Button
// State returns the state of the button.
State() bool
// SetState sets the state of the button.
// In case of RadioButton, the button's RadioGroup is managed
// so that only one can be selected.
SetState(state bool)
}
// CheckBox interface defines a check box, a button which has
// 2 states: selected/deselected.
//
// Suggested event type to handle changes: ETypeClick
//
// Default style classes: "gwu-CheckBox", "gwu-CheckBox-Disabled"
type CheckBox interface {
// CheckBox is a StateButton.
StateButton
}
// SwitchButton interface defines a button which can be switched
// ON and OFF.
//
// Suggested event type to handle changes: ETypeClick
//
// Default style classes: "gwu-SwitchButton", "gwu-SwitchButton-On-Active"
// "gwu-SwitchButton-On-Inactive", "gwu-SwitchButton-Off-Active",
// "gwu-SwitchButton-Off-Inactive"
type SwitchButton interface {
// SwitchButton is a component.
Comp
// SwitchButton can be enabled/disabled.
HasEnabled
// State returns the state of the switch button.
State() bool
// SetState sets the state of the switch button.
SetState(state bool)
// On returns the text displayed for the ON side.
On() string
// Off returns the text displayed for the OFF side.
Off() string
// SetOnOff sets the texts of the ON and OFF sides.
SetOnOff(on, off string)
}
// RadioGroup interface defines the group for grouping radio buttons.
type RadioGroup interface {
// Name returns the name of the radio group.
Name() string
// Selected returns the selected radio button of the group.
Selected() RadioButton
// PrevSelected returns the radio button that was selected
// before the current selected radio button.
PrevSelected() RadioButton
// setSelected sets the selected radio button of the group,
// and before that sets the current selected as the prev selected
setSelected(selected RadioButton)
}
// RadioButton interface defines a radio button, a button which has
// 2 states: selected/deselected.
// In addition to the state, radio buttons belong to a group,
// and in each group only one radio button can be selected.
// Selecting an unselected radio button deselects the selected
// radio button of the group, if there was one.
//
// Suggested event type to handle changes: ETypeClick
//
// Default style classes: "gwu-RadioButton", "gwu-RadioButton-Disabled"
type RadioButton interface {
// RadioButton is a StateButton.
StateButton
// Group returns the group of the radio button.
Group() RadioGroup
// setStateProp sets the state of the button
// without managing the group of the radio button.
setStateProp(state bool)
}
// RadioGroup implementation.
type radioGroupImpl struct {
name string // Name of the radio group
selected RadioButton // Selected radio button of the group
prevSelected RadioButton // Previous selected radio button of the group
}
// StateButton implementation.
type stateButtonImpl struct {
buttonImpl // Button implementation
state bool // State of the button
inputType []byte // Type of the underlying input tag
group RadioGroup // Group of the button
inputID ID // distinct ID for the rendered input tag
disabledClass string // Disabled style class
}
// SwitchButton implementation.
type switchButtonImpl struct {
compImpl // Component implementation
onButton, offButton *buttonImpl // ON and OFF button implementations
state bool // State of the switch
}
// NewRadioGroup creates a new RadioGroup.
func NewRadioGroup(name string) RadioGroup {
return &radioGroupImpl{name: name}
}
var (
strCheckbox = []byte("checkbox") // "checkbox"
strRadio = []byte("radio") // "radio"
strThisChecked = []byte("this.checked") // "this.checked"
)
// NewCheckBox creates a new CheckBox.
// The initial state is false.
func NewCheckBox(text string) CheckBox {
c := newStateButtonImpl(text, strCheckbox, nil, "gwu-CheckBox-Disabled")
c.Style().AddClass("gwu-CheckBox")
return c
}
// NewSwitchButton creates a new SwitchButton.
// Default texts for ON and OFF sides are: "ON" and "OFF".
// The initial state is false (OFF).
func NewSwitchButton() SwitchButton {
onButton := newButtonImpl(nil, "ON")
offButton := newButtonImpl(nil, "OFF")
// We only want to switch the state if the opposite button is pressed
// (e.g. OFF is pressed when switch is ON and vice versa;
// if ON is pressed when switch is ON, do not switch to OFF):
valueProviderJs := []byte("sbtnVal(event,'" + onButton.ID().String() + "','" + offButton.ID().String() + "')")
c := &switchButtonImpl{newCompImpl(valueProviderJs), &onButton, &offButton, true} // Note the "true" state, so the following SetState(false) will be executed (different states)!
c.AddSyncOnETypes(ETypeClick)
c.SetAttr("cellspacing", "0")
c.SetAttr("cellpadding", "0")
c.Style().AddClass("gwu-SwitchButton")
c.SetState(false)
return c
}
// NewRadioButton creates a new radio button.
// The initial state is false.
func NewRadioButton(text string, group RadioGroup) RadioButton {
c := newStateButtonImpl(text, strRadio, group, "gwu-RadioButton-Disabled")
c.Style().AddClass("gwu-RadioButton")
return c
}
// newStateButtonImpl creates a new stateButtonImpl.
func newStateButtonImpl(text string, inputType []byte, group RadioGroup, disabledClass string) *stateButtonImpl {
c := &stateButtonImpl{newButtonImpl(strThisChecked, text), false, inputType, group, nextCompID(), disabledClass}
// Use ETypeClick because IE fires onchange only when focus is lost...
c.AddSyncOnETypes(ETypeClick)
return c
}
func (r *radioGroupImpl) Name() string {
return r.name
}
func (r *radioGroupImpl) Selected() RadioButton {
return r.selected
}
func (r *radioGroupImpl) PrevSelected() RadioButton {
return r.prevSelected
}
func (r *radioGroupImpl) setSelected(selected RadioButton) {
r.prevSelected = r.selected
r.selected = selected
}
// SetEnabled sets the enabled property.
// We have some extra job to do when changing enabled status:
// we have to manage disabled class style.
func (c *stateButtonImpl) SetEnabled(enabled bool) {
if enabled {
c.Style().RemoveClass(c.disabledClass)
} else {
c.Style().AddClass(c.disabledClass)
}
c.hasEnabledImpl.SetEnabled(enabled)
}
func (c *stateButtonImpl) State() bool {
return c.state
}
func (c *stateButtonImpl) SetState(state bool) {
// Only continue if state changes:
if c.state == state {
return
}
if c.group != nil {
// Group management: if a new radio button is selected, the old one must be deselected.
sel := c.group.Selected()
if sel == nil {
// no prev selection
if state {
c.group.setSelected(c)
}
} else {
// There is a prev selection
if state {
if !sel.Equals(c) {
sel.setStateProp(false)
c.group.setSelected(c)
}
} else {
// There is prev selection, and our new state is false
// (and our prev state was true => we are selected)
c.group.setSelected(nil)
}
}
}
c.state = state
}
func (c *stateButtonImpl) Group() RadioGroup {
return c.group
}
func (c *stateButtonImpl) setStateProp(state bool) {
c.state = state
}
func (c *stateButtonImpl) preprocessEvent(event Event, r *http.Request) {
value := r.FormValue(paramCompValue)
if len(value) == 0 {
return
}
if v, err := strconv.ParseBool(value); err == nil {
// Call SetState instead of assigning to the state property
// because SetState properly manages radio groups.
c.SetState(v)
}
}
var (
strInput = []byte(`<input type="`) // `<input type="`
strID = []byte(`" id="`) // `" id="`
strName = []byte(` name="`) // ` name="`
strChecked = []byte(` checked="checked"`) // ` checked="checked"`
strLabelFor = []byte(`><label for="`) // `><label for="`
strLabelCl = []byte("</label>") // "</label>"
)
func (c *stateButtonImpl) Render(w Writer) {
// Proper state button consists of multiple HTML tags (input and label), so render a wrapper tag for them:
w.Write(strSpanOp)
c.renderAttrsAndStyle(w)
w.Write(strGT)
w.Write(strInput)
w.Write(c.inputType)
w.Write(strID)
w.Writev(int(c.inputID))
w.Write(strQuote)
if c.group != nil {
w.Write(strName)
w.Writes(c.group.Name())
w.Write(strQuote)
}
if c.state {
w.Write(strChecked)
}
c.renderEnabled(w)
c.renderEHandlers(w)
w.Write(strLabelFor)
w.Writev(int(c.inputID))
w.Write(strQuote)
// TODO readding click handler here causes double event sending...
// But we might add mouseover and other handlers still...
//c.renderEHandlers(w)
w.Write(strGT)
c.renderText(w)
w.Write(strLabelCl)
w.Write(strSpanCl)
}
func (c *switchButtonImpl) Enabled() bool {
return c.onButton.Enabled()
}
func (c *switchButtonImpl) SetEnabled(enabled bool) {
c.onButton.SetEnabled(enabled)
c.offButton.SetEnabled(enabled)
}
func (c *switchButtonImpl) State() bool {
return c.state
}
func (c *switchButtonImpl) SetState(state bool) {
// Only continue if state changes:
if c.state == state {
return
}
c.state = state
if c.state {
c.onButton.Style().SetClass("gwu-SwitchButton-On-Active")
c.offButton.Style().SetClass("gwu-SwitchButton-Off-Inactive")
} else {
c.onButton.Style().SetClass("gwu-SwitchButton-On-Inactive")
c.offButton.Style().SetClass("gwu-SwitchButton-Off-Active")
}
}
func (c *switchButtonImpl) On() string {
return c.onButton.Text()
}
func (c *switchButtonImpl) Off() string {
return c.offButton.Text()
}
func (c *switchButtonImpl) SetOnOff(on, off string) {
c.onButton.SetText(on)
c.offButton.SetText(off)
}
func (c *switchButtonImpl) preprocessEvent(event Event, r *http.Request) {
value := r.FormValue(paramCompValue)
if len(value) == 0 {
return
}
if v, err := strconv.ParseBool(value); err == nil {
// Call SetState instead of assigning to the state property
// because SetState properly changes style classes.
c.SetState(v)
// SwitchButtons' client code properly updates internal buttons' style,
// so we're good not to mark the switch button dirty if state changes.
}
}
var (
strClTr = []byte("><tr>") // "><tr>"
strTD50 = []byte(`<td width="50%">`) // `<td width="50%">`
)
func (c *switchButtonImpl) Render(w Writer) {
w.Write(strTableOp)
c.renderAttrsAndStyle(w)
c.renderEHandlers(w)
// For Internet Explorer only:
// Since state synchronization is done on ETypeClick, which will add a click handler
// to the wrapper tag and not to the on/off buttons, the wrapper tag itself must be
// disabled (must have a 'disabled' attribute) if the switch button is disabled in order
// for clicks really be disabled.
c.onButton.renderEnabled(w)
w.Write(strClTr)
w.Write(strTD50)
c.onButton.Render(w)
w.Write(strTD50)
c.offButton.Render(w)
w.Write(strTableCl)
}