inputswitch.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.INPUTSWITCH_VALUE_ACCESSOR = {
  16. provide: forms_1.NG_VALUE_ACCESSOR,
  17. useExisting: core_1.forwardRef(function () { return InputSwitch; }),
  18. multi: true
  19. };
  20. var InputSwitch = /** @class */ (function () {
  21. function InputSwitch(cd) {
  22. this.cd = cd;
  23. this.onChange = new core_1.EventEmitter();
  24. this.checked = false;
  25. this.focused = false;
  26. this.onModelChange = function () { };
  27. this.onModelTouched = function () { };
  28. }
  29. InputSwitch.prototype.onClick = function (event, cb) {
  30. if (!this.disabled && !this.readonly) {
  31. this.toggle(event);
  32. cb.focus();
  33. }
  34. };
  35. InputSwitch.prototype.onInputChange = function (event) {
  36. if (!this.readonly) {
  37. var inputChecked = event.target.checked;
  38. this.updateModel(event, inputChecked);
  39. }
  40. };
  41. InputSwitch.prototype.toggle = function (event) {
  42. this.updateModel(event, !this.checked);
  43. };
  44. InputSwitch.prototype.updateModel = function (event, value) {
  45. this.checked = value;
  46. this.onModelChange(this.checked);
  47. this.onChange.emit({
  48. originalEvent: event,
  49. checked: this.checked
  50. });
  51. };
  52. InputSwitch.prototype.onFocus = function (event) {
  53. this.focused = true;
  54. };
  55. InputSwitch.prototype.onBlur = function (event) {
  56. this.focused = false;
  57. this.onModelTouched();
  58. };
  59. InputSwitch.prototype.writeValue = function (checked) {
  60. this.checked = checked;
  61. this.cd.markForCheck();
  62. };
  63. InputSwitch.prototype.registerOnChange = function (fn) {
  64. this.onModelChange = fn;
  65. };
  66. InputSwitch.prototype.registerOnTouched = function (fn) {
  67. this.onModelTouched = fn;
  68. };
  69. InputSwitch.prototype.setDisabledState = function (val) {
  70. this.disabled = val;
  71. };
  72. __decorate([
  73. core_1.Input(),
  74. __metadata("design:type", Object)
  75. ], InputSwitch.prototype, "style", void 0);
  76. __decorate([
  77. core_1.Input(),
  78. __metadata("design:type", String)
  79. ], InputSwitch.prototype, "styleClass", void 0);
  80. __decorate([
  81. core_1.Input(),
  82. __metadata("design:type", Number)
  83. ], InputSwitch.prototype, "tabindex", void 0);
  84. __decorate([
  85. core_1.Input(),
  86. __metadata("design:type", String)
  87. ], InputSwitch.prototype, "inputId", void 0);
  88. __decorate([
  89. core_1.Input(),
  90. __metadata("design:type", String)
  91. ], InputSwitch.prototype, "name", void 0);
  92. __decorate([
  93. core_1.Input(),
  94. __metadata("design:type", Boolean)
  95. ], InputSwitch.prototype, "disabled", void 0);
  96. __decorate([
  97. core_1.Input(),
  98. __metadata("design:type", Boolean)
  99. ], InputSwitch.prototype, "readonly", void 0);
  100. __decorate([
  101. core_1.Output(),
  102. __metadata("design:type", core_1.EventEmitter)
  103. ], InputSwitch.prototype, "onChange", void 0);
  104. InputSwitch = __decorate([
  105. core_1.Component({
  106. selector: 'p-inputSwitch',
  107. template: "\n <div [ngClass]=\"{'ui-inputswitch ui-widget': true, 'ui-inputswitch-checked': checked, 'ui-state-disabled': disabled, 'ui-inputswitch-readonly': readonly, 'ui-inputswitch-focus': focused}\" \n [ngStyle]=\"style\" [class]=\"styleClass\" (click)=\"onClick($event, cb)\" role=\"checkbox\" [attr.aria-checked]=\"checked\">\n <div class=\"ui-helper-hidden-accessible\">\n <input #cb type=\"checkbox\" [attr.id]=\"inputId\" [attr.name]=\"name\" [attr.tabindex]=\"tabindex\" [checked]=\"checked\" (change)=\"onInputChange($event)\"\n (focus)=\"onFocus($event)\" (blur)=\"onBlur($event)\" [disabled]=\"disabled\" />\n </div>\n <span class=\"ui-inputswitch-slider\"></span>\n </div>\n ",
  108. providers: [exports.INPUTSWITCH_VALUE_ACCESSOR]
  109. }),
  110. __metadata("design:paramtypes", [core_1.ChangeDetectorRef])
  111. ], InputSwitch);
  112. return InputSwitch;
  113. }());
  114. exports.InputSwitch = InputSwitch;
  115. var InputSwitchModule = /** @class */ (function () {
  116. function InputSwitchModule() {
  117. }
  118. InputSwitchModule = __decorate([
  119. core_1.NgModule({
  120. imports: [common_1.CommonModule],
  121. exports: [InputSwitch],
  122. declarations: [InputSwitch]
  123. })
  124. ], InputSwitchModule);
  125. return InputSwitchModule;
  126. }());
  127. exports.InputSwitchModule = InputSwitchModule;
  128. //# sourceMappingURL=inputswitch.js.map