@angular-mdc/web
Version:
657 lines (653 loc) • 21.7 kB
JavaScript
/**
* @license
* Copyright (c) Dominic Carretto
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/trimox/angular-mdc-web/blob/master/LICENSE
*/
import { forwardRef, EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, NgZone, ChangeDetectorRef, ElementRef, Optional, Input, Output, ViewChild, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MdcFormFieldControl, MdcFormField, MdcFormFieldModule } from '@angular-mdc/web/form-field';
import { __extends, __awaiter, __generator, __assign } from 'tslib';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { supportsPassiveEventListeners, Platform } from '@angular/cdk/platform';
import { Subject, fromEvent } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';
import { matches } from '@angular-mdc/web/dom';
import { MDCRippleFoundation } from '@material/ripple';
import { MDCCheckboxFoundation } from '@material/checkbox';
import { MDCComponent } from '@angular-mdc/web/base';
import { MdcRipple } from '@angular-mdc/web/ripple';
/**
* @fileoverview added by tsickle
* Generated from: checkbox/checkbox.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var nextUniqueId = 0;
/**
* Change event object emitted by MdcCheckbox.
*/
var /**
* Change event object emitted by MdcCheckbox.
*/
MdcCheckboxChange = /** @class */ (function () {
function MdcCheckboxChange(source, checked) {
this.source = source;
this.checked = checked;
}
return MdcCheckboxChange;
}());
/** @type {?} */
var MDC_CHECKBOX_CONTROL_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef((/**
* @return {?}
*/
function () { return MdcCheckbox; })),
multi: true
};
var MdcCheckbox = /** @class */ (function (_super) {
__extends(MdcCheckbox, _super);
function MdcCheckbox(_platform, _ngZone, _changeDetectorRef, elementRef, ripple, _parentFormField) {
var _this = _super.call(this, elementRef) || this;
_this._platform = _platform;
_this._ngZone = _ngZone;
_this._changeDetectorRef = _changeDetectorRef;
_this.elementRef = elementRef;
_this.ripple = ripple;
_this._parentFormField = _parentFormField;
/**
* Emits whenever the component is destroyed.
*/
_this._destroy = new Subject();
_this._initialized = false;
_this._uniqueId = "mdc-checkbox-" + ++nextUniqueId;
_this.id = _this._uniqueId;
_this.name = null;
_this._checked = false;
_this._touch = false;
_this._disabled = false;
/**
* The value attribute of the native input element
*/
_this.value = null;
_this._indeterminate = false;
_this._indeterminateToChecked = true;
_this._disableRipple = false;
_this.tabIndex = 0;
_this.ariaLabel = '';
_this.ariaLabelledby = null;
/**
* Fired when checkbox is checked or unchecked, but not when set
* indeterminate. Sends the state of [checked].
*/
_this.change = new EventEmitter();
/**
* Fired when checkbox goes in and out of indeterminate state, but not when
* set to checked. Sends the state of [indeterminate];
*/
_this.indeterminateChange = new EventEmitter();
/**
* View to model callback called when value changes
*/
_this._onChange = (/**
* @return {?}
*/
function () { });
/**
* View to model callback called when component has been touched
*/
_this._onTouched = (/**
* @return {?}
*/
function () { });
_this._root = _this.elementRef.nativeElement;
if (_this._parentFormField) {
_parentFormField.elementRef.nativeElement.classList.add('mdc-form-field');
}
return _this;
}
Object.defineProperty(MdcCheckbox.prototype, "inputId", {
/** Returns the unique id for the visual hidden input. */
get: /**
* Returns the unique id for the visual hidden input.
* @return {?}
*/
function () {
return (this.id || this._uniqueId) + "-input";
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "checked", {
get: /**
* @return {?}
*/
function () {
return this._checked;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
if (value !== this.checked) {
this._checked = coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "touch", {
get: /**
* @return {?}
*/
function () {
return this._touch;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._touch = coerceBooleanProperty(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "disabled", {
get: /**
* @return {?}
*/
function () {
return this._disabled;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this.setDisabledState(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "indeterminate", {
/**
* Alternative state of the checkbox, not user set-able state. Between
* [checked] and [indeterminate], only one can be true, though both can be
* false.
* `true` is INDETERMINATE and `false` is not.
*/
get: /**
* Alternative state of the checkbox, not user set-able state. Between
* [checked] and [indeterminate], only one can be true, though both can be
* false.
* `true` is INDETERMINATE and `false` is not.
* @return {?}
*/
function () {
return this._indeterminate;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
var _a;
if (this.disabled) {
return;
}
/** @type {?} */
var newValue = coerceBooleanProperty(value);
if (newValue !== this._indeterminate) {
this._indeterminate = newValue;
if (newValue) {
this.checked = false;
}
this.indeterminateChange.emit({ source: this, indeterminate: this._indeterminate });
this._changeDetectorRef.markForCheck();
(_a = this._foundation) === null || _a === void 0 ? void 0 : _a.handleChange();
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "indeterminateToChecked", {
/**
* Determines the state to go into when [indeterminate] state is toggled.
* `true` will go to checked and `false` will go to unchecked.
*/
get: /**
* Determines the state to go into when [indeterminate] state is toggled.
* `true` will go to checked and `false` will go to unchecked.
* @return {?}
*/
function () {
return this._indeterminateToChecked;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._indeterminateToChecked = coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcCheckbox.prototype, "disableRipple", {
/** Whether the ripple ink is disabled. */
get: /**
* Whether the ripple ink is disabled.
* @return {?}
*/
function () {
return this._disableRipple;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._disableRipple = coerceBooleanProperty(value);
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
MdcCheckbox.prototype.getDefaultFoundation = /**
* @return {?}
*/
function () {
var _this = this;
// Do not initialize foundation until ngAfterViewInit runs
if (!this._initialized) {
return undefined;
}
/** @type {?} */
var adapter = {
addClass: (/**
* @param {?} className
* @return {?}
*/
function (className) { return _this._root.classList.add(className); }),
removeClass: (/**
* @param {?} className
* @return {?}
*/
function (className) { return _this._root.classList.remove(className); }),
setNativeControlAttr: (/**
* @param {?} attr
* @param {?} value
* @return {?}
*/
function (attr, value) {
return _this._inputElement.nativeElement.setAttribute(attr, value);
}),
removeNativeControlAttr: (/**
* @param {?} attr
* @return {?}
*/
function (attr) { return _this._inputElement.nativeElement.removeAttribute(attr); }),
isIndeterminate: (/**
* @return {?}
*/
function () { return _this.indeterminate; }),
isChecked: (/**
* @return {?}
*/
function () { return _this.checked; }),
hasNativeControl: (/**
* @return {?}
*/
function () { return true; }),
setNativeControlDisabled: (/**
* @param {?} disabled
* @return {?}
*/
function (disabled) { return _this._inputElement.nativeElement.disabled = disabled; }),
forceLayout: (/**
* @return {?}
*/
function () { return ((/** @type {?} */ (_this._root))).offsetWidth; }),
isAttachedToDOM: (/**
* @return {?}
*/
function () { return true; })
};
return new MDCCheckboxFoundation(adapter);
};
/**
* @return {?}
*/
MdcCheckbox.prototype._asyncBuildFoundation = /**
* @return {?}
*/
function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this._foundation = this.getDefaultFoundation();
return [2 /*return*/];
});
});
};
/**
* @return {?}
*/
MdcCheckbox.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
var _this = this;
this._initialized = true;
this._asyncBuildFoundation()
.then((/**
* @return {?}
*/
function () {
_this._foundation.init();
_this.setDisabledState(_this._inputElement.nativeElement.disabled);
_this.ripple = _this._createRipple();
_this.ripple.init();
}));
this._loadListeners();
};
/**
* @return {?}
*/
MdcCheckbox.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this._destroy.next();
this._destroy.complete();
this.ripple.destroy();
this._foundation.destroy();
};
/**
* @param {?} value
* @return {?}
*/
MdcCheckbox.prototype.writeValue = /**
* @param {?} value
* @return {?}
*/
function (value) {
this.checked = !!value;
};
/**
* @param {?} fn
* @return {?}
*/
MdcCheckbox.prototype.registerOnChange = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
this._onChange = fn;
};
/**
* @param {?} fn
* @return {?}
*/
MdcCheckbox.prototype.registerOnTouched = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
this._onTouched = fn;
};
/** Focuses the checkbox. */
/**
* Focuses the checkbox.
* @return {?}
*/
MdcCheckbox.prototype.focus = /**
* Focuses the checkbox.
* @return {?}
*/
function () {
if (!this.disabled) {
this._inputElement.nativeElement.focus();
}
};
/**
* @param {?=} checked
* @return {?}
*/
MdcCheckbox.prototype.toggle = /**
* @param {?=} checked
* @return {?}
*/
function (checked) {
this._setState(checked);
};
/**
* @param {?} evt
* @return {?}
*/
MdcCheckbox.prototype._onInteraction = /**
* @param {?} evt
* @return {?}
*/
function (evt) {
// We have to stop propagation for click events on the visual hidden input element.
// Preventing bubbling for the second event will solve that issue.
evt.stopPropagation();
this._setState();
this._onChange(this.checked);
this._changeDetectorRef.markForCheck();
this.change.emit(new MdcCheckboxChange(this, this.checked));
};
/**
* @param {?} evt
* @return {?}
*/
MdcCheckbox.prototype._onInputClick = /**
* @param {?} evt
* @return {?}
*/
function (evt) {
evt.stopPropagation();
};
/**
* @param {?} disabled
* @return {?}
*/
MdcCheckbox.prototype.setDisabledState = /**
* @param {?} disabled
* @return {?}
*/
function (disabled) {
var _a;
/** @type {?} */
var newValue = coerceBooleanProperty(disabled);
if (newValue !== this._disabled) {
this._disabled = newValue;
(_a = this._foundation) === null || _a === void 0 ? void 0 : _a.setDisabled(newValue);
this._changeDetectorRef.markForCheck();
}
};
/**
* @private
* @param {?=} checked
* @return {?}
*/
MdcCheckbox.prototype._setState = /**
* @private
* @param {?=} checked
* @return {?}
*/
function (checked) {
var _this = this;
if (this.disabled) {
return;
}
if (this.indeterminate) {
this._checked = this.indeterminateToChecked;
this.indeterminate = false;
}
else {
this.checked = checked || !this.checked;
}
// Reset native input when clicked with noop. The native checkbox becomes checked after
// click, reset it to be align with `checked` value of `mdc-checkbox`.
this._inputElement.nativeElement.checked = this.checked;
this._ngZone.runOutsideAngular((/**
* @return {?}
*/
function () {
return requestAnimationFrame((/**
* @return {?}
*/
function () { return _this._foundation.handleChange(); }));
}));
};
/**
* @private
* @return {?}
*/
MdcCheckbox.prototype._createRipple = /**
* @private
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var adapter = __assign(__assign({}, MdcRipple.createAdapter(this)), { isSurfaceActive: (/**
* @return {?}
*/
function () { return matches(_this._inputElement.nativeElement, ':active'); }), isUnbounded: (/**
* @return {?}
*/
function () { return true; }), isSurfaceDisabled: (/**
* @return {?}
*/
function () { return _this.disableRipple; }), deregisterInteractionHandler: (/**
* @param {?} evtType
* @param {?} handler
* @return {?}
*/
function (evtType, handler) {
return _this._inputElement.nativeElement.removeEventListener(evtType, handler, supportsPassiveEventListeners());
}), registerInteractionHandler: (/**
* @param {?} evtType
* @param {?} handler
* @return {?}
*/
function (evtType, handler) {
return _this._inputElement.nativeElement.addEventListener(evtType, handler, supportsPassiveEventListeners());
}) });
return new MdcRipple(this.elementRef, new MDCRippleFoundation(adapter));
};
/**
* @private
* @return {?}
*/
MdcCheckbox.prototype._loadListeners = /**
* @private
* @return {?}
*/
function () {
var _this = this;
if (!this._platform.isBrowser) {
return;
}
this._ngZone.runOutsideAngular((/**
* @return {?}
*/
function () {
return fromEvent(_this._root, 'animationend')
.pipe(takeUntil(_this._destroy), filter((/**
* @param {?} e
* @return {?}
*/
function (e) {
return e.target === _this._root;
})))
.subscribe((/**
* @return {?}
*/
function () {
return _this._ngZone.run((/**
* @return {?}
*/
function () { return _this._foundation.handleAnimationEnd(); }));
}));
}));
};
MdcCheckbox.decorators = [
{ type: Component, args: [{selector: 'mdc-checkbox',
exportAs: 'mdcCheckbox',
host: {
'[id]': 'id',
'class': 'mdc-checkbox',
'[class.mdc-checkbox--touch]': 'touch',
},
template: "\n <input type=\"checkbox\"\n #input\n class=\"mdc-checkbox__native-control\"\n [id]=\"inputId\"\n [attr.name]=\"name\"\n [tabIndex]=\"tabIndex\"\n [attr.aria-label]=\"ariaLabel || null\"\n [attr.aria-labelledby]=\"ariaLabelledby\"\n [disabled]=\"disabled\"\n [checked]=\"checked\"\n [attr.value]=\"value\"\n [indeterminate]=\"indeterminate\"\n (change)=\"_onInteraction($event)\"\n (click)=\"_onInputClick($event)\"/>\n <div class=\"mdc-checkbox__background\">\n <svg\n class=\"mdc-checkbox__checkmark\"\n viewBox=\"0 0 24 24\"\n focusable=\"false\">\n <path class=\"mdc-checkbox__checkmark-path\"\n fill=\"none\"\n d=\"M1.73,12.91 8.1,19.28 22.79,4.59\"/>\n </svg>\n <div class=\"mdc-checkbox__mixedmark\"></div>\n </div>\n <div *ngIf=\"!disableRipple && !disabled\" class=\"mdc-checkbox__ripple\"></div>\n ",
providers: [
MDC_CHECKBOX_CONTROL_VALUE_ACCESSOR,
MdcRipple,
{ provide: MdcFormFieldControl, useExisting: MdcCheckbox }
],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
},] },
];
/** @nocollapse */
MdcCheckbox.ctorParameters = function () { return [
{ type: Platform },
{ type: NgZone },
{ type: ChangeDetectorRef },
{ type: ElementRef },
{ type: MdcRipple },
{ type: MdcFormField, decorators: [{ type: Optional }] }
]; };
MdcCheckbox.propDecorators = {
id: [{ type: Input }],
name: [{ type: Input }],
checked: [{ type: Input }],
touch: [{ type: Input }],
disabled: [{ type: Input }],
value: [{ type: Input }],
indeterminate: [{ type: Input }],
indeterminateToChecked: [{ type: Input }],
disableRipple: [{ type: Input }],
tabIndex: [{ type: Input }],
ariaLabel: [{ type: Input, args: ['aria-label',] }],
ariaLabelledby: [{ type: Input, args: ['aria-labelledby',] }],
change: [{ type: Output }],
indeterminateChange: [{ type: Output }],
_inputElement: [{ type: ViewChild, args: ['input', { static: true },] }]
};
return MdcCheckbox;
}(MDCComponent));
/**
* @fileoverview added by tsickle
* Generated from: checkbox/module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var MdcCheckboxModule = /** @class */ (function () {
function MdcCheckboxModule() {
}
MdcCheckboxModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, MdcFormFieldModule],
exports: [
MdcFormFieldModule,
MdcCheckbox
],
declarations: [MdcCheckbox]
},] },
];
return MdcCheckboxModule;
}());
export { MDC_CHECKBOX_CONTROL_VALUE_ACCESSOR, MdcCheckbox, MdcCheckboxChange, MdcCheckboxModule };
//# sourceMappingURL=checkbox.es5.js.map