3 * Copyright 2015 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 * Class constructor for Radio MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
27 * @param {HTMLElement} element The element that will be upgraded.
29 var MaterialRadio = function MaterialRadio(element) {
30 this.element_ = element;
32 // Initialize instance.
35 window['MaterialRadio'] = MaterialRadio;
38 * Store constants in one place so they can be updated easily.
40 * @enum {string | number}
43 MaterialRadio.prototype.Constant_ = {
48 * Store strings for class names defined by this component that are used in
49 * JavaScript. This allows us to simply change it in one place should we
50 * decide to modify at a later date.
55 MaterialRadio.prototype.CssClasses_ = {
56 IS_FOCUSED: 'is-focused',
57 IS_DISABLED: 'is-disabled',
58 IS_CHECKED: 'is-checked',
59 IS_UPGRADED: 'is-upgraded',
60 JS_RADIO: 'mdl-js-radio',
61 RADIO_BTN: 'mdl-radio__button',
62 RADIO_OUTER_CIRCLE: 'mdl-radio__outer-circle',
63 RADIO_INNER_CIRCLE: 'mdl-radio__inner-circle',
64 RIPPLE_EFFECT: 'mdl-js-ripple-effect',
65 RIPPLE_IGNORE_EVENTS: 'mdl-js-ripple-effect--ignore-events',
66 RIPPLE_CONTAINER: 'mdl-radio__ripple-container',
67 RIPPLE_CENTER: 'mdl-ripple--center',
72 * Handle change of state.
74 * @param {Event} event The event that fired.
77 MaterialRadio.prototype.onChange_ = function(event) {
78 // Since other radio buttons don't get change events, we need to look for
79 // them to update their classes.
80 var radios = document.getElementsByClassName(this.CssClasses_.JS_RADIO);
81 for (var i = 0; i < radios.length; i++) {
82 var button = radios[i].querySelector('.' + this.CssClasses_.RADIO_BTN);
83 // Different name == different group, so no point updating those.
84 if (button.getAttribute('name') === this.btnElement_.getAttribute('name')) {
85 if (typeof radios[i]['MaterialRadio'] !== 'undefined') {
86 radios[i]['MaterialRadio'].updateClasses_();
95 * @param {Event} event The event that fired.
98 MaterialRadio.prototype.onFocus_ = function(event) {
99 this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
105 * @param {Event} event The event that fired.
108 MaterialRadio.prototype.onBlur_ = function(event) {
109 this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
115 * @param {Event} event The event that fired.
118 MaterialRadio.prototype.onMouseup_ = function(event) {
127 MaterialRadio.prototype.updateClasses_ = function() {
128 this.checkDisabled();
129 this.checkToggleState();
137 MaterialRadio.prototype.blur_ = function() {
139 // TODO: figure out why there's a focus event being fired after our blur,
140 // so that we can avoid this hack.
141 window.setTimeout(function() {
142 this.btnElement_.blur();
143 }.bind(this), /** @type {number} */ (this.Constant_.TINY_TIMEOUT));
149 * Check the components disabled state.
153 MaterialRadio.prototype.checkDisabled = function() {
154 if (this.btnElement_.disabled) {
155 this.element_.classList.add(this.CssClasses_.IS_DISABLED);
157 this.element_.classList.remove(this.CssClasses_.IS_DISABLED);
160 MaterialRadio.prototype['checkDisabled'] =
161 MaterialRadio.prototype.checkDisabled;
164 * Check the components toggled state.
168 MaterialRadio.prototype.checkToggleState = function() {
169 if (this.btnElement_.checked) {
170 this.element_.classList.add(this.CssClasses_.IS_CHECKED);
172 this.element_.classList.remove(this.CssClasses_.IS_CHECKED);
175 MaterialRadio.prototype['checkToggleState'] =
176 MaterialRadio.prototype.checkToggleState;
183 MaterialRadio.prototype.disable = function() {
184 this.btnElement_.disabled = true;
185 this.updateClasses_();
187 MaterialRadio.prototype['disable'] = MaterialRadio.prototype.disable;
194 MaterialRadio.prototype.enable = function() {
195 this.btnElement_.disabled = false;
196 this.updateClasses_();
198 MaterialRadio.prototype['enable'] = MaterialRadio.prototype.enable;
205 MaterialRadio.prototype.check = function() {
206 this.btnElement_.checked = true;
207 this.onChange_(null);
209 MaterialRadio.prototype['check'] = MaterialRadio.prototype.check;
216 MaterialRadio.prototype.uncheck = function() {
217 this.btnElement_.checked = false;
218 this.onChange_(null);
220 MaterialRadio.prototype['uncheck'] = MaterialRadio.prototype.uncheck;
223 * Initialize element.
225 MaterialRadio.prototype.init = function() {
227 this.btnElement_ = this.element_.querySelector('.' +
228 this.CssClasses_.RADIO_BTN);
230 this.boundChangeHandler_ = this.onChange_.bind(this);
231 this.boundFocusHandler_ = this.onChange_.bind(this);
232 this.boundBlurHandler_ = this.onBlur_.bind(this);
233 this.boundMouseUpHandler_ = this.onMouseup_.bind(this);
235 var outerCircle = document.createElement('span');
236 outerCircle.classList.add(this.CssClasses_.RADIO_OUTER_CIRCLE);
238 var innerCircle = document.createElement('span');
239 innerCircle.classList.add(this.CssClasses_.RADIO_INNER_CIRCLE);
241 this.element_.appendChild(outerCircle);
242 this.element_.appendChild(innerCircle);
245 if (this.element_.classList.contains(
246 this.CssClasses_.RIPPLE_EFFECT)) {
247 this.element_.classList.add(
248 this.CssClasses_.RIPPLE_IGNORE_EVENTS);
249 rippleContainer = document.createElement('span');
250 rippleContainer.classList.add(
251 this.CssClasses_.RIPPLE_CONTAINER);
252 rippleContainer.classList.add(this.CssClasses_.RIPPLE_EFFECT);
253 rippleContainer.classList.add(this.CssClasses_.RIPPLE_CENTER);
254 rippleContainer.addEventListener('mouseup', this.boundMouseUpHandler_);
256 var ripple = document.createElement('span');
257 ripple.classList.add(this.CssClasses_.RIPPLE);
259 rippleContainer.appendChild(ripple);
260 this.element_.appendChild(rippleContainer);
263 this.btnElement_.addEventListener('change', this.boundChangeHandler_);
264 this.btnElement_.addEventListener('focus', this.boundFocusHandler_);
265 this.btnElement_.addEventListener('blur', this.boundBlurHandler_);
266 this.element_.addEventListener('mouseup', this.boundMouseUpHandler_);
268 this.updateClasses_();
269 this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
273 // The component registers itself. It can assume componentHandler is available
274 // in the global scope.
275 componentHandler.register({
276 constructor: MaterialRadio,
277 classAsString: 'MaterialRadio',
278 cssClass: 'mdl-js-radio',