|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + function escapeHtml(s) { |
| 5 | + return String(s).replace(/[&<>"']/g, function (c) { |
| 6 | + return ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]; |
| 7 | + }); |
| 8 | + } |
| 9 | + |
| 10 | + // Filters the rows of the big apps table in-place. |
| 11 | + function initTableFilter() { |
| 12 | + var input = document.getElementById('app-search-input'); |
| 13 | + var counter = document.getElementById('app-search-count'); |
| 14 | + if (!input) return; |
| 15 | + |
| 16 | + var table = input.closest('div').nextElementSibling; |
| 17 | + while (table && table.tagName !== 'TABLE') table = table.nextElementSibling; |
| 18 | + if (!table) return; |
| 19 | + |
| 20 | + var rows = Array.prototype.slice.call(table.tBodies[0] ? table.tBodies[0].rows : table.rows); |
| 21 | + if (rows.length && rows[0].cells[0] && rows[0].cells[0].tagName === 'TH') rows.shift(); |
| 22 | + |
| 23 | + var haystacks = rows.map(function (row) { |
| 24 | + return (row.textContent || '').toLowerCase(); |
| 25 | + }); |
| 26 | + var total = rows.length; |
| 27 | + |
| 28 | + function update() { |
| 29 | + var q = input.value.trim().toLowerCase(); |
| 30 | + var visible = 0; |
| 31 | + for (var i = 0; i < rows.length; i++) { |
| 32 | + var match = q === '' || haystacks[i].indexOf(q) !== -1; |
| 33 | + rows[i].style.display = match ? '' : 'none'; |
| 34 | + if (match) visible++; |
| 35 | + } |
| 36 | + counter.textContent = q === '' |
| 37 | + ? '(' + total + ' apps)' |
| 38 | + : '(' + visible + ' of ' + total + ' shown)'; |
| 39 | + } |
| 40 | + |
| 41 | + input.addEventListener('input', update); |
| 42 | + |
| 43 | + var params = new URLSearchParams(window.location.search); |
| 44 | + var initialQ = params.get('q'); |
| 45 | + if (initialQ) { |
| 46 | + input.value = initialQ; |
| 47 | + input.focus(); |
| 48 | + } |
| 49 | + update(); |
| 50 | + } |
| 51 | + |
| 52 | + // Fetches apps.json and renders a short list of matches with links. |
| 53 | + function initJsonSearch() { |
| 54 | + var MAX_RESULTS = 20; |
| 55 | + var APPS_URL = 'apps.json'; |
| 56 | + var APPS_PAGE = 'apps.html'; |
| 57 | + |
| 58 | + var input = document.getElementById('home-search-input'); |
| 59 | + var status = document.getElementById('home-search-status'); |
| 60 | + var results = document.getElementById('home-search-results'); |
| 61 | + var more = document.getElementById('home-search-more'); |
| 62 | + if (!input) return; |
| 63 | + |
| 64 | + var apps = null; |
| 65 | + var pending = null; |
| 66 | + |
| 67 | + function load() { |
| 68 | + if (apps || pending) return pending; |
| 69 | + pending = fetch(APPS_URL).then(function (r) { return r.json(); }).then(function (data) { |
| 70 | + apps = data; |
| 71 | + status.textContent = apps.length + ' apps in the database.'; |
| 72 | + if (input.value.trim()) render(input.value.trim()); |
| 73 | + }).catch(function () { |
| 74 | + status.textContent = 'Could not load the app database. Try again later.'; |
| 75 | + }); |
| 76 | + return pending; |
| 77 | + } |
| 78 | + |
| 79 | + function render(q) { |
| 80 | + results.innerHTML = ''; |
| 81 | + more.innerHTML = ''; |
| 82 | + if (!apps) return; |
| 83 | + if (!q) { |
| 84 | + status.textContent = apps.length + ' apps in the database.'; |
| 85 | + return; |
| 86 | + } |
| 87 | + var needle = q.toLowerCase(); |
| 88 | + var matches = []; |
| 89 | + for (var i = 0; i < apps.length; i++) { |
| 90 | + var a = apps[i]; |
| 91 | + var hay = (a.packageName + ' ' + (a.description || '')).toLowerCase(); |
| 92 | + if (hay.indexOf(needle) !== -1) matches.push(a); |
| 93 | + } |
| 94 | + if (!matches.length) { |
| 95 | + status.textContent = 'No apps match "' + q + '".'; |
| 96 | + return; |
| 97 | + } |
| 98 | + status.textContent = matches.length + ' match' + (matches.length === 1 ? '' : 'es') |
| 99 | + + (matches.length > MAX_RESULTS ? ' (showing first ' + MAX_RESULTS + ')' : '') + ':'; |
| 100 | + var shown = matches.slice(0, MAX_RESULTS); |
| 101 | + var html = ''; |
| 102 | + for (var j = 0; j < shown.length; j++) { |
| 103 | + var m = shown[j]; |
| 104 | + var safeName = escapeHtml(m.packageName); |
| 105 | + var safeDesc = escapeHtml((m.description || '').replace(/\.\.\.$/, '')); |
| 106 | + var safeIcon = escapeHtml(m.icon || ''); |
| 107 | + html += '<li style="display: flex; align-items: center; gap: 0.6em; padding: 0.35em 0.25em; border-bottom: 1px solid #eee;">' |
| 108 | + + '<img loading="lazy" src="' + safeIcon + '" alt="" width="32" height="32" style="flex: 0 0 32px;">' |
| 109 | + + '<span style="flex: 1;">' |
| 110 | + + '<a href="apps/' + safeName + '.html"><strong>' + safeName + '</strong></a>' |
| 111 | + + '<br><span style="color: #555; font-size: 0.9em;">' + safeDesc + '</span>' |
| 112 | + + '</span>' |
| 113 | + + '</li>'; |
| 114 | + } |
| 115 | + results.innerHTML = html; |
| 116 | + if (matches.length > MAX_RESULTS) { |
| 117 | + more.innerHTML = '<a href="' + APPS_PAGE + '?q=' + encodeURIComponent(q) + '">See all ' |
| 118 | + + matches.length + ' matches on the apps page →</a>'; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + input.addEventListener('focus', load); |
| 123 | + input.addEventListener('input', function () { |
| 124 | + if (apps) render(input.value.trim()); |
| 125 | + else load(); |
| 126 | + }); |
| 127 | + |
| 128 | + load(); |
| 129 | + } |
| 130 | + |
| 131 | + function init() { |
| 132 | + initTableFilter(); |
| 133 | + initJsonSearch(); |
| 134 | + } |
| 135 | + |
| 136 | + if (document.readyState === 'loading') { |
| 137 | + document.addEventListener('DOMContentLoaded', init); |
| 138 | + } else { |
| 139 | + init(); |
| 140 | + } |
| 141 | +})(); |
0 commit comments