-
Notifications
You must be signed in to change notification settings - Fork 0
/
food-menu.js
84 lines (69 loc) · 2.03 KB
/
food-menu.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
// var foodOrder = {}; //global variable to store the foodOrder
var order;
$(document).ready(function() {
var JSONObject = localStorage.getItem("foodOrder"); //JSONObject is a string
order = JSONObject === null ? {} : JSON.parse(JSONObject); //parse converts strings back to key-values
if(order["items"] == undefined)
order["items"] = {};
//For each input box on the page
$(':input[type="number"]').each(function(){
var item = this.id;
var quantity = order["items"][item];
//If the item is stored in the database, retrieve the quantity
if(quantity == undefined) {
order["items"][item] = 0;
quantity = 0;
}
this.value = quantity;
});
$(':input[type="number"]').on("change, keyup, mouseup", calcTotal);
calcTotal();
});
/*Fuction for incrementing in the food menu*/
function incrementValue(id){
var item = "#" + id;
var quantity = parseInt($(item).val());
$(item).val(quantity + 1);
calcTotal();
}
function decrementValue(id){
var item = "#" + id;
var quantity = parseInt($(item).val());
// if(quantity == 0)
// return;
$(item).val(quantity - 1);
calcTotal();
}
function clearOrder(){
// For each input item
$(':input[type="number"]').each(function(){
var item = this.id;
//Update the value on the GUI
this.value = 0;
//Update the value in the database
order["items"][item] = this.value;
});
//Write the database back into storage
calcTotal();
//change the displayed total to 0
var td = document.getElementById("totalDisplay");
td.innerHTML = "$" + 0;
}
function calcTotal() {
var total = 0;
$(':input[type="number"]').each(function() {
if(this.value < 0)
this.value = 0;
var item = this.id;
var quantity = this.value;
order["items"][item] = quantity;
var price = prices[item];
var subtotal = quantity*price;
total += subtotal;
});
// save to database
var JSONObject = JSON.stringify(order);
localStorage.setItem("foodOrder", JSONObject);
// total display
$("#totalDisplay").html("$" + total);
}