~jamesh/go-unityscopes/go-1.6-compat

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
package scopes

// RadioButtonsFilter is a filter that displays mutually exclusive list of options
type RadioButtonsFilter struct {
	filterWithOptions
	Label string
}

// NewRadioButtonsFilter creates a new radio button filter.
func NewRadioButtonsFilter(id, label string) *RadioButtonsFilter {
	return &RadioButtonsFilter{
		filterWithOptions: filterWithOptions{
			filterBase: filterBase{
				Id:           id,
				DisplayHints: FilterDisplayDefault,
				FilterType:   "radio_buttons",
			},
		},
		Label: label,
	}
}

// UpdateState updates the value of a particular option in the filter state.
func (f *RadioButtonsFilter) UpdateState(state FilterState, optionId string, active bool) {
	if !f.isValidOption(optionId) {
		panic("invalid option ID")
	}
	// If the state isn't in a form we expect, treat it as empty
	selected, _ := state[f.Id].([]interface{})

	if active {
		if len(selected) == 0 {
			// just add the optionId
			selected = append(selected, optionId)
		} else if len(selected) > 0 && selected[0] != optionId {
			// we have another option selected, just select the current one
			selected[0] = optionId
		}
	} else {
		if len(selected) > 0 && selected[0] == optionId {
			// we have 1 option selected and it's the current one.
			// clear the state
			selected = make([]interface{}, 0)
		}
	}
	state[f.Id] = selected
}

func (f *RadioButtonsFilter) serializeFilter() map[string]interface{} {
	v := f.filterBase.serializeFilter()
	v["label"] = f.Label
	v["options"] = f.Options
	return v
}