UNPKG

6.57 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, __read, __spreadArray } from "tslib";
24import { MDCComponent } from '@material/base/component';
25import { applyPassive } from '@material/dom/events';
26import { matches } from '@material/dom/ponyfill';
27import { MDCRipple } from '@material/ripple/component';
28import { MDCRippleFoundation } from '@material/ripple/foundation';
29import { MDCSwitchFoundation } from './foundation';
30var MDCSwitch = /** @class */ (function (_super) {
31 __extends(MDCSwitch, _super);
32 function MDCSwitch() {
33 var _this = _super !== null && _super.apply(this, arguments) || this;
34 _this.rippleSurface = _this.createRipple();
35 return _this;
36 }
37 MDCSwitch.attachTo = function (root) {
38 return new MDCSwitch(root);
39 };
40 MDCSwitch.prototype.destroy = function () {
41 _super.prototype.destroy.call(this);
42 this.rippleSurface.destroy();
43 this.nativeControl.removeEventListener('change', this.changeHandler);
44 };
45 MDCSwitch.prototype.initialSyncWithDOM = function () {
46 var _this = this;
47 this.changeHandler = function () {
48 var _a;
49 var args = [];
50 for (var _i = 0; _i < arguments.length; _i++) {
51 args[_i] = arguments[_i];
52 }
53 (_a = _this.foundation).handleChange.apply(_a, __spreadArray([], __read(args)));
54 };
55 this.nativeControl.addEventListener('change', this.changeHandler);
56 // Sometimes the checked state of the input element is saved in the history.
57 // The switch styling should match the checked state of the input element.
58 // Do an initial sync between the native control and the foundation.
59 this.checked = this.checked;
60 };
61 MDCSwitch.prototype.getDefaultFoundation = function () {
62 var _this = this;
63 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
64 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
65 var adapter = {
66 addClass: function (className) { return _this.root.classList.add(className); },
67 removeClass: function (className) { return _this.root.classList.remove(className); },
68 setNativeControlChecked: function (checked) { return _this.nativeControl.checked =
69 checked; },
70 setNativeControlDisabled: function (disabled) { return _this.nativeControl.disabled =
71 disabled; },
72 setNativeControlAttr: function (attr, value) {
73 _this.nativeControl.setAttribute(attr, value);
74 },
75 };
76 return new MDCSwitchFoundation(adapter);
77 };
78 Object.defineProperty(MDCSwitch.prototype, "ripple", {
79 get: function () {
80 return this.rippleSurface;
81 },
82 enumerable: false,
83 configurable: true
84 });
85 Object.defineProperty(MDCSwitch.prototype, "checked", {
86 get: function () {
87 return this.nativeControl.checked;
88 },
89 set: function (checked) {
90 this.foundation.setChecked(checked);
91 },
92 enumerable: false,
93 configurable: true
94 });
95 Object.defineProperty(MDCSwitch.prototype, "disabled", {
96 get: function () {
97 return this.nativeControl.disabled;
98 },
99 set: function (disabled) {
100 this.foundation.setDisabled(disabled);
101 },
102 enumerable: false,
103 configurable: true
104 });
105 MDCSwitch.prototype.createRipple = function () {
106 var _this = this;
107 var RIPPLE_SURFACE_SELECTOR = MDCSwitchFoundation.strings.RIPPLE_SURFACE_SELECTOR;
108 var rippleSurface = this.root.querySelector(RIPPLE_SURFACE_SELECTOR);
109 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
110 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
111 var adapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { addClass: function (className) { return rippleSurface.classList.add(className); }, computeBoundingRect: function () { return rippleSurface.getBoundingClientRect(); }, deregisterInteractionHandler: function (evtType, handler) {
112 _this.nativeControl.removeEventListener(evtType, handler, applyPassive());
113 }, isSurfaceActive: function () { return matches(_this.nativeControl, ':active'); }, isUnbounded: function () { return true; }, registerInteractionHandler: function (evtType, handler) {
114 _this.nativeControl.addEventListener(evtType, handler, applyPassive());
115 }, removeClass: function (className) {
116 rippleSurface.classList.remove(className);
117 }, updateCssVariable: function (varName, value) {
118 rippleSurface.style.setProperty(varName, value);
119 } });
120 return new MDCRipple(this.root, new MDCRippleFoundation(adapter));
121 };
122 Object.defineProperty(MDCSwitch.prototype, "nativeControl", {
123 get: function () {
124 var NATIVE_CONTROL_SELECTOR = MDCSwitchFoundation.strings.NATIVE_CONTROL_SELECTOR;
125 return this.root.querySelector(NATIVE_CONTROL_SELECTOR);
126 },
127 enumerable: false,
128 configurable: true
129 });
130 return MDCSwitch;
131}(MDCComponent));
132export { MDCSwitch };
133//# sourceMappingURL=component.js.map
\No newline at end of file