This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
/
MaterialRadio.dart
325 lines (240 loc) · 10.3 KB
/
MaterialRadio.dart
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
/**
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of mdlcomponents;
/// Store strings for class names defined by this component that are used in
/// Dart. This allows us to simply change it in one place should we
/// decide to modify at a later date.
class _MaterialRadioCssClasses {
static const String MAIN_CLASS = "mdl-js-radio";
static const String GROUP_CLASS = 'mdl-radio-group';
final String IS_FOCUSED = 'is-focused';
final String IS_DISABLED = 'is-disabled';
final String IS_CHECKED = 'is-checked';
final String IS_UPGRADED = 'is-upgraded';
final String JS_RADIO = 'mdl-js-radio';
final String RADIO_BTN = 'mdl-radio__button';
final String RADIO_OUTER_CIRCLE = 'mdl-radio__outer-circle';
final String RADIO_INNER_CIRCLE = 'mdl-radio__inner-circle';
final String RIPPLE_EFFECT = 'mdl-js-ripple-effect';
final String RIPPLE_IGNORE_EVENTS = 'mdl-js-ripple-effect--ignore-events';
final String RIPPLE_CONTAINER = 'mdl-radio__ripple-container';
final String RIPPLE_CENTER = 'mdl-ripple--center';
final String RIPPLE = 'mdl-ripple';
const _MaterialRadioCssClasses();
}
/// Store constants in one place so they can be updated easily.
class _MaterialRadioConstant {
final int TINY_TIMEOUT_IN_MS = 10;
const _MaterialRadioConstant();
}
/// creates MdlConfig for MaterialRadio
MdlConfig materialRadioConfig() => new MdlWidgetConfig<MaterialRadio>(
_MaterialRadioCssClasses.MAIN_CLASS, (final dom.HtmlElement element,final di.Injector injector)
=> new MaterialRadio.fromElement(element,injector));
/// registration-Helper
void registerMaterialRadio() => componentHandler().register(materialRadioConfig());
/// creates MdlConfig for MaterialRadio
MdlConfig materialRadioGroupConfig() => new MdlWidgetConfig<MaterialRadioGroup>(
_MaterialRadioCssClasses.GROUP_CLASS, (final dom.HtmlElement element,final di.Injector injector)
=> new MaterialRadioGroup.fromElement(element,injector));
/// registration-Helper
void registerMaterialRadioGroup() => componentHandler().register(materialRadioGroupConfig());
/**
* Sample:
* <label class="mdl-radio mdl-js-radio mdl-js-ripple-effect" for="wifi1">
* <input type="radio" id="wifi1" class="mdl-radio__button" name="wifi[]" value="1" checked />
* <span class="mdl-radio__label">Always</span>
* </label>
*/
class MaterialRadio extends MdlComponent {
final Logger _logger = new Logger('mdlcomponents.MaterialRadio');
static const _MaterialRadioConstant _constant = const _MaterialRadioConstant();
static const _MaterialRadioCssClasses _cssClasses = const _MaterialRadioCssClasses();
dom.RadioButtonInputElement _btnElement = null;
//factory MaterialRadio(final dom.HtmlElement element) => mdlComponent(element,MaterialRadio) as MaterialRadio;
MaterialRadio.fromElement(final dom.HtmlElement element,final di.Injector injector)
: super(element,injector) {
_init();
}
static MaterialRadio widget(final dom.HtmlElement element) => mdlComponent(element,MaterialRadio) as MaterialRadio;
dom.Element get hub => btnElement;
dom.RadioButtonInputElement get btnElement {
if(_btnElement == null) {
_btnElement = element.querySelector(".${_cssClasses.RADIO_BTN}");
}
return _btnElement;
}
/// Disable radio.
/// @public
/// MaterialRadio.prototype.disable = /*function*/ () {
void disable() {
btnElement.disabled = true;
_updateClasses(btnElement, element);
}
/// Enable radio.
/// @public
/// MaterialRadio.prototype.enable = /*function*/ () {
void enable() {
btnElement.disabled = false;
_updateClasses(btnElement, element);
}
/// Check radio.
/// @public
/// MaterialRadio.prototype.check = /*function*/ () {
void check() {
_uncheckSiblings();
btnElement.checked = true;
_updateClasses(btnElement, element);
}
/// Uncheck radio.
/// @public
/// MaterialRadio.prototype.uncheck = /*function*/ () {
void uncheck() {
btnElement.checked = false;
_updateClasses(btnElement, element);
}
bool get checked => btnElement.checked;
String get value => btnElement.value;
//- private -----------------------------------------------------------------------------------
void _init() {
_logger.fine("MaterialRadio - init");
if (element != null) {
final outerCircle = new dom.SpanElement();
outerCircle.classes.add(_cssClasses.RADIO_OUTER_CIRCLE);
final innerCircle = new dom.SpanElement();
innerCircle.classes.add(_cssClasses.RADIO_INNER_CIRCLE);
element.append(outerCircle);
element.append(innerCircle);
if (element.classes.contains(
_cssClasses.RIPPLE_EFFECT)) {
element.classes.add(
_cssClasses.RIPPLE_IGNORE_EVENTS);
final dom.SpanElement rippleContainer = new dom.SpanElement();
rippleContainer.classes.add(_cssClasses.RIPPLE_CONTAINER);
rippleContainer.classes.add(_cssClasses.RIPPLE_EFFECT);
rippleContainer.classes.add(_cssClasses.RIPPLE_CENTER);
rippleContainer.onMouseUp.listen( _onMouseUp );
final dom.SpanElement ripple = new dom.SpanElement();
ripple.classes.add(_cssClasses.RIPPLE);
rippleContainer.append(ripple);
element.append(rippleContainer);
}
btnElement.onChange.listen( _onChange );
btnElement.onFocus.listen( _onFocus );
btnElement.onBlur.listen( _onBlur );
element.onMouseUp.listen( _onMouseUp );
_updateClasses(btnElement, element);
element.classes.add(_cssClasses.IS_UPGRADED);
}
}
/// Handle change of state.
/// @param {Event} event The event that fired.
void _onChange(final dom.Event event) {
_updateClasses(_btnElement, element);
// Since other radio buttons don't get change events, we need to look for
// them to update their classes.
final List<dom.Element> radios = dom.querySelectorAll('.' + _cssClasses.JS_RADIO);
for (int i = 0; i < radios.length; i++) {
final dom.RadioButtonInputElement button = radios[i].querySelector('.' + _cssClasses.RADIO_BTN);
// Different name == different group, so no point updating those.
if (button.getAttribute('name') == _btnElement.getAttribute('name')) {
_updateClasses(button, radios[i]);
}
}
}
/// Handle focus.
void _onFocus(final dom.Event event) {
element.classes.add(_cssClasses.IS_FOCUSED);
}
/// Handle lost focus.
void _onBlur(final dom.Event event) {
element.classes.remove(_cssClasses.IS_FOCUSED);
}
/// Handle mouseup.
void _onMouseUp(final dom.MouseEvent event) {
_blur();
}
/// Update classes.
void _updateClasses(final dom.RadioButtonInputElement button, final dom.HtmlElement label) {
if (button.disabled) {
label.classes.add(_cssClasses.IS_DISABLED);
} else {
label.classes.remove(_cssClasses.IS_DISABLED);
}
if (button.checked) {
label.classes.add(_cssClasses.IS_CHECKED);
} else {
label.classes.remove(_cssClasses.IS_CHECKED);
}
}
void _blur() {
new Timer(new Duration(milliseconds : _constant.TINY_TIMEOUT_IN_MS ), () {
btnElement.blur();
});
}
/// Looks for the parent class .mdl-radio-group. If it finds the class
/// it iterates over its children and unchecks each mdl-radio child.
void _uncheckSiblings() {
if(element.parent.classes.contains(_MaterialRadioCssClasses.GROUP_CLASS)) {
final dom.HtmlElement group = element.parent;
group.children.forEach((final dom.Element child) {
final MaterialRadio widget = MaterialRadio.widget(child.querySelector(".${_cssClasses.RADIO_BTN}"));
if(widget != null && widget != this) {
widget.uncheck();
}
});
}
}
}
class MaterialRadioGroup extends MdlComponent {
final Logger _logger = new Logger('mdlcomponents.MaterialRadioGroup');
static const _MaterialRadioCssClasses _cssClasses = const _MaterialRadioCssClasses();
MaterialRadioGroup.fromElement(final dom.HtmlElement element,final di.Injector injector)
: super(element,injector) {
_init();
}
static MaterialRadioGroup widget(final dom.HtmlElement element) => mdlComponent(element,MaterialRadioGroup) as MaterialRadioGroup;
bool get hasValue {
bool _hasValue = false;
element.children.forEach((final dom.HtmlElement child) {
final MaterialRadio radio = MaterialRadio.widget(child.querySelector(".${_cssClasses.RADIO_BTN}"));
if(radio.checked) {
_hasValue = true;
}
});
return _hasValue;
}
String get value {
String _value = "";
element.children.forEach((final dom.HtmlElement child) {
final MaterialRadio radio = MaterialRadio.widget(child.querySelector(".${_cssClasses.RADIO_BTN}"));
if(radio.checked) {
_value = radio.value;
}
});
return _value;
}
//- private -----------------------------------------------------------------------------------
void _init() {
_logger.fine("MaterialRadioGroup - init");
if (element != null) {
element.classes.add(_cssClasses.IS_UPGRADED);
}
}
}