@tarienna/ng-tri-state-checkbox
Version:
A TriStateCheckbox for Angular 9
16 lines (14 loc) • 9.13 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/forms"),require("@angular/common")):"function"==typeof define&&define.amd?define("@tarienna/ng-tri-state-checkbox",["exports","@angular/core","@angular/forms","@angular/common"],t):t(((e=e||self).tarienna=e.tarienna||{},e.tarienna["ng-tri-state-checkbox"]={}),e.ng.core,e.ng.forms,e.ng.common)}(this,(function(e,t,r,o){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var n=function(e,t){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};var i;function a(e,t,r,o){var n,i=arguments.length,a=i<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(a=(i<3?n(a):i>3?n(t,r,a):n(t,r))||a);return i>3&&a&&Object.defineProperty(t,r,a),a}function s(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],o=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&o>=e.length&&(e=void 0),{value:e&&e[o++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}!function(e){e.NONE="NONE",e.SOME="SOME",e.ALL="ALL"}(i||(i={}));var c=function(){function e(e){this.renderer=e,this.checkboxClass="tri-state-checkbox-default",this._isDisabled=!1,this.controls=new Map,this.currentState=i.NONE,this.onChanged=function(e){},this.onTouched=function(){}}var o;return o=e,e.prototype.ngOnDestroy=function(){this.controls.clear()},e.prototype.registerOnChange=function(e){this.onChanged="function"==typeof e?e:function(e){}},e.prototype.registerOnTouched=function(e){this.onTouched="function"==typeof e?e:function(){}},e.prototype.setDisabledState=function(e){var t,r;this._isDisabled=e;try{for(var o=s(this.controls.keys()),n=o.next();!n.done;n=o.next()){n.value.setDisabled(e)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(t)throw t.error}}},Object.defineProperty(e.prototype,"isDisabled",{get:function(){return this._isDisabled},enumerable:!0,configurable:!0}),e.prototype.addControl=function(e){var t=this;e&&(this.controls.set(e,e.readValue()),this.refreshState(),setTimeout((function(){t.emitValueChanged()})))},e.prototype.removeControl=function(e){var t=this;this.controls.delete(e),this.refreshState(),setTimeout((function(){t.emitValueChanged()}))},e.prototype.changeControlValue=function(e,t){this.controls.set(e,t),this.refreshState(),this.emitValueChanged()},e.prototype.refreshState=function(){this.currentState=this.getState(),this.refreshCheckboxState()},e.prototype.getState=function(){var e,t,r=!1,o=i.NONE;try{for(var n=s(this.controls.values()),a=n.next();!a.done;a=n.next()){var c=a.value;c&&(o=i.SOME),c||(r=!0)}}catch(t){e={error:t}}finally{try{a&&!a.done&&(t=n.return)&&t.call(n)}finally{if(e)throw e.error}}return r||o!==i.SOME||(o=i.ALL),o},e.prototype.onClick=function(){var e=this.getState();e===i.SOME||e===i.ALL?(this.writeAllControlValues(!1),this.currentState=i.NONE):e===i.NONE&&(this.writeAllControlValues(!0),this.currentState=i.ALL),this.refreshCheckboxState(),this.emitValueChanged()},e.prototype.writeAllControlValues=function(e){var t,r;try{for(var o=s(this.controls.keys()),n=o.next();!n.done;n=o.next()){var i=n.value;i&&i.writeValue(e)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(t)throw t.error}}},e.prototype.refreshCheckboxState=function(){if(this.customCheckbox&&this.customCheckbox.nativeElement){var e=!1,t=!1;this.currentState===i.ALL?(e=!0,t=!1):this.currentState===i.SOME&&(e=!0,t=!0),this.renderer.setProperty(this.customCheckbox.nativeElement,"checked",e),this.customCheckbox.nativeElement.indeterminate=t}},e.prototype.emitValueChanged=function(){var e,t,r=[];try{for(var o=s(this.controls.values()),n=o.next();!n.done;n=o.next()){var i=n.value;i&&r.push(i)}}catch(t){e={error:t}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(e)throw e.error}}this.onChanged(r),this.onTouched()},e.prototype.reset=function(){this.writeAllControlValues(!1),this.refreshState(),this.emitValueChanged()},e.prototype.writeValue=function(e){e&&e.hasOwnProperty("length")&&e.length>0&&console.warn("It's currently not possible to set values. Maybe later there is a solution to do that.")},e.ctorParameters=function(){return[{type:t.Renderer2}]},a([t.Input()],e.prototype,"checkboxClass",void 0),a([t.ViewChild("customCheckbox",{static:!1})],e.prototype,"customCheckbox",void 0),e=o=a([t.Component({selector:"ng-tri-state-checkbox",template:'<input type="checkbox" [class]="checkboxClass" (change)="onClick()" #customCheckbox [disabled]="isDisabled">\n',providers:[{provide:r.NG_VALUE_ACCESSOR,useExisting:t.forwardRef((function(){return o})),multi:!0}],exportAs:"ngTriStateCheckbox",encapsulation:t.ViewEncapsulation.None,styles:[".tri-state-checkbox-default{height:1rem;width:1rem;display:block;position:relative;pointer-events:auto;-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:#fff;background-image:none;border:1px solid #adb5bd;border-radius:.15rem;box-sizing:border-box}.tri-state-checkbox-default:checked:not(:indeterminate){background-color:#0099da;color:#fff}.tri-state-checkbox-default:checked:not(:indeterminate):after{content:\"\";display:block;position:absolute;top:-1px;left:-1px;width:1rem;height:1rem;pointer-events:none;color:#fff;background-color:#0099da;background-repeat:no-repeat;background-size:50% 50%;background-position:50%;background-image:url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%23fff' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3e%3c/svg%3e\");border:1px solid #0099da;border-radius:.15rem}.tri-state-checkbox-default:indeterminate{background-color:#fff;color:#adb5bd;border-color:#0099da}.tri-state-checkbox-default:indeterminate:after{content:\"\";display:block;position:absolute;top:calc(.225rem - 1px);left:calc(.225rem - 1px);width:.55rem;height:.55rem;pointer-events:none;background-color:#0099da;border:1px solid #0099da;border-radius:.15rem}"]})],e)}(),l=function(e){function o(t,r){var o=e.call(this,t,r)||this;return o.renderer=t,o.elementRef=r,o.checkedValue=null,o.isDisabled=!1,o}var i;return function(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}(o,e),i=o,o.prototype.ngOnInit=function(){this.registry&&this.registry.addControl(this)},o.prototype.ngOnDestroy=function(){this.registry&&this.registry.removeControl(this)},o.prototype.ngOnChanges=function(e){if(e&&e.hasOwnProperty("registry")&&!e.registry.firstChange){var t=e.registry;t.previousValue&&t.previousValue.removeControl(this),t.currentValue&&t.currentValue.addControl(this)}},o.prototype.readValue=function(){return this.checkedValue?this.value?this.value:this.checkedValue:null},o.prototype.writeValue=function(t){e.prototype.writeValue.call(this,t),this.checkedValue=t,this.updateValueOnRegistry()},o.prototype.setDisabled=function(t){this.isDisabled=t,e.prototype.setDisabledState.call(this,t)},o.prototype.updateValueOnRegistry=function(){this.registry&&this.registry.changeControlValue(this,this.readValue())},o.prototype.onChangeValue=function(e){this.checkedValue=e,this.updateValueOnRegistry()},o.ctorParameters=function(){return[{type:t.Renderer2},{type:t.ElementRef}]},a([t.Input()],o.prototype,"value",void 0),a([t.Input("ngTriStateControl")],o.prototype,"registry",void 0),a([t.HostListener("change",["$event.target.checked"])],o.prototype,"onChangeValue",null),o=i=a([t.Directive({selector:"input[type=checkbox][ngTriStateControl]",providers:[{provide:r.NG_VALUE_ACCESSOR,useExisting:t.forwardRef((function(){return i})),multi:!0}],exportAs:"ngTriStateControl"})],o)}(r.CheckboxControlValueAccessor),u=function(){function e(){}return e=a([t.NgModule({declarations:[c,l],imports:[o.CommonModule,r.FormsModule,r.ReactiveFormsModule],exports:[c,l]})],e)}();e.NgTriStateCheckboxComponent=c,e.NgTriStateCheckboxModule=u,e.NgTriStateControlDirective=l,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=tarienna-ng-tri-state-checkbox.umd.min.js.map