-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (143 loc) · 4.61 KB
/
index.js
File metadata and controls
152 lines (143 loc) · 4.61 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
let cart = [] ;
window.onload = function () {
loadItems();
RetriveCart();
var cartBox = document.getElementById("cart");
cartBox.addEventListener('click', ()=>{ window.location.href = "Cart.html"});
}
let AddtoCart = (ev) =>{
let d = ev.target.id;
// console.log(d);
if(ev.target.id[0]==='b'||ev.target.id[0]==='i')
{
d=d.slice(d.length-1,d.length);
}
const i = cart.findIndex(obj => obj.id === d);
console.log(i);
console.log(cart)
if(i>=0)
{
cart = [
...cart.slice(0,i),
{...cart[i],quantity:cart[i].quantity+1},
...cart.slice(i+1)
]
}
else
cart.push({id:d,quantity:1});
SaveCart();
let length = 0;
for(let i =0;i< cart.length;i++)
length = length + cart[i].quantity;
let c = document.getElementById('count');
c.innerHTML = length;
}
let SaveCart = ()=> {
localStorage.setItem("Cart",JSON.stringify(cart));
}
let RetriveCart = ()=> {
let getcart = localStorage.getItem("Cart");
if(getcart)
{
cart = JSON.parse(getcart);
}
let length = 0;
for(let i =0;i< cart.length;i++)
length = length + cart[i].quantity;
let c = document.getElementById('count');
c.innerHTML = length;
}
let AddListeners = ()=>{
for(let i =0;i<9;i++)
{
let j = "" + i;
let k = document.getElementById(j);
// console.log(k)
k.addEventListener('click', AddtoCart);
}
}
let loadItems = () =>{
$.getJSON('data.json',(data) =>{
//console.log(data.length);
//var size = data.length;
for(let i=0;i<data.length-3;i+=3) {
// console.log("hello i am in");
$("#catalog").append(`
<div class="catalog--row">
<div class="item">
<img src=${data[i].url} class="item--image" >
<div class="item--add" id=${data[i].id}>
<div class="item--add--btn" id="btn--${data[i].id}">
<img src="img/shopping-cart-white.svg" class="icon" id="img--${data[i].id}">
</div>
</div>
<div class="item--detail">
<div class="item--name">
${data[i].name}
</div>
<div class="item--price">
${data[i].price}
</div>
</div>
</div>
<div class="item">
<img src=${data[i + 1].url} class="item--image" >
<div class="item--add" id=${data[i + 1].id}>
<div class="item--add--btn" id="btn--${data[i + 1].id}">
<img src="img/shopping-cart-white.svg" class="icon" id="img--${data[i+2].id}">
</div>
</div>
<div class="item--detail">
<div class="item--name">
${data[i + 1].name}
</div>
<div class="item--price">
${data[i + 1].price}
</div>
</div>
</div>
<div class="item">
<img src=${data[i + 2].url} class="item--image">
<div class="item--add" id=${data[i + 2].id}>
<div class="item--add--btn" id="btn--${data[i + 2].id}">
<img src="img/shopping-cart-white.svg" class="icon" id="img--${data[i+2].id}">
</div>
</div>
<div class="item--detail">
<div class="item--name">
${data[i + 2].name}
</div>
<div class="item--price">
${data[i + 2].price}
</div>
</div>
</div>
</div>`)
}
AddListeners();
});
// console.log("hello i am out ")
}
(function($) { // Begin jQuery
$(function() { // DOM ready
// If a link has a dropdown, add sub menu toggle.
$('nav ul li a:not(:only-child)').click(function(e) {
$(this).siblings('.nav-dropdown').toggle();
// Close one dropdown when selecting another
$('.nav-dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
// Clicking away from dropdown will remove the dropdown class
$('html').click(function() {
$('.nav-dropdown').hide();
});
// Toggle open and close nav styles on click
$('#nav-toggle').click(function() {
$('nav ul').slideToggle();
});
// Hamburger to X toggle
$('#nav-toggle').on('click', function() {
this.classList.toggle('active');
});
}); // end DOM ready
})(jQuery); // end jQuery