rating.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.RATING_VALUE_ACCESSOR = {
  16. provide: forms_1.NG_VALUE_ACCESSOR,
  17. useExisting: core_1.forwardRef(function () { return Rating; }),
  18. multi: true
  19. };
  20. var Rating = /** @class */ (function () {
  21. function Rating(cd) {
  22. this.cd = cd;
  23. this.stars = 5;
  24. this.cancel = true;
  25. this.iconOnClass = 'pi pi-star';
  26. this.iconOffClass = 'pi pi-star-o';
  27. this.iconCancelClass = 'pi pi-ban';
  28. this.onRate = new core_1.EventEmitter();
  29. this.onCancel = new core_1.EventEmitter();
  30. this.onModelChange = function () { };
  31. this.onModelTouched = function () { };
  32. }
  33. Rating.prototype.ngOnInit = function () {
  34. this.starsArray = [];
  35. for (var i = 0; i < this.stars; i++) {
  36. this.starsArray[i] = i;
  37. }
  38. };
  39. Rating.prototype.rate = function (event, i) {
  40. if (!this.readonly && !this.disabled) {
  41. this.value = (i + 1);
  42. this.onModelChange(this.value);
  43. this.onModelTouched();
  44. this.onRate.emit({
  45. originalEvent: event,
  46. value: (i + 1)
  47. });
  48. }
  49. event.preventDefault();
  50. };
  51. Rating.prototype.clear = function (event) {
  52. if (!this.readonly && !this.disabled) {
  53. this.value = null;
  54. this.onModelChange(this.value);
  55. this.onModelTouched();
  56. this.onCancel.emit(event);
  57. }
  58. event.preventDefault();
  59. };
  60. Rating.prototype.writeValue = function (value) {
  61. this.value = value;
  62. this.cd.detectChanges();
  63. };
  64. Rating.prototype.registerOnChange = function (fn) {
  65. this.onModelChange = fn;
  66. };
  67. Rating.prototype.registerOnTouched = function (fn) {
  68. this.onModelTouched = fn;
  69. };
  70. Rating.prototype.setDisabledState = function (val) {
  71. this.disabled = val;
  72. };
  73. __decorate([
  74. core_1.Input(),
  75. __metadata("design:type", Boolean)
  76. ], Rating.prototype, "disabled", void 0);
  77. __decorate([
  78. core_1.Input(),
  79. __metadata("design:type", Boolean)
  80. ], Rating.prototype, "readonly", void 0);
  81. __decorate([
  82. core_1.Input(),
  83. __metadata("design:type", Number)
  84. ], Rating.prototype, "stars", void 0);
  85. __decorate([
  86. core_1.Input(),
  87. __metadata("design:type", Boolean)
  88. ], Rating.prototype, "cancel", void 0);
  89. __decorate([
  90. core_1.Input(),
  91. __metadata("design:type", String)
  92. ], Rating.prototype, "iconOnClass", void 0);
  93. __decorate([
  94. core_1.Input(),
  95. __metadata("design:type", Object)
  96. ], Rating.prototype, "iconOnStyle", void 0);
  97. __decorate([
  98. core_1.Input(),
  99. __metadata("design:type", String)
  100. ], Rating.prototype, "iconOffClass", void 0);
  101. __decorate([
  102. core_1.Input(),
  103. __metadata("design:type", Object)
  104. ], Rating.prototype, "iconOffStyle", void 0);
  105. __decorate([
  106. core_1.Input(),
  107. __metadata("design:type", String)
  108. ], Rating.prototype, "iconCancelClass", void 0);
  109. __decorate([
  110. core_1.Input(),
  111. __metadata("design:type", Object)
  112. ], Rating.prototype, "iconCancelStyle", void 0);
  113. __decorate([
  114. core_1.Output(),
  115. __metadata("design:type", core_1.EventEmitter)
  116. ], Rating.prototype, "onRate", void 0);
  117. __decorate([
  118. core_1.Output(),
  119. __metadata("design:type", core_1.EventEmitter)
  120. ], Rating.prototype, "onCancel", void 0);
  121. Rating = __decorate([
  122. core_1.Component({
  123. selector: 'p-rating',
  124. template: "\n <div class=\"ui-rating\" [ngClass]=\"{'ui-state-disabled': disabled}\">\n <a [attr.tabindex]=\"disabled ? null : '0'\" *ngIf=\"cancel\" (click)=\"clear($event)\" (keydown.enter)=\"clear($event)\" class=\"ui-rating-cancel\">\n <span class=\"ui-rating-icon\" [ngClass]=\"iconCancelClass\" [ngStyle]=\"iconCancelStyle\"></span>\n </a>\n <a [attr.tabindex]=\"disabled ? null : '0'\" *ngFor=\"let star of starsArray;let i=index\" (click)=\"rate($event,i)\" (keydown.enter)=\"rate($event,i)\">\n <span class=\"ui-rating-icon\" \n [ngClass]=\"(!value || i >= value) ? iconOffClass : iconOnClass\"\n [ngStyle]=\"(!value || i >= value) ? iconOffStyle : iconOnStyle\"\n ></span>\n </a>\n </div>\n ",
  125. providers: [exports.RATING_VALUE_ACCESSOR]
  126. }),
  127. __metadata("design:paramtypes", [core_1.ChangeDetectorRef])
  128. ], Rating);
  129. return Rating;
  130. }());
  131. exports.Rating = Rating;
  132. var RatingModule = /** @class */ (function () {
  133. function RatingModule() {
  134. }
  135. RatingModule = __decorate([
  136. core_1.NgModule({
  137. imports: [common_1.CommonModule],
  138. exports: [Rating],
  139. declarations: [Rating]
  140. })
  141. ], RatingModule);
  142. return RatingModule;
  143. }());
  144. exports.RatingModule = RatingModule;
  145. //# sourceMappingURL=rating.js.map