forked from Anagrr/Fuse.FoodAppExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainView.js
More file actions
104 lines (85 loc) · 2.44 KB
/
Copy pathMainView.js
File metadata and controls
104 lines (85 loc) · 2.44 KB
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
95
96
97
98
99
100
101
102
103
104
// imports
var Observable = require('FuseJS/Observable');
var categoriesList = require('/Data').categoriesList;
var accounting = require("/Modules/Accounting"); // http://openexchangerates.github.io/accounting.js/
// definitions
var categories = Observable();
categories.addAll(categoriesList);
var pageIndex = Observable(0);
var selectedCategory = Observable();
var cartItems = Observable();
var cartItemsCount = cartItems.count();
var subtotal = Observable(0);
var total = Observable(0);
var taxValue = 1;
// listeners
cartItems.onValueChanged(module, calculateSummury); // re-calculate summary after dish was added to cart
// functions
var goToDetails = function(args) {
selectedCategory.value = args.data;
pageIndex.value += 1;
};
var goBack = function() {
pageIndex.value -= 1;
};
var goToStart = function() {
pageIndex.value = 0;
};
var goToEnd = function() {
pageIndex.value = 2;
};
var addToCart = function(args) {
var dish = args.data;
var cartItem = { dish, amount : Observable(1) };
cartItem.amount.onValueChanged(module, calculateSummury); // re-calculate summary after amount of dishes was changed
cartItems.add(cartItem);
var index = selectedCategory.value.dishes.indexOf(dish);
selectedCategory.value.dishes[index].inCart.value = true;
};
function increaseAmount(args) {
var index = cartItems.indexOf(args.data);
cartItems.toArray()[index].amount.value++;
}
function decreaseAmount(args) {
var index = cartItems.indexOf(args.data);
cartItems.toArray()[index].amount.value--;
}
function calculateSummury() {
var sub = 0;
cartItems.forEach(item => {
sub += item.amount.value * item.dish.price;
});
subtotal.value = getPrice(sub);
total.value = getPrice(sub + taxValue);
}
function checkOut() {
// clear cart
cartItems.clear();
// set dishes inCart status to false to prevent imposibility
// of select them again in next order
categories.forEach(ctgr => ctgr.dishes.forEach(d => d.inCart.value = false));
// navigate to main page
goToStart();
}
function getPrice(value) {
return accounting.formatMoney(value, "", 2, " ", ".");
}
module.exports = {
categories,
pageIndex,
selectedCategory,
cartItems,
cartItemsCount,
taxValue,
subtotal,
total,
taxPrice : getPrice(taxValue),
goToDetails,
goBack,
goToStart,
goToEnd,
addToCart,
increaseAmount,
decreaseAmount,
checkOut,
};