sushi.js
4.92 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// models / data
var items = new kendo.data.DataSource({
schema: { model: {} },
transport: { read: { url: window.contentPath + "/menu.json", dataType: "json" } }
});
var cart = kendo.observable({
contents: [],
cleared: false,
contentsCount: function() {
return this.get("contents").length;
},
add: function(item) {
var found = false;
this.set("cleared", false);
for (var i = 0; i < this.contents.length; i ++) {
var current = this.contents[i];
if (current.item === item) {
current.set("quantity", current.get("quantity") + 1);
found = true;
break;
}
}
if (!found) {
this.contents.push({ item: item, quantity: 1 });
}
},
remove: function(item) {
for (var i = 0; i < this.contents.length; i ++) {
var current = this.contents[i];
if (current === item) {
this.contents.splice(i, 1);
break;
}
}
},
empty: function() {
var contents = this.get("contents");
contents.splice(0, contents.length);
},
clear: function() {
var contents = this.get("contents");
contents.splice(0, contents.length);
this.set("cleared", true);
},
total: function() {
var price = 0,
contents = this.get("contents"),
length = contents.length,
i = 0;
for (; i < length; i ++) {
price += parseInt(contents[i].item.price) * contents[i].quantity;
}
return kendo.format("{0:c}", price);
}
});
var layoutModel = kendo.observable({
cart: cart
});
var cartPreviewModel = kendo.observable({
cart: cart,
cartContentsClass: function() {
return this.cart.contentsCount() > 0 ? "active" : "empty";
},
removeFromCart: function(e) {
this.get("cart").remove(e.data);
},
emptyCart: function() {
cart.empty();
},
itemPrice: function(cartItem) {
return kendo.format("{0:c}", cartItem.item.price);
},
totalPrice: function() {
return this.get("cart").total();
},
proceed: function(e) {
this.get("cart").clear();
sushi.navigate("/");
}
});
var indexModel = kendo.observable({
items: items,
cart: cart,
addToCart: function(e) {
cart.add(e.data);
}
});
var detailModel = kendo.observable({
imgUrl: function() {
return window.contentPath + "/images/200/" + this.get("current").image
},
price: function() {
return kendo.format("{0:c}", this.get("current").price);
},
addToCart: function(e) {
cart.add(this.get("current"));
},
setCurrent: function(itemID) {
this.set("current", items.get(itemID));
},
previousHref: function() {
var id = this.get("current").id - 1;
if (id === 0) {
id = items.data().length - 1;
}
return "#/menu/" + id;
},
nextHref: function() {
var id = this.get("current").id + 1;
if (id === items.data().length) {
id = 1;
}
return "#/menu/" + id;
},
kCal: function() {
return kendo.toString(this.get("current").stats.energy / 4.184, "0.0000");
}
});
// Views and layouts
var layout = new kendo.Layout("layout-template", { model: layoutModel });
var cartPreview = new kendo.Layout("cart-preview-template", { model: cartPreviewModel });
var index = new kendo.View("index-template", { model: indexModel });
var checkout = new kendo.View("checkout-template", {model: cartPreviewModel });
var detail = new kendo.View("detail-template", { model: detailModel });
var sushi = new kendo.Router({
init: function() {
layout.render("#application");
}
});
var viewingDetail = false;
// Routing
sushi.route("/", function() {
viewingDetail = false;
layout.showIn("#content", index);
layout.showIn("#pre-content", cartPreview);
});
sushi.route("/checkout", function() {
viewingDetail = false;
layout.showIn("#content", checkout);
cartPreview.hide();
});
sushi.route("/menu/:id", function(itemID) {
layout.showIn("#pre-content", cartPreview);
var transition = "",
current = detailModel.get("current");
if (viewingDetail && current) {
transition = current.id < itemID ? "tileleft" : "tileright";
}
items.fetch(function(e) {
if (detailModel.get("current")) { // existing view, start transition, then update content. This is necessary for the correct view transition clone to be created.
layout.showIn("#content", detail, transition);
detailModel.setCurrent(itemID);
} else {
// update content first
detailModel.setCurrent(itemID);
layout.showIn("#content", detail, transition);
}
});
viewingDetail = true;
});
$(function() {
sushi.start();
});