This repository has been archived by the owner on Feb 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iron-star-rating.js
140 lines (130 loc) · 4.16 KB
/
iron-star-rating.js
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
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import '@polymer/iron-icon/iron-icon.js';
import '@polymer/iron-icons/iron-icons.js';
/**
* `iron-star-rating`
* 5-star rating element (Polymer 3.x)
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class IronStarRating extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: inline-block;
--iron-icon-width: var(--star-rating-size, 24px);
--iron-icon-height: var(--star-rating-size, 24px);
}
:host(:not([readonly])) {
cursor: pointer;
}
:host[hidden] {
display: none !important;
}
iron-icon {
color: #eeeeee;
float: right;
}
iron-icon.whole {
-webkit-clip-path: inset(0 0 0 50%);
-moz-clip-path: inset(0 0 0 50%);
-ms-clip-path: inset(0 0 0 50%);
-o-clip-path: inset(0 0 0 50%);
clip-path: inset(0 0 0 50%);
position: relative;
left: calc(var(--iron-icon-width) * -1);
margin-right: calc(var(--iron-icon-width) * -1);
}
iron-icon.half {
-webkit-clip-path: inset(0 50% 0 0);
-moz-clip-path: inset(0 50% 0 0);
-ms-clip-path: inset(0 50% 0 0);
-o-clip-path: inset(0 50% 0 0);
clip-path: inset(0 50% 0 0);
position: relative;
}
iron-icon.selected,
iron-icon.selected ~ iron-icon {
color: #fdd835;
}
:host(:not([readonly])) iron-icon:hover,
:host(:not([readonly])) iron-icon:hover ~ iron-icon {
color: #ffeb3b !important;
}
</style>
<template is="dom-repeat" items="[[ratings]]">
<iron-icon icon="[[icon]]" class\$="[[item.class]] [[_getSelected(item.selected)]]" on-click="_starClicked"></iron-icon>
</template>
`;
}
static get is() { return 'iron-star-rating'; }
static get properties() {
return {
value: {
type: Number,
value: 0,
notify: true,
observer: '_valueChanged'
},
icon: {
type: String,
value: 'icons:star'
},
disableAutoUpdate: {
type: Boolean,
value: false,
},
readonly: {
type: Boolean,
value: false,
reflectToAttribute: true,
}
};
}
constructor() {
super();
this.ratings = [
{value: 5, class: 'whole', selected: false},
{value: 4.5, class: 'half', selected: false},
{value: 4, class: 'whole', selected: false},
{value: 3.5, class: 'half', selected: false},
{value: 3, class: 'whole', selected: false},
{value: 2.5, class: 'half', selected: false},
{value: 2, class: 'whole', selected: false},
{value: 1.5, class: 'half', selected: false},
{value: 1, class: 'whole', selected: false},
{value: 0.5, class: 'half', selected: false},
];
}
_valueChanged(newValue, oldValue) {
if (newValue !== 0 && !newValue) {
return;
}
var self = this;
this.ratings.forEach(function(rating, index) {
if (rating.value === newValue) {
rating.selected = true;
} else {
rating.selected = false;
}
self.notifyPath('ratings.' + index + '.selected')
});
}
_getSelected(selected) {
return selected ? 'selected' : '';
}
_starClicked(e) {
e.preventDefault();
if (this.readonly) {
return;
}
if (!this.disableAutoUpdate) {
this.value = e.model.item.value;
}
this.dispatchEvent(new CustomEvent('rating-selected', {detail: {rating: e.model.item.value}}));
}
}
window.customElements.define(IronStarRating.is, IronStarRating);