This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
radio-button.js
395 lines (353 loc) · 12.6 KB
/
radio-button.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* @ngdoc module
* @name material.components.radioButton
* @description radioButton module!
*/
angular.module('material.components.radioButton', [
'material.core'
])
.directive('mdRadioGroup', mdRadioGroupDirective)
.directive('mdRadioButton', mdRadioButtonDirective);
/**
* @type {Readonly<{NEXT: number, CURRENT: number, PREVIOUS: number}>}
*/
var incrementSelection = Object.freeze({PREVIOUS: -1, CURRENT: 0, NEXT: 1});
/**
* @ngdoc directive
* @module material.components.radioButton
* @name mdRadioGroup
*
* @restrict E
*
* @description
* The `<md-radio-group>` directive identifies a grouping
* container for the 1..n grouped radio buttons; specified using nested
* `<md-radio-button>` elements.
*
* The radio button uses the accent color by default. The primary color palette may be used with
* the `md-primary` class.
*
* Note: `<md-radio-group>` and `<md-radio-button>` handle `tabindex` differently
* than the native `<input type="radio">` controls. Whereas the native controls
* force the user to tab through all the radio buttons, `<md-radio-group>`
* is focusable and by default the `<md-radio-button>`s are not.
*
* @param {string} ng-model Assignable angular expression to data-bind to.
* @param {string=} ng-change AngularJS expression to be executed when input changes due to user
* interaction.
* @param {boolean=} md-no-ink If present, disables ink ripple effects.
*
* @usage
* <hljs lang="html">
* <md-radio-group ng-model="selected">
* <md-radio-button ng-repeat="item in items"
* ng-value="item.value" aria-label="{{item.label}}">
* {{ item.label }}
* </md-radio-button>
* </md-radio-group>
* </hljs>
*/
function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming, $timeout) {
RadioGroupController.prototype = createRadioGroupControllerProto();
return {
restrict: 'E',
controller: ['$element', RadioGroupController],
require: ['mdRadioGroup', '?ngModel'],
link: { pre: linkRadioGroup }
};
function linkRadioGroup(scope, element, attr, controllers) {
// private md component indicator for styling
element.addClass('_md');
$mdTheming(element);
var radioGroupController = controllers[0];
var ngModelCtrl = controllers[1] || $mdUtil.fakeNgModel();
radioGroupController.init(ngModelCtrl);
scope.mouseActive = false;
element
.attr({
'role': 'radiogroup',
'tabIndex': element.attr('tabindex') || '0'
})
.on('keydown', keydownListener)
.on('mousedown', function() {
scope.mouseActive = true;
$timeout(function() {
scope.mouseActive = false;
}, 100);
})
.on('focus', function() {
if (scope.mouseActive === false) {
radioGroupController.$element.addClass('md-focused');
}
})
.on('blur', function() {
radioGroupController.$element.removeClass('md-focused');
});
// Initially set the first radio button as the aria-activedescendant. This will be overridden
// if a 'checked' radio button gets rendered. We need to wait for the nextTick here so that the
// radio buttons have their id values assigned.
$mdUtil.nextTick(function () {
var radioButtons = getRadioButtons(radioGroupController.$element);
if (radioButtons.count() &&
!radioGroupController.$element[0].hasAttribute('aria-activedescendant')) {
radioGroupController.setActiveDescendant(radioButtons.first().id);
}
});
/**
* Apply the md-focused class if it isn't already applied.
*/
function setFocus() {
if (!element.hasClass('md-focused')) { element.addClass('md-focused'); }
}
/**
* @param {KeyboardEvent} keyboardEvent
*/
function keydownListener(keyboardEvent) {
var keyCode = keyboardEvent.which || keyboardEvent.keyCode;
// Only listen to events that we originated ourselves
// so that we don't trigger on things like arrow keys in inputs.
if (keyCode !== $mdConstant.KEY_CODE.ENTER &&
keyboardEvent.currentTarget !== keyboardEvent.target) {
return;
}
switch (keyCode) {
case $mdConstant.KEY_CODE.LEFT_ARROW:
case $mdConstant.KEY_CODE.UP_ARROW:
keyboardEvent.preventDefault();
radioGroupController.selectPrevious();
setFocus();
break;
case $mdConstant.KEY_CODE.RIGHT_ARROW:
case $mdConstant.KEY_CODE.DOWN_ARROW:
keyboardEvent.preventDefault();
radioGroupController.selectNext();
setFocus();
break;
case $mdConstant.KEY_CODE.SPACE:
keyboardEvent.preventDefault();
radioGroupController.selectCurrent();
break;
case $mdConstant.KEY_CODE.ENTER:
var form = angular.element($mdUtil.getClosest(element[0], 'form'));
if (form.length > 0) {
form.triggerHandler('submit');
}
break;
}
}
}
/**
* @param {JQLite} $element
* @constructor
*/
function RadioGroupController($element) {
this._radioButtonRenderFns = [];
this.$element = $element;
}
function createRadioGroupControllerProto() {
return {
init: function(ngModelCtrl) {
this._ngModelCtrl = ngModelCtrl;
this._ngModelCtrl.$render = angular.bind(this, this.render);
},
add: function(rbRender) {
this._radioButtonRenderFns.push(rbRender);
},
remove: function(rbRender) {
var index = this._radioButtonRenderFns.indexOf(rbRender);
if (index !== -1) {
this._radioButtonRenderFns.splice(index, 1);
}
},
render: function() {
this._radioButtonRenderFns.forEach(function(rbRender) {
rbRender();
});
},
setViewValue: function(value, eventType) {
this._ngModelCtrl.$setViewValue(value, eventType);
// update the other radio buttons as well
this.render();
},
getViewValue: function() {
return this._ngModelCtrl.$viewValue;
},
selectCurrent: function() {
return changeSelectedButton(this.$element, incrementSelection.CURRENT);
},
selectNext: function() {
return changeSelectedButton(this.$element, incrementSelection.NEXT);
},
selectPrevious: function() {
return changeSelectedButton(this.$element, incrementSelection.PREVIOUS);
},
setActiveDescendant: function (radioId) {
this.$element.attr('aria-activedescendant', radioId);
},
isDisabled: function() {
return this.$element[0].hasAttribute('disabled');
}
};
}
/**
* Coerce all child radio buttons into an array, then wrap them in an iterator.
* @param parent {!JQLite}
* @return {{add: add, next: (function()), last: (function(): any|null), previous: (function()), count: (function(): number), hasNext: (function(*=): Array.length|*|number|boolean), inRange: (function(*): boolean), remove: remove, contains: (function(*=): *|boolean), itemAt: (function(*=): any|null), findBy: (function(*, *): *[]), hasPrevious: (function(*=): Array.length|*|number|boolean), items: (function(): *[]), indexOf: (function(*=): number), first: (function(): any|null)}}
*/
function getRadioButtons(parent) {
return $mdUtil.iterator(parent[0].querySelectorAll('md-radio-button'), true);
}
/**
* Change the radio group's selected button by a given increment.
* If no button is selected, select the first button.
* @param {JQLite} parent the md-radio-group
* @param {incrementSelection} increment enum that determines whether the next or
* previous button is clicked. For current, only the first button is selected, otherwise the
* current selection is maintained (by doing nothing).
*/
function changeSelectedButton(parent, increment) {
var buttons = getRadioButtons(parent);
var target;
if (buttons.count()) {
var validate = function (button) {
// If disabled, then NOT valid
return !angular.element(button).attr("disabled");
};
var selected = parent[0].querySelector('md-radio-button.md-checked');
if (!selected) {
target = buttons.first();
} else if (increment === incrementSelection.PREVIOUS ||
increment === incrementSelection.NEXT) {
target = buttons[
increment === incrementSelection.PREVIOUS ? 'previous' : 'next'
](selected, validate);
}
if (target) {
// Activate radioButton's click listener (triggerHandler won't create a real click event)
angular.element(target).triggerHandler('click');
}
}
}
}
/**
* @ngdoc directive
* @module material.components.radioButton
* @name mdRadioButton
*
* @restrict E
*
* @description
* The `<md-radio-button>`directive is the child directive required to be used within `<md-radio-group>` elements.
*
* While similar to the `<input type="radio" ng-model="" value="">` directive,
* the `<md-radio-button>` directive provides ink effects, ARIA support, and
* supports use within named radio groups.
*
* One of `value` or `ng-value` must be set so that the `md-radio-group`'s model is set properly when the
* `md-radio-button` is selected.
*
* @param {string} value The value to which the model should be set when selected.
* @param {string} ng-value AngularJS expression which sets the value to which the model should
* be set when selected.
* @param {string=} name Property name of the form under which the control is published.
* @param {string=} aria-label Adds label to radio button for accessibility.
* Defaults to radio button's text. If no text content is available, a warning will be logged.
*
* @usage
* <hljs lang="html">
*
* <md-radio-button value="1" aria-label="Label 1">
* Label 1
* </md-radio-button>
*
* <md-radio-button ng-value="specialValue" aria-label="Green">
* Green
* </md-radio-button>
*
* </hljs>
*
*/
function mdRadioButtonDirective($mdAria, $mdUtil, $mdTheming) {
var CHECKED_CSS = 'md-checked';
return {
restrict: 'E',
require: '^mdRadioGroup',
transclude: true,
template: '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
'<div class="md-off"></div>' +
'<div class="md-on"></div>' +
'</div>' +
'<div ng-transclude class="md-label"></div>',
link: link
};
function link(scope, element, attr, radioGroupController) {
var lastChecked;
$mdTheming(element);
configureAria(element);
element.addClass('md-auto-horizontal-margin');
// ngAria overwrites the aria-checked inside a $watch for ngValue.
// We should defer the initialization until all the watches have fired.
// This can also be fixed by removing the `lastChecked` check, but that'll
// cause more DOM manipulation on each digest.
if (attr.ngValue) {
$mdUtil.nextTick(initialize, false);
} else {
initialize();
}
/**
* Initializes the component.
*/
function initialize() {
if (!radioGroupController) {
throw 'RadioButton: No RadioGroupController could be found.';
}
radioGroupController.add(render);
attr.$observe('value', render);
element
.on('click', listener)
.on('$destroy', function() {
radioGroupController.remove(render);
});
}
/**
* On click functionality.
*/
function listener(ev) {
if (element[0].hasAttribute('disabled') || radioGroupController.isDisabled()) return;
scope.$apply(function() {
radioGroupController.setViewValue(attr.value, ev && ev.type);
});
}
/**
* Add or remove the `.md-checked` class from the RadioButton (and conditionally its parent).
* Update the `aria-activedescendant` attribute.
*/
function render() {
var checked = radioGroupController.getViewValue() == attr.value;
if (checked === lastChecked) return;
if (element[0] && element[0].parentNode &&
element[0].parentNode.nodeName.toLowerCase() !== 'md-radio-group') {
// If the radioButton is inside a div, then add class so highlighting will work.
element.parent().toggleClass(CHECKED_CSS, checked);
}
if (checked) {
radioGroupController.setActiveDescendant(element.attr('id'));
}
lastChecked = checked;
element
.attr('aria-checked', checked)
.toggleClass(CHECKED_CSS, checked);
}
/**
* Inject ARIA-specific attributes appropriate for each radio button
*/
function configureAria(element) {
element.attr({
id: attr.id || 'radio_' + $mdUtil.nextUid(),
role: 'radio',
'aria-checked': 'false'
});
$mdAria.expectWithText(element, 'aria-label');
}
}
}