This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeerList.js
349 lines (318 loc) · 9.29 KB
/
beerList.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
var items = [];
var beers = [];
var cart = [];
var undo = []
var redo = []
var cartCount = 0;
var user = sessionStorage.getItem("login");
$(document).ready(function(){
/**
* Fetch beers and populates beer list
*/
updateCredit();
$.getJSON('http://pub.jamaica-inn.net/fpdb/api.php?username=svetor&password=svetor&action=inventory_get',function(inventory){
items = inventory.payload;
$.each(items, function(i, item){
if (item.namn != "" && item.count > 0) {
var $beerList = $('#beerList');
var $beer = $('<div class="beerItem" draggable="true" ondragstart="drag(event)" id="' + item.beer_id + '"></div>');
var $beerName = $('<div class="beerName">' + item.namn + '</div>');
var $beerPubPrice = $('<div class="beerPubPrice">£' + item.pub_price + '</div>');
var $beerAdd = $('<div id="' + item.beer_id + '" class="beerAddList">+</div>');
$beerAdd.on('click', function() {
event.stopPropagation();
var item = getItemById(this.id);
if (item != null && cartCount < 5) {
cartCount++;
addToCart(item)
}
});
var $beerCategory = $('<div class="beerCategory" id="category' + item.beer_id + '"></div>');
var $beerCountry= $('<div class="beerCountry" id="country' + item.beer_id + '"></div>');
var $beerAlc= $('<div class="beerAlc" id="alc' + item.beer_id + '"></div>');
$beer.append($beerName);
$beer.append($beerPubPrice);
$beer.append($beerAdd);
$beer.append($beerCategory);
$beer.append($beerCountry);
$beer.append($beerAlc);
$beer.on('click', function(){
if ($beer.hasClass('expanded')) {
$beer.removeClass('expanded')
}
else {
var id = this.id;
$.getJSON('http://pub.jamaica-inn.net/fpdb/api.php?username=jorass&password=jorass&action=beer_data_get&beer_id=' + id, function(beerData) {
$('#category' + id).html(beerData.payload[0].varugrupp);
$('#country' + id).html(beerData.payload[0].ursprunglandnamn);
$('#alc' + id).html(beerData.payload[0].alkoholhalt);
});
$beer.addClass('expanded');
}
});
$beerList.append($beer);
}
});
});
/**
* Search function for beer list
*/
$('#searchBeer').on('keyup', function(){
var searchStr = $(this).val().toLowerCase();
$(".beerName").each(function(){
var str = $(this).html().toLowerCase();
str.indexOf(searchStr) !== -1 ? $(this).parent().show() : $(this).parent().hide();
});
});
$('#orderButton').on('click', function(){
if (cartCount) {
for (i = 0; i < cart.length; i++) {
for (j = 0; j < cart[i].count; j++) {
$.getJSON('http://pub.jamaica-inn.net/fpdb/api.php?username=' + user + '&password=' + user + '&action=purchases_append&beer_id' + cart[i].item.beer_id, function (){
});
}
}
$.getJSON('http://pub.jamaica-inn.net/fpdb/api.php?username=' + user + '&password=' + user + '&action=payments_append&amount' + $('#cartSumAmount').html(), function (){
});
$('#cartList').empty();
cart = [];
undo = [];
redo = [];
cartCount = 0;
updateCredit();
$('#cartSumAmount').html(0);
$('#cartUndo').css('opacity', 0.5);
$('#cartUndo').css('cursor', 'default');
$('#cartRedo').css('opacity', 0.5);
$('#cartRedo').css('cursor', 'default');
orderButtonStatus(0);
}
});
});
function allowDrop(e) {
e.preventDefault();
}
function drag(e) {
e.dataTransfer.setData("text", e.target.id);
}
/**
* Adds an item to cart when drag and dropped
*/
function drop(e) {
e.preventDefault();
var id = e.dataTransfer.getData("text");
var item = getItemById(id);
if(item != null && cartCount < 5) {
addToCart(item);
cartCount++;
}
}
/**
* Return the item with matching id in items. If no element is found null is returned
*/
function getItemById(id){
for (var i = 0; i < items.length; i++) {
if (id == items[i].beer_id) {
return items[i];
}
}
return null;
}
/**
* Return e is a object returns its ID, else return e.
*/
function getId(e) {
if (typeof(e) == "object") {
return e.target.id;
}
else {
return e;
}
}
/**
* Remove beer from cart
*/
function beerRemove(e) {
var id = getId(e);
if (inCart(id)) {
updateCart(false, id);
cartCount--;
var action = {
action: 'remove',
id: id
};
if (cartCount == 0) {
orderButtonStatus(0);
}
undo.push(action);
}
};
/**
* Add beer to cart
*/
function beerAdd(e) {
var id = getId(e);
if (inCart(id) && cartCount < 5) {
updateCart(true, id)
cartCount++;
var action = {
action: 'add',
id: id
};
undo.push(action);
}
}
/**
* Return true if beer with ID id is in cart
*/
function inCart(id) {
for (var i = 0; i < cart.length; i++) {
if (id == cart[i].item.beer_id) {
return true;
}
}
return false;
}
/**
* Update quantity of item with ID id in cart
* +1 if increase = true, else -1
*/
function updateCart(increase, id) {
var obj = null;
var index = -1;
for (var i = 0; i < cart.length; i++) {
if (id == cart[i].item.beer_id) {
obj = cart[i];
index = i;
}
}
if (increase){
obj.count++;
updateCartSum(true, obj.item.pub_price);
} else {
obj.count--;
updateCartSum(false, obj.item.pub_price);
if (obj.count < 1){
cart.splice(index, 1);
$('#cartObj' + obj.item.beer_id).remove();
}
}
$('#cart' + id).html('X' + obj.count);
}
/**
* Updates the carts sum. If increase == true then add amount to sum, else remove amount from sum.
*/
function updateCartSum(increase, amount) {
amount = parseInt(amount);
var sum = parseInt($('#cartSumAmount').html());
increase ? sum = sum + amount : sum = sum - amount;
$('#cartSumAmount').html(sum);
}
/**
* Fetches user credits from API and updates the view.
*/
function updateCredit() {
$.getJSON('http://pub.jamaica-inn.net/fpdb/api.php?username=' + user + '&password=' + user + '&action=iou_get',function(inventory){
$("#userCreditAmount").html(inventory.payload[0].assets);
});
}
/**
* If status == true then make order button look "clickable", else make order button look not "clickable"
*/
function orderButtonStatus(status) {
if (status) {
$('#orderButton').css('opacity', 1);
$('#orderButton').css('cursor', 'pointer');
}
else {
$('#orderButton').css('opacity', 0.5);
$('#orderButton').css('cursor', 'default');
}
}
/**
* Update an item in the cart if the item is in cart,
* else creates new item and adds to cart.
*/
function addToCart(item) {
if (inCart(item.beer_id)) {
updateCart(true, item.beer_id);
}
else {
var cartItem = {
count: 1,
item: item
};
cart.push(cartItem);
orderButtonStatus(1);
$('#cartUndo').css('opacity', 1);
$('#cartUndo').css('cursor', 'pointer')
updateCartSum(true, item.pub_price);
// Creates cart item
var $cartList = $('#cartList');
var $cartObj = $('<div class="cartObj" id="cartObj' + cartItem.item.beer_id + '"></div>');
var $beer = $('<div class="beerCartItem" id="' + cartItem.item.beer_id + '"></div>');
var $beerName = $('<div class="beerName">' + cartItem.item.namn + '</div>');
var $beerPubPrice = $('<div class="beerPubPrice">£' + cartItem.item.pub_price + '</div>');
var $beerCount = $('<div class="beerCount" id="cart' + cartItem.item.beer_id + '">X' + cartItem.count + '</div>');
var $beerAddRemove = $('<div class="beerAddRemove"</div>');
var $beerRemove = $('<div id="' + cartItem.item.beer_id + '" class="beerRemove" onclick="beerRemove(event)">-</div>');
var $beerAdd = $('<div id="' + cartItem.item.beer_id + '" class="beerAdd" onclick="beerAdd(event)">+</div>');
$beer.append($beerName);
$beer.append($beerPubPrice);
$cartObj.append($beer);
$cartObj.append($beerCount);
$beerAddRemove.append($beerAdd);
$beerAddRemove.append($beerRemove);
$cartObj.append($beerAddRemove);
$cartList.append($cartObj);
}
var action = {
action: 'add',
id: item.beer_id
};
undo.push(action);
}
/**
* Removes latest action made to the cart. If user added a beer, it is removed. If user removed a beer, it is added.
*/
$("#cartUndo").click(function() {
if (undo.length > 0) {
var action = undo.splice(-1,1);
if (action[0].action == 'remove' && cartCount < 5) {
addToCart(getItemById(action[0].id));
cartCount++;
redo.push(action[0]);
undo.pop();
}
else {
beerRemove(action[0].id);
redo.push(action[0]);
undo.pop();
}
$('#cartRedo').css('opacity', 1);
$('#cartRedo').css('cursor', 'pointer')
}
if (undo.length == 0) {
$(this).css('opacity', 0.5);
$(this).css('cursor', 'default')
}
});
/**
* Redos the latest action which was undone by the undo.
*/
$("#cartRedo").click(function() {
if (redo.length > 0 && cartCount < 5) {
var action = redo.splice(-1,1);
if (action[0].action == 'add' && cartCount < 5) {
cartCount++;
addToCart(getItemById(action[0].id));
}
else {
beerRemove(action[0].id);
}
}
if (redo.length == 0) {
$(this).css('opacity', 0.5);
$(this).css('cursor', 'default')
}
});