-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_select.go
334 lines (287 loc) · 9.29 KB
/
list_select.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
package control
import (
"bytes"
"context"
"fmt"
"github.com/goradd/gengen/pkg/maps"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/event"
"reflect"
"strconv"
)
type SelectListI interface {
page.ControlI
ItemListI
DataManagerEmbedder
SetValue(v interface{})
Value() interface{}
IntValue() int
}
// SelectList is typically a dropdown list with a single selection. Items are selected by id number, and the SelectList
// completely controls the ids in the list. Create the list by calling AddItem or AddItems to add *ListItem objects.
// Or, use the embedded DataManager to load items. Set the size attribute if you want to display it as a
// scrolling list rather than a dropdown list.
type SelectList struct {
page.ControlBase
ItemList
DataManager
selectedValue string
}
// NewSelectList creates a new select list
func NewSelectList(parent page.ControlI, id string) *SelectList {
t := &SelectList{}
t.Self = t
t.Init(parent, id)
return t
}
// Init is called by subclasses.
func (l *SelectList) Init(parent page.ControlI, id string) {
l.ControlBase.Init(parent, id)
l.ItemList = NewItemList(l.this())
l.Tag = "select"
}
func (l *SelectList) this() SelectListI {
return l.Self.(SelectListI)
}
// Validate is called by the framework to validate the contents of the control. For a SelectList,
// this is typically just checking to see if something was selected if a selection is required.
func (l *SelectList) Validate(ctx context.Context) bool {
if v := l.ControlBase.Validate(ctx); !v {
return false
}
sel := l.SelectedItem()
if l.IsRequired() && (sel == nil || sel.IsEmptyValue()) {
if l.ErrorForRequired == "" {
l.SetValidationError(l.GT("A selection is required"))
} else {
l.SetValidationError(l.ErrorForRequired)
}
return false
}
return true
}
// UpdateFormValues is used by the framework to cause the control to retrieve its values from the form
func (l *SelectList) UpdateFormValues(ctx *page.Context) {
id := l.ID()
if v, ok := ctx.FormValue(id); ok {
l.selectedValue = v
}
}
// SelectedItem will return the currently selected item. If no item has been selected, it will return the first item
// in the list, since that is what will be showing in the selection list, and will update its internal pointer to
// make the first item the current selection.
func (l *SelectList) SelectedItem() *ListItem {
if l.Len() == 0 {
return nil
}
if l.selectedValue == "" {
l.selectedValue = l.items[0].Value()
return l.items[0]
}
_,i := l.GetItemByValue(l.selectedValue)
return i
}
// SetSelectedValue sets the current selection to the given id. You must ensure that the item with the id exists, it will
// not attempt to make sure the item exists.
func (l *SelectList) SetSelectedValue(v string) {
l.selectedValue = v
l.AddRenderScript("val", v)
}
// Value implements the Valuer interface for general purpose value getting and setting
func (l *SelectList) Value() interface{} {
if l.selectedValue == "" {
return nil
} else {
return l.selectedValue
}
}
// SetValue implements the Valuer interface for general purpose value getting and setting
func (l *SelectList) SetValue(v interface{}) {
l.SetSelectedValue(fmt.Sprint(v))
}
// IntValue returns the select value as an integer.
func (l *SelectList) IntValue() int {
if l.selectedValue == "" {
return 0
} else {
i,_ := strconv.Atoi(l.selectedValue)
return i
}
}
// StringValue returns the selected value as a string
func (l *SelectList) StringValue() string {
return l.selectedValue
}
// SelectedLabel returns the label of the selected item
func (l *SelectList) SelectedLabel() string {
item := l.SelectedItem()
if item != nil {
return item.Label()
}
return ""
}
// MarshalState is an internal function to save the state of the control
func (l *SelectList) MarshalState(m maps.Setter) {
m.Set("sel", l.selectedValue)
}
// UnmarshalState is an internal function to restore the state of the control
func (l *SelectList) UnmarshalState(m maps.Loader) {
if v, ok := m.Load("sel"); ok {
if s, ok := v.(string); ok {
l.selectedValue = s
}
}
}
// DrawingAttributes retrieves the tag's attributes at draw time. You should not normally need to call this, and the
// attributes are disposed of after drawing, so they are essentially read-only.
func (l *SelectList) DrawingAttributes(ctx context.Context) html.Attributes {
a := l.ControlBase.DrawingAttributes(ctx)
a.SetDataAttribute("grctl", "selectlist")
a.Set("name", l.ID()) // needed for posts
if l.IsRequired() {
a.Set("required", "") // required for some css frameworks, but browser validation is flaky.
// set the "novalidate" attribute on the form for server-side validation only.
}
return a
}
func (l *SelectList) DrawTag(ctx context.Context) string {
if l.HasDataProvider() {
l.LoadData(ctx, l.this())
defer l.ResetData()
}
return l.ControlBase.DrawTag(ctx)
}
// DrawInnerHtml is called by the framework during drawing of the control to draw the inner html of the control
func (l *SelectList) DrawInnerHtml(ctx context.Context, buf *bytes.Buffer) (err error) {
h := l.getItemsHtml(l.items)
buf.WriteString(h)
return nil
}
func (l *SelectList) getItemsHtml(items []*ListItem) string {
var h = ""
for _, item := range items {
if item.HasChildItems() {
tag := "optgroup"
innerhtml := l.getItemsHtml(item.ListItems())
attributes := item.Attributes().Copy()
attributes.Set("label", item.Label())
h += html.RenderTag(tag, attributes, innerhtml)
} else {
attributes := item.Attributes().Copy()
// TODO: add the option to encrypt values in case values are sensitive
attributes.Set("value", item.Value())
if l.selectedValue == item.Value() {
attributes.Set("selected", "")
}
h += html.RenderTag("option", attributes, item.RenderLabel())
}
}
return h
}
// SetData overrides the default data setter to add objects to the item list.
// The result is kept in memory currently.
// ItemLister, ItemIDer, Labeler or Stringer types. This function can accept one or more lists of items, or
// single items.
func (l *SelectList) SetData(data interface{}) {
kind := reflect.TypeOf(data).Kind()
if !(kind == reflect.Slice || kind == reflect.Array) {
panic("you must call SetData with a slice or array")
}
l.ItemList.Clear()
l.AddListItems(data)
}
func (l *SelectList) Serialize(e page.Encoder) (err error) {
if err = l.ControlBase.Serialize(e); err != nil {
return
}
if err = l.ItemList.Serialize(e); err != nil {
return
}
if err = l.DataManager.Serialize(e); err != nil {
return
}
if err = e.Encode(l.selectedValue); err != nil {
return
}
return
}
func (l *SelectList) Deserialize(dec page.Decoder) (err error) {
if err = l.ControlBase.Deserialize(dec); err != nil {
return
}
if err = l.ItemList.Deserialize(dec); err != nil {
return
}
if err = l.DataManager.Deserialize(dec); err != nil {
return
}
if err = dec.Decode(&l.selectedValue); err != nil {
return
}
return
}
type SelectListCreator struct {
ID string
// Items is a static list of labels and values that will be in the list. Or, use a DataProvider to dynamically generate the items.
Items []ListValue
// NilItem is a helper to add an item at the top of the list with a nil value. This is often
// used to specify no selection, or a message that a selection is required.
NilItem string
// DataProvider is the control that will dynamically provide the data for the list and that implements the DataBinder interface.
DataProvider DataBinder
// DataProviderID is the id of a control that will dynamically provide the data for the list and that implements the DataBinder interface.
DataProviderID string
// Size specifies how many items to show, and turns the list into a scrolling list
Size int
// Value is the initial value of the list. Often its best to load the value in a separate Load step after creating the control.
Value string
// OnChange is an action to take when the user changes what is selected (as in, when the javascript change event fires).
OnChange action.ActionI
// SaveState saves the selected value so that it is restored if the form is returned to.
SaveState bool
page.ControlOptions
}
func (c SelectListCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewSelectList(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
func (c SelectListCreator) Init(ctx context.Context, ctrl SelectListI) {
if c.NilItem != "" {
ctrl.AddItem(c.NilItem, "")
}
if c.Items != nil {
ctrl.AddListItems(c.Items)
}
if c.DataProvider != nil {
ctrl.SetDataProvider(c.DataProvider)
} else if c.DataProviderID != "" {
provider := ctrl.Page().GetControl(c.DataProviderID).(DataBinder)
ctrl.SetDataProvider(provider)
}
if c.Value != "" {
ctrl.SetValue(c.Value)
}
if c.Size != 0 {
ctrl.SetAttribute("size", c.Size)
}
if c.OnChange != nil {
ctrl.On(event.Change(), c.OnChange)
}
ctrl.ApplyOptions(ctx, c.ControlOptions)
if c.SaveState {
ctrl.SaveState(ctx, c.SaveState)
}
}
// GetSelectList is a convenience method to return the control with the given id from the page.
func GetSelectList(c page.ControlI, id string) *SelectList {
return c.Page().GetControl(id).(*SelectList)
}
func GetSelectListI(c page.ControlI, id string) SelectListI {
return c.Page().GetControl(id).(SelectListI)
}
func init() {
page.RegisterControl(&SelectList{})
}