UNPKG

ng-checkall

Version:

A simple directive to "Check All" checkboxes behavior

197 lines (191 loc) 7.84 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/forms')) : typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/forms'], factory) : (factory((global['ng-checkall'] = {}),global.ng.core,global.ng.forms)); }(this, (function (exports,core,forms) { 'use strict'; var 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]; }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var AbstractCheckDirective = (function () { function AbstractCheckDirective(_model) { var _this = this; this._model = _model; this.ngModelChange = new core.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: core.Output },], }; var CheckAllDirective = (function (_super) { __extends(CheckAllDirective, _super); function CheckAllDirective(model) { var _this = _super.call(this, model) || this; _this.model = model; _this.checkedAll = new core.EventEmitter(); _this.onModelChange = _this._onModelChange; _this.checkedEmitter = _this.checkedAll; return _this; } return CheckAllDirective; }(AbstractCheckDirective)); CheckAllDirective.decorators = [ { type: core.Directive, args: [{ selector: '[checkAll]', },] }, ]; CheckAllDirective.ctorParameters = function () { return [ { type: forms.NgModel, }, ]; }; CheckAllDirective.propDecorators = { "checkedAll": [{ type: core.Output },], "onModelChange": [{ type: core.HostListener, args: ['ngModelChange', ['$event'],] },], }; var CheckOneDirective = (function (_super) { __extends(CheckOneDirective, _super); function CheckOneDirective(model) { var _this = _super.call(this, model) || this; _this.model = model; _this.checkedOne = new core.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: core.Directive, args: [{ selector: '[checkOne]', },] }, ]; CheckOneDirective.ctorParameters = function () { return [ { type: forms.NgModel, }, ]; }; CheckOneDirective.propDecorators = { "disabled": [{ type: core.Input, args: ['disabled',] },], "checkedOne": [{ type: core.Output },], }; var CheckContainerDirective = (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: core.Directive, args: [{ selector: '[checkContainer]', },] }, ]; CheckContainerDirective.ctorParameters = function () { return []; }; CheckContainerDirective.propDecorators = { "checkAllDirectives": [{ type: core.ContentChildren, args: [CheckAllDirective,] },], "checkOneDirectives": [{ type: core.ContentChildren, args: [CheckOneDirective,] },], }; var NgCheckallModule = (function () { function NgCheckallModule() { } return NgCheckallModule; }()); NgCheckallModule.decorators = [ { type: core.NgModule, args: [{ declarations: [ CheckAllDirective, CheckContainerDirective, CheckOneDirective, ], exports: [ CheckAllDirective, CheckContainerDirective, CheckOneDirective, ], },] }, ]; NgCheckallModule.ctorParameters = function () { return []; }; exports.NgCheckallModule = NgCheckallModule; exports.ɵb = AbstractCheckDirective; exports.ɵa = CheckAllDirective; exports.ɵc = CheckContainerDirective; exports.ɵd = CheckOneDirective; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ng-checkall.umd.js.map