(searchTerm)
| 4996 | |
| 4997 | // Lazy + backward-compatible search |
| 4998 | function searchFiles(searchTerm) { |
| 4999 | if (!searchTerm) return fileData; |
| 5000 | |
| 5001 | // kick off Fuse load in the background, but don't await |
| 5002 | lazyLoadFuse().catch(() => { /* ignore */ }); |
| 5003 | |
| 5004 | // keys config (matches your original) |
| 5005 | const fuseKeys = [ |
| 5006 | { name: 'name', weight: 0.1 }, |
| 5007 | { name: 'uploader', weight: 0.1 }, |
| 5008 | { name: 'tags.name', weight: 0.1 } |
| 5009 | ]; |
| 5010 | if (window.advancedSearchEnabled) { |
| 5011 | fuseKeys.push({ name: 'content', weight: 0.7 }); |
| 5012 | } |
| 5013 | |
| 5014 | // If Fuse is present, use it right away (synchronous API) |
| 5015 | if (window.Fuse) { |
| 5016 | const options = { |
| 5017 | keys: fuseKeys, |
| 5018 | threshold: 0.4, |
| 5019 | minMatchCharLength: 2, |
| 5020 | ignoreLocation: true |
| 5021 | }; |
| 5022 | const fuse = new window.Fuse(fileData, options); |
| 5023 | const results = fuse.search(searchTerm); |
| 5024 | return results.map(r => r.item); |
| 5025 | } |
| 5026 | |
| 5027 | // Fallback (first keystrokes before Fuse finishes loading): |
| 5028 | // simple case-insensitive substring match on the same fields |
| 5029 | const q = String(searchTerm).toLowerCase(); |
| 5030 | const hay = (v) => (v == null ? '' : String(v)).toLowerCase(); |
| 5031 | return fileData.filter(item => { |
| 5032 | if (hay(item.name).includes(q)) return true; |
| 5033 | if (hay(item.uploader).includes(q)) return true; |
| 5034 | if (Array.isArray(item.tags) && item.tags.some(t => hay(t?.name).includes(q))) return true; |
| 5035 | if (window.advancedSearchEnabled && hay(item.content).includes(q)) return true; |
| 5036 | return false; |
| 5037 | }); |
| 5038 | } |
| 5039 | |
| 5040 | /** |
| 5041 | * View mode toggle |
no test coverage detected