ngx-bootstrap
Version:
Angular Bootstrap
118 lines • 4.88 kB
JavaScript
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, forwardRef, HostListener, Input, Output, TemplateRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { RatingConfig } from './rating.config';
export const RATING_CONTROL_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RatingComponent),
multi: true
};
export class RatingComponent {
constructor(changeDetection, config) {
this.changeDetection = changeDetection;
/** number of icons */
this.max = 5;
/** if true will not react on any user events */
this.readonly = false;
/** array of icons titles, default: (["one", "two", "three", "four", "five"]) */
this.titles = [];
/** fired when icon selected, $event:number equals to selected rating */
this.onHover = new EventEmitter();
/** fired when icon selected, $event:number equals to previous rating value */
this.onLeave = new EventEmitter();
this.onChange = Function.prototype;
this.onTouched = Function.prototype;
/** aria label for rating */
this.ariaLabel = 'rating';
this.range = [];
this.value = 0;
Object.assign(this, config);
}
onKeydown(event) {
if ([37, 38, 39, 40].indexOf(event.which) === -1) {
return;
}
event.preventDefault();
event.stopPropagation();
const sign = event.which === 38 || event.which === 39 ? 1 : -1;
this.rate(this.value + sign);
}
ngOnInit() {
this.max = this.max || 5;
this.titles =
typeof this.titles !== 'undefined' && this.titles.length > 0
? this.titles
: [];
this.range = this.buildTemplateObjects(this.max);
}
// model -> view
writeValue(value) {
if (value % 1 !== value) {
this.value = Math.round(value);
this.preValue = value;
this.changeDetection.markForCheck();
return;
}
this.preValue = value;
this.value = value;
this.changeDetection.markForCheck();
}
enter(value) {
if (!this.readonly) {
this.value = value;
this.changeDetection.markForCheck();
this.onHover.emit(value);
}
}
reset() {
if (typeof this.preValue === 'number') {
this.value = Math.round(this.preValue);
this.changeDetection.markForCheck();
this.onLeave.emit(this.value);
}
}
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
rate(value) {
if (!this.readonly && this.range
&& value >= 0 && value <= this.range.length) {
this.writeValue(value);
this.onChange(value);
}
}
buildTemplateObjects(max) {
const result = [];
for (let i = 0; i < max; i++) {
result.push({
index: i,
title: this.titles[i] || i + 1
});
}
return result;
}
}
RatingComponent.decorators = [
{ type: Component, args: [{
selector: 'rating',
template: "<span (mouseleave)=\"reset()\" (keydown)=\"onKeydown($event)\" tabindex=\"0\"\n role=\"slider\" aria-valuemin=\"0\"\n [attr.aria-label]=\"ariaLabel\"\n [attr.aria-valuemax]=\"range.length\"\n [attr.aria-valuenow]=\"value\">\n <ng-template #star let-value=\"value\" let-index=\"index\">{{ index < value ? '★' : '☆' }}</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",
providers: [RATING_CONTROL_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush
},] }
];
RatingComponent.ctorParameters = () => [
{ type: ChangeDetectorRef },
{ type: RatingConfig }
];
RatingComponent.propDecorators = {
max: [{ type: Input }],
readonly: [{ type: Input }],
titles: [{ type: Input }],
customTemplate: [{ type: Input }],
onHover: [{ type: Output }],
onLeave: [{ type: Output }],
onKeydown: [{ type: HostListener, args: ['keydown', ['$event'],] }]
};
//# sourceMappingURL=rating.component.js.map