Ok!
The code will display: the entire array and content (top of the screen), the current filter values (color & second filter), the filtered array and the filtering options (second filter then colors to filter).
Project structure:
MainView.ux
/Data
Data.js
Context.js
/MyViews
FirstView.ux
MyJava.js
unoproj:
{
"RootNamespace":"",
"Packages": [
"Fuse",
"FuseJS"
],
"Includes": [
"*",
"Data/*.js:Bundle",
]
}
MainView.ux:
<App>
<ClientPanel>
<MyViews.FirstView />
</ClientPanel>
</App>
Data.js:
function Item(id, parentColor, discount, favorite, recommended){
this.id = id;
this.parentColor = parentColor;
this.discount = discount;
this.favorite = favorite;
this.recommended = recommended;
}
var COLOR_TYPES = {
PINK: '#FFDCE7',
RED: '#C20032',
PURPLE: '#561235',
BLUE: '#0033FF',
GREEN: '#89FAA6',
ALL: '#FFFFFF'
};
var array = [
new Item(0, COLOR_TYPES.PINK, true, false, false),
new Item(1, COLOR_TYPES.PINK, false, true, true),
new Item(2, COLOR_TYPES.PINK, false, false, false),
new Item(3, COLOR_TYPES.RED, true, true, true),
new Item(4, COLOR_TYPES.RED, true, true, false),
new Item(5, COLOR_TYPES.PURPLE, false, false, true),
new Item(6, COLOR_TYPES.BLUE, true, true, true),
new Item(7, COLOR_TYPES.GREEN, true, false, true)
];
function getArray() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(array);
}, 0);
});
}
module.exports = {
getArray: getArray
};
Context.js:
var Observable = require("FuseJS/Observable");
var Data = require("./Data");
var array = Observable();
Data.getArray()
.then(function(newArray) {
array.replaceAll(newArray);
})
.catch(function(error) {
console.log("Couldn't get array: " + error);
});
module.exports = {
array: array
};
MyJava.js:
var Observable = require('FuseJS/Observable');
var Context = require("../Data/Context");
var COLOR_TYPES = {
PINK: '#FFDCE7',
RED: '#C20032',
PURPLE: '#561235',
BLUE: '#0033FF',
GREEN: '#89FAA6',
ALL: '#FFFFFF'
};
var colors = Observable(
{
name: "all",
color: COLOR_TYPES.ALL
},
{
name: "pink",
color: COLOR_TYPES.PINK
},
{
name: "red",
color: COLOR_TYPES.RED
},
{
name: "purple",
color: COLOR_TYPES.PURPLE
},
{
name: "blue",
color: COLOR_TYPES.BLUE
},
{
name: "green",
color: COLOR_TYPES.GREEN
});
var selectedColor = Observable(COLOR_TYPES.ALL);
function setSelectedColor(x){
selectedColor.value = x.data.color;
}
var FILTER_TYPES = {
NONE: 'none',
FAVORITES: 'favorites',
RECOMMENDED: 'recommended',
DEALS: 'deals'
};
var filterButtons = Observable(
{
text: Observable("All"),
type: Observable(FILTER_TYPES.NONE)
},
{
text: Observable("Favorites"),
type: Observable(FILTER_TYPES.FAVORITES)
},
{
text: Observable("Recommended"),
type: Observable(FILTER_TYPES.RECOMMENDED)
},
{
text: Observable("Deals"),
type: Observable(FILTER_TYPES.DEALS)
});
var activeFilter = Observable(FILTER_TYPES.NONE);
function setActiveFilter(x){
activeFilter.value = x.data.type.value;
}
var array = Context.array;
var filteredArray = selectedColor.map(function(f1) {
return activeFilter.map(function(f2) {
return array.where(function(item) {
return (
(item.parentColor == f1.value || f1.value == COLOR_TYPES.ALL )
&&
(
f2.value == FILTER_TYPES.NONE ||
(f2.value == FILTER_TYPES.DEALS && item.discount ) ||
(f2.value == FILTER_TYPES.FAVORITES && item.favorite ) ||
(f2.value == FILTER_TYPES.RECOMMENDED && item.recommended )
)
);
});
}).inner();
}).inner();
module.exports = {
colors: colors,
selectedColor: selectedColor,
array: array,
filteredArray: filteredArray,
filterButtons: filterButtons,
activeFilter: activeFilter,
setSelectedColor: setSelectedColor,
setActiveFilter: setActiveFilter
};
FirstView.ux:
<Panel ux:Class="MyViews.FirstView">
<JavaScript File="MyJava.js" />
<StackPanel Alignment="Top" >
<Text Value="Full Array:" FontSize="20" />
<StackPanel Orientation="Horizontal" ItemSpacing="10" >
<Text Value="Id" />
<Text Value="Color" />
<Text Value="Discount" />
<Text Value="Favorite" />
<Text Value="Recommended" />
</StackPanel>
<Each Items="{array}" >
<StackPanel Orientation="Horizontal" ItemSpacing="10" >
<Text Value="{id}" />
<Text Value="{parentColor}" />
<Text Value="{discount}" />
<Text Value="{favorite}" />
<Text Value="{recommended}" />
</StackPanel>
</Each>
<Text Value="Filter values:" FontSize="20" />
<StackPanel Orientation="Horizontal" >
<Text Value="Selected color: " />
<Text Value="{selectedColor}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Text Value="Selected filter: " />
<Text Value="{activeFilter}" />
</StackPanel>
</StackPanel>
<StackPanel Alignment="Center">
<Text Value="Filtered array:" FontSize="20" />
<Each Items="{filteredArray}" >
<Text Value="{id}" />
</Each>
<WhileEmpty Items="{filteredArray}">
<Text>No results</Text>
</WhileEmpty>
</StackPanel>
<StackPanel Alignment="Bottom" >
<StackPanel Orientation="Horizontal" ItemSpacing="30" >
<Each Items="{filterButtons}" >
<Button Text="{text}" Clicked="{setActiveFilter}" />
</Each>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Each Items="{colors}" >
<Rectangle Width="60" Height="60" Color="{color}" Clicked="{setSelectedColor}" />
</Each>
</StackPanel>
</StackPanel>
</Panel>
Thanks for your support,