-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
226 lines (199 loc) · 8.6 KB
/
scripts.js
File metadata and controls
226 lines (199 loc) · 8.6 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
const okcMainCardId = 'okc-main-card'
const okcMenuCardId = 'okc-menu-card'
import {
mostPopularCoffeesList,
renderThemeButton,
updateTheme,
applyTheme,
mostPopularDessertList,
} from './okc-menu.objects.js';
// Importar el gestor de favoritos
import {
setupFavoriteButton,
updateFavoritesCounter,
initializeFavoriteButtons,
toggleFavorite,
isFavorite
} from './favorites-manager.js';
const headerNav = document.querySelector('.header-nav')
const asideNavContainer = document.getElementById('aside-nav-container');
const mainContainer = document.querySelector('.main-container')
const MostPopularCoffesSectionContainer = document.getElementById('products-container');
const dessertContainer = document.getElementById('dessert-container');
const asideMenuContainer = document.querySelector('.aside-menu-container')
const aside = document.getElementById('aside-menu');
const asideButtonMenu = document.getElementById('aside-menu-button');
const productsButton = document.getElementById('products-button');
const viewMoreButton = document.getElementById('view-more-button');
const viewMoreButtonDessert = document.getElementById('view-more-button-dessert');
const viewProductsButtonDessert = document.getElementById('view-products-dessert');
const cardCloser = document.getElementById('card-closer')
const cardShadow = document.getElementById('okc-main-card')
const viewProductsButton = document.getElementById('view-products-button');
const headerMenuLabel = document.getElementById('checkbox-menu-label');
const asideMainMenu = document.getElementById('aside-nav-menu');
const asideMainMenuTittle = document.getElementById('aside-secction-h2-menu-tittle');
const asideNavChildNodes = document.querySelectorAll('.aside-nav-item')
const note = document.querySelector('.note')
let openMostPopularCoffesSection = false;
let openDessertSection = false;
renderThemeButton(headerNav)
renderThemeButton(asideMainMenu, true)
const themeButton = document.querySelector('.theme-button')
const asideThemeButton = document.querySelector('.aside-theme-button')
asideThemeButton.style.color = '#fcd596'
applyTheme(mainContainer)
// Función modificada para incluir funcionalidad de favoritos
function createProducts(productsList, containerId, productType = 'coffee', productCategory = 'hot') {
const container = document.getElementById(containerId);
container.innerHTML = '';
productsList.forEach((product, index) => {
const productContainer = document.createElement('div');
const productImgContainer = document.createElement('div');
const productTextContainer = document.createElement('div');
const productFilter = document.createElement('div');
const productH3 = document.createElement('h3');
const productH4 = document.createElement('h4');
const productHSpan = document.createElement('span');
let productFavButton = document.createElement('button');
productFavButton.className = 'material-symbols-outlined product-fav-btn';
productFavButton.innerText = 'favorite';
productFavButton.title = 'añadir a favoritos';
productH3.textContent = product.name;
productH4.textContent = 'precio: ';
productHSpan.textContent = `$${product.price}`;
productImgContainer.style.backgroundImage = `url(${product.source})`;
productH4.appendChild(productHSpan);
productFilter.appendChild(productFavButton)
productTextContainer.append(productH3, productH4);
productImgContainer.appendChild(productFilter);
productContainer.appendChild(productImgContainer);
productContainer.appendChild(productTextContainer);
container.appendChild(productContainer);
productFilter.className = 'product-filter';
productImgContainer.className = 'product-img-container';
productContainer.tabIndex = 0
productContainer.ariaLabel = product.name
productContainer.className = 'product-container';
if (containerId.includes('dessert')) {
productContainer.classList.add('dessert-product-container')
}
productTextContainer.className = 'product-text-container';
productContainer.addEventListener('click', event=> {
if (event.target.tagName !== 'SPAN') {
if (window.location.href.includes('okc-menu')) {
product.renderCardItem(okcMenuCardId)
return
}
product.renderCardItem(okcMainCardId)
}
});
productContainer.addEventListener('keydown', event=> {
if (event.key === 'Enter') {
productContainer.click()
}
})
// Configurar botón de favoritos
setupFavoriteButton(productFavButton, product, productType, productCategory);
setTimeout(() => {
productContainer.style.animationDelay = `${(index % 9) * 0.1}s`;
}, 10);
});
}
function openMostPopularCoffeesSection() {
if (!openMostPopularCoffesSection) {
createProducts(mostPopularCoffeesList.slice(0, 9), 'products-container', 'coffee', 'hot');
viewMoreButton.textContent = 'ver menos';
openMostPopularCoffesSection = true;
return;
} else {
createProducts(mostPopularCoffeesList.slice(0, 3), 'products-container', 'coffee', 'hot');
viewMoreButton.textContent = 'ver más';
openMostPopularCoffesSection = false;
return;
}
}
function openAside(showMenuNav= false) {
if (aside.classList.contains('visible')) {
aside.classList.remove('visible');
} else {
aside.classList.add('visible');
asideButtonMenu.focus() //accesibilidad web
}
(showMenuNav) ? asideMainMenu.style.display = 'flex' : asideMainMenu.style.display = 'none';
}
// Crear productos con funcionalidad de favoritos
createProducts(mostPopularCoffeesList.slice(0,3), 'products-container', 'coffee', 'hot');
createProducts(mostPopularDessertList.slice(0,3), 'dessert-container', 'dessert', 'dessert');
// Actualizar contador de favoritos
updateFavoritesCounter();
// Event Listeners
productsButton.addEventListener('click', ()=> {
openAside()
});
headerMenuLabel.addEventListener('click', ()=> {
openAside(true)
});
asideButtonMenu.addEventListener('click', ()=> {
openAside()
});
// accesibilidad web
asideButtonMenu.addEventListener('focus', ()=> {
aside.classList.add('visible');
asideButtonMenu.focus()
});
viewProductsButton.addEventListener('click', () => {
openAside();
});
viewProductsButtonDessert.addEventListener('click', event=> {
openAside()
});
viewMoreButton.addEventListener('click', event=> {
openMostPopularCoffeesSection()
});
viewMoreButtonDessert.addEventListener('click', event=> {
if (!openDessertSection) {
createProducts(mostPopularDessertList.slice(0, 9), 'dessert-container', 'dessert', 'dessert');
viewMoreButtonDessert.textContent = 'Ver menos';
openDessertSection = true;
return;
} else {
createProducts(mostPopularDessertList.slice(0, 3), 'dessert-container', 'dessert', 'dessert');
viewMoreButtonDessert.textContent = 'Ver más';
openDessertSection = false;
return;
}
});
themeButton.addEventListener('click', ()=> {
updateTheme(themeButton, mainContainer)
})
asideThemeButton.addEventListener('click', ()=> {
updateTheme(asideThemeButton, mainContainer)
})
// cierra la ventana de carta con el botón
cardCloser.addEventListener('click', () => {
cardShadow.style.display = 'none'
})
// cierra la ventana de carta al clickear en cualquier sitio que no sea la propia carta
cardShadow.addEventListener('click', event=> {
if (event.target.id === cardShadow.id || event.target.id === cardShadow.firstElementChild.id) {
cardShadow.style.display = 'none'
}
})
note.addEventListener('click', ()=> {
note.style.backgroundColor = '#9711118f' //sutíl interaccion con la nota
})
/* evento para la tecla de escape para accesibilidad web*/
document.addEventListener('keydown', event=> {
if(event.key === 'Escape') {
if (cardShadow.style.display === 'flex') {
cardShadow.style.display = 'none'
return
}
openAside()
}
})
// Inicializar botones de favoritos cuando la página se carga completamente
document.addEventListener('DOMContentLoaded', () => {
initializeFavoriteButtons();
});