import { Component, ElementRef, OnInit, OnChanges, Input, Output, EventEmitter, ContentChildren, QueryList } from '@angular/core'; import { CfCoreComponent } from '../core/core.component'; import { RatingModel } from '../../models/rating/rating.model'; import { RatingStylingModel } from '../../models/rating/rating-styling.model'; /** *

Rating component which has vertical/horizontal icons, icon order changing and other basic functionality.

*

Example of using Menu:

*
 * <cf-rating [properties]="myRating" [styling]="myRatingStyles"></cf-rating>
 * 
*/ @Component({ selector: 'cf-rating', template: "
{{cfRating.label}}
", styles: [".cf-rating { display: inline-flex; flex-direction: column; } .cf-rating-label { display: flex; color: rgba(0, 0, 0, 0.38); font-size: 12px; } .cf-rating-items { display: flex; align-items: center; justify-content: center; } .cf-rating-items.count-from-end:not(.icons-vertical) { flex-direction: row-reverse; } .cf-rating-items.count-from-end.icons-vertical { flex-direction: column; } .cf-rating-items.icons-vertical { flex-direction: column-reverse; } .cf-rating-item { display: flex; border-radius: 50%; } .cf-rating-item /deep/ md-icon { transition: ease 0.15s all; } .cf-rating-item.cf-rating-disabled:hover /deep/ md-icon { cursor: default !important; } .cf-rating-item:not(.cf-rating-disabled):hover /deep/ md-icon { transform: scale(1.1); } [hidden] { display: none !important; } "] }) export class CfRatingComponent extends CfCoreComponent implements OnInit { /** * It is an RatingModel object with options to be rendered into component template */ cfRating: RatingModel; /** * It is an array to be filled with items according to MAX property */ cfItems: number[] = []; /** * It is an attribute [properties] of directive cf-rating which take RatingModel object. *

Example of Rating json object:

*
{
   *  label: 'Rating label',
   *  icon: 'stars',
   *  value: 1,
   *  max: 5,
   *  countFromEnd: false,
   *  iconsVertical: false
   *  disabled: false
   * }
*

label: string

* Label for component *

icon: string

* Name of icon *

value: number

* Value of rating and means number of filled/selected icons *

max: number

* Number of rating icons *

countFromEnd: boolean

* Means to start icons counting order *

iconsVertical: boolean

* Means to show icons vertically *

disabled: boolean

* Means component to be disabled */ @Input() properties: RatingModel; /** * It is an object with styling component elements RatingStylingModel object */ @Input() styling: RatingStylingModel; constructor(public elementRef: ElementRef) { super(elementRef); } /** * Initialize all data */ ngOnInit() { this.cfRating = this.properties ? this.properties : new RatingModel(); if(this.styling==null) this.styling = new RatingStylingModel(); for (var i = 0; i < this.cfRating.max; ++i) { this.cfItems.push(i); } } // ngOnChanges(changes: any) { // console.log('changes', changes); // } /** * Function to set component value * @param index it is used to calculate component value */ private setRating(index) { if(!this.cfRating.disabled) this.cfRating.value = index + 1; } /** * Function to unselect current item by user double-click and change component value * @param index it is used to calculate component value */ private resetCurrentItem(index) { if(!this.cfRating.disabled) this.cfRating.value = index; } /** * Function to set filled color for icons */ private getHighlitedColor() { return !this.cfRating.disabled ? this.styling.filledColor : 'rgba(0,0,0,0.5)'; } /** * Function to set empty color for icons */ private getEmptyColor() { return !this.cfRating.disabled ? this.styling.emptyColor : 'rgba(0,0,0,0.2)'; } }