From 25917ed0c1e01b6d7cc2ddac5bfca1c4238e96c2 Mon Sep 17 00:00:00 2001 From: muqiuhan Date: Sun, 15 Mar 2026 15:09:53 +0000 Subject: deploy: 6a321a6e20ead9aeea588c009b5445c76260ea92 --- js/search.js | 114 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 52 insertions(+), 62 deletions(-) (limited to 'js/search.js') diff --git a/js/search.js b/js/search.js index 2c7188af..27abb4e0 100644 --- a/js/search.js +++ b/js/search.js @@ -1,78 +1,68 @@ -// Global searchConfig +// Search functionality for standalone search page -document.addEventListener('DOMContentLoaded', () => { +(function () { + 'use strict'; - const input = document.querySelector('.search-input'); - const container = document.querySelector('.search-result-container'); + let localSearch = null; + let isDataLoaded = false; - const localSearch = new LocalSearch({ - path : searchConfig.path, - top_n_per_article: searchConfig.top_n_per_article, - unescape : searchConfig.unescape - }); + function initSearch() { + const input = document.getElementById('search-input'); + const container = document.getElementById('search-results'); + + if (!input || !container || typeof LocalSearch === 'undefined') { + container.innerHTML = '
搜索功能初始化失败
'; + return; + } + + localSearch = new LocalSearch({ + path: searchConfig.path, + top_n_per_article: searchConfig.top_n_per_article, + unescape: searchConfig.unescape + }); - if (searchConfig.preload) { - // preload the search data when the page loads - console.log("loading page"); localSearch.fetchData(); - } - function openSearchPopup() { - document.querySelector('.search-popup').classList.add('search-activate'); - if (!localSearch.isfetched) { - localSearch.fetchData(); - } - } + window.addEventListener('search:loaded', function () { + isDataLoaded = true; + container.innerHTML = '
'; + }); - function closeSearchPopup() { - document.querySelector('.search-popup').classList.remove('search-activate'); - // refresh search box - input.value = ''; - container.innerHTML = `
`; - } + function displaySearchResult() { + if (!isDataLoaded) { + container.innerHTML = '
加载中...
'; + return; + } - // open search box - document.querySelector('.search-btn').addEventListener('click', openSearchPopup); + const searchText = input.value.trim().toLowerCase(); - // close search box - document.querySelector('.search-popup-overlay').addEventListener('click', closeSearchPopup); - document.querySelector('.search-close-btn').addEventListener('click', closeSearchPopup); + if (searchText.length === 0) { + container.innerHTML = '
'; + return; + } - function displaySearchResult() { - if (!localSearch.isfetched) return; - const searchText = input.value.trim().toLowerCase(); - const keywords = searchText.split(/[-\s]+/); - if (searchText.length > 0) { - resultItems = localSearch.getResultItems(keywords); - } + const keywords = searchText.split(/[-\s]+/); + const resultItems = localSearch.getResultItems(keywords); - if (keywords.length === 1 && keywords[0] === '') { - // no input - container.innerHTML = `
` - } else if (resultItems.length === 0) { - // no result - container.innerHTML = `
No result found
`; - } else { - // display result(s) - container.innerHTML = ` -
${resultItems.length} result(s) found
- `; + if (resultItems.length === 0) { + container.innerHTML = '
未找到相关结果
'; + } else { + container.innerHTML = ` +
找到 ${resultItems.length} 个结果
+ + `; + } } - }; - - if (searchConfig.trigger == 'auto') { - // whenever there is input, update search result input.addEventListener('input', displaySearchResult); + input.focus(); + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', initSearch); } else { - // update search result when press "enter" - input.addEventListener('keypress', event => { - if (event.key === 'Enter') { - displaySearchResult(); - } - }) + initSearch(); } - window.addEventListener('search:loaded', displaySearchResult); -}); - \ No newline at end of file +})(); -- cgit v1.2.3