blob: e71a591eda5504d9b29bf2cf1b64dd7969650ebb [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001/**
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 Checkbox 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 MaterialSwitch = function MaterialSwitch(element) {
30 this.element_ = element;
31
32 // Initialize instance.
33 this.init();
34 };
35 window['MaterialSwitch'] = MaterialSwitch;
36
37 /**
38 * Store constants in one place so they can be updated easily.
39 *
40 * @enum {string | number}
41 * @private
42 */
43 MaterialSwitch.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 MaterialSwitch.prototype.CssClasses_ = {
56 INPUT: 'mdl-switch__input',
57 TRACK: 'mdl-switch__track',
58 THUMB: 'mdl-switch__thumb',
59 FOCUS_HELPER: 'mdl-switch__focus-helper',
60 RIPPLE_EFFECT: 'mdl-js-ripple-effect',
61 RIPPLE_IGNORE_EVENTS: 'mdl-js-ripple-effect--ignore-events',
62 RIPPLE_CONTAINER: 'mdl-switch__ripple-container',
63 RIPPLE_CENTER: 'mdl-ripple--center',
64 RIPPLE: 'mdl-ripple',
65 IS_FOCUSED: 'is-focused',
66 IS_DISABLED: 'is-disabled',
67 IS_CHECKED: 'is-checked'
68 };
69
70 /**
71 * Handle change of state.
72 *
73 * @param {Event} event The event that fired.
74 * @private
75 */
76 MaterialSwitch.prototype.onChange_ = function(event) {
77 this.updateClasses_();
78 };
79
80 /**
81 * Handle focus of element.
82 *
83 * @param {Event} event The event that fired.
84 * @private
85 */
86 MaterialSwitch.prototype.onFocus_ = function(event) {
87 this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
88 };
89
90 /**
91 * Handle lost focus of element.
92 *
93 * @param {Event} event The event that fired.
94 * @private
95 */
96 MaterialSwitch.prototype.onBlur_ = function(event) {
97 this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
98 };
99
100 /**
101 * Handle mouseup.
102 *
103 * @param {Event} event The event that fired.
104 * @private
105 */
106 MaterialSwitch.prototype.onMouseUp_ = function(event) {
107 this.blur_();
108 };
109
110 /**
111 * Handle class updates.
112 *
113 * @private
114 */
115 MaterialSwitch.prototype.updateClasses_ = function() {
116 this.checkDisabled();
117 this.checkToggleState();
118 };
119
120 /**
121 * Add blur.
122 *
123 * @private
124 */
125 MaterialSwitch.prototype.blur_ = function() {
126 // TODO: figure out why there's a focus event being fired after our blur,
127 // so that we can avoid this hack.
128 window.setTimeout(function() {
129 this.inputElement_.blur();
130 }.bind(this), /** @type {number} */ (this.Constant_.TINY_TIMEOUT));
131 };
132
133 // Public methods.
134
135 /**
136 * Check the components disabled state.
137 *
138 * @public
139 */
140 MaterialSwitch.prototype.checkDisabled = function() {
141 if (this.inputElement_.disabled) {
142 this.element_.classList.add(this.CssClasses_.IS_DISABLED);
143 } else {
144 this.element_.classList.remove(this.CssClasses_.IS_DISABLED);
145 }
146 };
147 MaterialSwitch.prototype['checkDisabled'] =
148 MaterialSwitch.prototype.checkDisabled;
149
150 /**
151 * Check the components toggled state.
152 *
153 * @public
154 */
155 MaterialSwitch.prototype.checkToggleState = function() {
156 if (this.inputElement_.checked) {
157 this.element_.classList.add(this.CssClasses_.IS_CHECKED);
158 } else {
159 this.element_.classList.remove(this.CssClasses_.IS_CHECKED);
160 }
161 };
162 MaterialSwitch.prototype['checkToggleState'] =
163 MaterialSwitch.prototype.checkToggleState;
164
165 /**
166 * Disable switch.
167 *
168 * @public
169 */
170 MaterialSwitch.prototype.disable = function() {
171 this.inputElement_.disabled = true;
172 this.updateClasses_();
173 };
174 MaterialSwitch.prototype['disable'] = MaterialSwitch.prototype.disable;
175
176 /**
177 * Enable switch.
178 *
179 * @public
180 */
181 MaterialSwitch.prototype.enable = function() {
182 this.inputElement_.disabled = false;
183 this.updateClasses_();
184 };
185 MaterialSwitch.prototype['enable'] = MaterialSwitch.prototype.enable;
186
187 /**
188 * Activate switch.
189 *
190 * @public
191 */
192 MaterialSwitch.prototype.on = function() {
193 this.inputElement_.checked = true;
194 this.updateClasses_();
195 };
196 MaterialSwitch.prototype['on'] = MaterialSwitch.prototype.on;
197
198 /**
199 * Deactivate switch.
200 *
201 * @public
202 */
203 MaterialSwitch.prototype.off = function() {
204 this.inputElement_.checked = false;
205 this.updateClasses_();
206 };
207 MaterialSwitch.prototype['off'] = MaterialSwitch.prototype.off;
208
209 /**
210 * Initialize element.
211 */
212 MaterialSwitch.prototype.init = function() {
213 if (this.element_) {
214 this.inputElement_ = this.element_.querySelector('.' +
215 this.CssClasses_.INPUT);
216
217 var track = document.createElement('div');
218 track.classList.add(this.CssClasses_.TRACK);
219
220 var thumb = document.createElement('div');
221 thumb.classList.add(this.CssClasses_.THUMB);
222
223 var focusHelper = document.createElement('span');
224 focusHelper.classList.add(this.CssClasses_.FOCUS_HELPER);
225
226 thumb.appendChild(focusHelper);
227
228 this.element_.appendChild(track);
229 this.element_.appendChild(thumb);
230
231 this.boundMouseUpHandler = this.onMouseUp_.bind(this);
232
233 if (this.element_.classList.contains(
234 this.CssClasses_.RIPPLE_EFFECT)) {
235 this.element_.classList.add(
236 this.CssClasses_.RIPPLE_IGNORE_EVENTS);
237 this.rippleContainerElement_ = document.createElement('span');
238 this.rippleContainerElement_.classList.add(
239 this.CssClasses_.RIPPLE_CONTAINER);
240 this.rippleContainerElement_.classList.add(this.CssClasses_.RIPPLE_EFFECT);
241 this.rippleContainerElement_.classList.add(this.CssClasses_.RIPPLE_CENTER);
242 this.rippleContainerElement_.addEventListener('mouseup', this.boundMouseUpHandler);
243
244 var ripple = document.createElement('span');
245 ripple.classList.add(this.CssClasses_.RIPPLE);
246
247 this.rippleContainerElement_.appendChild(ripple);
248 this.element_.appendChild(this.rippleContainerElement_);
249 }
250
251 this.boundChangeHandler = this.onChange_.bind(this);
252 this.boundFocusHandler = this.onFocus_.bind(this);
253 this.boundBlurHandler = this.onBlur_.bind(this);
254
255 this.inputElement_.addEventListener('change', this.boundChangeHandler);
256 this.inputElement_.addEventListener('focus', this.boundFocusHandler);
257 this.inputElement_.addEventListener('blur', this.boundBlurHandler);
258 this.element_.addEventListener('mouseup', this.boundMouseUpHandler);
259
260 this.updateClasses_();
261 this.element_.classList.add('is-upgraded');
262 }
263 };
264
265 // The component registers itself. It can assume componentHandler is available
266 // in the global scope.
267 componentHandler.register({
268 constructor: MaterialSwitch,
269 classAsString: 'MaterialSwitch',
270 cssClass: 'mdl-js-switch',
271 widget: true
272 });
273})();