-
Notifications
You must be signed in to change notification settings - Fork 31
/
menu.js
173 lines (148 loc) · 5.32 KB
/
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
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
$(document).ready(function () {
// create cart object or get it if it already exists
if (localStorage.getItem("cart") == null) {
var cart = {};
} else {
cart = JSON.parse(localStorage.getItem("cart"));
}
updatecart(cart);
$(".sidebar").hide();
// if add to cart button is clicked, add that item to cart
$(".add").on("click",".cart",function () {
var ids = $(this).attr("id");
if (cart[ids] != undefined) {
cart[ids].quantity += 1;
} else {
let itemname = $("#item"+ids).text();
let itemprice = $("#price"+ids).text();
itemprice = parseInt(itemprice.slice(3,));
cart[ids] = {name:itemname,price:itemprice,quantity:1};
}
updatecart(cart);
});
// show and hide cart
$(".cartsidebar").click(function () {
checkcart(cart);
$(".sidebar").show(500);
$(this).hide();
});
$(".close").click(function () {
$(".sidebar").hide(500);
$(".cartsidebar").show();
});
// adjust the quantity of an item in the cart
$(".add").on("click","button.minus",function(){//*plus
let a=this.id.slice(5,8);
cart[a].quantity-=1;
cart[a].quantity=Math.max(0,cart[a].quantity);
updatecart(cart);
});
$(".add").on("click","button.plus",function(){//*minus
let a=this.id.slice(4,8);
cart[a].quantity+=1;
updatecart(cart);
});
//clear cart
$("#clear").click(function(){
clearcart(cart);
});
//remove item from cart.
//!the jquery is not working properly in the commented part below because the event binding is not done after the cart is refreshed. so the event is not bound to the new elements
// $("button.remove").on("click",function(){
// remove(this,cart);
// });
//*use this instead.it links the event to the document which ensures that all elements with the class remove are linked to the event
$(document).on("click","button.remove",function(){
remove(this,cart);
});
});
//JAVASCRIPT FUNCTION DEFINITIONS. THEY ARE CALLED IN THE JQUERY FUNCTIONS ABOVE
// update cart.
//* if quantity is 0, delete item from cart
//* if quantity is not 0, update the quantity
function updatecart(cart){
for(var item in cart){
if(cart[item].quantity==0){
delete cart[item];
document.getElementById("r"+item).innerHTML=`<button id="`+item+`" class="btn cart" type="button">Add</button>`;
}
else{
document.getElementById("r"+item).innerHTML=`<div><button id='minus${item}' class='btn minus'> -</button></div><div id='val${item}' style='font-size: 3vh;'> ${cart[item].quantity} </div> <div><button id='plus${item}' class='btn plus'>+ </button> </div>`;
}
}
localStorage.setItem('cart', JSON.stringify(cart));
addtocart(cart);
// console.log(cart);
};
// add items to the cart sidebar
function addtocart(cart){
// console.log(1);
checkcart(cart);
let str=``
for(let item in cart){
str+=`
<div class="row">
<div class="col-6">
<strong>`+cart[item].name+`</strong>
</div>
<div class="col-6" style="text-align:end;" >
<strong>Rs.`+cart[item].price+`</strong>
</div>
</div>
<div class="row" style="height:5px"></div>
<div class="row">
<div class="col-6 cartq" style="text-align:start;">
<label>Quantity: <strong>`+cart[item].quantity+`</strong></label>
</div>
<div class="col-6 cartprice" style="padding-right:10px;justify-content:end;">
<button class="remove" id="remove`+item+`"><i class='bx bx-trash-alt' style="color:black" undefined ></i></button>
</div>
</div>
<hr>
`;
}
document.getElementById("cartcontainer").innerHTML=str;
totprice(cart);
}
//calculating totprice = itemprice*qty
function totprice(cart){
let lblprice = document.getElementById('lbltotal');
let finalprice = 0;
for(let item in cart){
finalprice += (cart[item].price * cart[item].quantity);
}
lblprice.innerHTML= finalprice;
}
//check the cart if it is empty or not
function checkcart(cart){
if(Object.keys(cart).length === 0){
document.getElementById('divempty').style.display = 'block';
document.getElementById('divcart').style.display = 'none';
}
else{
document.getElementById('divempty').style.display = 'none';
document.getElementById('divcart').style.display = 'block';
}
};
//clear cart by looping through cart and deleting all items
function clearcart(cart){
for( let item in cart){
document.getElementById("r"+item).innerHTML=`<button id="`+item+`" class="btn cart" type="button">Add</button>`;
}
localStorage.clear();
for(let i in cart){
delete cart[i];
}
updatecart(cart);
}
//* remove item from cart by making the quantity 0, then it will be deleted in updatecart
function remove(e,cart){
let b = e.id.slice(6,);
cart[b].quantity=0;
// console.log(e.id);
// addtocart(cart);
updatecart(cart);
}
// document.getElementsByClassName("remove").addEventListener("click",function(){
// console.log(1);
// });