-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·278 lines (259 loc) · 10.1 KB
/
script.js
File metadata and controls
executable file
·278 lines (259 loc) · 10.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
let filter_btnBrand = document.getElementsByClassName("f_by_b_nam3");
let filter_btnDelivery = document.getElementsByClassName("f_by_d");
let sort_btn = document.getElementsByClassName("s_item_order");
let filter_productByName = document.querySelectorAll('.box');
let filter_productByPrice = document.querySelectorAll(".price_tag");
let filter_productByRating = document.querySelectorAll(".random_rating")
let filter_productByDelivery = document.querySelectorAll('.delivery');
count = 0;
// Ham Menu
function pop() {
document.getElementById("ham_nav").style.height = "200px"
}
function close_nav() {
document.getElementById("ham_nav").style.height = "0px"
}
// For Filter
function expand_filter() {
let filter_menu = document.getElementById('Filter').value;
if (filter_menu == "brand") {
document.getElementById("f_bb_name").style.display = 'flex'
}
else if (filter_menu == "Price") {
document.getElementById("f_bp").style.display = 'flex'
}
else if(filter_menu == 'Star Rating'){
document.getElementById("f_br").style.display = 'flex'
}
else {
document.getElementById('f_bd').style.display = 'flex'
}
}
function expand_sort(){
let sort_menu = document.getElementById("sort").value;
if((sort_menu == 'brand') || (sort_menu == 'price') || (sort_menu == 'rating')){
document.getElementById("s_item").style.display = 'flex'
}
}
// quantity counter for all
document.getElementById('qty').innerText = filter_productByName.length
// logic for filter by brand name
for (i=0; i<filter_btnBrand.length; i++){
filter_btnBrand[i].addEventListener('click', (e) =>{
e.preventDefault()
document.getElementById("f_bb_name").style.display = 'none'
const filter_by_brand = e.target.value;
filter_productByName.forEach((brand_div)=>{
if(brand_div.innerText.includes(filter_by_brand)){
brand_div.style.display = 'flex';
count+=1
document.getElementById("qty").innerText = count;
}
else{
brand_div.style.display = 'none';
}
})
})
}
// logic for filter by price
function filter_price(a, b){
document.getElementById("f_bp").style.display = 'none'
filter_productByPrice.forEach((price_div) =>{
let converted_price = parseInt(price_div.innerText)
if((converted_price > a) && (converted_price <= b)){
price_div.parentElement.parentElement.parentElement.style.display = 'flex'
count+=1
document.getElementById("qty").innerText = count;
}
else{
price_div.parentElement.parentElement.parentElement.style.display = 'none'
}
})
}
// logic for Filter By Rating
function filter_rating(a){
document.getElementById("f_br").style.display = 'none'
filter_productByRating.forEach((rating_div) => {
let converted_rating = parseInt(rating_div.innerText)
if (a >= 3){
if ((converted_rating == a) || (converted_rating > a)){
rating_div.parentElement.parentElement.parentElement.parentElement.style.display = 'flex'
count+=1
document.getElementById("qty").innerText = count;
}
else{
rating_div.parentElement.parentElement.parentElement.parentElement.style.display = 'none'
}
}
else{
if((converted_rating < a)){
rating_div.parentElement.parentElement.parentElement.parentElement.style.display = 'flex'
count+=1
document.getElementById("qty").innerText = count;
}
else{
rating_div.parentElement.parentElement.parentElement.parentElement.style.display = 'none'
}
}
})
}
// logic for filter product by delivery date
for(i=0; i<filter_btnDelivery.length; i++){
filter_btnDelivery[i].addEventListener('click', (e) =>{
e.preventDefault()
document.getElementById('f_bd').style.display = 'none'
const filter_product_by_deliver = e.target.value;
filter_productByDelivery.forEach((delivery_div) =>{
if(filter_product_by_deliver == delivery_div.innerText){
delivery_div.parentElement.parentElement.parentElement.style.display = 'flex'
count+=1
document.getElementById("qty").innerText = count;
}
else{
delivery_div.parentElement.parentElement.parentElement.style.display = 'none'
}
})
})
}
// For Rating color
filter_productByRating.forEach((rating_box_color) =>{
let x = parseFloat(rating_box_color.innerText)
if(x > 4){
rating_box_color.parentElement.style.backgroundColor = 'lightgreen'
}
else if(x < 2){
rating_box_color.parentElement.style.backgroundColor = 'red'
rating_box_color.parentElement.style.color = 'white'
}
})
// Sorting function
let sort_product_by_brand = document.getElementsByClassName('brand');
let sort_product_by_price = document.getElementsByClassName('price_tag');
let sort_product_by_rating = document.getElementsByClassName('random_rating')
for(i=0; i<sort_btn.length; i++){
sort_btn[i].addEventListener('click', (e) =>{
document.getElementById("s_item").style.display = 'none'
let sort_menu = document.getElementById("sort").value;
const sort_order = e.target.value
if (sort_menu == 'brand'){
let unsorted_brand = []
for( j=0; j< sort_product_by_brand.length; j++){
unsorted_brand.push(sort_product_by_brand[j].innerText)
}
sorted_array=unsorted_brand.sort();
sort_by_brand(sorted_array, sort_order);
}
else if(sort_menu == 'price'){
let unsorted_price = []
for(j=0; j<sort_product_by_price.length; j++){
unsorted_price.push(sort_product_by_price[j].innerText);
}
sorted_price = unsorted_price.sort();
sort_by_price(sorted_price, sort_order);
}
else{
let unsorted_rating = []
for(j=0; j<sort_product_by_rating.length; j++){
unsorted_rating.push(sort_product_by_rating[j].innerText);
}
sorted_rating = unsorted_rating.sort()
sort_by_rating(sorted_rating, sort_order)
}
})
}
function sort_by_brand(x, y){
let div_brand_array = []
if(y == 'increasing'){
for(i=0; i<x.length; i++){
for(j=0; j<sort_product_by_brand.length; j++){
if(x[i] == (sort_product_by_brand[j].innerText)){
div_brand_array.push(sort_product_by_brand[j])
}
}
}
for(i=0; i<div_brand_array.length; i++){
let c = div_brand_array[i].parentElement.parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
else{
let rev_x = x.reverse()
for(i=0; i<rev_x.length; i++){
for(j=0; j<sort_product_by_brand.length; j++){
if(rev_x[i] == (sort_product_by_brand[j].innerText)){
div_brand_array.push(sort_product_by_brand[j])
}
}
}
for(i=0; i<div_brand_array.length; i++){
let c = div_brand_array[i].parentElement.parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
}
function sort_by_price(x, y){
let div_price_array = []
if(y == 'increasing'){
for(i=0; i<x.length; i++){
for(j=0; j<sort_product_by_price.length; j++){
if(x[i] == (sort_product_by_price[j].innerText)){
div_price_array.push(sort_product_by_price[j])
}
}
}
for(i=0; i<div_price_array.length; i++){
let c = div_price_array[i].parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
else{
let rev_x = x.reverse()
for(i=0; i<rev_x.length; i++){
for(j=0; j<sort_product_by_price.length; j++){
if(rev_x[i] == (sort_product_by_price[j].innerText)){
div_price_array.push(sort_product_by_price[j])
}
}
}
for(i=0; i<div_price_array.length; i++){
let c = div_price_array[i].parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
}
function sort_by_rating(x, y){
let div_rating_array = []
if(y == 'increasing'){
for(i=0; i<x.length; i++){
for(j=0; j<sort_product_by_rating.length; j++){
if(x[i] == (sort_product_by_rating[j].innerText)){
div_rating_array.push(sort_product_by_rating[j])
}
}
}
for(i=0; i<div_rating_array.length; i++){
let c = div_rating_array[i].parentElement.parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
else{
let rev_x = x.reverse()
for(i=0; i<rev_x.length; i++){
for(j=0; j<sort_product_by_rating.length; j++){
if(rev_x[i] == (sort_product_by_rating[j].innerText)){
div_rating_array.push(sort_product_by_rating[j])
}
}
}
for(i=0; i<div_rating_array.length; i++){
let c = div_rating_array[i].parentElement.parentElement.parentElement.parentElement;
let element = document.getElementById('catalog');
element.appendChild(c);
}
}
}