forked from fuse-open/fuse-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlidingRatingComponent.ux
81 lines (70 loc) · 2.55 KB
/
SlidingRatingComponent.ux
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
<Panel ux:Class="SlidingRatingComponent" Height="40">
<float ux:Property="Rating" />
<int ux:Property="Stars" />
<JavaScript>
var Observable = require("FuseJS/Observable");
var progress = Observable(0);
var currentRating = Observable(0);
var total = this.Stars;
var rating = this.Rating.mapTwoWay(function(v) {
progress.value = v * (100 / total.value);
return v;
}, function(v, sv) {
return v;
});
var stars = [];
for (var i = 0; i < total.value; i++) {
stars.push(new Star(i));
}
function Star(id) {
this.id = id;
this.isActive = Observable(false);
this.image = Observable("Assets/star-empty.png");
}
progress.onValueChanged(module, function(x) {
var newVal = Math.round((x * total.value) / 50) / 2;
if (currentRating.value != newVal) currentRating.value = newVal;
});
currentRating.onValueChanged(module, function(x) {
rating.value = x;
});
rating.onValueChanged(module, function(x) {
var floored = Math.floor(x);
var paintHalf = (x - floored) > 0;
stars.forEach(function(s) {
if (s.id == (floored - 1) && !paintHalf) {
s.image.value = "Assets/star-full.png";
s.isActive.value = true;
} else if (s.id == floored && paintHalf) {
s.image.value = "Assets/star-half.png";
s.isActive.value = true;
} else if (s.id < floored) {
s.image.value = "Assets/star-full.png";
s.isActive.value = false;
} else {
s.image.value = "Assets/star-empty.png";
s.isActive.value = false;
}
});
});
module.exports = {
progress: progress,
stars: stars
};
</JavaScript>
<RangeControl Value="{progress}" UserStep="10" HitTestMode="LocalBounds">
<LinearRangeBehavior />
</RangeControl>
<Panel ux:Class="TheStar">
<Scaling ux:Name="starScaling" Factor="1" />
<WhileFalse Value="{isActive}">
<Change starScaling.Factor="0.86" Duration="0.16" />
</WhileFalse>
<Image File="{image}" Color="#FFC107" />
</Panel>
<StackPanel Orientation="Horizontal">
<Each Items="{stars}">
<TheStar />
</Each>
</StackPanel>
</Panel>