/
radio-button-controller.js
150 lines (129 loc) · 3.98 KB
/
radio-button-controller.js
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
var Montage = require("./core").Montage,
RangeController = require("./range-controller").RangeController;
/**
* The radio button controller intermediates between a set of options and their
* visual representation as radio buttons. The controller maintains the
* invariant that only one radio button at a time may be selected and provides
* a value property with the currently-selected option.
*
* @class RadioButtonController
* @classdesc Manages the selection of mutually-exclusive [RadioButton]{@link
* AbstractRadioButton}s.
* @extends Montage
*/
exports.RadioButtonController = Montage.specialize(/** @lends RadioButtonController# */ {
_radioButtons: {
value: null
},
_content: {
value: null
},
/**
* The list of possible options.
* @type Array.<Object>
*/
content: {
get: function () {
return this.getPath("contentController.content");
},
set: function (content) {
this.contentController = new RangeController()
.initWithContent(content);
}
},
contentController: {
value: null
},
/**
* The radio button component corresponding to the currently-selected option.
* @type {?Component}
*/
selectedRadioButton: {
value: null
},
_value: {
value: null
},
/**
* The currently-selected option.
*/
value: {
set: function(value) {
if (this._value !== value) {
this._value = value;
this._updateRadioButtons();
}
},
get: function() {
return this._value;
}
},
constructor: {
value: function RadioButtonController() {
this._radioButtons = [];
this.addRangeAtPathChangeListener("_radioButtons.map{checked}", this, "handleRadioButtonChange");
this.defineBinding("value ", {
"<->": "contentController.selection.0"
});
}
},
_updateRadioButtons: {
value: function() {
var value = this._value;
for (var i = 0, ii = this._radioButtons.length; i < ii; i++) {
if (value === this._radioButtons[i].value) {
this._radioButtons[i].checked = true;
break;
}
}
}
},
/**
* Add a radio button to be managed by this controller.
* @method
* @param {RadioButton} radioButton
* @returns {undefined}
*/
registerRadioButton: {
value: function(radioButton) {
if (this._radioButtons.indexOf(radioButton) === -1) {
this._radioButtons.push(radioButton);
this._updateRadioButtons();
}
}
},
/**
* Remove a radio button from being managed by this controller.
* @method
* @param {RadioButton} radioButton
* @returns {undefined}
*/
unregisterRadioButton: {
value: function(radioButton) {
var ix = this._radioButtons.indexOf(radioButton);
if (ix >= 0) {
this._radioButtons.splice(ix, 1);
if (radioButton === this.selectedRadioButton) {
this.selectedRadioButton = null;
}
}
}
},
handleRadioButtonChange: {
value: function(plus, minus, index) {
if (plus[0] === true) {
for (var i = 0, ii = this._radioButtons.length; i < ii; i++) {
if (i === index) {
this.selectedRadioButton = this._radioButtons[i];
this.value = this.selectedRadioButton.value;
} else {
this._radioButtons[i].checked = false;
}
}
}
}
}
}, /** @lends RadioButtonController. */ {
blueprintModuleId:require("./core")._blueprintModuleIdDescriptor,
blueprint:require("./core")._blueprintDescriptor
});