// Main JavaScript for CryptoPulse Explorer
// API Configuration
const COINGECKO_API = 'https://api.coingecko.com/api/v3';
const CRYPTO_DATA = [
{ id: 'bitcoin', symbol: 'btc', name: 'Bitcoin', color: '#f7931a' },
{ id: 'ethereum', symbol: 'eth', name: 'Ethereum', color: '#627eea' },
{ id: 'cardano', symbol: 'ada', name: 'Cardano', color: '#0033ad' },
{ id: 'solana', symbol: 'sol', name: 'Solana', color: '#00ffa3' },
{ id: 'polkadot', symbol: 'dot', name: 'Polkadot', color: '#e6007a' },
{ id: 'dogecoin', symbol: 'doge', name: 'Dogecoin', color: '#c2a633' },
{ id: 'chainlink', symbol: 'link', name: 'Chainlink', color: '#2a5ada' },
{ id: 'litecoin', symbol: 'ltc', name: 'Litecoin', color: '#bfbbbb' }
];
// Load Crypto Data
async function loadCryptoData() {
const tableBody = document.getElementById('crypto-table-body');
if (!tableBody) return;
// Show loading state
tableBody.innerHTML = `
|
Loading market data...
|
`;
try {
// In a real application, you would fetch from the API
// For demo purposes, we'll use mock data
const mockData = generateMockCryptoData();
// Clear loading state
tableBody.innerHTML = '';
// Populate table with data
mockData.forEach(crypto => {
const row = document.createElement('tr');
row.className = 'hover:bg-gray-50 transition duration-150';
const changeClass = crypto.change24h >= 0 ? 'price-up' : 'price-down';
const changeIcon = crypto.change24h >= 0 ? 'trending-up' : 'trending-down';
row.innerHTML = `
${crypto.symbol.toUpperCase().charAt(0)}
${crypto.name}
${crypto.symbol.toUpperCase()}
|
$${crypto.price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
${crypto.change24h >= 0 ? '+' : ''}${crypto.change24h.toFixed(2)}%
|
$${(crypto.marketCap / 1000000000).toFixed(2)}B
|
$${(crypto.volume24h / 1000000000).toFixed(2)}B
|
Trade
|
`;
tableBody.appendChild(row);
});
// Update Feather icons
feather.replace();
} catch (error) {
console.error('Error loading crypto data:', error);
tableBody.innerHTML = `
|
Failed to load market data. Please try again later.
|
`;
feather.replace();
}
}
// Generate Mock Crypto Data
function generateMockCryptoData() {
const basePrices = {
bitcoin: 45000,
ethereum: 3000,
cardano: 2.5,
solana: 100,
polkadot: 20,
dogecoin: 0.15,
chainlink: 25,
litecoin: 75
};
return CRYPTO_DATA.map(crypto => {
const basePrice = basePrices[crypto.id];
const change24h = (Math.random() * 10 - 5); // Random change between -5% and +5%
const currentPrice = basePrice * (1 + change24h / 100);
return {
...crypto,
price: currentPrice,
change24h: change24h,
marketCap: currentPrice * (1000000 + Math.random() * 5000000),
volume24h: currentPrice * (100000 + Math.random() * 500000)
};
});
}
// Add to Watchlist
function addToWatchlist(coinId) {
const coin = CRYPTO_DATA.find(c => c.id === coinId);
if (!coin) return;
// In a real app, this would save to localStorage or backend
alert(`Added ${coin.name} to your watchlist!`);
// Update the star icon
feather.replace();
}
// Update Market Stats
function updateMarketStats() {
// This would be updated with real API data
const stats = {
marketCap: 2.1 * (1 + Math.random() * 0.02 - 0.01), // Random fluctuation
btcDominance: 52.4 * (1 + Math.random() * 0.005 - 0.0025),
volume24h: 84.2 * (1 + Math.random() * 0.05 - 0.025),
fearGreed: Math.floor(Math.random() * 20 + 70) // Random between 70-90
};
// Update DOM elements if they exist
const marketCapEl = document.querySelector('[data-stat="market-cap"]');
const btcDomEl = document.querySelector('[data-stat="btc-dominance"]');
const volumeEl = document.querySelector('[data-stat="volume"]');
const fearGreedEl = document.querySelector('[data-stat="fear-greed"]');
if (marketCapEl) marketCapEl.textContent = `$${stats.marketCap.toFixed(1)}T`;
if (btcDomEl) btcDomEl.textContent = `${stats.btcDominance.toFixed(1)}%`;
if (volumeEl) volumeEl.textContent = `$${stats.volume24h.toFixed(1)}B`;
if (fearGreedEl) fearGreedEl.textContent = stats.fearGreed;
}
// Initialize when page loads
document.addEventListener('DOMContentLoaded', function() {
// Update market stats every 30 seconds
setInterval(updateMarketStats, 30000);
// Initialize tooltips
initTooltips();
});
// Initialize Tooltips
function initTooltips() {
const tooltipElements = document.querySelectorAll('[data-tooltip]');
tooltipElements.forEach(el => {
el.addEventListener('mouseenter', function(e) {
const tooltipText = this.getAttribute('data-tooltip');
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-50 px-3 py-2 text-sm text-white bg-gray-900 rounded-lg shadow-lg';
tooltip.textContent = tooltipText;
tooltip.style.top = `${e.clientY - 40}px`;
tooltip.style.left = `${e.clientX}px`;
tooltip.id = 'custom-tooltip';
document.body.appendChild(tooltip);
});
el.addEventListener('mouseleave', function() {
const tooltip = document.getElementById('custom-tooltip');
if (tooltip) {
tooltip.remove();
}
});
});
}
// Theme Toggle (for future dark mode support)
function toggleTheme() {
const html = document.documentElement;
const currentTheme = html.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
// Update theme icon
const themeIcon = document.getElementById('theme-icon');
if (themeIcon) {
themeIcon.setAttribute('data-feather', newTheme === 'dark' ? 'sun' : 'moon');
feather.replace();
}
}
// Check for saved theme preference
function loadThemePreference() {
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
}