@angular-mdc/web
Version:
721 lines (679 loc) • 23.8 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
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular-mdc/web/form-field'), require('@angular/forms'), require('@angular/cdk/platform'), require('@angular/cdk/coercion'), require('@material/switch'), require('@material/ripple'), require('@angular-mdc/web/dom'), require('@angular-mdc/web/base'), require('@angular-mdc/web/ripple')) :
typeof define === 'function' && define.amd ? define('@angular-mdc/web/switch', ['exports', '@angular/core', '@angular-mdc/web/form-field', '@angular/forms', '@angular/cdk/platform', '@angular/cdk/coercion', '@material/switch', '@material/ripple', '@angular-mdc/web/dom', '@angular-mdc/web/base', '@angular-mdc/web/ripple'], factory) :
(global = global || self, factory((global.ng = global.ng || {}, global.ng.web = global.ng.web || {}, global.ng.web.switch = {}), global.ng.core, global.ng.web.formField, global.ng.forms, global.ng.cdk.platform, global.ng.cdk.coercion, global.mdc.switch, global.mdc.ripple, global.ng.web.dom, global.ng.web.base, global.ng.web.ripple));
}(this, (function (exports, core, formField, forms, platform, coercion, _switch, ripple, dom, base, ripple$1) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var _extendStatics = function extendStatics(d, b) {
_extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) {
if (b.hasOwnProperty(p)) d[p] = b[p];
}
};
return _extendStatics(d, b);
};
function __extends(d, b) {
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var _assign = function __assign() {
_assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return _assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = {
label: 0,
sent: function sent() {
if (t[0] & 1) throw t[1];
return t[1];
},
trys: [],
ops: []
},
f,
y,
t,
g;
return g = {
next: verb(0),
"throw": verb(1),
"return": verb(2)
}, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
return this;
}), g;
function verb(n) {
return function (v) {
return step([n, v]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) {
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return {
value: op[1],
done: false
};
case 5:
_.label++;
y = op[1];
op = [0];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2]) _.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [6, e];
y = 0;
} finally {
f = t = 0;
}
}
if (op[0] & 5) throw op[1];
return {
value: op[0] ? op[1] : void 0,
done: true
};
}
}
/**
* @fileoverview added by tsickle
* Generated from: switch/switch.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var MDC_SWITCH_CONTROL_VALUE_ACCESSOR = {
provide: forms.NG_VALUE_ACCESSOR,
useExisting: core.forwardRef((/**
* @return {?}
*/
function () { return MdcSwitch; })),
multi: true
};
/**
* Change event object emitted by MdcSwitch.
*/
var /**
* Change event object emitted by MdcSwitch.
*/
MdcSwitchChange = /** @class */ (function () {
function MdcSwitchChange(source, checked) {
this.source = source;
this.checked = checked;
}
return MdcSwitchChange;
}());
/** @type {?} */
var nextUniqueId = 0;
var MdcSwitch = /** @class */ (function (_super) {
__extends(MdcSwitch, _super);
function MdcSwitch(_changeDetectorRef, ripple, elementRef, _parentFormField) {
var _this = _super.call(this, elementRef) || this;
_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 core.EventEmitter();
/**
* View to model callback called when value changes
*/
_this._onChange = (/**
* @param {?} _
* @return {?}
*/
function (_) { });
/**
* View to model callback called when control 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(MdcSwitch.prototype, "checked", {
get: /**
* @return {?}
*/
function () {
return this._checked;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
if (this.disabled) {
return;
}
this._checked = coercion.coerceBooleanProperty(value);
this._changeDetectorRef.markForCheck();
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcSwitch.prototype, "disabled", {
get: /**
* @return {?}
*/
function () {
return this._disabled;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this.setDisabledState(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcSwitch.prototype, "required", {
get: /**
* @return {?}
*/
function () {
return this._required;
},
set: /**
* @param {?} value
* @return {?}
*/
function (value) {
this._required = coercion.coerceBooleanProperty(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(MdcSwitch.prototype, "inputId", {
get: /**
* @return {?}
*/
function () {
return (this.id || this._uniqueId) + "-input";
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
MdcSwitch.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._getHostElement().classList.add(className); }),
removeClass: (/**
* @param {?} className
* @return {?}
*/
function (className) { return _this._getHostElement().classList.remove(className); }),
setNativeControlChecked: (/**
* @param {?} checked
* @return {?}
*/
function (checked) { return _this._getInputElement().checked = checked; }),
setNativeControlDisabled: (/**
* @param {?} disabled
* @return {?}
*/
function (disabled) { return _this._getInputElement().disabled = disabled; }),
setNativeControlAttr: (/**
* @param {?} attr
* @param {?} value
* @return {?}
*/
function (attr, value) { return _this._getInputElement().setAttribute(attr, value); })
};
return new _switch.MDCSwitchFoundation(adapter);
};
/**
* @return {?}
*/
MdcSwitch.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
var _this = this;
this._initialized = true;
this.ripple = this._createRipple();
this.ripple.init();
this._asyncBuildFoundation()
.then((/**
* @return {?}
*/
function () {
_this._foundation.init();
_this.setDisabledState(_this._inputElement.nativeElement.disabled);
}));
};
/**
* @return {?}
*/
MdcSwitch.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.ripple.destroy();
this.destroy();
};
/**
* @return {?}
*/
MdcSwitch.prototype._asyncBuildFoundation = /**
* @return {?}
*/
function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this._foundation = this.getDefaultFoundation();
return [2 /*return*/];
});
});
};
/**
* @param {?} evt
* @return {?}
*/
MdcSwitch.prototype.onChange = /**
* @param {?} evt
* @return {?}
*/
function (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 {?}
*/
MdcSwitch.prototype.onInputClick = /**
* @param {?} evt
* @return {?}
*/
function (evt) {
evt.stopPropagation();
};
/**
* @return {?}
*/
MdcSwitch.prototype.onBlur = /**
* @return {?}
*/
function () {
this._onTouched();
};
/**
* @param {?} value
* @return {?}
*/
MdcSwitch.prototype.writeValue = /**
* @param {?} value
* @return {?}
*/
function (value) {
this.checked = !!value;
};
/**
* @param {?} fn
* @return {?}
*/
MdcSwitch.prototype.registerOnChange = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
this._onChange = fn;
};
/**
* @param {?} fn
* @return {?}
*/
MdcSwitch.prototype.registerOnTouched = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
this._onTouched = fn;
};
/** Toggles the checked state of the switch. */
/**
* Toggles the checked state of the switch.
* @return {?}
*/
MdcSwitch.prototype.toggle = /**
* Toggles the checked state of the switch.
* @return {?}
*/
function () {
this.checked = !this.checked;
this._onChange(this.checked);
};
/**
* @param {?} disabled
* @return {?}
*/
MdcSwitch.prototype.setDisabledState = /**
* @param {?} disabled
* @return {?}
*/
function (disabled) {
var _a;
/** @type {?} */
var newValue = coercion.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 {?}
*/
MdcSwitch.prototype.focus = /**
* @return {?}
*/
function () {
this._inputElement.nativeElement.focus();
};
/**
* @private
* @return {?}
*/
MdcSwitch.prototype._createRipple = /**
* @private
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var rippleSurface = (/** @type {?} */ (this.thumbUnderlay.nativeElement));
/** @type {?} */
var adapter = _assign(_assign({}, ripple$1.MdcRipple.createAdapter(this)), { addClass: (/**
* @param {?} className
* @return {?}
*/
function (className) { return rippleSurface.classList.add(className); }), computeBoundingRect: (/**
* @return {?}
*/
function () { return rippleSurface.getBoundingClientRect(); }), deregisterInteractionHandler: (/**
* @param {?} evtType
* @param {?} handler
* @return {?}
*/
function (evtType, handler) {
return _this._inputElement.nativeElement.removeEventListener(evtType, handler, platform.supportsPassiveEventListeners());
}), isSurfaceActive: (/**
* @return {?}
*/
function () { return dom.matches(_this._inputElement.nativeElement, ':active'); }), isUnbounded: (/**
* @return {?}
*/
function () { return true; }), registerInteractionHandler: (/**
* @param {?} evtType
* @param {?} handler
* @return {?}
*/
function (evtType, handler) {
return _this._inputElement.nativeElement.addEventListener(evtType, handler, platform.supportsPassiveEventListeners());
}), removeClass: (/**
* @param {?} className
* @return {?}
*/
function (className) { return rippleSurface.classList.remove(className); }), updateCssVariable: (/**
* @param {?} varName
* @param {?} value
* @return {?}
*/
function (varName, value) { return rippleSurface.style.setProperty(varName, value); }) });
return new ripple$1.MdcRipple(this.elementRef, new ripple.MDCRippleFoundation(adapter));
};
/**
* Emits a change event on the `change` output. Also notifies the FormControl about the change.
*/
/**
* Emits a change event on the `change` output. Also notifies the FormControl about the change.
* @private
* @return {?}
*/
MdcSwitch.prototype._emitChangeEvent = /**
* Emits a change event on the `change` output. Also notifies the FormControl about the change.
* @private
* @return {?}
*/
function () {
this._onChange(this.checked);
this.change.emit(new MdcSwitchChange(this, this.checked));
};
/** Retrieves the DOM element of the component input. */
/**
* Retrieves the DOM element of the component input.
* @private
* @return {?}
*/
MdcSwitch.prototype._getInputElement = /**
* Retrieves the DOM element of the component input.
* @private
* @return {?}
*/
function () {
return this._inputElement.nativeElement;
};
/** Retrieves the DOM element of the component host. */
/**
* Retrieves the DOM element of the component host.
* @private
* @return {?}
*/
MdcSwitch.prototype._getHostElement = /**
* Retrieves the DOM element of the component host.
* @private
* @return {?}
*/
function () {
return this.elementRef.nativeElement;
};
MdcSwitch.decorators = [
{ type: core.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: "\n <div class=\"mdc-switch__track\"></div>\n <div #thumbUnderlay class=\"mdc-switch__thumb-underlay\">\n <div class=\"mdc-switch__thumb\"></div>\n <input type=\"checkbox\"\n #input\n role=\"switch\"\n class=\"mdc-switch__native-control\"\n [id]=\"inputId\"\n [attr.name]=\"name\"\n [attr.aria-label]=\"ariaLabel\"\n [attr.aria-labelledby]=\"ariaLabelledby\"\n [tabIndex]=\"tabIndex\"\n [disabled]=\"disabled\"\n [required]=\"required\"\n [checked]=\"checked\"\n (blur)=\"onBlur()\"\n (click)=\"onInputClick($event)\"\n (change)=\"onChange($event)\"/>\n </div>\n ",
changeDetection: core.ChangeDetectionStrategy.OnPush,
encapsulation: core.ViewEncapsulation.None,
providers: [
MDC_SWITCH_CONTROL_VALUE_ACCESSOR,
{ provide: formField.MdcFormFieldControl, useExisting: MdcSwitch },
ripple$1.MdcRipple
]
},] },
];
/** @nocollapse */
MdcSwitch.ctorParameters = function () { return [
{ type: core.ChangeDetectorRef },
{ type: ripple$1.MdcRipple },
{ type: core.ElementRef },
{ type: formField.MdcFormField, decorators: [{ type: core.Optional }] }
]; };
MdcSwitch.propDecorators = {
id: [{ type: core.Input }],
name: [{ type: core.Input }],
tabIndex: [{ type: core.Input }],
value: [{ type: core.Input }],
checked: [{ type: core.Input }],
disabled: [{ type: core.Input }],
required: [{ type: core.Input }],
ariaLabel: [{ type: core.Input, args: ['aria-label',] }],
ariaLabelledby: [{ type: core.Input, args: ['aria-labelledby',] }],
change: [{ type: core.Output }],
_inputElement: [{ type: core.ViewChild, args: ['input', { static: true },] }],
thumbUnderlay: [{ type: core.ViewChild, args: ['thumbUnderlay', { static: false },] }]
};
return MdcSwitch;
}(base.MDCComponent));
/**
* @fileoverview added by tsickle
* Generated from: switch/module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var MdcSwitchModule = /** @class */ (function () {
function MdcSwitchModule() {
}
MdcSwitchModule.decorators = [
{ type: core.NgModule, args: [{
imports: [formField.MdcFormFieldModule],
exports: [
formField.MdcFormFieldModule,
MdcSwitch
],
declarations: [MdcSwitch]
},] },
];
return MdcSwitchModule;
}());
exports.MDC_SWITCH_CONTROL_VALUE_ACCESSOR = MDC_SWITCH_CONTROL_VALUE_ACCESSOR;
exports.MdcSwitch = MdcSwitch;
exports.MdcSwitchChange = MdcSwitchChange;
exports.MdcSwitchModule = MdcSwitchModule;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=web-switch.umd.js.map