2 * Angular Material Design
3 * https://github.com/angular/material
7 (function( window, angular, undefined ){
12 * @name material.components.radioButton
13 * @description radioButton module!
15 mdRadioGroupDirective['$inject'] = ["$mdUtil", "$mdConstant", "$mdTheming", "$timeout"];
16 mdRadioButtonDirective['$inject'] = ["$mdAria", "$mdUtil", "$mdTheming"];
17 angular.module('material.components.radioButton', [
20 .directive('mdRadioGroup', mdRadioGroupDirective)
21 .directive('mdRadioButton', mdRadioButtonDirective);
25 * @module material.components.radioButton
31 * The `<md-radio-group>` directive identifies a grouping
32 * container for the 1..n grouped radio buttons; specified using nested
33 * `<md-radio-button>` tags.
35 * As per the [material design spec](http://www.google.com/design/spec/style/color.html#color-ui-color-application)
36 * the radio button is in the accent color by default. The primary color palette may be used with
37 * the `md-primary` class.
39 * Note: `<md-radio-group>` and `<md-radio-button>` handle tabindex differently
40 * than the native `<input type='radio'>` controls. Whereas the native controls
41 * force the user to tab through all the radio buttons, `<md-radio-group>`
42 * is focusable, and by default the `<md-radio-button>`s are not.
44 * @param {string} ng-model Assignable angular expression to data-bind to.
45 * @param {boolean=} md-no-ink Use of attribute indicates flag to disable ink ripple effects.
49 * <md-radio-group ng-model="selected">
52 * ng-repeat="d in colorOptions"
53 * ng-value="d.value" aria-label="{{ d.label }}">
63 function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming, $timeout) {
64 RadioGroupController.prototype = createRadioGroupControllerProto();
68 controller: ['$element', RadioGroupController],
69 require: ['mdRadioGroup', '?ngModel'],
70 link: { pre: linkRadioGroup }
73 function linkRadioGroup(scope, element, attr, ctrls) {
74 element.addClass('_md'); // private md component indicator for styling
77 var rgCtrl = ctrls[0];
78 var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
80 rgCtrl.init(ngModelCtrl);
82 scope.mouseActive = false;
87 'tabIndex': element.attr('tabindex') || '0'
89 .on('keydown', keydownListener)
90 .on('mousedown', function(event) {
91 scope.mouseActive = true;
93 scope.mouseActive = false;
96 .on('focus', function() {
97 if(scope.mouseActive === false) {
98 rgCtrl.$element.addClass('md-focused');
101 .on('blur', function() {
102 rgCtrl.$element.removeClass('md-focused');
108 function setFocus() {
109 if (!element.hasClass('md-focused')) { element.addClass('md-focused'); }
115 function keydownListener(ev) {
116 var keyCode = ev.which || ev.keyCode;
118 // Only listen to events that we originated ourselves
119 // so that we don't trigger on things like arrow keys in
122 if (keyCode != $mdConstant.KEY_CODE.ENTER &&
123 ev.currentTarget != ev.target) {
128 case $mdConstant.KEY_CODE.LEFT_ARROW:
129 case $mdConstant.KEY_CODE.UP_ARROW:
131 rgCtrl.selectPrevious();
135 case $mdConstant.KEY_CODE.RIGHT_ARROW:
136 case $mdConstant.KEY_CODE.DOWN_ARROW:
142 case $mdConstant.KEY_CODE.ENTER:
143 var form = angular.element($mdUtil.getClosest(element[0], 'form'));
144 if (form.length > 0) {
145 form.triggerHandler('submit');
153 function RadioGroupController($element) {
154 this._radioButtonRenderFns = [];
155 this.$element = $element;
158 function createRadioGroupControllerProto() {
160 init: function(ngModelCtrl) {
161 this._ngModelCtrl = ngModelCtrl;
162 this._ngModelCtrl.$render = angular.bind(this, this.render);
164 add: function(rbRender) {
165 this._radioButtonRenderFns.push(rbRender);
167 remove: function(rbRender) {
168 var index = this._radioButtonRenderFns.indexOf(rbRender);
170 this._radioButtonRenderFns.splice(index, 1);
174 this._radioButtonRenderFns.forEach(function(rbRender) {
178 setViewValue: function(value, eventType) {
179 this._ngModelCtrl.$setViewValue(value, eventType);
180 // update the other radio buttons as well
183 getViewValue: function() {
184 return this._ngModelCtrl.$viewValue;
186 selectNext: function() {
187 return changeSelectedButton(this.$element, 1);
189 selectPrevious: function() {
190 return changeSelectedButton(this.$element, -1);
192 setActiveDescendant: function (radioId) {
193 this.$element.attr('aria-activedescendant', radioId);
195 isDisabled: function() {
196 return this.$element[0].hasAttribute('disabled');
201 * Change the radio group's selected button by a given increment.
202 * If no button is selected, select the first button.
204 function changeSelectedButton(parent, increment) {
205 // Coerce all child radio buttons into an array, then wrap then in an iterator
206 var buttons = $mdUtil.iterator(parent[0].querySelectorAll('md-radio-button'), true);
208 if (buttons.count()) {
209 var validate = function (button) {
210 // If disabled, then NOT valid
211 return !angular.element(button).attr("disabled");
214 var selected = parent[0].querySelector('md-radio-button.md-checked');
215 var target = buttons[increment < 0 ? 'previous' : 'next'](selected, validate) || buttons.first();
217 // Activate radioButton's click listener (triggerHandler won't create a real click event)
218 angular.element(target).triggerHandler('click');
226 * @module material.components.radioButton
227 * @name mdRadioButton
232 * The `<md-radio-button>`directive is the child directive required to be used within `<md-radio-group>` elements.
234 * While similar to the `<input type="radio" ng-model="" value="">` directive,
235 * the `<md-radio-button>` directive provides ink effects, ARIA support, and
236 * supports use within named radio groups.
238 * @param {string} ngModel Assignable angular expression to data-bind to.
239 * @param {string=} ngChange Angular expression to be executed when input changes due to user
240 * interaction with the input element.
241 * @param {string} ngValue Angular expression which sets the value to which the expression should
242 * be set when selected.
243 * @param {string} value The value to which the expression should be set when selected.
244 * @param {string=} name Property name of the form under which the control is published.
245 * @param {string=} aria-label Adds label to radio button for accessibility.
246 * Defaults to radio button's text. If no text content is available, a warning will be logged.
251 * <md-radio-button value="1" aria-label="Label 1">
255 * <md-radio-button ng-model="color" ng-value="specialValue" aria-label="Green">
262 function mdRadioButtonDirective($mdAria, $mdUtil, $mdTheming) {
264 var CHECKED_CSS = 'md-checked';
268 require: '^mdRadioGroup',
270 template: '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
271 '<div class="md-off"></div>' +
272 '<div class="md-on"></div>' +
274 '<div ng-transclude class="md-label"></div>',
278 function link(scope, element, attr, rgCtrl) {
282 configureAria(element, scope);
284 // ngAria overwrites the aria-checked inside a $watch for ngValue.
285 // We should defer the initialization until all the watches have fired.
286 // This can also be fixed by removing the `lastChecked` check, but that'll
287 // cause more DOM manipulation on each digest.
289 $mdUtil.nextTick(initialize, false);
295 * Initializes the component.
297 function initialize() {
299 throw 'RadioButton: No RadioGroupController could be found.';
303 attr.$observe('value', render);
306 .on('click', listener)
307 .on('$destroy', function() {
308 rgCtrl.remove(render);
313 * On click functionality.
315 function listener(ev) {
316 if (element[0].hasAttribute('disabled') || rgCtrl.isDisabled()) return;
318 scope.$apply(function() {
319 rgCtrl.setViewValue(attr.value, ev && ev.type);
324 * Add or remove the `.md-checked` class from the RadioButton (and conditionally its parent).
325 * Update the `aria-activedescendant` attribute.
328 var checked = rgCtrl.getViewValue() == attr.value;
330 if (checked === lastChecked) return;
332 if (element[0].parentNode.nodeName.toLowerCase() !== 'md-radio-group') {
333 // If the radioButton is inside a div, then add class so highlighting will work
334 element.parent().toggleClass(CHECKED_CSS, checked);
338 rgCtrl.setActiveDescendant(element.attr('id'));
341 lastChecked = checked;
344 .attr('aria-checked', checked)
345 .toggleClass(CHECKED_CSS, checked);
349 * Inject ARIA-specific attributes appropriate for each radio button
351 function configureAria(element, scope){
353 id: attr.id || 'radio_' + $mdUtil.nextUid(),
355 'aria-checked': 'false'
358 $mdAria.expectWithText(element, 'aria-label');
363 })(window, window.angular);