Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / third_party / mdl / repo / src / radio / radio.js
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         if (typeof radios[i]['MaterialRadio'] !== 'undefined') {
86           radios[i]['MaterialRadio'].updateClasses_();
87         }
88       }
89     }
90   };
91
92   /**
93    * Handle focus.
94    *
95    * @param {Event} event The event that fired.
96    * @private
97    */
98   MaterialRadio.prototype.onFocus_ = function(event) {
99     this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
100   };
101
102   /**
103    * Handle lost focus.
104    *
105    * @param {Event} event The event that fired.
106    * @private
107    */
108   MaterialRadio.prototype.onBlur_ = function(event) {
109     this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
110   };
111
112   /**
113    * Handle mouseup.
114    *
115    * @param {Event} event The event that fired.
116    * @private
117    */
118   MaterialRadio.prototype.onMouseup_ = function(event) {
119     this.blur_();
120   };
121
122   /**
123    * Update classes.
124    *
125    * @private
126    */
127   MaterialRadio.prototype.updateClasses_ = function() {
128     this.checkDisabled();
129     this.checkToggleState();
130   };
131
132   /**
133    * Add blur.
134    *
135    * @private
136    */
137   MaterialRadio.prototype.blur_ = function() {
138
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));
144   };
145
146   // Public methods.
147
148   /**
149    * Check the components disabled state.
150    *
151    * @public
152    */
153   MaterialRadio.prototype.checkDisabled = function() {
154     if (this.btnElement_.disabled) {
155       this.element_.classList.add(this.CssClasses_.IS_DISABLED);
156     } else {
157       this.element_.classList.remove(this.CssClasses_.IS_DISABLED);
158     }
159   };
160   MaterialRadio.prototype['checkDisabled'] =
161       MaterialRadio.prototype.checkDisabled;
162
163   /**
164    * Check the components toggled state.
165    *
166    * @public
167    */
168   MaterialRadio.prototype.checkToggleState = function() {
169     if (this.btnElement_.checked) {
170       this.element_.classList.add(this.CssClasses_.IS_CHECKED);
171     } else {
172       this.element_.classList.remove(this.CssClasses_.IS_CHECKED);
173     }
174   };
175   MaterialRadio.prototype['checkToggleState'] =
176       MaterialRadio.prototype.checkToggleState;
177
178   /**
179    * Disable radio.
180    *
181    * @public
182    */
183   MaterialRadio.prototype.disable = function() {
184     this.btnElement_.disabled = true;
185     this.updateClasses_();
186   };
187   MaterialRadio.prototype['disable'] = MaterialRadio.prototype.disable;
188
189   /**
190    * Enable radio.
191    *
192    * @public
193    */
194   MaterialRadio.prototype.enable = function() {
195     this.btnElement_.disabled = false;
196     this.updateClasses_();
197   };
198   MaterialRadio.prototype['enable'] = MaterialRadio.prototype.enable;
199
200   /**
201    * Check radio.
202    *
203    * @public
204    */
205   MaterialRadio.prototype.check = function() {
206     this.btnElement_.checked = true;
207     this.onChange_(null);
208   };
209   MaterialRadio.prototype['check'] = MaterialRadio.prototype.check;
210
211   /**
212    * Uncheck radio.
213    *
214    * @public
215    */
216   MaterialRadio.prototype.uncheck = function() {
217     this.btnElement_.checked = false;
218     this.onChange_(null);
219   };
220   MaterialRadio.prototype['uncheck'] = MaterialRadio.prototype.uncheck;
221
222   /**
223    * Initialize element.
224    */
225   MaterialRadio.prototype.init = function() {
226     if (this.element_) {
227       this.btnElement_ = this.element_.querySelector('.' +
228           this.CssClasses_.RADIO_BTN);
229
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);
234
235       var outerCircle = document.createElement('span');
236       outerCircle.classList.add(this.CssClasses_.RADIO_OUTER_CIRCLE);
237
238       var innerCircle = document.createElement('span');
239       innerCircle.classList.add(this.CssClasses_.RADIO_INNER_CIRCLE);
240
241       this.element_.appendChild(outerCircle);
242       this.element_.appendChild(innerCircle);
243
244       var rippleContainer;
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_);
255
256         var ripple = document.createElement('span');
257         ripple.classList.add(this.CssClasses_.RIPPLE);
258
259         rippleContainer.appendChild(ripple);
260         this.element_.appendChild(rippleContainer);
261       }
262
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_);
267
268       this.updateClasses_();
269       this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
270     }
271   };
272
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',
279     widget: true
280   });
281 })();