-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
289 lines (236 loc) · 8.49 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
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
279
280
281
282
283
284
285
286
287
288
289
const cartItems = {};
// Función para agregar una fruta al carro de la compra
function addToCart(fruitName, price, quantityInputId) {
const quantityInput = document.getElementById(quantityInputId);
const quantity = parseInt(quantityInput.value, 10);
if (quantity > 0) {
if (cartItems[fruitName]) {
cartItems[fruitName].quantity += quantity;
} else {
cartItems[fruitName] = { price, quantity };
}
const productElement = document.createElement('div');
productElement.className = 'addToCartAnimation';
productElement.textContent = `${quantity}x ${fruitName}`;
// Add the product element to the cart icon
const cartIcon = document.getElementById('cart-icon');
cartIcon.appendChild(productElement);
setTimeout(() => {
productElement.classList.add('cart-added');
setTimeout(() => {
cartIcon.removeChild(productElement);
}, 2000);
}, 0);
updateCartDisplay();
quantityInput.value = 0;
}
}
function removeFromCart(productName) {
if (cartItems[productName]) {
delete cartItems[productName];
updateCartDisplay();
}
}
// Función para actualizar la visualización del carrito
function updateCartDisplay() {
const cart = document.getElementById('cart');
cart.innerHTML = '';
const uniqueProducts = Object.keys(cartItems).length;
for (const itemName in cartItems) {
const item = cartItems[itemName];
const cartItem = document.createElement('div');
cartItem.className = 'cart-item';
const quantityInput = document.createElement('input');
quantityInput.type = 'number';
quantityInput.value = item.quantity;
quantityInput.min = '0';
quantityInput.addEventListener('change', (event) => {
const newQuantity = parseInt(event.target.value, 10);
if (newQuantity >= 0) {
item.quantity = newQuantity;
cartItem.querySelector('span').textContent = `${itemName} ${newQuantity} $${item.price.toFixed(2)}`;
updateCartDisplay();
}
});
cartItem.innerHTML = `<span>${itemName} ${item.price.toFixed(2)} €/Kg </span>`;
cartItem.querySelector('span').appendChild(quantityInput);
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', () => {
removeFromCart(itemName);
});
cartItem.appendChild(removeButton);
cart.appendChild(cartItem);
}
const total = calculateTotal();
updateTotal(total);
document.getElementById('cart-icon').querySelector('span').textContent = uniqueProducts;
}
// Función para calcular el total
function calculateTotal() {
let total = 0;
for (const itemName in cartItems) {
const item = cartItems[itemName];
total += item.price * item.quantity;
}
return total;
}
// Función para actualizar el total
function updateTotal(total) {
const totalElement = document.getElementById('totalPrice');
totalElement.textContent = total.toFixed(2);
}
// Función para abrir el carrito en un popup
function openCart() {
const cartPopup = document.getElementById('cartPopup');
cartPopup.style.display = 'block';
}
// Función para cerrar el carrito
function closeCart() {
const cartPopup = document.getElementById('cartPopup');
cartPopup.style.display = 'none';
}
// ...
// Function to open the contact popup
function openContactPopup() {
const contactPopup = document.getElementById('contactPopup');
contactPopup.style.display = 'block';
}
// Function to close the contact popup
function closeContactPopup() {
const contactPopup = document.getElementById('contactPopup');
contactPopup.style.display = 'none';
document.getElementById('contactForm').reset(); // Clear form
}
// Function to submit the contact form with validations
function submitForm(event) {
event.preventDefault(); // Prevent the default form submission
const title = document.getElementById('contactTitle').value;
const email = document.getElementById('contactEmail').value;
const text = document.getElementById('contactText').value;
// Add your validation logic here
if (!title || !email || !text) {
alert('Please fill in all fields.');
return;
}
if (!isValidEmail(email)) {
alert('Please enter a valid email address.');
return;
}
// Submit the form (you can add your code to send the form data here)
alert('Form submitted successfully!');
closeContactPopup();
}
// Function to validate an email address using a regular expression
function isValidEmail(email) {
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(email);
}
// Function to submit the contact form with validations
function submitForm(event) {
event.preventDefault(); // Prevent the default form submission
const title = document.getElementById('contactTitle').value;
const email = document.getElementById('contactEmail').value;
const text = document.getElementById('contactText').value;
// Reset error messages
document.getElementById('contactTitleError').style.display = 'none';
document.getElementById('contactEmailError').style.display = 'none';
document.getElementById('contactTextError').style.display = 'none';
// Add your validation logic here
let isValid = true;
if (!title) {
document.getElementById('contactTitleError').style.display = 'block';
isValid = false;
}
if (!isValidEmail(email)) {
document.getElementById('contactEmailError').style.display = 'block';
isValid = false;
}
if (!text) {
document.getElementById('contactTextError').style.display = 'block';
isValid = false;
}
if (isValid) {
// Submit the form (you can add your code to send the form data here)
alert('Form submitted successfully!');
closeContactPopup();
}
}
function searchProducts() {
const searchTerm = document.getElementById('product-search').value.toLowerCase();
fruits.forEach((fruit) => {
const fruitName = fruit.querySelector('h2').textContent.toLowerCase();
if (fruitName.includes(searchTerm) || searchTerm === '') {
fruit.style.display = 'block';
} else {
fruit.style.display = 'none';
}
});
// If the search term is empty, reset to the first page
if (searchTerm === '') {
navigateToPage(1);
}
// Update the pagination buttons
updateButtons();
}
document.getElementById('product-search').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
searchProducts();
}
});
// Pagination staff
let currentPage = 1;
const itemsPerPage = 12;
const fruits = document.querySelectorAll('.fruit');
function showPage(page) {
fruits.forEach((fruit, index) => {
if (index >= (page - 1) * itemsPerPage && index < page * itemsPerPage) {
fruit.style.display = 'block';
} else {
fruit.style.display = 'none';
}
});
}
// Updated navigateToPage function
function navigateToPage(page) {
if (page >= 1 && page <= 3) {
currentPage = page;
}
showPage(currentPage);
updateButtons();
}
// Initial page display
showPage(currentPage);
// Event listeners for page navigation
document.getElementById('prevPage').addEventListener('click', () => {
navigateToPage(currentPage-1);
});
document.getElementById('page1Button').addEventListener('click', () => {
navigateToPage(1);
});
document.getElementById('page2Button').addEventListener('click', () => {
navigateToPage(2);
});
document.getElementById('page3Button').addEventListener('click', () => {
navigateToPage(3);
});
document.getElementById('nextPage').addEventListener('click', () => {
navigateToPage(currentPage+1);
});
function updateButtons() {
document.getElementById('prevPage').disabled = currentPage === 1;
document.getElementById('page1Button').disabled = currentPage === 1;
document.getElementById('page2Button').disabled = currentPage === 2;
document.getElementById('page3Button').disabled = currentPage === 3;
document.getElementById('nextPage').disabled = currentPage === Math.ceil(fruits.length / itemsPerPage);
for (let i = 1; i <= 3; i++) {
const button = document.getElementById(`page${i}Button`);
if (i === currentPage) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
}
}
// Initial button states
updateButtons();