-
Notifications
You must be signed in to change notification settings - Fork 0
/
rating-stars.js
51 lines (38 loc) · 1.33 KB
/
rating-stars.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
// (c) 2018-present, The Awesome Engineering Company, https://awesomeneg.com
/* eslint no-console: off */
import {ZephComponents} from "./Zeph.js";
import {html,css,asset,attribute,property,bind,onProperty,onEventAt} from "./Zeph.js";
ZephComponents.define("rating-stars",()=>{
html("./rating-stars.html");
css("./rating-stars.css");
asset(".set","./rating-stars.filled.png");
asset(".unset","./rating-stars.empty.png");
attribute("value","0");
attribute("disabled",undefined);
property("value",0);
bind("@value",".",".value",(value)=>{
value = parseInt(value);
if (!value || isNaN(value)) value = 0;
return value;
});
onProperty("value",(name,value,element,content)=>{
update(element,content);
});
onEventAt("div.star","click",(event,selected,element,content)=>{
if (element.hasAttribute("disabled")) {
event.stopPropagation();
event.preventDefault();
return;
}
let value = [...content.querySelectorAll(".stars > div.star")].indexOf(selected)+1;
if (element.hasAttribute("value") && parseInt(element.getAttribute("value"))===value) value = 0;
element.setAttribute("value",value);
});
const update = (element,content)=>{
let value = element.value;
content.querySelectorAll(".stars > div.star").forEach((e,i)=>{
e.removeAttribute("selected");
if (value>i) e.setAttribute("selected","");
});
};
});