-
Notifications
You must be signed in to change notification settings - Fork 1
/
star-rating.html
166 lines (161 loc) · 5.13 KB
/
star-rating.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-repeat.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="star-icons.html">
<dom-module id="star-rating">
<template>
<custom-style>
<style include="iron-flex">
:host {
@apply --star-rating
}
iron-icon {
color: var(--star-color, #faca43);
width: 19px;
height: 19px;
padding: 0;
margin: 0;
}
paper-icon-button {
color: var(--star-color, #faca43);
padding: 0px;
/* offset by 1px for svg/background gap */
width: 31px;
height: 31px;
--paper-icon-button-disabled: {
color: var(--star-color, #faca43);
};
}
.blank-star {
color: var(--empty-color, #bebebe);
}
/* HACK: split the svg in two and merged with css
NOTE: iron-icon uses img so svg styling won't work
see: https://github.com/PolymerElements/iron-icon/issues/99 */
.half-star {
color: var(--star-color, #faca43);
background-image: url('./right-star.svg');
/* offset by 1px for svg/background gap */
--iron-icon-width: 19px;
}
.details {
margin-left: .4em;
color: var(--detail-color, #999999);
font-family: 'Roboto', 'Noto', sans-serif;
-webkit-font-smoothing: antialiased;
font-size: 15px;
font-weight: 400;
line-height: 19px;
@apply --star-rating-details
}
</style>
</custom-style>
<div class="layout horizontal center-center">
<template id="stars" is="dom-repeat" items="{{_stars}}" as="star">
<iron-icon class$="[[star]]" hidden$="[[!readOnly]]" icon$="star-icons:[[star]]"></iron-icon>
<paper-icon-button class$="[[star]]" hidden$="[[readOnly]]" id="{{index}}" on-click="_updateRating" icon$="star-icons:[[star]]"></paper-icon-button>
</template>
<div hidden$="[[!_showDetails]]" class="details">
<span>[[rating]]</span>
<span hidden$="[[!votes]]">([[votes]] votes)</span>
<span hidden$="[[votes]]">(No votes)</span>
</div>
</div>
</template>
<script>
/**
* Shows 5 star rating or enables a 5 star rating using paper-icon-button and iron-icon.*
*
* ### Usage
* Rating can be set as an attribute. It will round to the nearest half star,
* so a rating of 2.3 would end up as 2.5 stars.
*
* ```html
* <star-rating rating="2.3"></star-rating>
* ```
*
* ### Styling
* Custom property | Description | Default
* ----------------|-------------|----------
* `--star-color` | Color set for stars | `#faca43`
* `--star-rating` | Mixin for star-rating | `{}`
* `--star-rating-detail` | Mixin for details | `{}`
* `--empty-color` | Color set for blank stars | `#bebebe`
* `--detail-color` | Text color for details | `#999999`
*
* @demo demo/index.html
*/
class StarRating extends Polymer.Element {
static get is() { return 'star-rating'; }
static get properties() {
return {
_showDetails: {
type: Boolean,
computed: '_displayDetails(details, readOnly)'
},
_stars: {
type: Array,
value: function() {
return ["star", "star", "star", "star", "star"];
}
},
// number of stars assigned for score
rating: {
type: Number,
value: 0,
notify: true,
observer: '_updateStars'
},
// sets stars to editable and hides rating details
readOnly: {
type: Boolean,
value: false
},
// show rating and votes when readOnly is true
details: {
type: Boolean,
value: false
},
// number of votes to display
votes: {
type: Number,
value: 0
}
}
}
connectedCallback() {
super.connectedCallback();
Polymer.RenderStatus.beforeNextRender(this, () => {
this._updateStars();
this.$.stars.render();
});
}
_updateRating(e) {
let id = parseInt(e.currentTarget.id);
this.rating = id + 1;
this._updateStars();
}
_updateStars() {
let int = Math.floor(this.rating);
let decimal = this.rating % 1;
for (var i = 0; i < 5; i++) {
this._stars[i] = "blank-star";
if (i < int)
this._stars[i] = "star";
}
if (decimal >= 0.5)
this._stars[int] = "half-star";
var array = this._stars;
this._stars = [];
this._stars = array;
}
_displayDetails(details, readOnly) {
return details === true && readOnly === true;
}
}
customElements.define(StarRating.is, StarRating);
</script>
</dom-module>