UNPKG

@angular-mdc/web

Version:
22 lines (20 loc) 10.2 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 */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(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")):"function"==typeof define&&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"],t):t(((e=e||self).ng=e.ng||{},e.ng.web=e.ng.web||{},e.ng.web.switch={}),e.ng.core,e.ng.web.formField,e.ng.forms,e.ng.cdk.platform,e.ng.cdk.coercion,e.mdc.switch,e.mdc.ripple,e.ng.web.dom,e.ng.web.base,e.ng.web.ripple)}(this,(function(e,t,n,i,r,o,a,c,u,l,s){"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. ***************************************************************************** */var d=function(e,t){return(d=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)};var p=function(){return(p=Object.assign||function(e){for(var t,n=1,i=arguments.length;n<i;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};function h(e,t,n,i){return new(n||(n=Promise))((function(r,o){function a(e){try{u(i.next(e))}catch(e){o(e)}}function c(e){try{u(i.throw(e))}catch(e){o(e)}}function u(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,c)}u((i=i.apply(e,t||[])).next())}))}function f(e,t){var n,i,r,o,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(o){return function(c){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,i&&(r=2&o[0]?i.return:o[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,i=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(r=(r=a.trys).length>0&&r[r.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){a.label=o[1];break}if(6===o[0]&&a.label<r[1]){a.label=r[1],r=o;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(o);break}r[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],i=0}finally{n=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}}var m={provide:i.NG_VALUE_ACCESSOR,useExisting:t.forwardRef((function(){return y})),multi:!0},b=function(e,t){this.source=e,this.checked=t},g=0,y=function(e){function i(n,i,r,o){var a=e.call(this,r)||this;return a._changeDetectorRef=n,a.ripple=i,a.elementRef=r,a._parentFormField=o,a._uniqueId="mdc-switch-"+ ++g,a._initialized=!1,a.id=a._uniqueId,a.name=null,a.tabIndex=0,a.value=null,a._checked=!1,a._disabled=!1,a._required=!1,a.ariaLabel=null,a.ariaLabelledby=null,a.change=new t.EventEmitter,a._onChange=function(e){},a._onTouched=function(){},a._root=a.elementRef.nativeElement,a._parentFormField&&o.elementRef.nativeElement.classList.add("mdc-form-field"),a}return function(e,t){function n(){this.constructor=e}d(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}(i,e),Object.defineProperty(i.prototype,"checked",{get:function(){return this._checked},set:function(e){this.disabled||(this._checked=o.coerceBooleanProperty(e),this._changeDetectorRef.markForCheck())},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"disabled",{get:function(){return this._disabled},set:function(e){this.setDisabledState(e)},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"required",{get:function(){return this._required},set:function(e){this._required=o.coerceBooleanProperty(e)},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"inputId",{get:function(){return(this.id||this._uniqueId)+"-input"},enumerable:!0,configurable:!0}),i.prototype.getDefaultFoundation=function(){var e=this;if(this._initialized){var t={addClass:function(t){return e._getHostElement().classList.add(t)},removeClass:function(t){return e._getHostElement().classList.remove(t)},setNativeControlChecked:function(t){return e._getInputElement().checked=t},setNativeControlDisabled:function(t){return e._getInputElement().disabled=t},setNativeControlAttr:function(t,n){return e._getInputElement().setAttribute(t,n)}};return new a.MDCSwitchFoundation(t)}},i.prototype.ngAfterViewInit=function(){var e=this;this._initialized=!0,this.ripple=this._createRipple(),this.ripple.init(),this._asyncBuildFoundation().then((function(){e._foundation.init(),e.setDisabledState(e._inputElement.nativeElement.disabled)}))},i.prototype.ngOnDestroy=function(){this.ripple.destroy(),this.destroy()},i.prototype._asyncBuildFoundation=function(){return h(this,void 0,void 0,(function(){return f(this,(function(e){return this._foundation=this.getDefaultFoundation(),[2]}))}))},i.prototype.onChange=function(e){e.stopPropagation(),this._foundation.handleChange(e),this._checked=this._inputElement.nativeElement.checked,this._foundation.setChecked(this._checked),this._emitChangeEvent(),this._changeDetectorRef.markForCheck()},i.prototype.onInputClick=function(e){e.stopPropagation()},i.prototype.onBlur=function(){this._onTouched()},i.prototype.writeValue=function(e){this.checked=!!e},i.prototype.registerOnChange=function(e){this._onChange=e},i.prototype.registerOnTouched=function(e){this._onTouched=e},i.prototype.toggle=function(){this.checked=!this.checked,this._onChange(this.checked)},i.prototype.setDisabledState=function(e){var t,n=o.coerceBooleanProperty(e);n!==this._disabled&&(this._disabled=n,null===(t=this._foundation)||void 0===t||t.setDisabled(n),this._changeDetectorRef.markForCheck())},i.prototype.focus=function(){this._inputElement.nativeElement.focus()},i.prototype._createRipple=function(){var e=this,t=this.thumbUnderlay.nativeElement,n=p(p({},s.MdcRipple.createAdapter(this)),{addClass:function(e){return t.classList.add(e)},computeBoundingRect:function(){return t.getBoundingClientRect()},deregisterInteractionHandler:function(t,n){return e._inputElement.nativeElement.removeEventListener(t,n,r.supportsPassiveEventListeners())},isSurfaceActive:function(){return u.matches(e._inputElement.nativeElement,":active")},isUnbounded:function(){return!0},registerInteractionHandler:function(t,n){return e._inputElement.nativeElement.addEventListener(t,n,r.supportsPassiveEventListeners())},removeClass:function(e){return t.classList.remove(e)},updateCssVariable:function(e,n){return t.style.setProperty(e,n)}});return new s.MdcRipple(this.elementRef,new c.MDCRippleFoundation(n))},i.prototype._emitChangeEvent=function(){this._onChange(this.checked),this.change.emit(new b(this,this.checked))},i.prototype._getInputElement=function(){return this._inputElement.nativeElement},i.prototype._getHostElement=function(){return this.elementRef.nativeElement},i.decorators=[{type:t.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:t.ChangeDetectionStrategy.OnPush,encapsulation:t.ViewEncapsulation.None,providers:[m,{provide:n.MdcFormFieldControl,useExisting:i},s.MdcRipple]}]}],i.ctorParameters=function(){return[{type:t.ChangeDetectorRef},{type:s.MdcRipple},{type:t.ElementRef},{type:n.MdcFormField,decorators:[{type:t.Optional}]}]},i.propDecorators={id:[{type:t.Input}],name:[{type:t.Input}],tabIndex:[{type:t.Input}],value:[{type:t.Input}],checked:[{type:t.Input}],disabled:[{type:t.Input}],required:[{type:t.Input}],ariaLabel:[{type:t.Input,args:["aria-label"]}],ariaLabelledby:[{type:t.Input,args:["aria-labelledby"]}],change:[{type:t.Output}],_inputElement:[{type:t.ViewChild,args:["input",{static:!0}]}],thumbUnderlay:[{type:t.ViewChild,args:["thumbUnderlay",{static:!1}]}]},i}(l.MDCComponent),_=function(){function e(){}return e.decorators=[{type:t.NgModule,args:[{imports:[n.MdcFormFieldModule],exports:[n.MdcFormFieldModule,y],declarations:[y]}]}],e}();e.MDC_SWITCH_CONTROL_VALUE_ACCESSOR=m,e.MdcSwitch=y,e.MdcSwitchChange=b,e.MdcSwitchModule=_,Object.defineProperty(e,"__esModule",{value:!0})}));