checkbox.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var forms_1 = require("@angular/forms");
  15. exports.CHECKBOX_VALUE_ACCESSOR = {
  16. provide: forms_1.NG_VALUE_ACCESSOR,
  17. useExisting: core_1.forwardRef(function () { return Checkbox; }),
  18. multi: true
  19. };
  20. var Checkbox = /** @class */ (function () {
  21. function Checkbox(cd) {
  22. this.cd = cd;
  23. this.checkboxIcon = 'pi pi-check';
  24. this.onChange = new core_1.EventEmitter();
  25. this.onModelChange = function () { };
  26. this.onModelTouched = function () { };
  27. this.focused = false;
  28. this.checked = false;
  29. }
  30. Checkbox.prototype.onClick = function (event, checkbox, focus) {
  31. event.preventDefault();
  32. if (this.disabled || this.readonly) {
  33. return;
  34. }
  35. this.checked = !this.checked;
  36. this.updateModel();
  37. if (focus) {
  38. checkbox.focus();
  39. }
  40. };
  41. Checkbox.prototype.updateModel = function () {
  42. if (!this.binary) {
  43. if (this.checked)
  44. this.addValue();
  45. else
  46. this.removeValue();
  47. this.onModelChange(this.model);
  48. if (this.formControl) {
  49. this.formControl.setValue(this.model);
  50. }
  51. }
  52. else {
  53. this.onModelChange(this.checked);
  54. }
  55. this.onChange.emit(this.checked);
  56. };
  57. Checkbox.prototype.handleChange = function (event) {
  58. if (!this.readonly) {
  59. this.checked = event.target.checked;
  60. this.updateModel();
  61. }
  62. };
  63. Checkbox.prototype.isChecked = function () {
  64. if (this.binary)
  65. return this.model;
  66. else
  67. return this.model && this.model.indexOf(this.value) > -1;
  68. };
  69. Checkbox.prototype.removeValue = function () {
  70. var _this = this;
  71. this.model = this.model.filter(function (val) { return val !== _this.value; });
  72. };
  73. Checkbox.prototype.addValue = function () {
  74. if (this.model)
  75. this.model = this.model.concat([this.value]);
  76. else
  77. this.model = [this.value];
  78. };
  79. Checkbox.prototype.onFocus = function (event) {
  80. this.focused = true;
  81. };
  82. Checkbox.prototype.onBlur = function (event) {
  83. this.focused = false;
  84. this.onModelTouched();
  85. };
  86. Checkbox.prototype.writeValue = function (model) {
  87. this.model = model;
  88. this.checked = this.isChecked();
  89. this.cd.markForCheck();
  90. };
  91. Checkbox.prototype.registerOnChange = function (fn) {
  92. this.onModelChange = fn;
  93. };
  94. Checkbox.prototype.registerOnTouched = function (fn) {
  95. this.onModelTouched = fn;
  96. };
  97. Checkbox.prototype.setDisabledState = function (val) {
  98. this.disabled = val;
  99. };
  100. __decorate([
  101. core_1.Input(),
  102. __metadata("design:type", Object)
  103. ], Checkbox.prototype, "value", void 0);
  104. __decorate([
  105. core_1.Input(),
  106. __metadata("design:type", String)
  107. ], Checkbox.prototype, "name", void 0);
  108. __decorate([
  109. core_1.Input(),
  110. __metadata("design:type", Boolean)
  111. ], Checkbox.prototype, "disabled", void 0);
  112. __decorate([
  113. core_1.Input(),
  114. __metadata("design:type", String)
  115. ], Checkbox.prototype, "binary", void 0);
  116. __decorate([
  117. core_1.Input(),
  118. __metadata("design:type", String)
  119. ], Checkbox.prototype, "label", void 0);
  120. __decorate([
  121. core_1.Input(),
  122. __metadata("design:type", Number)
  123. ], Checkbox.prototype, "tabindex", void 0);
  124. __decorate([
  125. core_1.Input(),
  126. __metadata("design:type", String)
  127. ], Checkbox.prototype, "inputId", void 0);
  128. __decorate([
  129. core_1.Input(),
  130. __metadata("design:type", Object)
  131. ], Checkbox.prototype, "style", void 0);
  132. __decorate([
  133. core_1.Input(),
  134. __metadata("design:type", String)
  135. ], Checkbox.prototype, "styleClass", void 0);
  136. __decorate([
  137. core_1.Input(),
  138. __metadata("design:type", String)
  139. ], Checkbox.prototype, "labelStyleClass", void 0);
  140. __decorate([
  141. core_1.Input(),
  142. __metadata("design:type", forms_1.FormControl)
  143. ], Checkbox.prototype, "formControl", void 0);
  144. __decorate([
  145. core_1.Input(),
  146. __metadata("design:type", String)
  147. ], Checkbox.prototype, "checkboxIcon", void 0);
  148. __decorate([
  149. core_1.Input(),
  150. __metadata("design:type", Boolean)
  151. ], Checkbox.prototype, "readonly", void 0);
  152. __decorate([
  153. core_1.Output(),
  154. __metadata("design:type", core_1.EventEmitter)
  155. ], Checkbox.prototype, "onChange", void 0);
  156. Checkbox = __decorate([
  157. core_1.Component({
  158. selector: 'p-checkbox',
  159. template: "\n <div [ngStyle]=\"style\" [ngClass]=\"{'ui-chkbox ui-widget': true,'ui-chkbox-readonly': readonly}\" [class]=\"styleClass\">\n <div class=\"ui-helper-hidden-accessible\">\n <input #cb type=\"checkbox\" [attr.id]=\"inputId\" [name]=\"name\" [readonly]=\"readonly\" [value]=\"value\" [checked]=\"checked\" (focus)=\"onFocus($event)\" (blur)=\"onBlur($event)\"\n [ngClass]=\"{'ui-state-focus':focused}\" (change)=\"handleChange($event)\" [disabled]=\"disabled\" [attr.tabindex]=\"tabindex\">\n </div>\n <div class=\"ui-chkbox-box ui-widget ui-corner-all ui-state-default\" (click)=\"onClick($event,cb,true)\"\n [ngClass]=\"{'ui-state-active':checked,'ui-state-disabled':disabled,'ui-state-focus':focused}\">\n <span class=\"ui-chkbox-icon ui-clickable\" [ngClass]=\"checked ? checkboxIcon : null\"></span>\n </div>\n </div>\n <label (click)=\"onClick($event,cb,true)\" [class]=\"labelStyleClass\"\n [ngClass]=\"{'ui-chkbox-label': true, 'ui-label-active':checked, 'ui-label-disabled':disabled, 'ui-label-focus':focused}\"\n *ngIf=\"label\" [attr.for]=\"inputId\">{{label}}</label>\n ",
  160. providers: [exports.CHECKBOX_VALUE_ACCESSOR]
  161. }),
  162. __metadata("design:paramtypes", [core_1.ChangeDetectorRef])
  163. ], Checkbox);
  164. return Checkbox;
  165. }());
  166. exports.Checkbox = Checkbox;
  167. var CheckboxModule = /** @class */ (function () {
  168. function CheckboxModule() {
  169. }
  170. CheckboxModule = __decorate([
  171. core_1.NgModule({
  172. imports: [common_1.CommonModule],
  173. exports: [Checkbox],
  174. declarations: [Checkbox]
  175. })
  176. ], CheckboxModule);
  177. return CheckboxModule;
  178. }());
  179. exports.CheckboxModule = CheckboxModule;
  180. //# sourceMappingURL=checkbox.js.map