Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (199 loc) · 9.06 KB
/
Copy pathscript.js
File metadata and controls
237 lines (199 loc) · 9.06 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
227
228
229
230
231
232
233
234
235
236
237
/**
* The Village Restaurant & Lounge — script.js
* Pure vanilla JavaScript. No frameworks. No build step.
*
* Features covered:
* 1. Sticky nav shadow on scroll
* 2. Smooth-scroll for all anchor links
* 3. Mobile hamburger menu toggle
* 4. Close mobile menu when a link is clicked
* 5. Populate guest-count dropdown (1–20)
* 6. Booking form → WhatsApp pre-filled message
* 7. Set current year in footer
* 8. Intersection Observer for fade-in animations
*/
/* ── Run after the DOM is ready ─────────────────────────── */
document.addEventListener('DOMContentLoaded', () => {
initNavScroll();
initMobileMenu();
initSmoothScroll();
populateGuestSelect();
initBookingForm();
setFooterYear();
initFadeIn();
});
/* ────────────────────────────────────────────────────────
1. Sticky nav: add shadow class when user scrolls down
──────────────────────────────────────────────────────── */
function initNavScroll() {
const navbar = document.getElementById('navbar');
if (!navbar) return;
const onScroll = () => {
navbar.classList.toggle('scrolled', window.scrollY > 20);
};
window.addEventListener('scroll', onScroll, { passive: true });
}
/* ────────────────────────────────────────────────────────
2. Mobile hamburger menu
──────────────────────────────────────────────────────── */
function initMobileMenu() {
const toggle = document.getElementById('nav-toggle');
const navLinks = document.getElementById('nav-links');
if (!toggle || !navLinks) return;
toggle.addEventListener('click', () => {
const isOpen = navLinks.classList.toggle('mobile-open');
toggle.classList.toggle('is-open', isOpen);
toggle.setAttribute('aria-expanded', String(isOpen));
});
}
/* ────────────────────────────────────────────────────────
3. Smooth scroll + close mobile menu on link click
──────────────────────────────────────────────────────── */
function initSmoothScroll() {
const NAV_HEIGHT = parseInt(
getComputedStyle(document.documentElement).getPropertyValue('--nav-height') || '64',
10
);
document.querySelectorAll('a[href^="#"]').forEach(link => {
link.addEventListener('click', e => {
const targetId = link.getAttribute('href');
if (targetId === '#') return;
const target = document.querySelector(targetId);
if (!target) return;
e.preventDefault();
// Close mobile menu if open
const navLinks = document.getElementById('nav-links');
const toggle = document.getElementById('nav-toggle');
if (navLinks && navLinks.classList.contains('mobile-open')) {
navLinks.classList.remove('mobile-open');
toggle && toggle.classList.remove('is-open');
toggle && toggle.setAttribute('aria-expanded', 'false');
}
// Scroll with offset for fixed navbar
const targetTop = target.getBoundingClientRect().top + window.scrollY - NAV_HEIGHT;
window.scrollTo({ top: targetTop, behavior: 'smooth' });
});
});
}
/* ────────────────────────────────────────────────────────
4. Populate guest count select (1–20)
──────────────────────────────────────────────────────── */
function populateGuestSelect() {
const select = document.getElementById('guests');
if (!select) return;
for (let i = 1; i <= 20; i++) {
const option = document.createElement('option');
option.value = String(i);
option.textContent = i === 1 ? '1 Guest' : `${i} Guests`;
if (i === 2) option.selected = true; // default
select.appendChild(option);
}
}
/* ────────────────────────────────────────────────────────
5. Booking form → WhatsApp
──────────────────────────────────────────────────────── */
function initBookingForm() {
const form = document.getElementById('reservation-form');
if (!form) return;
// Set minimum date to today
const dateInput = document.getElementById('date');
if (dateInput) {
const today = new Date().toISOString().split('T')[0];
dateInput.setAttribute('min', today);
}
form.addEventListener('submit', e => {
e.preventDefault();
// Collect values
const name = getValue('name');
const email = getValue('email');
const phone = getValue('phone');
const date = getValue('date');
const time = getValue('time');
const guests = getValue('guests');
const message = getValue('message');
// Format a friendly WhatsApp message
const whatsappMessage =
`Hello! I'd like to make a reservation at The Village Restaurant & Lounge:\n\n` +
`👤 Name: ${name}\n` +
`📧 Email: ${email}\n` +
`📞 Phone: ${phone}\n` +
`📅 Date: ${formatDate(date)}\n` +
`🕐 Time: ${formatTime(time)}\n` +
`👥 Guests: ${guests}\n` +
(message ? `📝 Special Requests: ${message}\n` : '') +
`\nThank you!`;
const phoneNumber = '2349151820000'; // international format for WhatsApp
const url = `https://wa.me/${phoneNumber}?text=${encodeURIComponent(whatsappMessage)}`;
window.open(url, '_blank', 'noopener,noreferrer');
});
}
/** Get the trimmed value of a form field by id */
function getValue(id) {
const el = document.getElementById(id);
return el ? el.value.trim() : '';
}
/** Format YYYY-MM-DD to a more human-friendly string */
function formatDate(dateStr) {
if (!dateStr) return '';
const [year, month, day] = dateStr.split('-');
const d = new Date(Number(year), Number(month) - 1, Number(day));
return d.toLocaleDateString('en-NG', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
}
/** Format HH:MM to 12-hour string */
function formatTime(timeStr) {
if (!timeStr) return '';
const [h, m] = timeStr.split(':').map(Number);
const period = h >= 12 ? 'PM' : 'AM';
const hour12 = h % 12 || 12;
return `${hour12}:${String(m).padStart(2, '0')} ${period}`;
}
/* ────────────────────────────────────────────────────────
6. Footer: current year
──────────────────────────────────────────────────────── */
function setFooterYear() {
const el = document.getElementById('footer-year');
if (el) el.textContent = new Date().getFullYear();
}
/* ────────────────────────────────────────────────────────
7. Intersection Observer – fade-in on scroll
──────────────────────────────────────────────────────── */
function initFadeIn() {
// Add the CSS for fade-in dynamically so it degrades gracefully
const style = document.createElement('style');
style.textContent = `
.fade-target {
opacity: 0;
transform: translateY(24px);
transition: opacity 0.55s ease, transform 0.55s ease;
}
.fade-target.visible {
opacity: 1;
transform: translateY(0);
}
`;
document.head.appendChild(style);
// Select all cards and section content blocks as fade targets
const targets = document.querySelectorAll(
'.feature-card, .offering-card, .highlight-card, .menu-card, ' +
'.gallery-item, .story-box, .payment-box, .booking-form-box, ' +
'.info-box, .walkin-box, .address-box, .parking-box, .tile'
);
targets.forEach(el => el.classList.add('fade-target'));
if (!('IntersectionObserver' in window)) {
// Fallback: just show everything immediately
targets.forEach(el => el.classList.add('visible'));
return;
}
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target); // fire once only
}
});
}, {
threshold: 0.1,
rootMargin: '0px 0px -40px 0px'
});
targets.forEach(el => observer.observe(el));
}