-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (171 loc) · 3.74 KB
/
Copy pathscript.js
File metadata and controls
203 lines (171 loc) · 3.74 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
function Product(id,name,price)
{
this.id=id;
this.name=name;
this.price=price;
}
var p1 = new Product(1,"Google Pixel",45000)
var p2 = new Product(2,"Iphone",60000)
var p3 = new Product(3,"MacBook Pro",130000)
var p4 = new Product(4,"Sony Headphones",12000)
var p5 = new Product(5,"Play Station 4",60000)
var p6 = new Product(6,"Microsoft Surface",55000)
var products = [p1,p2,p3,p4,p5,p6];
var purchase = [];
function retrievePurchase()
{
var purchase = JSON.parse(localStorage.getItem('purchases')?localStorage.getItem('purchases'):"[]");
return purchase
}
function savePurchase()
{
localStorage.setItem('purchases',JSON.stringify(purchase));
}
function updatePurchases(purch)
{
localStorage.setItem('purchases',JSON.stringify(purch));
}
function savedetails()
{
localStorage.setItem('products',JSON.stringify(products));
}
function refreshTotal(total)
{
// console.log(total);
var x = document.getElementById('totalLabel');
x.innerText=total;
}
function refreshTable()
{
$('#item-table-body').html('');
var pur = retrievePurchase()
var t = JSON.parse(localStorage.getItem('total'));
var total=0;
for(var p of pur)
{
var pq = (p.qty)*(p.price)
$('#item-table').append("<tr> <td>" + p.id + " </td> <td>" + p.name + "</td> <td>" + p.price + " </td> <td>" + " <button id=plus"+p.id+">+</button> " + p.qty + " <button id=sub"+p.id+">-</button>" + "</td> <td>" + pq + " </td></tr>")
total+=pq;
}
$("button").click(function(e){
var idClicked = e.target.id;
var pp = retrievePurchase();
for(i=0;i<pp.length;i++)
{
var ob=pp[i];
if(("plus"+ob.id)===idClicked)
{
ob.qty++;
}
if(("sub"+ob.id)===idClicked)
{
ob.qty--;
if(ob.qty<=0)
{
pp.splice(i,1);
}
}
}
updatePurchases(pp)
refreshTable()
});
refreshTotal(total);
}
function checkPurchases(id)
{
var pur = retrievePurchase();
for(var p of pur)
{
if(p.id==id)
{
return p.qty;
}
}
return 0;
}
window.onscroll = function() { scrollFunction() };
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
function topFunction() {
// console.log("top button clicked")
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
$(function()
{
savedetails()
retrievePurchase();
refreshTable();
$("button").click(function(e)
{
var idClicked = e.target.id;
for(i=1;i<=6;i++)
{
if(idClicked===("btn"+i))
{
console.log(idClicked)
purchase = retrievePurchase();
var id=i;
var det = getDetails(id);
if(det!==0)
{
purchase.push({
id:id,
name:det.name,
price:det.price,
qty:det.qty
});
}
savePurchase();
retrievePurchase();
refreshTable();
}
}
});
function getDetails(id)
{
var pro;
var containsAlready = checkPurchases(id);
var inc = containsAlready+1;
// console.log(containsAlready)
var products = JSON.parse(localStorage.getItem('products'));
if(containsAlready==0)
{
for(p of products)
{
if(p.id===id)
{
pro =
{
name:p.name,
price:p.price,
qty:1
}
}
}
return pro
}
else
{
window.alert("This item exists in Cart. Kindly view your cart !");
return 0;
}
}
})