UNPKG

8.04 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2015 Google Inc. All Rights Reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18(function() {
19 'use strict';
20
21 /**
22 * Class constructor for Radio MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
25 *
26 * @constructor
27 * @param {HTMLElement} element The element that will be upgraded.
28 */
29 var MaterialRadio = function MaterialRadio(element) {
30 this.element_ = element;
31
32 // Initialize instance.
33 this.init();
34 };
35 window['MaterialRadio'] = MaterialRadio;
36
37 /**
38 * Store constants in one place so they can be updated easily.
39 *
40 * @enum {string | number}
41 * @private
42 */
43 MaterialRadio.prototype.Constant_ = {
44 TINY_TIMEOUT: 0.001
45 };
46
47 /**
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.
51 *
52 * @enum {string}
53 * @private
54 */
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',
68 RIPPLE: 'mdl-ripple'
69 };
70
71 /**
72 * Handle change of state.
73 *
74 * @param {Event} event The event that fired.
75 * @private
76 */
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 radios[i]['MaterialRadio'].updateClasses_();
86 }
87 }
88 };
89
90 /**
91 * Handle focus.
92 *
93 * @param {Event} event The event that fired.
94 * @private
95 */
96 MaterialRadio.prototype.onFocus_ = function(event) {
97 this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
98 };
99
100 /**
101 * Handle lost focus.
102 *
103 * @param {Event} event The event that fired.
104 * @private
105 */
106 MaterialRadio.prototype.onBlur_ = function(event) {
107 this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
108 };
109
110 /**
111 * Handle mouseup.
112 *
113 * @param {Event} event The event that fired.
114 * @private
115 */
116 MaterialRadio.prototype.onMouseup_ = function(event) {
117 this.blur_();
118 };
119
120 /**
121 * Update classes.
122 *
123 * @private
124 */
125 MaterialRadio.prototype.updateClasses_ = function() {
126 this.checkDisabled();
127 this.checkToggleState();
128 };
129
130 /**
131 * Add blur.
132 *
133 * @private
134 */
135 MaterialRadio.prototype.blur_ = function() {
136
137 // TODO: figure out why there's a focus event being fired after our blur,
138 // so that we can avoid this hack.
139 window.setTimeout(function() {
140 this.btnElement_.blur();
141 }.bind(this), /** @type {number} */ (this.Constant_.TINY_TIMEOUT));
142 };
143
144 // Public methods.
145
146 /**
147 * Check the components disabled state.
148 *
149 * @public
150 */
151 MaterialRadio.prototype.checkDisabled = function() {
152 if (this.btnElement_.disabled) {
153 this.element_.classList.add(this.CssClasses_.IS_DISABLED);
154 } else {
155 this.element_.classList.remove(this.CssClasses_.IS_DISABLED);
156 }
157 };
158 MaterialRadio.prototype['checkDisabled'] =
159 MaterialRadio.prototype.checkDisabled;
160
161 /**
162 * Check the components toggled state.
163 *
164 * @public
165 */
166 MaterialRadio.prototype.checkToggleState = function() {
167 if (this.btnElement_.checked) {
168 this.element_.classList.add(this.CssClasses_.IS_CHECKED);
169 } else {
170 this.element_.classList.remove(this.CssClasses_.IS_CHECKED);
171 }
172 };
173 MaterialRadio.prototype['checkToggleState'] =
174 MaterialRadio.prototype.checkToggleState;
175
176 /**
177 * Disable radio.
178 *
179 * @public
180 */
181 MaterialRadio.prototype.disable = function() {
182 this.btnElement_.disabled = true;
183 this.updateClasses_();
184 };
185 MaterialRadio.prototype['disable'] = MaterialRadio.prototype.disable;
186
187 /**
188 * Enable radio.
189 *
190 * @public
191 */
192 MaterialRadio.prototype.enable = function() {
193 this.btnElement_.disabled = false;
194 this.updateClasses_();
195 };
196 MaterialRadio.prototype['enable'] = MaterialRadio.prototype.enable;
197
198 /**
199 * Check radio.
200 *
201 * @public
202 */
203 MaterialRadio.prototype.check = function() {
204 this.btnElement_.checked = true;
205 this.updateClasses_();
206 };
207 MaterialRadio.prototype['check'] = MaterialRadio.prototype.check;
208
209 /**
210 * Uncheck radio.
211 *
212 * @public
213 */
214 MaterialRadio.prototype.uncheck = function() {
215 this.btnElement_.checked = false;
216 this.updateClasses_();
217 };
218 MaterialRadio.prototype['uncheck'] = MaterialRadio.prototype.uncheck;
219
220 /**
221 * Initialize element.
222 */
223 MaterialRadio.prototype.init = function() {
224 if (this.element_) {
225 this.btnElement_ = this.element_.querySelector('.' +
226 this.CssClasses_.RADIO_BTN);
227
228 this.boundChangeHandler_ = this.onChange_.bind(this);
229 this.boundFocusHandler_ = this.onChange_.bind(this);
230 this.boundBlurHandler_ = this.onBlur_.bind(this);
231 this.boundMouseUpHandler_ = this.onMouseup_.bind(this);
232
233 var outerCircle = document.createElement('span');
234 outerCircle.classList.add(this.CssClasses_.RADIO_OUTER_CIRCLE);
235
236 var innerCircle = document.createElement('span');
237 innerCircle.classList.add(this.CssClasses_.RADIO_INNER_CIRCLE);
238
239 this.element_.appendChild(outerCircle);
240 this.element_.appendChild(innerCircle);
241
242 var rippleContainer;
243 if (this.element_.classList.contains(
244 this.CssClasses_.RIPPLE_EFFECT)) {
245 this.element_.classList.add(
246 this.CssClasses_.RIPPLE_IGNORE_EVENTS);
247 rippleContainer = document.createElement('span');
248 rippleContainer.classList.add(
249 this.CssClasses_.RIPPLE_CONTAINER);
250 rippleContainer.classList.add(this.CssClasses_.RIPPLE_EFFECT);
251 rippleContainer.classList.add(this.CssClasses_.RIPPLE_CENTER);
252 rippleContainer.addEventListener('mouseup', this.boundMouseUpHandler_);
253
254 var ripple = document.createElement('span');
255 ripple.classList.add(this.CssClasses_.RIPPLE);
256
257 rippleContainer.appendChild(ripple);
258 this.element_.appendChild(rippleContainer);
259 }
260
261 this.btnElement_.addEventListener('change', this.boundChangeHandler_);
262 this.btnElement_.addEventListener('focus', this.boundFocusHandler_);
263 this.btnElement_.addEventListener('blur', this.boundBlurHandler_);
264 this.element_.addEventListener('mouseup', this.boundMouseUpHandler_);
265
266 this.updateClasses_();
267 this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
268 }
269 };
270
271 // The component registers itself. It can assume componentHandler is available
272 // in the global scope.
273 componentHandler.register({
274 constructor: MaterialRadio,
275 classAsString: 'MaterialRadio',
276 cssClass: 'mdl-js-radio',
277 widget: true
278 });
279})();