ng-checkall
Version:
A simple directive to "Check All" checkboxes behavior
178 lines (175 loc) • 7.02 kB
JavaScript
import { __extends } from 'tslib';
import { Output, EventEmitter, Directive, HostListener, Input, ContentChildren, NgModule } from '@angular/core';
import { NgModel } from '@angular/forms';
var AbstractCheckDirective = /** @class */ (function () {
function AbstractCheckDirective(_model) {
var _this = this;
this._model = _model;
this.ngModelChange = new EventEmitter();
this.registered = false;
this.lockFn = function () { return false; };
this.isRegistered = function () { return _this.registered; };
}
AbstractCheckDirective.prototype.registerLockFn = function (fn) {
this.registered = true;
this.lockFn = fn;
};
AbstractCheckDirective.prototype._onModelChange = function ($event) {
if (!this.lockFn() && !this._model.disabled) {
this.checkedEmitter.emit($event);
}
};
AbstractCheckDirective.prototype.setValue = function (value, runBody) {
if (runBody === void 0) { runBody = true; }
if (runBody && !this._model.disabled) {
this.ngModelChange.emit(value);
this._model.valueAccessor.writeValue(value);
}
};
return AbstractCheckDirective;
}());
AbstractCheckDirective.propDecorators = {
"ngModelChange": [{ type: Output },],
};
var CheckAllDirective = /** @class */ (function (_super) {
__extends(CheckAllDirective, _super);
function CheckAllDirective(model) {
var _this = _super.call(this, model) || this;
_this.model = model;
_this.checkedAll = new EventEmitter();
_this.onModelChange = _this._onModelChange;
_this.checkedEmitter = _this.checkedAll;
return _this;
}
return CheckAllDirective;
}(AbstractCheckDirective));
CheckAllDirective.decorators = [
{ type: Directive, args: [{
selector: '[checkAll]',
},] },
];
CheckAllDirective.ctorParameters = function () { return [
{ type: NgModel, },
]; };
CheckAllDirective.propDecorators = {
"checkedAll": [{ type: Output },],
"onModelChange": [{ type: HostListener, args: ['ngModelChange', ['$event'],] },],
};
var CheckOneDirective = /** @class */ (function (_super) {
__extends(CheckOneDirective, _super);
function CheckOneDirective(model) {
var _this = _super.call(this, model) || this;
_this.model = model;
_this.checkedOne = new EventEmitter();
_this.checkedEmitter = _this.checkedOne;
return _this;
}
Object.defineProperty(CheckOneDirective.prototype, "disabled", {
set: function (val) {
this._onModelChange.bind(this)(true);
},
enumerable: true,
configurable: true
});
CheckOneDirective.prototype.ngAfterViewInit = function () {
this.model.valueChanges.subscribe(this._onModelChange.bind(this));
};
CheckOneDirective.prototype.getValue = function () { return this.model.value; };
CheckOneDirective.prototype.isDisabled = function () { return this.model.disabled; };
return CheckOneDirective;
}(AbstractCheckDirective));
CheckOneDirective.decorators = [
{ type: Directive, args: [{
selector: '[checkOne]',
},] },
];
CheckOneDirective.ctorParameters = function () { return [
{ type: NgModel, },
]; };
CheckOneDirective.propDecorators = {
"disabled": [{ type: Input, args: ['disabled',] },],
"checkedOne": [{ type: Output },],
};
var CheckContainerDirective = /** @class */ (function () {
function CheckContainerDirective() {
this.subscribes = [];
this.lock = false;
}
CheckContainerDirective.prototype.process = function (list, fn) {
var _this = this;
var generatedFn = function (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(function (l) { return l.forEach(generatedFn); }));
};
CheckContainerDirective.prototype.ngAfterViewInit = function () {
this.process(this.checkOneDirectives, this.onCheckOne.bind(this));
this.process(this.checkAllDirectives, this.onCheckAll.bind(this));
};
CheckContainerDirective.prototype.ngOnDestroy = function () {
this.subscribes.forEach(function (s) { return s.unsubscribe(); });
};
CheckContainerDirective.prototype.getCheckboxesToCheck = function () { return this.checkOneDirectives.filter(function (item) { return !item.isDisabled(); }); };
CheckContainerDirective.prototype.getLock = function () { return this.lock; };
CheckContainerDirective.prototype.onCheckOne = function (event) {
var _this = this;
this.lock = true;
if (event) {
setTimeout(this.updateView.bind(this), 1);
}
else {
this.checkAllDirectives.forEach(function (item) { return item.setValue(false, _this.lock); });
this.lock = false;
}
};
CheckContainerDirective.prototype.onCheckAll = function (event) {
var _this = this;
this.lock = true;
this.getCheckboxesToCheck().forEach(function (item) { return item.setValue(event, _this.lock); });
this.lock = false;
};
CheckContainerDirective.prototype.updateView = function () {
var _this = this;
var checked = this.getCheckboxesToCheck().map(function (item) { return item.getValue(); });
var result = checked.every(function (i) { return i; });
this.checkAllDirectives.forEach(function (item) { return item.setValue(result, _this.lock); });
this.lock = false;
};
return CheckContainerDirective;
}());
CheckContainerDirective.decorators = [
{ type: Directive, args: [{
selector: '[checkContainer]',
},] },
];
CheckContainerDirective.ctorParameters = function () { return []; };
CheckContainerDirective.propDecorators = {
"checkAllDirectives": [{ type: ContentChildren, args: [CheckAllDirective,] },],
"checkOneDirectives": [{ type: ContentChildren, args: [CheckOneDirective,] },],
};
var NgCheckallModule = /** @class */ (function () {
function NgCheckallModule() {
}
return NgCheckallModule;
}());
NgCheckallModule.decorators = [
{ type: NgModule, args: [{
declarations: [
CheckAllDirective,
CheckContainerDirective,
CheckOneDirective,
],
exports: [
CheckAllDirective,
CheckContainerDirective,
CheckOneDirective,
],
},] },
];
NgCheckallModule.ctorParameters = function () { return []; };
export { NgCheckallModule, AbstractCheckDirective as ɵb, CheckAllDirective as ɵa, CheckContainerDirective as ɵc, CheckOneDirective as ɵd };
//# sourceMappingURL=ng-checkall.js.map