angularcli-star-rating
Version:
Star rating for Angualr Cli framework
155 lines (149 loc) • 4.55 kB
JavaScript
import { Component, Input, Output, EventEmitter, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class AngularcliStarRatingComponent {
constructor() {
this.rating = 0;
this.starReadonly = true;
this.onStarRate = new EventEmitter();
this.rateText = "Click to Rate.";
this.star = ["false", "false", "false", "false", "false"];
this.hoverStar = [false, false, false, false, false];
}
/**
* @return {?}
*/
ngAfterViewInit() {
this.checkRating();
}
/**
* @param {?} index
* @return {?}
*/
addHoverClass(index) {
this.hoverStar = [false, false, false, false, false];
for (let /** @type {?} */ p = 0; p < 5; p++) {
if (p <= index)
this.hoverStar[p] = true;
}
}
/**
* @return {?}
*/
resetHover() {
this.hoverStar = [false, false, false, false, false];
}
/**
* @return {?}
*/
checkRating() {
if (this.rating <= 5 && this.rating >= 0) {
if (this.rating > 0)
this.loadStar(this.rating);
}
else {
console.log("Rating should be between 0 and 5");
}
}
/**
* @return {?}
*/
resetStar() {
this.star = ["false", "false", "false", "false", "false"];
}
/**
* @param {?} num
* @return {?}
*/
rateIt(num) {
this.rating = num;
this.resetStar();
this.fillStar(this.rating);
let /** @type {?} */ value = num + 1;
this.onStarRate.emit([value]);
}
/**
* @param {?} num
* @return {?}
*/
fillStar(num) {
for (let /** @type {?} */ x = 0; x < 5; x++) {
if (num >= x)
this.star[x] = "full";
}
}
/**
* @param {?} num
* @return {?}
*/
loadStar(num) {
let /** @type {?} */ halfStar = num % 1 != 0 ? true : false; // check number is round or double
let /** @type {?} */ halfNum = Math.floor(num); //
for (let /** @type {?} */ x = 0; x < 5; x++) {
if (x < num)
this.star[x] = "full";
if (halfStar && halfNum == x)
this.star[x] = "half";
}
}
}
AngularcliStarRatingComponent.decorators = [
{ type: Component, args: [{
selector: 'cli-star-rating',
template: `<span class="cli-star" *ngIf="starReadonly">
<i *ngFor="let i of star" class="fa" aria-hidden="true"
[ngClass]="{'fa-star-o':i == 'false','fa-star' :i == 'full', 'fa-star-half-o' : i == 'half'}"></i>
</span>
<span title="{{rateText}}" class="cli-star cli-star-hover" *ngIf="!starReadonly">
<i *ngFor="let i of star;let ix = index" class="fa" aria-hidden="true"
(click) = "rateIt(ix)" (mouseover)="addHoverClass(ix)" (mouseleave)="resetHover()"
[ngClass]="{'fa-star-o':i == 'false','fa-star' :i == 'full' || hoverStar[ix],
'fa-star-half-o' : i == 'half'}"></i>
</span>`,
styles: [`.cli-star{display:inline-block;}
.cli-star-hover i:hover~i{opacity:0.3;}
.cli-star-hover:hover .fa-star-o.fa-star:before,
.cli-star-hover:hover .fa-star-half-o.fa-star:before{content:'\\F005';}`]
},] },
];
/** @nocollapse */
AngularcliStarRatingComponent.ctorParameters = () => [];
AngularcliStarRatingComponent.propDecorators = {
"rating": [{ type: Input, args: ['rating',] },],
"starReadonly": [{ type: Input, args: ['starReadonly',] },],
"onStarRate": [{ type: Output },],
"rateText": [{ type: Input, args: ['starRateText',] },],
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
class AngularcliStarRatingModule {
}
AngularcliStarRatingModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [AngularcliStarRatingComponent],
exports: [AngularcliStarRatingComponent]
},] },
];
/** @nocollapse */
AngularcliStarRatingModule.ctorParameters = () => [];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularcliStarRatingModule, AngularcliStarRatingComponent as ɵa };
//# sourceMappingURL=angularcli-star-rating.js.map