-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
225 lines (188 loc) · 9.36 KB
/
main.js
File metadata and controls
225 lines (188 loc) · 9.36 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
// Sets up +/- and Add to Cart button listeners.
// Called on DOMContentLoaded for static pages (productView),
// and again manually after renderBakedGoods injects dynamic cards.
function setupProductButtons(){
const productPurchaseActions = document.querySelectorAll(".product_purchase_actions");
productPurchaseActions.forEach((container) => {
// Skip if already initialized to avoid double-binding
if(container.dataset.initialized) return;
container.dataset.initialized = 'true';
const plusBtn = container.querySelector(".quantity_btn:nth-of-type(2)");
const minusBtn = container.querySelector(".quantity_btn:nth-of-type(1)");
const qtyInput = container.querySelector(".quantity_input");
const addBtn = container.querySelector(".add_to_cart_btn");
// Find the Product ID
let productId;
const link = container.closest('.bago_display')?.querySelector("a") ||
document.querySelector('.product-view-display');
if(link && link.href){
productId = new URLSearchParams(link.href.split('?')[1]).get('id');
}
else{
productId = new URLSearchParams(window.location.search).get('id');
}
if(plusBtn) plusBtn.onclick = () => qtyInput.value = parseInt(qtyInput.value) + 1;
if(minusBtn) minusBtn.onclick = () => {
if(parseInt(qtyInput.value) > 1) qtyInput.value = parseInt(qtyInput.value) - 1;
};
if(addBtn) addBtn.onclick = () => {
addToCart(productId, parseInt(qtyInput.value));
qtyInput.value = 1; // Reset after adding
};
});
}
document.addEventListener('DOMContentLoaded', () => {
setupProductButtons();
// Phone Masking Logic (For Cart Page)
const phoneInput = document.getElementById('phone-input');
if(phoneInput){
phoneInput.addEventListener('input', (e) => {
let input = e.target.value.replace(/\D/g, '');
let size = input.length;
if(size === 0) e.target.value = "";
else if(size <= 3) e.target.value = "(" + input;
else if(size <= 6) e.target.value = "(" + input.substring(0, 3) + ") " + input.substring(3);
else e.target.value = "(" + input.substring(0, 3) + ") " + input.substring(3, 6) + "-" + input.substring(6, 10);
});
}
});
document.addEventListener('DOMContentLoaded', () => {
// Setup the Observer options
const observerOptions = {
threshold: 0.15,
rootMargin: "0px 0px -50px 0px" // Trigger slightly before it hits the bottom
};
// Define what happens when a review is seen
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if(entry.isIntersecting){
entry.target.classList.add('show-review');
// Stop observing after it animates once
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Tell the observer to watch all your review cards
const reviews = document.querySelectorAll('.review');
reviews.forEach((el) => observer.observe(el));
});
// -------------------------------------- Function for the endless cycle in Home Page ----------------------------------------------------------
function renderNewProducts(){
const scrollContainer = document.querySelector('.bago_card_box');
if(!scrollContainer) return;
const newProducts = Object.entries(productData).filter(([id, product]) => {
return product.new === true;
});
let htmlContent = "";
newProducts.forEach(([id, product]) => {
htmlContent += `
<li class="bago_card">
<h3 class="bago_card_title">${product.title}</h3>
<a href="pages/productView.html?id=${id}" title="Go to Product View">
<img class="bago_card_image" src="${product.mainImg.replace('../', '')}" alt="Image of ${product.title}">
</a>
<p class="bago_card_description">${product.info}</p>
</li>
`;
});
scrollContainer.innerHTML = htmlContent;
if (newProducts.length > 0) {
scrollContainer.innerHTML += scrollContainer.innerHTML;
}
}
// Run the function when the page loads
document.addEventListener('DOMContentLoaded', renderNewProducts);
// ------------------------------------------ Baked Goods Page Renderer -------------------------------------------------------------
// Reads productData and builds all sections in bakedGoods.html automatically.
// To add/remove a product, just update products.js — no HTML changes needed.
function renderBakedGoods(){
const container = document.getElementById('all-goods_container');
if(!container) return; // Only runs on bakedGoods.html
// --- Section definitions: order on the page + display titles ---
const sections = [
{category: '_holiday-specials', title: 'Holiday Specials', ongoingSubtitle: null},
{category: '_sourdough', title: 'Sourdough Breads', ongoingSubtitle: null},
{category: '_other-treats', title: 'Our Other Treats', ongoingSubtitle: null},
{category: '_cookie', title: 'Cookies', ongoingSubtitle: "Cookies of the Month"},
];
let pageHTML = '';
sections.forEach((section) => {
// Filter products that belong to this section
const sectionProducts = Object.entries(productData).filter(([id]) =>
id.endsWith(section.category)
);
if(sectionProducts.length === 0) return; // Skip empty sections
// Separate ongoing (purchasable) from seasoned (display-only)
const ongoing = sectionProducts.filter(([, p]) => p.ongoing);
const seasoned = sectionProducts.filter(([, p]) => !p.ongoing);
// For holiday specials, only show the section if there are active items
if(section.category === '_holiday-specials' && ongoing.length === 0) return;
// Helper: build a single <li> card
function buildCard(id, product){
const isOngoing = product.ongoing;
const multipleChoices = !!(product.sizes || product.toppings); // Checks to see if the product has either of the variables
let priceHTML = '';
if(isOngoing){
if(product.sizes){
priceHTML = `<p class="bago_display_price">Starting at $${product.sizes[0].price.toFixed(2)}</p>`;
}
else if(product.toppings){
priceHTML = `<p class="bago_display_price">Starting at $${product.toppings[0].price.toFixed(2)}</p>`;
}
else{
priceHTML = `<p class="bago_display_price">${product.price}</p>`;
}
}
const cartHTML = (isOngoing && !multipleChoices) ? `
<div class="product_purchase_actions">
<div class="quantity_selector">
<button type="button" class="quantity_btn">−</button>
<input type="number" class="quantity_input" value="1" min="1">
<button type="button" class="quantity_btn">+</button>
</div>
<button type="submit" class="add_to_cart_btn">Add to Cart</button>
</div>` : '';
return `
<li class="bago_display">
<a href="productView.html?id=${id}" title="Go to Product View">
<img class="bago_display_image" src="${product.mainImg}" alt="Image of ${product.title}">
</a>
<div class="bago_display_info">
<h3 class="bago_display_title">${product.title}</h3>
${priceHTML}
${cartHTML}
</div>
</li>`;
}
// Subtitle above the ongoing products
const ongoingSubtitleHTML = section.ongoingSubtitle
? `<h4 class="container_sub_title">${section.ongoingSubtitle}</h4>`
: '';
const ongoingCardsHTML = ongoing.map(([id, p]) => buildCard(id, p)).join('');
// Past flavors block
let seasonedHTML = '';
if(seasoned.length > 0 && !(section.category === '_holiday-specials')){
const seasonedCards = seasoned.map(([id, p]) => buildCard(id, p)).join('');
seasonedHTML = `
<div class="section_divider"><hr></div>
<h4 class="container_sub_title cookies_title_pf">Past Flavors</h4>
<ul class="card_content">${seasonedCards}</ul>`;
}
// Divider between sections — only if something has already been rendered
const divider = pageHTML.length > 0
? `<div class="section_divider bread-cookies_divider"><hr></div>`
: '';
pageHTML += `
${divider}
<div class="bago_display_deck_container" id="${section.category.replace('_', '')}">
<h3 class="container_title">${section.title}</h3>
${ongoingSubtitleHTML}
<ul class="card_content">${ongoingCardsHTML}</ul>
${seasonedHTML}
</div>`;
});
container.innerHTML = pageHTML;
// Attach button listeners now that the cards are in the DOM
setupProductButtons();
}
document.addEventListener('DOMContentLoaded', renderBakedGoods);