-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
178 lines (147 loc) · 5.14 KB
/
script.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
174
175
176
177
let url = "http://localhost:3000/products";
let items;
function getItems() {
fetch(url)
.then(response => response.json())
.then(products => {
items = products;
let currentCategory = "all";
// let currentColumns = determineColumns();
// displayItems(currentCategory, currentColumns);
displayItems(currentCategory);
})
.catch(error => alert(error))
}
const itemSizeListData = ["XS", "S", "M", "L", "XL", "XXL"];
function getQueryParams() {
const params = new URLSearchParams(window.location.search);
const queryParams = {};
for (const [key, value] of params.entries()) {
queryParams[key] = value;
}
return queryParams;
}
// function determineColumns() {
// const screenWidth = window.innerWidth;
// let defaultColumns = 6;
// if (screenWidth <= 576) {
// defaultColumns = 1;
// } else if (screenWidth <= 768) {
// defaultColumns = 2;
// } else if (screenWidth <= 1200) {
// defaultColumns = 3;
// }
// return defaultColumns;
// }
function debounce(func, wait) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
function displayItemSize() {
const itemList = document.getElementById("itemSizeList");
itemList.innerHTML = "";
itemSizeListData.forEach((item) => {
const itemElement = document.createElement("div");
itemElement.className = "itemSizeElement";
itemElement.textContent = item;
itemList.appendChild(itemElement);
});
}
function displayItems(category) {
const itemList = document.getElementById("productList");
itemList.innerHTML = "";
items.forEach((item) => {
if (category === "all" || item.category === category) {
const itemElement = document.createElement("div");
itemElement.className = "item";
const imageElement = document.createElement("img");
imageElement.src = "images/" + item.image;
imageElement.alt = item.name;
itemElement.appendChild(imageElement);
const titleElement = document.createElement("div");
titleElement.textContent = item.name;
itemElement.appendChild(titleElement);
const priceElement = document.createElement("div");
priceElement.textContent = item.price + " \u20AC";
itemElement.appendChild(priceElement);
// itemElement.style.width = `${Math.floor(100 / columns) - 1}%`;
itemElement.addEventListener("click", () => {
window.location.href = `article.html?id=${item.id}`;
});
itemList.appendChild(itemElement);
}
});
}
let path = window.location.pathname;
if (path.includes("/index")) {
//INDEX Page script
document.addEventListener("DOMContentLoaded", () => {
const hamburgerMenu = document.getElementById("hamburgerMenu");
const overlay = document.getElementById("overlay");
hamburgerMenu.addEventListener("click", () => {
overlay.style.display =
overlay.style.display === "flex" ? "none" : "flex";
});
});
} else if (path.includes("/damen")) {
getItems();
document.querySelectorAll(".menuOptions a").forEach((anchor) => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
if (this.hasAttribute("dataCategory")) {
document.querySelectorAll("div a[dataCategory]").forEach((item) => {
item.classList.remove("active");
});
this.classList.add("active");
currentCategory = this.getAttribute("dataCategory");
// displayItems(currentCategory, currentColumns);
displayItems(currentCategory);
}
});
});
document.querySelectorAll(".columnsOption").forEach((icon) => {
icon.addEventListener("click", function (e) {
e.preventDefault();
document.querySelectorAll(".columnsOption").forEach((item) => {
item.classList.remove("active");
});
this.classList.add("active");
// const dataColumn = parseInt(this.getAttribute("data-columns"));
// const defaultColumn = determineColumns();
// displayItems(currentCategory, dataColumn * defaultColumn);
displayItems(currentCategory);
});
});
window.addEventListener(
"resize",
debounce(() => {
// const defaultColumns = determineColumns();
// displayItems(currentCategory, defaultColumns);
displayItems(currentCategory);
}, 300)
);
} else if (path.includes("/article")) {
const queryParams = getQueryParams();
const itemId = queryParams.id;
if (itemId) {
fetch(`${url}/${itemId}`)
.then(response => response.json())
.then(item => {
document.getElementById("itemTitle").textContent = item.name;
document.getElementById("itemPrice").textContent = item.price + " \u20AC";
document.getElementById("itemImage").src = "images/" + item.image;
document.getElementById("itemImage").alt = item.name;
document.getElementById("itemDescription").textContent = item.description;
document.getElementById("itemCategory").textContent = item.category;
})
.catch(error => alert(error))
// } else {
// document.getElementById("itemDetails").innerHTML =
// "<p>Item not found.</p>";
// }
}
displayItemSize();
}