File size: 9,500 Bytes
46925ca
b8048e2
 
46925ca
 
 
 
 
 
b8048e2
 
 
 
46925ca
 
 
b8048e2
46925ca
 
 
 
 
 
 
 
 
 
 
 
 
b8048e2
46925ca
 
 
 
 
 
 
b8048e2
 
 
 
46925ca
b8048e2
 
 
 
 
 
 
 
 
 
 
 
 
46925ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

// 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 = '<div class="spinner" style="width:20px;height:20px;border-width:2px;"></div>';
                
                // Simulate API call
                setTimeout(() => {
                    filterTools(searchTerm);
                    searchButton.innerHTML = '<i data-feather="search"></i>';
                    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 = `
        <div class="flex items-center">
            <i data-feather="${icon}" class="mr-2"></i>
            <span>${message}</span>
        </div>
    `;
    
    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;