// Initialize tool search functionality
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Search functionality with debounce
const searchInput = document.querySelector('input[type="text"]');
const searchButton = document.querySelector('button');
if (searchInput && searchButton) {
let searchTimeout;
const performSearch = () => {
const searchTerm = searchInput.value.trim();
if (searchTerm.length >= 2) {
// Show loading
searchButton.innerHTML = '
';
// Simulate API call
setTimeout(() => {
filterTools(searchTerm);
searchButton.innerHTML = '';
feather.replace();
showToast(`Found tools matching "${searchTerm}"`);
}, 500);
} else if (searchTerm.length === 0) {
resetSearch();
}
};
searchButton.addEventListener('click', performSearch);
searchInput.addEventListener('input', function() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(performSearch, 300);
});
searchInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
performSearch();
}
});
}
// Tool card hover effects
document.querySelectorAll('tool-card').forEach(card => {
card.addEventListener('mouseenter', function() {
this.classList.add('shadow-tool-hover');
});
card.addEventListener('mouseleave', function() {
this.classList.remove('shadow-tool-hover');
});
});
// Initialize tool stats
updateToolStats();
// Add copy functionality to code snippets
document.querySelectorAll('code').forEach(code => {
code.title = 'Click to copy';
code.style.cursor = 'pointer';
code.addEventListener('click', function() {
const text = this.textContent;
navigator.clipboard.writeText(text).then(() => {
showToast('Copied to clipboard!');
});
});
});
// Initialize dark mode from localStorage
const isDark = localStorage.getItem('darkMode') === 'true';
if (isDark) {
document.documentElement.classList.add('dark');
}
// Load featured tools from API (mock)
loadFeaturedTools();
});
// Filter tools based on search term
function filterTools(searchTerm) {
const tools = document.querySelectorAll('tool-card');
let count = 0;
tools.forEach(tool => {
const title = tool.getAttribute('title')?.toLowerCase() || '';
const desc = tool.getAttribute('description')?.toLowerCase() || '';
const category = tool.getAttribute('category')?.toLowerCase() || '';
if (title.includes(searchTerm.toLowerCase()) ||
desc.includes(searchTerm.toLowerCase()) ||
category.includes(searchTerm.toLowerCase())) {
tool.style.display = 'block';
tool.classList.add('slide-in');
count++;
} else {
tool.style.display = 'none';
}
});
if (count === 0) {
showToast('No tools found. Try a different search term.', 'warning');
}
}
function resetSearch() {
const tools = document.querySelectorAll('tool-card');
tools.forEach(tool => {
tool.style.display = 'block';
tool.classList.add('fade-in');
});
}
// Show toast notification
function showToast(message, type = 'success') {
// Remove existing toast
const existing = document.querySelector('.toast');
if (existing) existing.remove();
const toast = document.createElement('div');
toast.className = `toast ${type}`;
const icon = type === 'success' ? 'check-circle' : 'alert-circle';
toast.innerHTML = `
${message}
`;
document.body.appendChild(toast);
// Trigger reflow
toast.offsetWidth;
toast.classList.add('show');
if (typeof feather !== 'undefined') {
feather.replace();
}
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// Update tool statistics
function updateToolStats() {
const stats = {
total: 128,
categories: 12,
users: 15420,
updated: new Date().toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
};
document.querySelectorAll('.stat').forEach(stat => {
const key = stat.getAttribute('data-stat');
if (stats[key]) {
const value = stat.textContent;
let count = 0;
const target = stats[key];
const animate = () => {
if (typeof target === 'number') {
stat.textContent = Math.min(count, target).toLocaleString();
if (count < target) {
count += Math.ceil(target / 100);
setTimeout(animate, 20);
}
} else {
stat.textContent = target;
}
};
animate();
}
});
}
// Load featured tools from mock API
async function loadFeaturedTools() {
try {
// Mock API response
const tools = [
{
title: "JSON Formatter",
description: "Format, validate, and beautify JSON data with syntax highlighting.",
icon: "code",
category: "Developer",
color: "category-dev"
},
{
title: "Image Compressor",
description: "Compress PNG, JPEG, and WebP images without losing quality.",
icon: "image",
category: "Image",
color: "category-image"
},
{
title: "CSS Gradient Generator",
description: "Create beautiful CSS gradients with live preview and code export.",
icon: "droplet",
category: "Design",
color: "category-design"
},
{
title: "Base64 Encoder",
description: "Encode and decode Base64 strings for images and text.",
icon: "hash",
category: "Text",
color: "category-text"
}
];
// Inject into featured section if empty
const featuredSection = document.querySelector('#featured-tools .grid');
if (featuredSection && featuredSection.children.length <= 2) {
tools.forEach(tool => {
const card = document.createElement('tool-card');
card.setAttribute('title', tool.title);
card.setAttribute('description', tool.description);
card.setAttribute('icon', tool.icon);
card.setAttribute('category', tool.category);
card.setAttribute('link', '#');
card.classList.add(tool.color);
featuredSection.appendChild(card);
});
if (typeof feather !== 'undefined') {
feather.replace();
}
}
} catch (error) {
console.error('Failed to load tools:', error);
}
}
// Theme toggle with localStorage
function toggleTheme() {
const html = document.documentElement;
const isDark = html.classList.toggle('dark');
localStorage.setItem('darkMode', isDark);
const themeToggle = document.querySelector('#theme-toggle') ||
document.querySelector('custom-navbar')?.shadowRoot?.querySelector('#theme-toggle');
if (themeToggle) {
const icon = themeToggle.querySelector('i');
if (icon) {
icon.setAttribute('data-feather', isDark ? 'sun' : 'moon');
feather.replace();
}
}
showToast(`Switched to ${isDark ? 'dark' : 'light'} mode`);
}
// Export utilities to window
window.Toolsvana = {
searchTools: filterTools,
showToast: showToast,
toggleTheme: toggleTheme,
updateStats: updateToolStats
};
// Quick JSON formatter for the code snippet
function formatJsonSnippet() {
const input = document.getElementById('json-snippet');
const output = document.getElementById('json-result');
try {
const obj = JSON.parse(input.textContent);
const formatted = JSON.stringify(obj, null, 2);
output.textContent = formatted;
showToast('JSON formatted successfully!');
} catch (e) {
output.textContent = 'Invalid JSON: ' + e.message;
showToast('Invalid JSON format', 'warning');
}
}
function copyJsonResult() {
const result = document.getElementById('json-result');
navigator.clipboard.writeText(result.textContent).then(() => {
showToast('Copied formatted JSON!');
});
}
// Make theme toggle globally accessible
window.toggleTheme = toggleTheme;