-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
568 lines (491 loc) · 24.4 KB
/
script.js
File metadata and controls
568 lines (491 loc) · 24.4 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
document.addEventListener("DOMContentLoaded", () => {
// =========================================================================
// 1. Scroll Reveal — Fade-in elements with .reveal class
// =========================================================================
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.15
};
const revealObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('active');
observer.unobserve(entry.target);
}
});
}, observerOptions);
document.querySelectorAll('.reveal').forEach(el => revealObserver.observe(el));
// =========================================================================
// 2. Navbar & Navigation Logic
// =========================================================================
const navbar = document.getElementById('navbar');
if (navbar) {
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
}
// =========================================================================
// 3. Parallax Effect (Hero Section)
// =========================================================================
const parallaxContainer = document.getElementById('parallax-container');
const parallaxWrappers = document.querySelectorAll('.parallax-wrapper');
if (parallaxContainer && window.matchMedia("(min-width: 968px)").matches) {
parallaxContainer.addEventListener('mousemove', (e) => {
const x = e.clientX - window.innerWidth / 2;
const y = e.clientY - window.innerHeight / 2;
parallaxWrappers.forEach(wrapper => {
const speed = parseFloat(wrapper.getAttribute('data-speed'));
const xOffset = x * speed;
const yOffset = y * speed;
wrapper.style.transform = `translate(${xOffset}px, ${yOffset}px)`;
});
});
parallaxContainer.addEventListener('mouseleave', () => {
parallaxWrappers.forEach(wrapper => {
wrapper.style.transform = `translate(0px, 0px)`;
});
});
}
// =========================================================================
// 4. Particle Background Engine
// =========================================================================
const canvas = document.getElementById('particleCanvas');
if (canvas) {
const ctx = canvas.getContext('2d');
let particlesArray = [];
let mouse = { x: null, y: null, radius: 150 };
window.addEventListener('mousemove', (e) => {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
window.addEventListener('mouseleave', () => {
mouse.x = undefined;
mouse.y = undefined;
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initParticles();
});
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 2 + 0.5;
this.baseX = this.x;
this.baseY = this.y;
this.density = (Math.random() * 20) + 1;
this.color = Math.random() > 0.5 ? 'rgba(212, 168, 67, 0.4)' : 'rgba(45, 106, 79, 0.4)';
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
update() {
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
let forceDirectionX = dx / distance;
let forceDirectionY = dy / distance;
let maxDistance = mouse.radius;
let force = (maxDistance - distance) / maxDistance;
let directionX = forceDirectionX * force * this.density;
let directionY = forceDirectionY * force * this.density;
if (distance < maxDistance && mouse.x != null) {
this.x += directionX;
this.y += directionY;
} else {
if (this.x !== this.baseX) {
let dx = this.x - this.baseX;
this.x -= dx / 25;
}
if (this.y !== this.baseY) {
let dy = this.y - this.baseY;
this.y -= dy / 25;
}
}
this.draw();
}
}
function initParticles() {
particlesArray = [];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let numberOfParticles = (canvas.width * canvas.height) / 8000;
for (let i = 0; i < numberOfParticles; i++) {
particlesArray.push(new Particle());
}
}
function animateParticles() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particlesArray.length; i++) {
particlesArray[i].update();
}
requestAnimationFrame(animateParticles);
}
initParticles();
animateParticles();
}
// =========================================================================
// 5. Radial Orbital Timeline Engine
// =========================================================================
const orbitSystem = document.getElementById('orbit-system');
const ringsContainer = document.getElementById('orbit-rings');
if (orbitSystem && ringsContainer) {
const rawDbEvents = JSON.parse(orbitSystem.getAttribute('data-events') || '[]');
const orbitCards = [];
let eventsData = [
{ id: 1, name: "CpE 3rd Year 2nd Semester", date: "JUN 01", time: "11:59 PM", status: "Ongoing", statusDot: "#2D6A4F", colorClass: "glow-green" },
{ id: 2, name: "Proposal Hearing", date: "MAY 08", time: "10:00 AM", status: "Completed", statusDot: "#E53E3E", colorClass: "glow-red" },
{ id: 3, name: "John Enico's Birthday", date: "AUG 28", time: "8:28 PM", status: "Upcoming", statusDot: "#3182CE", colorClass: "glow-blue" }
];
if (rawDbEvents && rawDbEvents.length > 0) {
eventsData = rawDbEvents.map((ev, index) => {
const dateObj = new Date(ev.event_date);
const timeParts = ev.event_time.split(':');
let hours = parseInt(timeParts[0]);
const ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;
const formattedTime = `${hours}:${timeParts[1]} ${ampm}`;
const monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
const formattedDate = `${monthNames[dateObj.getMonth()]} ${dateObj.getDate().toString().padStart(2, '0')}`;
let statusColorClass = 'glow-blue', statusDotColor = '#3182CE';
if (ev.status === 'Ongoing') { statusColorClass = 'glow-green'; statusDotColor = '#2D6A4F'; }
else if (ev.status === 'Completed') { statusColorClass = 'glow-red'; statusDotColor = '#E53E3E'; }
return {
id: index + 1,
name: ev.event_name,
date: formattedDate,
time: formattedTime,
status: ev.status,
statusDot: statusDotColor,
colorClass: statusColorClass
};
});
}
const isMobile = window.innerWidth < 768;
const displayEvents = isMobile ? eventsData.slice(0, 3) : eventsData;
displayEvents.forEach((ev, index) => {
ev.radiusX = (isMobile ? 120 : 180) + (index * (isMobile ? 40 : 65));
ev.radiusY = ev.radiusX * 0.55;
ev.speed = 0.0015 - (index * 0.00015);
ev.angle = index * (Math.PI * 2 / displayEvents.length);
const ring = document.createElement('div');
ring.className = `orbit-ring ${ev.colorClass}`;
ring.style.width = `${ev.radiusX * 2}px`;
ring.style.height = `${ev.radiusX * 2}px`;
ringsContainer.appendChild(ring);
const el = document.createElement('div');
el.className = `orbit-card ${ev.colorClass}`;
el.innerHTML = `
<div class="timeline-connector"></div>
<div class="card-datetime">${ev.date} • ${ev.time}</div>
<div class="card-title">${ev.name}</div>
<div class="card-footer">
<div class="avatar-group"><div class="avatar-circle" style="background: ${ev.statusDot}"></div></div>
<div class="avatar-count">${ev.status}</div>
</div>
`;
orbitSystem.appendChild(el);
orbitCards.push({ el, data: ev });
});
function animateOrbit() {
orbitCards.forEach(card => {
const ev = card.data;
ev.angle += ev.speed;
const x = Math.cos(ev.angle) * ev.radiusX;
const y = Math.sin(ev.angle) * ev.radiusY;
const depth = Math.sin(ev.angle);
const scale = 0.85 + (depth * 0.15);
const opacity = 0.5 + (depth + 1) * 0.25;
let zIndex = Math.floor(depth * 10) + 20;
if (x < -100 && !isMobile) zIndex = 15;
card.el.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;
card.el.style.zIndex = zIndex;
card.el.style.opacity = opacity;
card.el.style.filter = depth < -0.5 ? `blur(${Math.abs(depth)*1.5}px)` : 'none';
});
requestAnimationFrame(animateOrbit);
}
animateOrbit();
}
// =========================================================================
// 6. Real-time Search Filtering (Dashboard Table)
// =========================================================================
const searchInput = document.querySelector('.search-input');
const eventsTable = document.querySelector('.events-table');
if (searchInput && eventsTable) {
const tbody = eventsTable.querySelector('tbody');
const rows = tbody ? Array.from(tbody.querySelectorAll('tr')) : [];
let debounceTimer;
searchInput.addEventListener('input', (e) => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
const query = e.target.value.toLowerCase().trim();
let visibleCount = 0;
rows.forEach(row => {
if (row.querySelector('.empty-row')) return;
const eventName = row.querySelector('.event-name-cell');
const locationCell = row.cells[3];
const categoryCell = row.cells[4];
const nameText = eventName ? eventName.textContent.toLowerCase() : '';
const locationText = locationCell ? locationCell.textContent.toLowerCase() : '';
const categoryText = categoryCell ? categoryCell.textContent.toLowerCase() : '';
const matches = nameText.includes(query) || locationText.includes(query) || categoryText.includes(query);
row.style.display = matches ? '' : 'none';
if (matches) visibleCount++;
});
let noResultsRow = tbody.querySelector('.js-no-results');
if (visibleCount === 0 && query.length > 0) {
if (!noResultsRow) {
noResultsRow = document.createElement('tr');
noResultsRow.className = 'js-no-results';
noResultsRow.innerHTML = '<td colspan="7" class="empty-row">No events match your search.</td>';
tbody.appendChild(noResultsRow);
}
noResultsRow.style.display = '';
} else if (noResultsRow) {
noResultsRow.style.display = 'none';
}
}, 200);
});
searchInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') e.preventDefault();
});
}
// =========================================================================
// 7. Delete Confirmation Modal (Styled)
// =========================================================================
if (!document.getElementById('deleteModal')) {
const modalOverlay = document.createElement('div');
modalOverlay.className = 'modal-overlay';
modalOverlay.id = 'deleteModal';
modalOverlay.innerHTML = `
<div class="modal-card">
<div class="modal-icon">⚠️</div>
<h2 class="modal-title">Delete Event?</h2>
<p class="modal-message">Are you sure you want to delete this event? This action cannot be undone.</p>
<div class="modal-actions">
<button class="btn btn-secondary modal-cancel" id="modalCancel">Cancel</button>
<button class="btn btn-primary btn-danger modal-confirm" id="modalConfirm">Delete</button>
</div>
</div>
`;
document.body.appendChild(modalOverlay);
let pendingDeleteId = null;
const modalCancelBtn = document.getElementById('modalCancel');
const modalConfirmBtn = document.getElementById('modalConfirm');
const closeModal = () => {
modalOverlay.classList.remove('modal-active');
pendingDeleteId = null;
};
const openDeleteModal = (id) => {
pendingDeleteId = id;
modalOverlay.classList.add('modal-active');
};
if (modalConfirmBtn) {
modalConfirmBtn.addEventListener('click', () => {
if (pendingDeleteId !== null) window.location.href = `delete.php?id=${pendingDeleteId}`;
});
}
if (modalCancelBtn) modalCancelBtn.addEventListener('click', closeModal);
modalOverlay.addEventListener('click', (e) => { if (e.target === modalOverlay) closeModal(); });
document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && modalOverlay.classList.contains('modal-active')) closeModal(); });
window.confirmDelete = openDeleteModal;
}
// =========================================================================
// 8. Form Validation (Create & Edit Pages)
// =========================================================================
const eventForm = document.querySelector('form[action*="create.php"], form[action*="update.php"]');
if (eventForm) {
const fields = {
event_name: { label: 'Event Name', minLength: 3 },
organizer: { label: 'Organizer', minLength: 2 },
event_date: { label: 'Event Date', type: 'date' },
event_time: { label: 'Event Time', type: 'time' },
location_select: { label: 'Location', minLength: 1 }
};
const showFieldError = (input, message) => {
clearFieldError(input);
input.classList.add('form-control-error');
const errorEl = document.createElement('span');
errorEl.className = 'field-error';
errorEl.textContent = message;
input.parentNode.appendChild(errorEl);
};
const clearFieldError = (input) => {
input.classList.remove('form-control-error');
const existing = input.parentNode.querySelector('.field-error');
if (existing) existing.remove();
};
const validateField = (name, input) => {
const rules = fields[name];
if (!rules) return true;
const value = input.value.trim();
if (!value) { showFieldError(input, `${rules.label} is required.`); return false; }
if (rules.minLength && value.length < rules.minLength) { showFieldError(input, `${rules.label} must be at least ${rules.minLength} characters.`); return false; }
if (rules.type === 'date') { const dateVal = new Date(value); if (isNaN(dateVal.getTime())) { showFieldError(input, 'Please enter a valid date.'); return false; } }
clearFieldError(input);
return true;
};
Object.keys(fields).forEach(name => {
const input = eventForm.querySelector(`[name="${name}"]`);
if (input) {
input.addEventListener('blur', () => validateField(name, input));
input.addEventListener('input', () => { if (input.classList.contains('form-control-error')) validateField(name, input); });
}
});
eventForm.addEventListener('submit', (e) => {
let isValid = true;
Object.keys(fields).forEach(name => {
const input = eventForm.querySelector(`[name="${name}"]`);
if (input && !validateField(name, input)) isValid = false;
});
// Validate custom location if "Other" is selected
const locSelect = eventForm.querySelector('#location_select');
const locOther = eventForm.querySelector('#location_other');
if (locSelect && locSelect.value === 'Other' && locOther && !locOther.value.trim()) {
showFieldError(locOther, 'Please enter a custom location.');
isValid = false;
}
// Validate custom category if "Other" is selected
const catSelect = eventForm.querySelector('#category_select');
const catOther = eventForm.querySelector('#category_other');
if (catSelect && catSelect.value === 'Other' && catOther && !catOther.value.trim()) {
showFieldError(catOther, 'Please enter a custom category.');
isValid = false;
}
if (!isValid) {
e.preventDefault();
const firstError = eventForm.querySelector('.form-control-error');
if (firstError) { firstError.focus(); firstError.scrollIntoView({ behavior: 'smooth', block: 'center' }); }
}
});
}
// =========================================================================
// 8b. "Other" Toggle for Location & Category Dropdowns
// =========================================================================
const locationSelect = document.getElementById('location_select');
const locationOther = document.getElementById('location_other');
if (locationSelect && locationOther) {
const toggleLocation = () => {
if (locationSelect.value === 'Other') {
locationOther.style.display = 'block';
locationOther.required = true;
locationSelect.removeAttribute('required');
} else {
locationOther.style.display = 'none';
locationOther.required = false;
locationOther.value = '';
}
};
locationSelect.addEventListener('change', toggleLocation);
// Run on load for edit pages with pre-selected "Other"
toggleLocation();
}
const categorySelect = document.getElementById('category_select');
const categoryOther = document.getElementById('category_other');
const categoryHidden = document.getElementById('category_final');
if (categorySelect && categoryOther && categoryHidden) {
const syncCategoryHidden = () => {
if (categorySelect.value === 'Other' && categoryOther.value.trim()) {
categoryHidden.value = categoryOther.value.trim();
} else if (categorySelect.value === 'Other') {
categoryHidden.value = 'Other';
} else {
categoryHidden.value = categorySelect.value;
}
};
const toggleCategory = () => {
if (categorySelect.value === 'Other') {
categoryOther.style.display = 'block';
categoryOther.required = true;
} else {
categoryOther.style.display = 'none';
categoryOther.required = false;
categoryOther.value = '';
}
syncCategoryHidden();
};
categorySelect.addEventListener('change', toggleCategory);
categoryOther.addEventListener('input', syncCategoryHidden);
categoryOther.addEventListener('change', syncCategoryHidden);
toggleCategory();
// Failsafe: sync hidden field right before form submits
const parentForm = categorySelect.closest('form');
if (parentForm) {
parentForm.addEventListener('submit', () => {
syncCategoryHidden();
});
}
}
// =========================================================================
// 8c. Auto-Compute Status from Event Date & Time
// =========================================================================
const eventDateInput = document.getElementById('event_date');
const eventTimeInput = document.getElementById('event_time');
const statusHidden = document.getElementById('status');
const statusDisplay = document.getElementById('status_display');
if (eventDateInput && eventTimeInput && statusHidden && statusDisplay) {
const computeStatus = () => {
const dateVal = eventDateInput.value;
const timeVal = eventTimeInput.value;
if (!dateVal || !timeVal) {
statusDisplay.value = '\u2014';
statusHidden.value = 'Upcoming';
statusDisplay.className = 'form-control';
return;
}
const now = new Date();
const eventDateTime = new Date(`${dateVal}T${timeVal}`);
const eventDateOnly = dateVal;
const todayOnly = now.toISOString().slice(0, 10);
let status;
if (eventDateOnly < todayOnly) {
status = 'Completed';
} else if (eventDateOnly === todayOnly) {
if (eventDateTime <= now) {
status = 'Completed';
} else {
status = 'Ongoing';
}
} else {
status = 'Upcoming';
}
statusHidden.value = status;
statusDisplay.value = status;
// Update visual style
statusDisplay.className = 'form-control status-auto status-auto-' + status.toLowerCase();
};
eventDateInput.addEventListener('change', computeStatus);
eventTimeInput.addEventListener('change', computeStatus);
// Run on page load (for edit pages)
computeStatus();
}
// =========================================================================
// 9. Flash Messages Logic
// =========================================================================
const urlParams = new URLSearchParams(window.location.search);
const successType = urlParams.get('success');
if (successType) {
const messages = { created: '🎉 Event created successfully!', updated: '✅ Event updated successfully!', deleted: '🗑️ Event deleted successfully!' };
const message = messages[successType];
if (message) {
const flash = document.createElement('div');
flash.className = 'flash-message flash-success';
flash.innerHTML = `<span class="flash-text">${message}</span><button class="flash-close" aria-label="Dismiss">×</button>`;
document.body.appendChild(flash);
requestAnimationFrame(() => flash.classList.add('flash-visible'));
flash.querySelector('.flash-close').addEventListener('click', () => { flash.classList.remove('flash-visible'); setTimeout(() => flash.remove(), 400); });
setTimeout(() => { flash.classList.remove('flash-visible'); setTimeout(() => flash.remove(), 400); }, 4000);
window.history.replaceState({}, '', window.location.pathname);
}
}
});