UNPKG

10.6 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2016 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, __values } from "tslib";
24import { getCorrectEventName } from '@material/animation/util';
25import { MDCComponent } from '@material/base/component';
26import { applyPassive } from '@material/dom/events';
27import { matches } from '@material/dom/ponyfill';
28import { MDCRipple } from '@material/ripple/component';
29import { MDCRippleFoundation } from '@material/ripple/foundation';
30import { strings } from './constants';
31import { MDCCheckboxFoundation } from './foundation';
32var CB_PROTO_PROPS = ['checked', 'indeterminate'];
33var MDCCheckbox = /** @class */ (function (_super) {
34 __extends(MDCCheckbox, _super);
35 function MDCCheckbox() {
36 var _this = _super !== null && _super.apply(this, arguments) || this;
37 _this.rippleSurface = _this.createRipple();
38 return _this;
39 }
40 MDCCheckbox.attachTo = function (root) {
41 return new MDCCheckbox(root);
42 };
43 Object.defineProperty(MDCCheckbox.prototype, "ripple", {
44 get: function () {
45 return this.rippleSurface;
46 },
47 enumerable: false,
48 configurable: true
49 });
50 Object.defineProperty(MDCCheckbox.prototype, "checked", {
51 get: function () {
52 return this.getNativeControl().checked;
53 },
54 set: function (checked) {
55 this.getNativeControl().checked = checked;
56 },
57 enumerable: false,
58 configurable: true
59 });
60 Object.defineProperty(MDCCheckbox.prototype, "indeterminate", {
61 get: function () {
62 return this.getNativeControl().indeterminate;
63 },
64 set: function (indeterminate) {
65 this.getNativeControl().indeterminate = indeterminate;
66 },
67 enumerable: false,
68 configurable: true
69 });
70 Object.defineProperty(MDCCheckbox.prototype, "disabled", {
71 get: function () {
72 return this.getNativeControl().disabled;
73 },
74 set: function (disabled) {
75 this.foundation.setDisabled(disabled);
76 },
77 enumerable: false,
78 configurable: true
79 });
80 Object.defineProperty(MDCCheckbox.prototype, "value", {
81 get: function () {
82 return this.getNativeControl().value;
83 },
84 set: function (value) {
85 this.getNativeControl().value = value;
86 },
87 enumerable: false,
88 configurable: true
89 });
90 MDCCheckbox.prototype.initialize = function () {
91 var DATA_INDETERMINATE_ATTR = strings.DATA_INDETERMINATE_ATTR;
92 this.getNativeControl().indeterminate =
93 this.getNativeControl().getAttribute(DATA_INDETERMINATE_ATTR) ===
94 'true';
95 this.getNativeControl().removeAttribute(DATA_INDETERMINATE_ATTR);
96 };
97 MDCCheckbox.prototype.initialSyncWithDOM = function () {
98 var _this = this;
99 this.handleChange = function () {
100 _this.foundation.handleChange();
101 };
102 this.handleAnimationEnd = function () {
103 _this.foundation.handleAnimationEnd();
104 };
105 this.getNativeControl().addEventListener('change', this.handleChange);
106 this.listen(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd);
107 this.installPropertyChangeHooks();
108 };
109 MDCCheckbox.prototype.destroy = function () {
110 this.rippleSurface.destroy();
111 this.getNativeControl().removeEventListener('change', this.handleChange);
112 this.unlisten(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd);
113 this.uninstallPropertyChangeHooks();
114 _super.prototype.destroy.call(this);
115 };
116 MDCCheckbox.prototype.getDefaultFoundation = function () {
117 var _this = this;
118 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
119 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
120 var adapter = {
121 addClass: function (className) { return _this.root.classList.add(className); },
122 forceLayout: function () { return _this.root.offsetWidth; },
123 hasNativeControl: function () { return !!_this.getNativeControl(); },
124 isAttachedToDOM: function () { return Boolean(_this.root.parentNode); },
125 isChecked: function () { return _this.checked; },
126 isIndeterminate: function () { return _this.indeterminate; },
127 removeClass: function (className) {
128 _this.root.classList.remove(className);
129 },
130 removeNativeControlAttr: function (attr) {
131 _this.getNativeControl().removeAttribute(attr);
132 },
133 setNativeControlAttr: function (attr, value) {
134 _this.getNativeControl().setAttribute(attr, value);
135 },
136 setNativeControlDisabled: function (disabled) {
137 _this.getNativeControl().disabled = disabled;
138 },
139 };
140 return new MDCCheckboxFoundation(adapter);
141 };
142 MDCCheckbox.prototype.createRipple = function () {
143 var _this = this;
144 // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
145 // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
146 var adapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { deregisterInteractionHandler: function (evtType, handler) {
147 _this.getNativeControl().removeEventListener(evtType, handler, applyPassive());
148 }, isSurfaceActive: function () { return matches(_this.getNativeControl(), ':active'); }, isUnbounded: function () { return true; }, registerInteractionHandler: function (evtType, handler) {
149 _this.getNativeControl().addEventListener(evtType, handler, applyPassive());
150 } });
151 return new MDCRipple(this.root, new MDCRippleFoundation(adapter));
152 };
153 MDCCheckbox.prototype.installPropertyChangeHooks = function () {
154 var e_1, _a;
155 var _this = this;
156 var nativeCb = this.getNativeControl();
157 var cbProto = Object.getPrototypeOf(nativeCb);
158 var _loop_1 = function (controlState) {
159 var desc = Object.getOwnPropertyDescriptor(cbProto, controlState);
160 // We have to check for this descriptor, since some browsers (Safari) don't support its return.
161 // See: https://bugs.webkit.org/show_bug.cgi?id=49739
162 if (!validDescriptor(desc)) {
163 return { value: void 0 };
164 }
165 // Type cast is needed for compatibility with Closure Compiler.
166 var nativeGetter = desc.get;
167 var nativeCbDesc = {
168 configurable: desc.configurable,
169 enumerable: desc.enumerable,
170 get: nativeGetter,
171 set: function (state) {
172 desc.set.call(nativeCb, state);
173 _this.foundation.handleChange();
174 },
175 };
176 Object.defineProperty(nativeCb, controlState, nativeCbDesc);
177 };
178 try {
179 for (var CB_PROTO_PROPS_1 = __values(CB_PROTO_PROPS), CB_PROTO_PROPS_1_1 = CB_PROTO_PROPS_1.next(); !CB_PROTO_PROPS_1_1.done; CB_PROTO_PROPS_1_1 = CB_PROTO_PROPS_1.next()) {
180 var controlState = CB_PROTO_PROPS_1_1.value;
181 var state_1 = _loop_1(controlState);
182 if (typeof state_1 === "object")
183 return state_1.value;
184 }
185 }
186 catch (e_1_1) { e_1 = { error: e_1_1 }; }
187 finally {
188 try {
189 if (CB_PROTO_PROPS_1_1 && !CB_PROTO_PROPS_1_1.done && (_a = CB_PROTO_PROPS_1.return)) _a.call(CB_PROTO_PROPS_1);
190 }
191 finally { if (e_1) throw e_1.error; }
192 }
193 };
194 MDCCheckbox.prototype.uninstallPropertyChangeHooks = function () {
195 var e_2, _a;
196 var nativeCb = this.getNativeControl();
197 var cbProto = Object.getPrototypeOf(nativeCb);
198 try {
199 for (var CB_PROTO_PROPS_2 = __values(CB_PROTO_PROPS), CB_PROTO_PROPS_2_1 = CB_PROTO_PROPS_2.next(); !CB_PROTO_PROPS_2_1.done; CB_PROTO_PROPS_2_1 = CB_PROTO_PROPS_2.next()) {
200 var controlState = CB_PROTO_PROPS_2_1.value;
201 var desc = Object.getOwnPropertyDescriptor(cbProto, controlState);
202 if (!validDescriptor(desc)) {
203 return;
204 }
205 Object.defineProperty(nativeCb, controlState, desc);
206 }
207 }
208 catch (e_2_1) { e_2 = { error: e_2_1 }; }
209 finally {
210 try {
211 if (CB_PROTO_PROPS_2_1 && !CB_PROTO_PROPS_2_1.done && (_a = CB_PROTO_PROPS_2.return)) _a.call(CB_PROTO_PROPS_2);
212 }
213 finally { if (e_2) throw e_2.error; }
214 }
215 };
216 MDCCheckbox.prototype.getNativeControl = function () {
217 var NATIVE_CONTROL_SELECTOR = strings.NATIVE_CONTROL_SELECTOR;
218 var el = this.root.querySelector(NATIVE_CONTROL_SELECTOR);
219 if (!el) {
220 throw new Error("Checkbox component requires a " + NATIVE_CONTROL_SELECTOR + " element");
221 }
222 return el;
223 };
224 return MDCCheckbox;
225}(MDCComponent));
226export { MDCCheckbox };
227function validDescriptor(inputPropDesc) {
228 return !!inputPropDesc && typeof inputPropDesc.set === 'function';
229}
230//# sourceMappingURL=component.js.map
\No newline at end of file