This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
salte-rating.html
171 lines (147 loc) · 4.14 KB
/
salte-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
167
168
169
170
171
<link rel="import" href="../polymer/polymer.html">
<!-- Iron Elements -->
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<!--
`salte-rating` allows you to display ratings with extreme precision.
### Styling
The following custom properties and mixins are also available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--salte-rating-limit-color` | Color of the max number of items | `rgba(200, 200, 200, 0.9)`
`--salte-rating-value-color` | Color of the current rating items | `rgba(55, 55, 55, 0.9)`
`--salte-rating-disabled-color` | Color of the current rating items when disabled | `rgba(55, 55, 55, 0.9)`
@demo demo/index.html
-->
<dom-module id="salte-rating">
<template>
<style>
:host {
display: block;
position: relative;
}
:host([disabled]) .foreground-rating .item {
cursor: default;
}
.background-rating {
color: var(--salte-rating-limit-color, rgba(200, 200, 200, 0.9));
}
:host([disabled]) .foreground-rating {
color: var(--salte-rating-disabled-color, rgba(55, 55, 55, 0.9));
}
.foreground-rating {
color: var(--salte-rating-value-color, rgba(55, 55, 55, 0.9));
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.item {
display: inline-block;
cursor: pointer;
}
.item-icon {
overflow: hidden;
}
</style>
<div class="background-rating">
<template is="dom-repeat" items="{{limitItems}}" strip-whitespace>
<iron-icon icon="[[icon]]"></iron-icon>
</template>
</div>
<div class="foreground-rating">
<template is="dom-repeat" items="{{limitItems}}" strip-whitespace>
<div class="item" on-mouseover="_previewRating" on-mouseout="reset" on-tap="_onRate">
<div class="item-icon">
<iron-icon icon="[[icon]]"></iron-icon>
</div>
</div>
</template>
</div>
</template>
<script>
Polymer({
is: 'salte-rating',
properties: {
/**
* The number of items to display
*/
limit: {
type: Number,
value: 5,
observer: 'renderItems'
},
/**
* The icon to use for the rating
*/
icon: {
type: String,
value: 'star'
},
/**
* The current rating
*/
value: {
type: Number,
value: 0,
observer: 'reset'
},
/**
* If true, the user will not be able to select a rating.
*/
disabled: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* The rating items displayed on the screen
*/
limitItems: {
type: Array,
readOnly: true
}
},
attached: function() {
this.async(this.reset);
},
/**
* Refreshes the rating display to the current rating
*/
reset: function() {
this._refreshRating(this.value);
},
/**
* Renders the items based on the limit
*/
renderItems: function() {
var limitItems = [];
for (var i = 0; i < this.limit; i++) {
limitItems.push(i);
}
this._setLimitItems(limitItems);
},
_refreshRating: function(value) {
var items = Polymer.dom(this.root).querySelectorAll('.item-icon');
for (var i = 0; i < items.length; i++) {
items[i].style.width = Math.min(Math.max(value - i, 0), 1) * 100 + '%';
}
},
_previewRating: function(e) {
if (this.disabled) return;
this._refreshRating(e.model.index + 1);
},
_onRate: function(e) {
if (this.disabled) return;
this.fire('rate', { rating: e.model.index + 1});
}
/**
* Fired when the user clicks one of the rating items
*
* @event rate
* @param {number} rating the rating the user selected.
*/
});
</script>
</dom-module>