| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * @license
- * Copyright 2016 Google Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- import * as tslib_1 from "tslib";
- import { getCorrectEventName } from '@material/animation/util';
- import { MDCComponent } from '@material/base/component';
- import { applyPassive } from '@material/dom/events';
- import { matches } from '@material/dom/ponyfill';
- import { MDCRipple } from '@material/ripple/component';
- import { MDCRippleFoundation } from '@material/ripple/foundation';
- import { MDCCheckboxFoundation } from './foundation';
- var CB_PROTO_PROPS = ['checked', 'indeterminate'];
- var MDCCheckbox = /** @class */ (function (_super) {
- tslib_1.__extends(MDCCheckbox, _super);
- function MDCCheckbox() {
- var _this = _super !== null && _super.apply(this, arguments) || this;
- _this.ripple_ = _this.createRipple_();
- return _this;
- }
- MDCCheckbox.attachTo = function (root) {
- return new MDCCheckbox(root);
- };
- Object.defineProperty(MDCCheckbox.prototype, "ripple", {
- get: function () {
- return this.ripple_;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(MDCCheckbox.prototype, "checked", {
- get: function () {
- return this.nativeControl_.checked;
- },
- set: function (checked) {
- this.nativeControl_.checked = checked;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(MDCCheckbox.prototype, "indeterminate", {
- get: function () {
- return this.nativeControl_.indeterminate;
- },
- set: function (indeterminate) {
- this.nativeControl_.indeterminate = indeterminate;
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(MDCCheckbox.prototype, "disabled", {
- get: function () {
- return this.nativeControl_.disabled;
- },
- set: function (disabled) {
- this.foundation_.setDisabled(disabled);
- },
- enumerable: true,
- configurable: true
- });
- Object.defineProperty(MDCCheckbox.prototype, "value", {
- get: function () {
- return this.nativeControl_.value;
- },
- set: function (value) {
- this.nativeControl_.value = value;
- },
- enumerable: true,
- configurable: true
- });
- MDCCheckbox.prototype.initialSyncWithDOM = function () {
- var _this = this;
- this.handleChange_ = function () { return _this.foundation_.handleChange(); };
- this.handleAnimationEnd_ = function () { return _this.foundation_.handleAnimationEnd(); };
- this.nativeControl_.addEventListener('change', this.handleChange_);
- this.listen(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
- this.installPropertyChangeHooks_();
- };
- MDCCheckbox.prototype.destroy = function () {
- this.ripple_.destroy();
- this.nativeControl_.removeEventListener('change', this.handleChange_);
- this.unlisten(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
- this.uninstallPropertyChangeHooks_();
- _super.prototype.destroy.call(this);
- };
- MDCCheckbox.prototype.getDefaultFoundation = function () {
- var _this = this;
- // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
- // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
- var adapter = {
- addClass: function (className) { return _this.root_.classList.add(className); },
- forceLayout: function () { return _this.root_.offsetWidth; },
- hasNativeControl: function () { return !!_this.nativeControl_; },
- isAttachedToDOM: function () { return Boolean(_this.root_.parentNode); },
- isChecked: function () { return _this.checked; },
- isIndeterminate: function () { return _this.indeterminate; },
- removeClass: function (className) { return _this.root_.classList.remove(className); },
- removeNativeControlAttr: function (attr) { return _this.nativeControl_.removeAttribute(attr); },
- setNativeControlAttr: function (attr, value) { return _this.nativeControl_.setAttribute(attr, value); },
- setNativeControlDisabled: function (disabled) { return _this.nativeControl_.disabled = disabled; },
- };
- return new MDCCheckboxFoundation(adapter);
- };
- MDCCheckbox.prototype.createRipple_ = function () {
- var _this = this;
- // DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
- // To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
- var adapter = tslib_1.__assign({}, MDCRipple.createAdapter(this), { deregisterInteractionHandler: function (evtType, handler) { return _this.nativeControl_.removeEventListener(evtType, handler, applyPassive()); }, isSurfaceActive: function () { return matches(_this.nativeControl_, ':active'); }, isUnbounded: function () { return true; }, registerInteractionHandler: function (evtType, handler) { return _this.nativeControl_.addEventListener(evtType, handler, applyPassive()); } });
- return new MDCRipple(this.root_, new MDCRippleFoundation(adapter));
- };
- MDCCheckbox.prototype.installPropertyChangeHooks_ = function () {
- var _this = this;
- var nativeCb = this.nativeControl_;
- var cbProto = Object.getPrototypeOf(nativeCb);
- CB_PROTO_PROPS.forEach(function (controlState) {
- var desc = Object.getOwnPropertyDescriptor(cbProto, controlState);
- // We have to check for this descriptor, since some browsers (Safari) don't support its return.
- // See: https://bugs.webkit.org/show_bug.cgi?id=49739
- if (!validDescriptor(desc)) {
- return;
- }
- // Type cast is needed for compatibility with Closure Compiler.
- var nativeGetter = desc.get;
- var nativeCbDesc = {
- configurable: desc.configurable,
- enumerable: desc.enumerable,
- get: nativeGetter,
- set: function (state) {
- desc.set.call(nativeCb, state);
- _this.foundation_.handleChange();
- },
- };
- Object.defineProperty(nativeCb, controlState, nativeCbDesc);
- });
- };
- MDCCheckbox.prototype.uninstallPropertyChangeHooks_ = function () {
- var nativeCb = this.nativeControl_;
- var cbProto = Object.getPrototypeOf(nativeCb);
- CB_PROTO_PROPS.forEach(function (controlState) {
- var desc = Object.getOwnPropertyDescriptor(cbProto, controlState);
- if (!validDescriptor(desc)) {
- return;
- }
- Object.defineProperty(nativeCb, controlState, desc);
- });
- };
- Object.defineProperty(MDCCheckbox.prototype, "nativeControl_", {
- get: function () {
- var NATIVE_CONTROL_SELECTOR = MDCCheckboxFoundation.strings.NATIVE_CONTROL_SELECTOR;
- var el = this.root_.querySelector(NATIVE_CONTROL_SELECTOR);
- if (!el) {
- throw new Error("Checkbox component requires a " + NATIVE_CONTROL_SELECTOR + " element");
- }
- return el;
- },
- enumerable: true,
- configurable: true
- });
- return MDCCheckbox;
- }(MDCComponent));
- export { MDCCheckbox };
- function validDescriptor(inputPropDesc) {
- return !!inputPropDesc && typeof inputPropDesc.set === 'function';
- }
- //# sourceMappingURL=component.js.map
|