forked from Ameerah-2005/final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDGS.js
More file actions
166 lines (145 loc) · 5.48 KB
/
DGS.js
File metadata and controls
166 lines (145 loc) · 5.48 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
// Cart implementation storing full items in localStorage
const CART_ITEMS_KEY = 'dg_cart_items';
function getCartItems(){
try{
const s = localStorage.getItem(CART_ITEMS_KEY);
return s ? JSON.parse(s) : [];
} catch(e){ return []; }
}
function setCartItems(items){
localStorage.setItem(CART_ITEMS_KEY, JSON.stringify(items));
// update cart count badge
const total = items.reduce((acc,i)=> acc + (i.quantity||0), 0);
const el = document.getElementById('cart-count');
if(el) el.textContent = total;
}
function getCartCount(){
return getCartItems().reduce((acc,i)=> acc + (i.quantity||0), 0);
}
function initCart(){
// render count on page load
const items = getCartItems();
setCartItems(items);
}
function bookNow(){
alert('Booking Successful!');
}
document.addEventListener('DOMContentLoaded', function(){
initCart();
// attach handlers to product cards' Add to Cart buttons
document.querySelectorAll('.product-card').forEach(card=>{
const btn = card.querySelector('button');
if(btn && !btn.classList.contains('js-bound')){
btn.classList.add('js-bound');
btn.addEventListener('click', ()=>{
addItemFromCard(card);
});
}
});
// Booking page handler: show confirmation and reset form
const bookBtn = document.getElementById('bookNowBtn');
if(bookBtn){
bookBtn.addEventListener('click', ()=>{
const form = document.getElementById('booking-form');
// simple validation: require at least chest measurement or instructions
const chest = form.querySelector('input[name="chest"]')?.value || '';
const instructions = form.querySelector('textarea[name="instructions"]')?.value || '';
if(!chest && !instructions){
showToast('Please provide measurements or instructions');
return;
}
showToast('Booking submitted — we will contact you shortly');
form.reset();
});
}
// initialize any sliders on the page
if(typeof initSliders === 'function') initSliders();
});
// Slider implementation: auto-advance and pause-on-hover
const SLIDE_INTERVAL = 3000;
function initSliders(){
document.querySelectorAll('.slider').forEach(slider=>{
const slides = slider.querySelector('.slides');
if(!slides) return;
const imgs = slides.querySelectorAll('img');
if(imgs.length < 2) return; // nothing to slide
// styling helpers
slides.style.display = 'flex';
slides.style.willChange = 'transform';
slides.style.transition = 'transform 600ms ease';
slider.style.overflow = 'hidden';
slider.style.position = 'relative';
// state
slider._idx = 0;
let timer = null;
function setSizes(){
const w = slider.clientWidth;
imgs.forEach(img=>{
img.style.width = w + 'px';
img.style.height = '100%';
img.style.objectFit = 'cover';
img.style.flex = '0 0 auto';
});
slides.style.width = (imgs.length * slider.clientWidth) + 'px';
slides.style.transform = `translateX(-${slider._idx * slider.clientWidth}px)`;
}
function start(){
stop();
timer = setInterval(()=>{
slider._idx = (slider._idx + 1) % imgs.length;
slides.style.transform = `translateX(-${slider._idx * slider.clientWidth}px)`;
}, SLIDE_INTERVAL);
}
function stop(){ if(timer) { clearInterval(timer); timer = null; } }
// pause on hover
slider.addEventListener('mouseenter', stop);
slider.addEventListener('mouseleave', start);
// responsive
window.addEventListener('resize', ()=>{
// small debounce
clearTimeout(slider._resizeTO);
slider._resizeTO = setTimeout(setSizes, 120);
});
// initialize sizes and start
setSizes();
start();
});
}
// Simple toast helper for user feedback
function showToast(message, duration = 1800){
const t = document.getElementById('toast');
if(!t) return;
t.textContent = message;
t.classList.add('show');
t.setAttribute('aria-hidden', 'false');
clearTimeout(t._hideTimeout);
t._hideTimeout = setTimeout(()=>{
t.classList.remove('show');
t.setAttribute('aria-hidden', 'true');
}, duration);
}
// Add an item object from a product card element
function addItemFromCard(card){
if(!card) return;
const name = card.querySelector('h3')?.textContent?.trim() || 'Item';
const priceText = card.querySelector('h4')?.textContent || '';
const price = parseFloat(priceText.replace(/[^0-9.]/g,'')) || 0;
const img = card.querySelector('img')?.getAttribute('src') || '';
const size = card.querySelector('select')?.value || '';
const qtyInput = card.querySelector('input[type="number"]');
const qty = qtyInput ? Math.max(1, parseInt(qtyInput.value)||1) : 1;
const id = name + '|' + size + '|' + price;
const items = getCartItems();
const existing = items.find(i=> i.id === id);
if(existing){
existing.quantity = (existing.quantity||0) + qty;
} else {
items.push({ id, name, price, quantity: qty, size, img });
}
setCartItems(items);
showToast('Added to cart');
}
// Utility to clear the cart (used by cart page)
function clearCart(){
setCartItems([]);
}