forked from icza/gowut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbox.go
228 lines (187 loc) · 5.48 KB
/
listbox.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
// 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/>.
// ListBox component interface and implementation.
package gwu
import (
"net/http"
"strconv"
"strings"
)
// ListBox interface defines a component which allows selecting one or multiple values
// from a predefined list.
//
// Suggested event type to handle changes: ETypeChange
//
// Default style class: "gwu-ListBox"
type ListBox interface {
// ListBox is a component
Comp
// ListBox can be enabled/disabled.
HasEnabled
// Multi tells if multiple selections are allowed.
Multi() bool
// SetMulti sets whether multiple selections are allowed.
SetMulti(multi bool)
// Rows returns the number of displayed rows.
Rows() int
// SetRows sets the number of displayed rows.
// rows=1 will make this ListBox a dropdown list (if multi is false!).
// Note that if rows is greater than 1, most browsers enforce a visual minimum size
// (about 4 rows) even if rows is less than that.
SetRows(rows int)
// SelectedValue retruns the first selected value.
// Empty string is returned if nothing is selected.
SelectedValue() string
// SelectedValues retruns all the selected values.
SelectedValues() []string
// Selected tells if the value at index i is selected.
Selected(i int) bool
// SelectedIdx returns the first selected index.
// Returns -1 if nothing is selected.
SelectedIdx() int
// SelectedIndices returns a slice of the indices of the selected values.
SelectedIndices() []int
// SetSelected sets the selection state of the value at index i.
SetSelected(i int, selected bool)
// SetSelectedIndices sets the (only) selected values.
// Only values will be selected that are contained in the specified indices slice.
SetSelectedIndices(indices []int)
// ClearSelected deselects all values.
ClearSelected()
}
// ListBox implementation.
type listBoxImpl struct {
compImpl // Component implementation
hasEnabledImpl // Has enabled implementation
values []string // Values to choose from
multi bool // Allow multiple selection
selected []bool // Array of selection state of the values
rows int // Number of displayed rows
}
var (
strSelidx = []byte("selIdxs(this)") // "selIdxs(this)"
)
// NewListBox creates a new ListBox.
func NewListBox(values []string) ListBox {
c := &listBoxImpl{newCompImpl(strSelidx), newHasEnabledImpl(), values, false, make([]bool, len(values)), 1}
c.AddSyncOnETypes(ETypeChange)
c.Style().AddClass("gwu-ListBox")
return c
}
func (c *listBoxImpl) Multi() bool {
return c.multi
}
func (c *listBoxImpl) SetMulti(multi bool) {
c.multi = multi
}
func (c *listBoxImpl) Rows() int {
return c.rows
}
func (c *listBoxImpl) SetRows(rows int) {
c.rows = rows
}
func (c *listBoxImpl) SelectedValue() string {
if i := c.SelectedIdx(); i >= 0 {
return c.values[i]
}
return ""
}
func (c *listBoxImpl) SelectedValues() (sv []string) {
for i, s := range c.selected {
if s {
sv = append(sv, c.values[i])
}
}
return
}
func (c *listBoxImpl) Selected(i int) bool {
return c.selected[i]
}
func (c *listBoxImpl) SelectedIdx() int {
for i, s := range c.selected {
if s {
return i
}
}
return -1
}
func (c *listBoxImpl) SelectedIndices() (si []int) {
for i, s := range c.selected {
if s {
si = append(si, i)
}
}
return
}
func (c *listBoxImpl) SetSelected(i int, selected bool) {
c.selected[i] = selected
}
func (c *listBoxImpl) SetSelectedIndices(indices []int) {
// First clear selected slice
for i := range c.selected {
c.selected[i] = false
}
// And now select that needs to be selected
for _, idx := range indices {
c.selected[idx] = true
}
}
func (c *listBoxImpl) ClearSelected() {
for i := range c.selected {
c.selected[i] = false
}
}
func (c *listBoxImpl) preprocessEvent(event Event, r *http.Request) {
value := r.FormValue(paramCompValue)
c.ClearSelected()
if len(value) == 0 {
return
}
// Set selected indices
for _, sidx := range strings.Split(value, ",") {
if idx, err := strconv.Atoi(sidx); err == nil {
c.selected[idx] = true
}
}
}
var (
strSelectOp = []byte("<select") // "<select"
strMultiple = []byte(` multiple="multiple"`) // ` multiple="multiple"`
strOptionOpSel = []byte(`<option selected="selected">`) // `<option selected="selected">`
strOptionOp = []byte("<option>") // "<option>"
strOptionCl = []byte("</option>") // "</option>"
strSelectCl = []byte("</select>") // "</select>"
)
func (c *listBoxImpl) Render(w Writer) {
w.Write(strSelectOp)
if c.multi {
w.Write(strMultiple)
}
w.WriteAttr("size", strconv.Itoa(c.rows))
c.renderAttrsAndStyle(w)
c.renderEnabled(w)
c.renderEHandlers(w)
w.Write(strGT)
for i, value := range c.values {
if c.selected[i] {
w.Write(strOptionOpSel)
} else {
w.Write(strOptionOp)
}
w.Writees(value)
w.Write(strOptionCl)
}
w.Write(strSelectCl)
}