UNPKG

ng-checkall

Version:

A simple directive to "Check All" checkboxes behavior

160 lines (153 loc) 5.19 kB
import { Output, EventEmitter, Directive, HostListener, Input, ContentChildren, NgModule } from '@angular/core'; import { NgModel } from '@angular/forms'; class AbstractCheckDirective { constructor(_model) { this._model = _model; this.ngModelChange = new EventEmitter(); this.registered = false; this.lockFn = () => false; this.isRegistered = () => this.registered; } registerLockFn(fn) { this.registered = true; this.lockFn = fn; } _onModelChange($event) { if (!this.lockFn() && !this._model.disabled) { this.checkedEmitter.emit($event); } } setValue(value, runBody = true) { if (runBody && !this._model.disabled) { this.ngModelChange.emit(value); this._model.valueAccessor.writeValue(value); } } } AbstractCheckDirective.propDecorators = { "ngModelChange": [{ type: Output },], }; class CheckAllDirective extends AbstractCheckDirective { constructor(model) { super(model); this.model = model; this.checkedAll = new EventEmitter(); this.onModelChange = this._onModelChange; this.checkedEmitter = this.checkedAll; } } CheckAllDirective.decorators = [ { type: Directive, args: [{ selector: '[checkAll]', },] }, ]; CheckAllDirective.ctorParameters = () => [ { type: NgModel, }, ]; CheckAllDirective.propDecorators = { "checkedAll": [{ type: Output },], "onModelChange": [{ type: HostListener, args: ['ngModelChange', ['$event'],] },], }; class CheckOneDirective extends AbstractCheckDirective { constructor(model) { super(model); this.model = model; this.checkedOne = new EventEmitter(); this.checkedEmitter = this.checkedOne; } set disabled(val) { this._onModelChange.bind(this)(true); } ngAfterViewInit() { this.model.valueChanges.subscribe(this._onModelChange.bind(this)); } getValue() { return this.model.value; } isDisabled() { return this.model.disabled; } } CheckOneDirective.decorators = [ { type: Directive, args: [{ selector: '[checkOne]', },] }, ]; CheckOneDirective.ctorParameters = () => [ { type: NgModel, }, ]; CheckOneDirective.propDecorators = { "disabled": [{ type: Input, args: ['disabled',] },], "checkedOne": [{ type: Output },], }; class CheckContainerDirective { constructor() { this.subscribes = []; this.lock = false; } process(list, fn) { const generatedFn = (item) => { if (!item.isRegistered()) { item.registerLockFn(this.getLock.bind(this)); this.subscribes.push(item.checkedEmitter.subscribe(fn)); } }; list.forEach(generatedFn); this.subscribes.push(list.changes.subscribe(l => l.forEach(generatedFn))); } ngAfterViewInit() { this.process(this.checkOneDirectives, this.onCheckOne.bind(this)); this.process(this.checkAllDirectives, this.onCheckAll.bind(this)); } ngOnDestroy() { this.subscribes.forEach(s => s.unsubscribe()); } getCheckboxesToCheck() { return this.checkOneDirectives.filter(item => !item.isDisabled()); } getLock() { return this.lock; } onCheckOne(event) { this.lock = true; if (event) { setTimeout(this.updateView.bind(this), 1); } else { this.checkAllDirectives.forEach(item => item.setValue(false, this.lock)); this.lock = false; } } onCheckAll(event) { this.lock = true; this.getCheckboxesToCheck().forEach(item => item.setValue(event, this.lock)); this.lock = false; } updateView() { const checked = this.getCheckboxesToCheck().map(item => item.getValue()); const result = checked.every(i => i); this.checkAllDirectives.forEach(item => item.setValue(result, this.lock)); this.lock = false; } } CheckContainerDirective.decorators = [ { type: Directive, args: [{ selector: '[checkContainer]', },] }, ]; CheckContainerDirective.ctorParameters = () => []; CheckContainerDirective.propDecorators = { "checkAllDirectives": [{ type: ContentChildren, args: [CheckAllDirective,] },], "checkOneDirectives": [{ type: ContentChildren, args: [CheckOneDirective,] },], }; class NgCheckallModule { } NgCheckallModule.decorators = [ { type: NgModule, args: [{ declarations: [ CheckAllDirective, CheckContainerDirective, CheckOneDirective, ], exports: [ CheckAllDirective, CheckContainerDirective, CheckOneDirective, ], },] }, ]; NgCheckallModule.ctorParameters = () => []; export { NgCheckallModule, AbstractCheckDirective as ɵb, CheckAllDirective as ɵa, CheckContainerDirective as ɵc, CheckOneDirective as ɵd }; //# sourceMappingURL=ng-checkall.js.map