bee367's picture
1. Make the project code asynchronous 2. fix the menu dashboard
b0767d7 verified
Raw History Blame Contribute Delete
5.02 kB
// Main JavaScript for 2 Clean 2 Green
document.addEventListener('DOMContentLoaded', async function() {
try {
// Smooth scrolling for anchor links
const anchors = document.querySelectorAll('a[href^="#"]');
anchors.forEach(anchor => {
anchor.addEventListener('click', async function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
await new Promise(resolve => {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
setTimeout(resolve, 1000);
});
}
});
});
// Initialize language selector on all pages
const langSelector = document.getElementById('language-selector');
if (langSelector) {
langSelector.addEventListener('change', async function() {
const lang = this.value;
await updateTranslations(lang);
});
}
// Load translations if needed
if (!window.translations) {
await loadTranslations();
}
} catch (error) {
console.error('Initialization error:', error);
}
});
async function loadTranslations() {
try {
// In a real app, this would fetch from an API
window.translations = {
en: {
nav_how_it_works: "How It Works",
nav_features: "Features",
nav_pricing: "Pricing",
nav_dashboard: "Dashboard",
nav_single_player: "Single Player",
nav_simulator: "Simulator",
nav_signup: "Sign Up"
},
ru: {
nav_how_it_works: "Как это работает",
nav_features: "Функции",
nav_pricing: "Цены",
nav_dashboard: "Панель",
nav_single_player: "Одиночная игра",
nav_simulator: "Симулятор",
nav_signup: "Регистрация"
},
zh: {
nav_how_it_works: "工作原理",
nav_features: "特征",
nav_pricing: "价钱",
nav_dashboard: "仪表板",
nav_single_player: "单人游戏",
nav_simulator: "模拟器",
nav_signup: "注册"
}
};
} catch (error) {
console.error('Failed to load translations:', error);
}
}
async function updateTranslations(lang) {
try {
const elements = document.querySelectorAll('[data-i18n]');
for (const el of elements) {
const key = el.getAttribute('data-i18n');
if (window.translations[lang] && window.translations[lang][key]) {
el.textContent = window.translations[lang][key];
}
}
} catch (error) {
console.error('Translation update failed:', error);
}
}
// Waste types classification
window.wasteCategories = {
plastic: {
name: "Plastic",
color: "#3B82F6",
icon: "♻",
recyclingTip: "Rinse containers before recycling. Remove caps and labels."
},
paper: {
name: "Paper",
color: "#10B981",
icon: "📄",
recyclingTip: "Keep dry and clean. Remove any plastic or metallic parts."
},
glass: {
name: "Glass",
color: "#6366F1",
icon: "🍷",
recyclingTip: "Rinse thoroughly. Separate by color if required."
},
metal: {
name: "Metal",
color: "#F59E0B",
icon: "🔩",
recyclingTip: "Rinse cans. Remove paper labels if possible."
},
ewaste: {
name: "E-Waste",
color: "#EC4899",
icon: "🔌",
recyclingTip: "Do not throw in regular trash. Find specialized recycling."
},
organic: {
name: "Organic",
color: "#84CC16",
icon: "🍂",
recyclingTip: "Compost if possible. Do not mix with recyclables."
}
};
// Global translations object
window.translations = {
en: {
nav_how_it_works: "How It Works",
nav_features: "Features",
nav_pricing: "Pricing",
nav_dashboard: "Dashboard",
nav_single_player: "Single Player",
nav_simulator: "Simulator",
nav_signup: "Sign Up"
},
ru: {
nav_how_it_works: "Как это работает",
nav_features: "Функции",
nav_pricing: "Цены",
nav_dashboard: "Панель",
nav_single_player: "Одиночная игра",
nav_simulator: "Симулятор",
nav_signup: "Регистрация"
},
zh: {
nav_how_it_works: "工作原理",
nav_features: "特征",
nav_pricing: "价钱",
nav_dashboard: "仪表板",
nav_single_player: "单人游戏",
nav_simulator: "模拟器",
nav_signup: "注册"
}
};