Skip to content

Commit 6678897

Browse files
Uldis ZeidursUldis Zeidurs
Uldis Zeidurs
authored and
Uldis Zeidurs
committed
adding Rating controls example
1 parent e9112b8 commit 6678897

10 files changed

+183
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ These samples are supplied directly from the feature teams of Fuse and we welcom
8181
<td><a href="Samples/Controls/Circular">Circular control</a></td>
8282
<td><a href="Samples/Controls/ButtonWithImage">Button with image</a></td>
8383
</tr>
84+
<tr>
85+
<td><a href="Samples/RatingComponents">Rating controls</a></td>
86+
</tr>
8487
</table>
8588

8689
## Misc
669 Bytes
Loading
475 Bytes
Loading
559 Bytes
Loading
438 Bytes
Loading

Samples/RatingComponents/MainView.ux

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<App Background="#fff">
2+
3+
<JavaScript>
4+
var Observable = require("FuseJS/Observable");
5+
var staticRating = Observable(2);
6+
var slidingRating = Observable(3);
7+
8+
staticRating.onValueChanged(module, function(x) {
9+
console.log("staticRating changed to: " + x);
10+
});
11+
slidingRating.onValueChanged(module, function(x) {
12+
console.log("slidingRating changed to: " + x);
13+
});
14+
15+
module.exports = {
16+
staticRating: staticRating,
17+
slidingRating, slidingRating
18+
};
19+
</JavaScript>
20+
21+
<ClientPanel>
22+
<StackPanel Alignment="Center" ItemSpacing="24">
23+
<StaticRatingComponent Stars="5" Rating="{staticRating}" />
24+
<SlidingRatingComponent Stars="5" Rating="{slidingRating}" />
25+
</StackPanel>
26+
</ClientPanel>
27+
</App>

Samples/RatingComponents/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Using `mapTwoWay` to create reusable rating components
2+
3+
This sample shows how to use `Observable.mapTwoWay()` to properly isolate reusable rating components.
4+
Two different components are implemented - one that records the rating value upon clicking a star, and the other with a `RangeControl` that allows for a swiping gesture to set the rating.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"RootNamespace": "",
3+
"Packages": [
4+
"Fuse",
5+
"FuseJS"
6+
],
7+
"Includes": [
8+
"*",
9+
"Assets/*.png:Bundle"
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<Panel ux:Class="SlidingRatingComponent" Height="40">
2+
<float ux:Property="Rating" />
3+
<int ux:Property="Stars" />
4+
5+
<JavaScript>
6+
var Observable = require("FuseJS/Observable");
7+
var progress = Observable(0);
8+
var currentRating = Observable(0);
9+
10+
var total = this.Stars;
11+
var rating = this.Rating.mapTwoWay(function(v) {
12+
progress.value = v * 20;
13+
return v;
14+
}, function(v, sv) {
15+
return v;
16+
});
17+
18+
var stars = [];
19+
for (var i = 0; i < total.value; i++) {
20+
stars.push(new Star(i));
21+
}
22+
23+
function Star(id) {
24+
this.id = id;
25+
this.isActive = Observable(false);
26+
this.image = Observable("Assets/star-empty.png");
27+
}
28+
29+
progress.onValueChanged(module, function(x) {
30+
var newVal = Math.round((x * 5) / 50) / 2;
31+
if (currentRating.value != newVal) currentRating.value = newVal;
32+
});
33+
34+
currentRating.onValueChanged(module, function(x) {
35+
rating.value = x;
36+
});
37+
38+
rating.onValueChanged(module, function(x) {
39+
var floored = Math.floor(x);
40+
var paintHalf = (x - floored) > 0;
41+
stars.forEach(function(s) {
42+
if (s.id == (floored - 1) && !paintHalf) {
43+
s.image.value = "Assets/star-full.png";
44+
s.isActive.value = true;
45+
} else if (s.id == floored && paintHalf) {
46+
s.image.value = "Assets/star-half.png";
47+
s.isActive.value = true;
48+
} else if (s.id < floored) {
49+
s.image.value = "Assets/star-full.png";
50+
s.isActive.value = false;
51+
} else {
52+
s.image.value = "Assets/star-empty.png";
53+
s.isActive.value = false;
54+
}
55+
});
56+
});
57+
58+
module.exports = {
59+
progress: progress,
60+
stars: stars
61+
};
62+
</JavaScript>
63+
64+
<RangeControl Value="{progress}" UserStep="10" HitTestMode="LocalBounds">
65+
<LinearRangeBehavior />
66+
</RangeControl>
67+
68+
<Panel ux:Class="TheStar">
69+
<FileSource ux:Property="Image" />
70+
<Scaling ux:Name="starScaling" Factor="1" />
71+
<WhileFalse Value="{isActive}">
72+
<Change starScaling.Factor="0.86" Duration="0.16" />
73+
</WhileFalse>
74+
<Image File="{ReadProperty Image}" Color="#FFC107" />
75+
</Panel>
76+
77+
<StackPanel Orientation="Horizontal">
78+
<Each Items="{stars}">
79+
<TheStar Image="{image}" />
80+
</Each>
81+
</StackPanel>
82+
</Panel>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<StackPanel ux:Class="StaticRatingComponent" Height="40" Orientation="Horizontal">
2+
<int ux:Property="Rating" />
3+
<int ux:Property="Stars" />
4+
5+
<JavaScript>
6+
var Observable = require("FuseJS/Observable");
7+
8+
var total = this.Stars;
9+
var rating = this.Rating.mapTwoWay(function(v) {
10+
return v;
11+
}, function(v, sv) {
12+
return v;
13+
});
14+
15+
var stars = [];
16+
for (var i = 0; i < total.value; i++) {
17+
stars.push(new Star(i));
18+
}
19+
20+
function Star(id) {
21+
this.id = id;
22+
this.isActive = Observable(false);
23+
}
24+
25+
function selectStar(args) {
26+
rating.value = args.data.id + 1;
27+
}
28+
29+
rating.onValueChanged(module, function(x) {
30+
stars.forEach(function(s) {
31+
if (s.id < x) {
32+
s.isActive.value = true;
33+
} else {
34+
s.isActive.value = false;
35+
}
36+
});
37+
});
38+
39+
module.exports = {
40+
stars: stars,
41+
selectStar: selectStar
42+
};
43+
</JavaScript>
44+
45+
<Each Items="{stars}">
46+
<Panel HitTestMode="LocalBounds">
47+
<Clicked>
48+
<Callback Handler="{selectStar}" />
49+
</Clicked>
50+
<WhileTrue Value="{isActive}">
51+
<Change theStar.Color="#FFC107" Duration="0.16" />
52+
</WhileTrue>
53+
<Image ux:Name="theStar" File="Assets/star.png" Color="#9E9E9E" />
54+
</Panel>
55+
</Each>
56+
</StackPanel>

0 commit comments

Comments
 (0)