Spaces:
Running
Running
File size: 9,867 Bytes
f77fe8b | 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 | // 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`;
}
}
}; |