ngx-bootstrap-rating.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { forwardRef, Component, ChangeDetectionStrategy, ChangeDetectorRef, Input, Output, HostListener, EventEmitter, NgModule } from '@angular/core';
  2. import { NG_VALUE_ACCESSOR } from '@angular/forms';
  3. import { CommonModule } from '@angular/common';
  4. /**
  5. * @fileoverview added by tsickle
  6. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  7. */
  8. /** @type {?} */
  9. var RATING_CONTROL_VALUE_ACCESSOR = {
  10. provide: NG_VALUE_ACCESSOR,
  11. /* tslint:disable-next-line: no-use-before-declare */
  12. useExisting: forwardRef((/**
  13. * @return {?}
  14. */
  15. function () { return RatingComponent; })),
  16. multi: true
  17. };
  18. var RatingComponent = /** @class */ (function () {
  19. function RatingComponent(changeDetection) {
  20. this.changeDetection = changeDetection;
  21. /**
  22. * number of icons
  23. */
  24. this.max = 5;
  25. /**
  26. * fired when icon selected, $event:number equals to selected rating
  27. */
  28. this.onHover = new EventEmitter();
  29. /**
  30. * fired when icon selected, $event:number equals to previous rating value
  31. */
  32. this.onLeave = new EventEmitter();
  33. // tslint:disable-next-line:no-any
  34. this.onChange = Function.prototype;
  35. // tslint:disable-next-line:no-any
  36. this.onTouched = Function.prototype;
  37. }
  38. /**
  39. * @param {?} event
  40. * @return {?}
  41. */
  42. RatingComponent.prototype.onKeydown = /**
  43. * @param {?} event
  44. * @return {?}
  45. */
  46. function (event) {
  47. /* tslint:disable-next-line: deprecation */
  48. if ([37, 38, 39, 40].indexOf(event.which) === -1) {
  49. return;
  50. }
  51. event.preventDefault();
  52. event.stopPropagation();
  53. /* tslint:disable-next-line: deprecation */
  54. /** @type {?} */
  55. var sign = event.which === 38 || event.which === 39 ? 1 : -1;
  56. this.rate(this.value + sign);
  57. };
  58. /**
  59. * @return {?}
  60. */
  61. RatingComponent.prototype.ngOnInit = /**
  62. * @return {?}
  63. */
  64. function () {
  65. this.max = typeof this.max !== 'undefined' ? this.max : 5;
  66. this.titles =
  67. typeof this.titles !== 'undefined' && this.titles.length > 0
  68. ? this.titles
  69. : [];
  70. this.range = this.buildTemplateObjects(this.max);
  71. };
  72. // model -> view
  73. // model -> view
  74. /**
  75. * @param {?} value
  76. * @return {?}
  77. */
  78. RatingComponent.prototype.writeValue =
  79. // model -> view
  80. /**
  81. * @param {?} value
  82. * @return {?}
  83. */
  84. function (value) {
  85. if (value % 1 !== value) {
  86. this.value = Math.round(value);
  87. this.preValue = value;
  88. this.changeDetection.markForCheck();
  89. return;
  90. }
  91. this.preValue = value;
  92. this.value = value;
  93. this.changeDetection.markForCheck();
  94. };
  95. /**
  96. * @param {?} value
  97. * @return {?}
  98. */
  99. RatingComponent.prototype.enter = /**
  100. * @param {?} value
  101. * @return {?}
  102. */
  103. function (value) {
  104. if (!this.readonly) {
  105. this.value = value;
  106. this.changeDetection.markForCheck();
  107. this.onHover.emit(value);
  108. }
  109. };
  110. /**
  111. * @return {?}
  112. */
  113. RatingComponent.prototype.reset = /**
  114. * @return {?}
  115. */
  116. function () {
  117. this.value = this.preValue;
  118. this.changeDetection.markForCheck();
  119. this.onLeave.emit(this.value);
  120. };
  121. /**
  122. * @param {?} fn
  123. * @return {?}
  124. */
  125. RatingComponent.prototype.registerOnChange = /**
  126. * @param {?} fn
  127. * @return {?}
  128. */
  129. function (fn) {
  130. this.onChange = fn;
  131. };
  132. /**
  133. * @param {?} fn
  134. * @return {?}
  135. */
  136. RatingComponent.prototype.registerOnTouched = /**
  137. * @param {?} fn
  138. * @return {?}
  139. */
  140. function (fn) {
  141. this.onTouched = fn;
  142. };
  143. /**
  144. * @param {?} value
  145. * @return {?}
  146. */
  147. RatingComponent.prototype.rate = /**
  148. * @param {?} value
  149. * @return {?}
  150. */
  151. function (value) {
  152. if (!this.readonly && value >= 0 && value <= this.range.length) {
  153. this.writeValue(value);
  154. this.onChange(value);
  155. }
  156. };
  157. /**
  158. * @protected
  159. * @param {?} max
  160. * @return {?}
  161. */
  162. RatingComponent.prototype.buildTemplateObjects = /**
  163. * @protected
  164. * @param {?} max
  165. * @return {?}
  166. */
  167. function (max) {
  168. /** @type {?} */
  169. var result = [];
  170. for (var i = 0; i < max; i++) {
  171. result.push({
  172. index: i,
  173. title: this.titles[i] || i + 1
  174. });
  175. }
  176. return result;
  177. };
  178. RatingComponent.decorators = [
  179. { type: Component, args: [{
  180. selector: 'rating',
  181. template: "<span (mouseleave)=\"reset()\" (keydown)=\"onKeydown($event)\" tabindex=\"0\"\n role=\"slider\" aria-valuemin=\"0\" [attr.aria-valuemax]=\"range.length\"\n [attr.aria-valuenow]=\"value\">\n <ng-template #star let-value=\"value\" let-index=\"index\">{{ index < value ? '&#9733;' : '&#9734;' }}</ng-template>\n <ng-template ngFor let-r [ngForOf]=\"range\" let-index=\"index\">\n <span class=\"sr-only\">({{ index < value ? '*' : ' ' }})</span>\n <span class=\"bs-rating-star\"\n (mouseenter)=\"enter(index + 1)\"\n (click)=\"rate(index + 1)\"\n [title]=\"r.title\"\n [style.cursor]=\"readonly ? 'default' : 'pointer'\"\n [class.active]=\"index < value\">\n <ng-template [ngTemplateOutlet]=\"customTemplate || star\"\n [ngTemplateOutletContext]=\"{index: index, value: value}\">\n </ng-template>\n </span>\n </ng-template>\n</span>\n",
  182. providers: [RATING_CONTROL_VALUE_ACCESSOR],
  183. changeDetection: ChangeDetectionStrategy.OnPush
  184. }] }
  185. ];
  186. /** @nocollapse */
  187. RatingComponent.ctorParameters = function () { return [
  188. { type: ChangeDetectorRef }
  189. ]; };
  190. RatingComponent.propDecorators = {
  191. max: [{ type: Input }],
  192. readonly: [{ type: Input }],
  193. titles: [{ type: Input }],
  194. customTemplate: [{ type: Input }],
  195. onHover: [{ type: Output }],
  196. onLeave: [{ type: Output }],
  197. onKeydown: [{ type: HostListener, args: ['keydown', ['$event'],] }]
  198. };
  199. return RatingComponent;
  200. }());
  201. /**
  202. * @fileoverview added by tsickle
  203. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  204. */
  205. var RatingModule = /** @class */ (function () {
  206. function RatingModule() {
  207. }
  208. /**
  209. * @return {?}
  210. */
  211. RatingModule.forRoot = /**
  212. * @return {?}
  213. */
  214. function () {
  215. return {
  216. ngModule: RatingModule,
  217. providers: []
  218. };
  219. };
  220. RatingModule.decorators = [
  221. { type: NgModule, args: [{
  222. imports: [CommonModule],
  223. declarations: [RatingComponent],
  224. exports: [RatingComponent]
  225. },] }
  226. ];
  227. return RatingModule;
  228. }());
  229. export { RatingComponent, RatingModule, RATING_CONTROL_VALUE_ACCESSOR as ɵa };
  230. //# sourceMappingURL=ngx-bootstrap-rating.js.map