-
Notifications
You must be signed in to change notification settings - Fork 0
/
rating-control.html
94 lines (87 loc) · 2.47 KB
/
rating-control.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
<!-- Imports polymer -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../paper-card/paper-card.html">
<!-- Import icon-toggle -->
<link rel="import" href="./icon-toggle.html">
<!--
Rating control customizable
Example:
Comentario de prueba SmartBoard
<rating-control top="5" type-icon="flag"></rating-control>
How to get the current rating:
document.querySelector('rating-control').current
@demo demo/index.html
-->
<dom-module id="rating-control">
<template>
<style>
:host {
--icon-toggle-color: lightgrey;
--icon-toggle-outline-color: black;
--icon-toggle-pressed-color: orange;
font-family: sans-serif;
};
</style>
<paper-card heading="Rating Control">
<div class="card-content">
<div id="rating"></div>
<div>Current rating {{current}}</div>
</div>
</paper-card>
</template>
<!-- Registers custom element -->
<script>
Polymer({
is: "rating-control",
properties: {
/**
* Top represents the rating's scale
*/
top: {
type: Number,
value: 5,
notify: true,
reflectToAttribute: true
},
/**
* The icon name, for default icons are bring from
* https://elements.polymer-project.org/elements/iron-icons?view=demo:demo/index.html
*/
typeIcon:
{
type: String,
value: 'star'
},
/*
* The current rating selected.
*/
current: {
type:Number,
value: 0,
reflectToAttribute: true
}
},
ready: function(){
for(let i = 0; i< this.top; i ++)
{
var el = document.createElement("icon-toggle");
el.setAttribute('toggle-icon',this.typeIcon);
el.setAttribute('star',i + 1);
el.addEventListener('set_rate', this._set_rate);
this.$.rating.appendChild(el);
}
},
_set_rate: function(e){
let current = this.getAttribute('star');
document.querySelector('rating-control').current = current;
document.querySelectorAll('icon-toggle').forEach(function(element) {
element.removeAttribute('pressed');
if (element.getAttribute('star') <= current) {
element.setAttribute('pressed',true);
}
});
}
});
</script>
</dom-module>