Spaces:
Running

mapcraft-navigator / script.js
Zx4o's picture
**Google Maps — described in MVP terms**
f77fe8b verified
Raw History Blame Contribute Delete
9.87 kB
// Global JavaScript for MapCraft Navigator
// DOM Ready
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Mobile Menu Toggle
const mobileMenuButton = document.getElementById('mobileMenuButton');
const mobileMenu = document.getElementById('mobileMenu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', function() {
mobileMenu.classList.toggle('hidden');
mobileMenu.classList.toggle('slide-in');
// Update menu icon
const menuIcon = mobileMenuButton.querySelector('[data-feather]');
if (menuIcon) {
const iconName = mobileMenu.classList.contains('hidden') ? 'menu' : 'x';
menuIcon.setAttribute('data-feather', iconName);
feather.replace();
}
});
}
// Smooth Scrolling for Anchor Links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const href = this.getAttribute('href');
// Only process internal anchor links
if (href !== '#' && href.startsWith('#')) {
e.preventDefault();
const targetId = href.substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
window.scrollTo({
top: targetElement.offsetTop - 80,
behavior: 'smooth'
});
}
}
});
});
// Form Validation
const forms = document.querySelectorAll('form');
forms.forEach(form => {
form.addEventListener('submit', function(e) {
const requiredInputs = this.querySelectorAll('[required]');
let isValid = true;
requiredInputs.forEach(input => {
if (!input.value.trim()) {
isValid = false;
input.classList.add('border-red-500');
// Add error message if not present
if (!input.nextElementSibling || !input.nextElementSibling.classList.contains('error-message')) {
const errorMsg = document.createElement('p');
errorMsg.className = 'error-message text-red-400 text-sm mt-1';
errorMsg.textContent = 'This field is required';
input.parentNode.appendChild(errorMsg);
}
} else {
input.classList.remove('border-red-500');
const errorMsg = input.parentNode.querySelector('.error-message');
if (errorMsg) {
errorMsg.remove();
}
}
});
if (!isValid) {
e.preventDefault();
}
});
});
// Remove error styles on input
document.querySelectorAll('input, textarea').forEach(input => {
input.addEventListener('input', function() {
this.classList.remove('border-red-500');
const errorMsg = this.parentNode.querySelector('.error-message');
if (errorMsg) {
errorMsg.remove();
}
});
});
// Back to Top Button
const backToTopButton = document.createElement('button');
backToTopButton.innerHTML = '<i data-feather="arrow-up"></i>';
backToTopButton.className = 'fixed bottom-6 right-6 w-12 h-12 bg-primary-600 hover:bg-primary-700 text-white rounded-full shadow-lg transition-all duration-300 transform hover:scale-110 hidden z-50 flex items-center justify-center';
backToTopButton.id = 'backToTop';
document.body.appendChild(backToTopButton);
window.addEventListener('scroll', function() {
if (window.pageYOffset > 300) {
backToTopButton.classList.remove('hidden');
backToTopButton.classList.add('fade-in');
} else {
backToTopButton.classList.add('hidden');
}
});
backToTopButton.addEventListener('click', function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
// Initialize Feather Icons in dynamically added elements
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
feather.replace();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Add active class to current page in navigation
const currentPath = window.location.pathname;
const navLinks = document.querySelectorAll('nav a');
navLinks.forEach(link => {
const linkPath = link.getAttribute('href');
if (linkPath && currentPath.includes(linkPath.replace('/', '')) && linkPath !== '/') {
link.classList.add('text-primary-400', 'font-semibold');
}
});
// Toast Notification System
window.showToast = function(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `fixed top-6 right-6 px-6 py-3 rounded-lg shadow-lg transform transition-all duration-300 z-50 ${
type === 'success' ? 'bg-green-600' : 'bg-red-600'
} text-white`;
toast.textContent = message;
toast.style.transform = 'translateX(100%)';
document.body.appendChild(toast);
setTimeout(() => {
toast.style.transform = 'translateX(0)';
}, 10);
setTimeout(() => {
toast.style.transform = 'translateX(100%)';
setTimeout(() => {
toast.remove();
}, 300);
}, 3000);
};
// Theme Toggle (for future light mode support)
const themeToggle = document.getElementById('themeToggle');
if (themeToggle) {
themeToggle.addEventListener('click', function() {
const html = document.documentElement;
const isDark = html.classList.contains('dark');
if (isDark) {
html.classList.remove('dark');
localStorage.setItem('theme', 'light');
showToast('Switched to light mode', 'success');
} else {
html.classList.add('dark');
localStorage.setItem('theme', 'dark');
showToast('Switched to dark mode', 'success');
}
feather.replace();
});
}
// Check for saved theme preference
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
document.documentElement.classList.remove('dark');
}
// Initialize tooltips
document.querySelectorAll('[data-tooltip]').forEach(element => {
element.addEventListener('mouseenter', function() {
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-50 px-3 py-2 text-sm bg-gray-900 text-gray-100 rounded-lg shadow-lg';
tooltip.textContent = this.dataset.tooltip;
tooltip.style.top = (this.getBoundingClientRect().top - 40) + 'px';
tooltip.style.left = (this.getBoundingClientRect().left + this.offsetWidth / 2) + 'px';
tooltip.style.transform = 'translateX(-50%)';
tooltip.id = 'tooltip-' + Date.now();
document.body.appendChild(tooltip);
this.addEventListener('mouseleave', function() {
document.getElementById(tooltip.id)?.remove();
});
});
});
});
// Utility Functions
window.debounce = function(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
window.throttle = function(func, limit) {
let inThrottle;
return function() {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
};
// Map Utility Functions
window.MapCraft = {
calculateDistance: function(lat1, lon1, lat2, lon2) {
// Haversine formula for distance calculation
const R = 3958.8; // Earth's radius in miles
const dLat = this.toRad(lat2 - lat1);
const dLon = this.toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(this.toRad(lat1)) * Math.cos(this.toRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
},
toRad: function(degrees) {
return degrees * (Math.PI / 180);
},
formatDistance: function(miles) {
if (miles < 0.1) {
return Math.round(miles * 5280) + ' feet';
} else if (miles < 10) {
return miles.toFixed(1) + ' miles';
} else {
return Math.round(miles) + ' miles';
}
},
formatTime: function(minutes) {
if (minutes < 60) {
return Math.round(minutes) + ' min';
} else {
const hours = Math.floor(minutes / 60);
const remainingMinutes = Math.round(minutes % 60);
return `${hours}h ${remainingMinutes}m`;
}
}
};