UNPKG

4.32 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23import { __assign, __extends } from "tslib";
24import { MDCFoundation } from '@material/base/foundation';
25import { cssClasses, strings } from './constants';
26var MDCSwitchFoundation = /** @class */ (function (_super) {
27 __extends(MDCSwitchFoundation, _super);
28 function MDCSwitchFoundation(adapter) {
29 return _super.call(this, __assign(__assign({}, MDCSwitchFoundation.defaultAdapter), adapter)) || this;
30 }
31 Object.defineProperty(MDCSwitchFoundation, "strings", {
32 /** The string constants used by the switch. */
33 get: function () {
34 return strings;
35 },
36 enumerable: false,
37 configurable: true
38 });
39 Object.defineProperty(MDCSwitchFoundation, "cssClasses", {
40 /** The CSS classes used by the switch. */
41 get: function () {
42 return cssClasses;
43 },
44 enumerable: false,
45 configurable: true
46 });
47 Object.defineProperty(MDCSwitchFoundation, "defaultAdapter", {
48 /** The default Adapter for the switch. */
49 get: function () {
50 return {
51 addClass: function () { return undefined; },
52 removeClass: function () { return undefined; },
53 setNativeControlChecked: function () { return undefined; },
54 setNativeControlDisabled: function () { return undefined; },
55 setNativeControlAttr: function () { return undefined; },
56 };
57 },
58 enumerable: false,
59 configurable: true
60 });
61 /** Sets the checked state of the switch. */
62 MDCSwitchFoundation.prototype.setChecked = function (checked) {
63 this.adapter.setNativeControlChecked(checked);
64 this.updateAriaChecked(checked);
65 this.updateCheckedStyling(checked);
66 };
67 /** Sets the disabled state of the switch. */
68 MDCSwitchFoundation.prototype.setDisabled = function (disabled) {
69 this.adapter.setNativeControlDisabled(disabled);
70 if (disabled) {
71 this.adapter.addClass(cssClasses.DISABLED);
72 }
73 else {
74 this.adapter.removeClass(cssClasses.DISABLED);
75 }
76 };
77 /** Handles the change event for the switch native control. */
78 MDCSwitchFoundation.prototype.handleChange = function (evt) {
79 var nativeControl = evt.target;
80 this.updateAriaChecked(nativeControl.checked);
81 this.updateCheckedStyling(nativeControl.checked);
82 };
83 /** Updates the styling of the switch based on its checked state. */
84 MDCSwitchFoundation.prototype.updateCheckedStyling = function (checked) {
85 if (checked) {
86 this.adapter.addClass(cssClasses.CHECKED);
87 }
88 else {
89 this.adapter.removeClass(cssClasses.CHECKED);
90 }
91 };
92 MDCSwitchFoundation.prototype.updateAriaChecked = function (checked) {
93 this.adapter.setNativeControlAttr(strings.ARIA_CHECKED_ATTR, "" + !!checked);
94 };
95 return MDCSwitchFoundation;
96}(MDCFoundation));
97export { MDCSwitchFoundation };
98// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
99export default MDCSwitchFoundation;
100//# sourceMappingURL=foundation.js.map
\No newline at end of file