UNPKG

@angular-mdc/web

Version:
446 lines (442 loc) 13.3 kB
/** * @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, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, ElementRef, Optional, Input, Output, ViewChild, NgModule } from '@angular/core'; import { MdcFormFieldControl, MdcFormField, MdcFormFieldModule } from '@angular-mdc/web/form-field'; import { __awaiter } from 'tslib'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { supportsPassiveEventListeners } from '@angular/cdk/platform'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { MDCSwitchFoundation } from '@material/switch'; import { MDCRippleFoundation } from '@material/ripple'; import { matches } from '@angular-mdc/web/dom'; import { MDCComponent } from '@angular-mdc/web/base'; import { MdcRipple } from '@angular-mdc/web/ripple'; /** * @fileoverview added by tsickle * Generated from: switch/switch.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const MDC_SWITCH_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef((/** * @return {?} */ () => MdcSwitch)), multi: true }; /** * Change event object emitted by MdcSwitch. */ class MdcSwitchChange { /** * @param {?} source * @param {?} checked */ constructor(source, checked) { this.source = source; this.checked = checked; } } /** @type {?} */ let nextUniqueId = 0; class MdcSwitch extends MDCComponent { /** * @param {?} _changeDetectorRef * @param {?} ripple * @param {?} elementRef * @param {?} _parentFormField */ constructor(_changeDetectorRef, ripple, elementRef, _parentFormField) { super(elementRef); this._changeDetectorRef = _changeDetectorRef; this.ripple = ripple; this.elementRef = elementRef; this._parentFormField = _parentFormField; this._uniqueId = `mdc-switch-${++nextUniqueId}`; this._initialized = false; this.id = this._uniqueId; this.name = null; this.tabIndex = 0; /** * The value attribute of the native input element */ this.value = null; this._checked = false; this._disabled = false; this._required = false; /** * Used to set the aria-label attribute on the underlying input element. */ this.ariaLabel = null; /** * Used to set the aria-labelledby attribute on the underlying input element. */ this.ariaLabelledby = null; this.change = new EventEmitter(); /** * View to model callback called when value changes */ this._onChange = (/** * @param {?} _ * @return {?} */ (_) => { }); /** * View to model callback called when control has been touched */ this._onTouched = (/** * @return {?} */ () => { }); this._root = this.elementRef.nativeElement; if (this._parentFormField) { _parentFormField.elementRef.nativeElement.classList.add('mdc-form-field'); } } /** * @return {?} */ get checked() { return this._checked; } /** * @param {?} value * @return {?} */ set checked(value) { if (this.disabled) { return; } this._checked = coerceBooleanProperty(value); this._changeDetectorRef.markForCheck(); } /** * @return {?} */ get disabled() { return this._disabled; } /** * @param {?} value * @return {?} */ set disabled(value) { this.setDisabledState(value); } /** * @return {?} */ get required() { return this._required; } /** * @param {?} value * @return {?} */ set required(value) { this._required = coerceBooleanProperty(value); } /** * @return {?} */ get inputId() { return `${this.id || this._uniqueId}-input`; } /** * @return {?} */ getDefaultFoundation() { // Do not initialize foundation until ngAfterViewInit runs if (!this._initialized) { return undefined; } /** @type {?} */ const adapter = { addClass: (/** * @param {?} className * @return {?} */ (className) => this._getHostElement().classList.add(className)), removeClass: (/** * @param {?} className * @return {?} */ (className) => this._getHostElement().classList.remove(className)), setNativeControlChecked: (/** * @param {?} checked * @return {?} */ (checked) => this._getInputElement().checked = checked), setNativeControlDisabled: (/** * @param {?} disabled * @return {?} */ (disabled) => this._getInputElement().disabled = disabled), setNativeControlAttr: (/** * @param {?} attr * @param {?} value * @return {?} */ (attr, value) => this._getInputElement().setAttribute(attr, value)) }; return new MDCSwitchFoundation(adapter); } /** * @return {?} */ ngAfterViewInit() { this._initialized = true; this.ripple = this._createRipple(); this.ripple.init(); this._asyncBuildFoundation() .then((/** * @return {?} */ () => { this._foundation.init(); this.setDisabledState(this._inputElement.nativeElement.disabled); })); } /** * @return {?} */ ngOnDestroy() { this.ripple.destroy(); this.destroy(); } /** * @return {?} */ _asyncBuildFoundation() { return __awaiter(this, void 0, void 0, function* () { this._foundation = this.getDefaultFoundation(); }); } /** * @param {?} evt * @return {?} */ onChange(evt) { evt.stopPropagation(); this._foundation.handleChange(evt); this._checked = this._inputElement.nativeElement.checked; this._foundation.setChecked(this._checked); this._emitChangeEvent(); this._changeDetectorRef.markForCheck(); } /** * @param {?} evt * @return {?} */ onInputClick(evt) { evt.stopPropagation(); } /** * @return {?} */ onBlur() { this._onTouched(); } /** * @param {?} value * @return {?} */ writeValue(value) { this.checked = !!value; } /** * @param {?} fn * @return {?} */ registerOnChange(fn) { this._onChange = fn; } /** * @param {?} fn * @return {?} */ registerOnTouched(fn) { this._onTouched = fn; } /** * Toggles the checked state of the switch. * @return {?} */ toggle() { this.checked = !this.checked; this._onChange(this.checked); } /** * @param {?} disabled * @return {?} */ setDisabledState(disabled) { var _a; /** @type {?} */ const 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(); } } /** * @return {?} */ focus() { this._inputElement.nativeElement.focus(); } /** * @private * @return {?} */ _createRipple() { /** @type {?} */ const rippleSurface = (/** @type {?} */ (this.thumbUnderlay.nativeElement)); /** @type {?} */ const adapter = Object.assign(Object.assign({}, MdcRipple.createAdapter(this)), { addClass: (/** * @param {?} className * @return {?} */ (className) => rippleSurface.classList.add(className)), computeBoundingRect: (/** * @return {?} */ () => rippleSurface.getBoundingClientRect()), deregisterInteractionHandler: (/** * @param {?} evtType * @param {?} handler * @return {?} */ (evtType, handler) => this._inputElement.nativeElement.removeEventListener(evtType, handler, supportsPassiveEventListeners())), isSurfaceActive: (/** * @return {?} */ () => matches(this._inputElement.nativeElement, ':active')), isUnbounded: (/** * @return {?} */ () => true), registerInteractionHandler: (/** * @param {?} evtType * @param {?} handler * @return {?} */ (evtType, handler) => this._inputElement.nativeElement.addEventListener(evtType, handler, supportsPassiveEventListeners())), removeClass: (/** * @param {?} className * @return {?} */ (className) => rippleSurface.classList.remove(className)), updateCssVariable: (/** * @param {?} varName * @param {?} value * @return {?} */ (varName, value) => rippleSurface.style.setProperty(varName, value)) }); return new MdcRipple(this.elementRef, new MDCRippleFoundation(adapter)); } /** * Emits a change event on the `change` output. Also notifies the FormControl about the change. * @private * @return {?} */ _emitChangeEvent() { this._onChange(this.checked); this.change.emit(new MdcSwitchChange(this, this.checked)); } /** * Retrieves the DOM element of the component input. * @private * @return {?} */ _getInputElement() { return this._inputElement.nativeElement; } /** * Retrieves the DOM element of the component host. * @private * @return {?} */ _getHostElement() { return this.elementRef.nativeElement; } } MdcSwitch.decorators = [ { type: Component, args: [{selector: 'mdc-switch', host: { '[id]': 'id', 'class': 'mdc-switch', '[class.mdc-switch--checked]': 'checked', '[class.mdc-switch--disabled]': 'disabled', '(focus)': '_inputElement.nativeElement.focus()' }, template: ` <div class="mdc-switch__track"></div> <div #thumbUnderlay class="mdc-switch__thumb-underlay"> <div class="mdc-switch__thumb"></div> <input type="checkbox" #input role="switch" class="mdc-switch__native-control" [id]="inputId" [attr.name]="name" [attr.aria-label]="ariaLabel" [attr.aria-labelledby]="ariaLabelledby" [tabIndex]="tabIndex" [disabled]="disabled" [required]="required" [checked]="checked" (blur)="onBlur()" (click)="onInputClick($event)" (change)="onChange($event)"/> </div> `, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, providers: [ MDC_SWITCH_CONTROL_VALUE_ACCESSOR, { provide: MdcFormFieldControl, useExisting: MdcSwitch }, MdcRipple ] },] }, ]; /** @nocollapse */ MdcSwitch.ctorParameters = () => [ { type: ChangeDetectorRef }, { type: MdcRipple }, { type: ElementRef }, { type: MdcFormField, decorators: [{ type: Optional }] } ]; MdcSwitch.propDecorators = { id: [{ type: Input }], name: [{ type: Input }], tabIndex: [{ type: Input }], value: [{ type: Input }], checked: [{ type: Input }], disabled: [{ type: Input }], required: [{ type: Input }], ariaLabel: [{ type: Input, args: ['aria-label',] }], ariaLabelledby: [{ type: Input, args: ['aria-labelledby',] }], change: [{ type: Output }], _inputElement: [{ type: ViewChild, args: ['input', { static: true },] }], thumbUnderlay: [{ type: ViewChild, args: ['thumbUnderlay', { static: false },] }] }; /** * @fileoverview added by tsickle * Generated from: switch/module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class MdcSwitchModule { } MdcSwitchModule.decorators = [ { type: NgModule, args: [{ imports: [MdcFormFieldModule], exports: [ MdcFormFieldModule, MdcSwitch ], declarations: [MdcSwitch] },] }, ]; export { MDC_SWITCH_CONTROL_VALUE_ACCESSOR, MdcSwitch, MdcSwitchChange, MdcSwitchModule }; //# sourceMappingURL=switch.js.map