-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
251 lines (220 loc) · 8.48 KB
/
script.js
File metadata and controls
251 lines (220 loc) · 8.48 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
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
// $ function is loaded only once HTML has completely loaded
$(function () {
var done = false;
function isCartEmpty() {
var counter = 0;
for(i=1;i<=3;i++)
{
if (localStorage.getItem('prod_' + i)) {
counter++;
}
}
return counter;
}
function setTotalCost() {
var total_cost = 0;
for(i=1;i<=3;i++)
{
if (localStorage.getItem('prod_' + i)) {
var qty = JSON.parse(localStorage.getItem('prod_' + i)).quantity;
var cost = +$('cost[id=' + i + ']').text();
total_cost += qty*cost;
}
}
if (localStorage.getItem('total_cost')) {
$('#totalCost').text(total_cost);
localStorage.setItem('total_cost', total_cost);
}
else {
localStorage.setItem('total_cost', '0');
$('#totalCost').text('0');
}
} // end of the function setTotalCost
function setNoOfProducts() {
var no_of_products = 0;
for(i=1;i<=3;i++)
{
if (localStorage.getItem('prod_' + i)) {
no_of_products += JSON.parse(localStorage.getItem('prod_' + i)).quantity;
}
}
localStorage.setItem('no_of_products', no_of_products);
if (localStorage.getItem('no_of_products')) {
no_of_products = +localStorage.getItem('no_of_products');
$('#noOfProducts').text(no_of_products);
}
else {
localStorage.setItem('no_of_products', '0');
no_of_products = +localStorage.getItem('no_of_products');
$('#noOfProducts').text(no_of_products);
}
} // end of the function setNoOfProucts
function updateCart() {
no_of_products = +localStorage.getItem('no_of_products');
if (no_of_products) {
if (!done) {
var cart_head = $('#cartItemsHead');
var head_string = "<tr><th>Product Name</th><th>Quantity</th><th>Amount</th><tr>";
cart_head.append(head_string);
done = true;
}
// display using cart.append
var cart_body = $('#cartItemsBody');
cart_body.empty(); // to delete its elements
for(i=1;i<=3;i++)
{
if (localStorage.getItem('prod_'+i)) {
cartItem = JSON.parse(localStorage.getItem('prod_'+i));
var name = $('product[id=' + i + ']').text();
var cost = +$('cost[id=' + i + ']').text();
var amount = cost*cartItem.quantity;
var delCart = "delCartItem";
var cartString = "<tr><td><button id=" + i + " class=" + "red" + " name=" + "delCartItem" + ">x</button><cname id=" + i + ">"+name+"</cname></td>";
cartString += "<td><button id=" + i + " name=" + "cminus" + " class=" + "red" + ">-</button>";
cartString += "<cquant id=" + i + ">"+cartItem.quantity+"</cquant>";
cartString += "<button id=" + i + " name=" + "cplus" + " class=" + "green" +">+</button></td>";
cartString +="<td><camount id=" + i + ">"+amount+"</camount></td></tr>";
cart_body.append(cartString);
}
}
setCartStyle(); // call of the function setCartStyle
}
else
{
var cart_head = $('#cartItemsHead');
cart_head.empty(); // to remove the child elements
var cart_body = $('#cartItemsBody');
cart_body.empty(); // to delete its elements
}
} // end of the function updateCart
function setCartStyle() { // Using this function to set style of cminus and cplus buttons
var cButtonsPlus = $('button[name="cplus"]');
var cButtonsMinus = $('button[name="cminus"]');
for (var i = 0; i < 3 ; i++) {
if (cButtonsMinus[i]) {
cButtonsMinus[i].style.float = "left";
cButtonsPlus[i].style.float = "right";
}
}
}
function cartRefresh() { // every time the page is loaded, the card is refreshed
setTotalCost(); // call of the function setTotalCost
setNoOfProducts(); // call of the function setNoOfProducts
updateCart(); // call of the function updateCart
} // end of the function cartRefresh
function init() {
// setTable
for (var i = 1; i <= 3 ; i++) {
$('quantity[id=' + i +']').text(1);
}
cartRefresh(); // call of the function cartRefresh
} // end of the function init
init(); // call of the function init
function reset() {
localStorage.setItem('no_of_products', 0);
no_of_products = +localStorage.getItem('no_of_products');
$('#totalCost').text(no_of_products);
localStorage.setItem('total_cost', 0);
total_cost = +localStorage.getItem('total_cost');
$('#noOfProducts').text(total_cost);
for (var i = 1; i <= 3 ; i++) {
$('quantity[id=' + i +']').text(1);
}
var cart_head = $('#cartItemsHead');
cart_head.empty(); // to remove the child elements
var cart_body = $('#cartItemsBody');
cart_body.empty(); // to remove the child elements
done = false;
} // end of the function reset
function qtyDecrement(qty_id) {
if (($('quantity[id=' + qty_id + ']').text())>1) { // Quantity can't be lesser than 1
var x = +$('quantity[id=' + qty_id + ']').text();
$('quantity[id=' + qty_id + ']').text(--x);
}
} // end of the function qtyDecrement
function qtyIncrement(qty_id) {
var x = +$('quantity[id=' + qty_id + ']').text();
$('quantity[id=' + qty_id + ']').text(++x);
} // end of the function qtyIncrement
// Very important function
$('body').on('click', '.red' , function() { // To delete elements after they've been dynamically updated
if (this.name == "delCartItem") {
localStorage.removeItem("prod_" + this.id);
cartRefresh();
if(!isCartEmpty())
{
done = false;
}
}
if (this.name == "cminus") {
$('cquant[id=' + this.id + ']').text()
if (($('cquant[id=' + this.id + ']').text())>1) { // Quantity can't be lesser than 1
var x = +$('cquant[id=' + this.id + ']').text();
$('cquant[id=' + this.id + ']').text(--x);
}
var qty = +$('cquant[id=' + this.id + ']').text();
cartItem = JSON.parse(localStorage.getItem('prod_'+this.id));
newcartItem = {
'id': this.id,
'quantity': (qty)
};
localStorage.removeItem('prod_'+this.id)
localStorage.setItem('prod_' + this.id, JSON.stringify(newcartItem));
cartRefresh();
}
});
$('body').on('click', '.green' , function() { // To delete elements after they've been dynamically updated
if (this.name == "cplus") {
var x = +$('cquant[id=' + this.id + ']').text();
$('cquant[id=' + this.id + ']').text(++x);
var qty = +$('cquant[id=' + this.id + ']').text();
cartItem = JSON.parse(localStorage.getItem('prod_'+this.id));
newcartItem = {
'id': this.id,
'quantity': (qty)
};
localStorage.removeItem('prod_'+this.id)
localStorage.setItem('prod_' + this.id, JSON.stringify(newcartItem));
cartRefresh();
}
});
$("button").click(function() { // button click function
if (this.name == "plus") { // if '-' button is clicked
qtyIncrement(this.id); // increase quantity by 1
}
if (this.name == "minus") { // if '-' button is clicked
qtyDecrement(this.id); // decrease quantity by 1
}
if (this.name == "add-to-cart") {
var qty = +$('quantity[id=' + this.id + ']').text();
var cost = +$('cost[id=' + this.id + ']').text();
// add to cart (local)
if (localStorage.getItem('prod_'+this.id)) {
cartItem = JSON.parse(localStorage.getItem('prod_'+this.id));
newcartItem = {
'id': this.id,
'quantity': (qty+cartItem.quantity)
};
localStorage.removeItem('prod_'+this.id)
localStorage.setItem('prod_' + this.id, JSON.stringify(newcartItem));
}
else {
newcartItem = {
'id': this.id,
'quantity': qty
};
localStorage.setItem('prod_' + this.id, JSON.stringify(newcartItem));
}
cartRefresh(); // call of the function cartRefresh
setCartStyle(); // call of the function setCartStyle
$('quantity[id=' + this.id + ']').text(1);
}
if (this.name == "checkout") {
alert('Thank you for shopping!');
reset(); // call of the function reset
for (var i = 3; i >= 1; i--) {
localStorage.removeItem('prod_' + i);
}
}
});
});