Guest User

Untitled

a guest
Jan 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <rating [(rating)]="rating"> </rating>
  2.  
  3. <div>
  4. <ion-icon name="star" *ngFor="let num of[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
  5. (click)="rate(num)" [ngStyle]="{'color': getColor(num)}">
  6. </ion-icon>
  7. </div>
  8.  
  9. @Input() rating: number;
  10.  
  11. @Output() ratingChange: EventEmitter<number> = new EventEmitter();
  12.  
  13. constructor() { }
  14.  
  15. rate(index: number){
  16. // function used to change the value of our rating
  17. // triggered when user, clicks a star to change the rating
  18. this.rating = index;
  19.  
  20. this.ratingChange.emit(this.rating);
  21.  
  22. }
  23.  
  24. getColor(index: number) {
  25. /* function to return the color of a star based on what
  26. index it is. All stars greater than the index are assigned
  27. a grey color , while those equal or less than the rating are
  28. assigned a color depending on the rating. Using the following criteria:
  29.  
  30. 1-2 stars: red
  31. 3 stars : yellow
  32. 4-5 stars: green
  33. */
  34.  
  35. if(this.isAboveRating(index)){
  36. return COLORS.GREY;
  37. }
  38.  
  39. switch(this.rating){
  40. case 1:
  41. case 2:
  42. case 3:
  43. case 4:
  44. return COLORS.RED;
  45. case 5:
  46. case 6:
  47. return COLORS.YELLOW;
  48. case 7:
  49. case 8:
  50. case 9:
  51. case 10:
  52. return COLORS.GREEN;
  53. default:
  54. return COLORS.GREY;
  55. }
  56.  
  57. }
  58.  
  59. isAboveRating(index: number): boolean {
  60. // returns whether or not the selected index is above ,the current rating
  61. // function is called from the getColor function.
  62.  
  63. return index> this.rating;
  64. }
  65.  
  66. }
  67.  
  68. enum COLORS {
  69. GREY = "#E0E0E0",
  70. GREEN = "#76FF03",
  71. YELLOW = "#FFCA28",
  72. RED = "#DD2C00"
  73. }
  74.  
  75. import { RatingComponent } from '../rating/rating.component';
  76.  
  77. @NgModule({
  78. imports: [
  79. CommonModule,
  80. FormsModule,
  81. IonicModule,
  82. RouterModule.forChild([
  83. {
  84. path: '',
  85. component: HomePage
  86. }
  87. ])
  88. ],
  89. declarations: [HomePage, RatingComponent ],
  90. schemas: []
  91. })
Add Comment
Please, Sign In to add comment