diff options
author | amzrk2 | 2020-06-16 14:00:53 +0800 |
---|---|---|
committer | amzrk2 | 2020-06-16 14:00:53 +0800 |
commit | c42924047c6f654c4723746790e041f8907ed378 (patch) | |
tree | 9d826ac811c3861da8ce9e7938d35a3147f4db36 /assets/js/fuji.js | |
parent | e7587236dbe98775315619d4f3b8d3f335001583 (diff) | |
download | hugo-theme-fuji-c42924047c6f654c4723746790e041f8907ed378.tar.gz hugo-theme-fuji-c42924047c6f654c4723746790e041f8907ed378.tar.bz2 hugo-theme-fuji-c42924047c6f654c4723746790e041f8907ed378.zip |
Searching now works
Diffstat (limited to 'assets/js/fuji.js')
-rw-r--r-- | assets/js/fuji.js | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/assets/js/fuji.js b/assets/js/fuji.js index 55e93b5..bf9eabc 100644 --- a/assets/js/fuji.js +++ b/assets/js/fuji.js @@ -85,38 +85,65 @@ document.querySelector('.btn .btn-toggle-mode').addEventListener('click', () => }); // search by fuse.js -function searchAll(key, index) { +function searchAll(key, index, counter) { let fuse = new Fuse(index, { - keys: ['title', 'tags'], + shouldSort: true, + distance: 10000, + keys: [ + { + name: 'title', + weight: 2.0, + }, + { + name: 'tags', + weight: 1.5, + }, + { + name: 'content', + weight: 1.0, + }, + ], }); let result = fuse.search(key); console.log(result); if (result.length > 0) { document.getElementById('search-result').innerHTML = template('search-result-template', result); + return [new Date().getTime() - counter, result.length]; } else { - document.getElementById('search-result').innerHTML = '<span>NOT FOUND</span>'; + return 'notFound'; } } let urlParams = new URLSearchParams(window.location.search); // get params from URL if (urlParams.has('s')) { - let key = urlParams.get('s'); // get search keyword + let counter = new Date().getTime(); + let infoElements = document.querySelectorAll('.search-result-info'); + let key = urlParams.get('s'); // get search keyword, divided by space document.querySelector('.search-input input').setAttribute('value', key); // get search index from json let xhr = new XMLHttpRequest(); xhr.open('GET', '/index.json', true); xhr.responseType = 'json'; - xhr.onerror = function (e) { - console.error(`${xhr.status} ${xhr.statusText}`); + xhr.onerror = (e) => { + infoElements[2].removeAttribute('style'); }; - xhr.onload = function () { + xhr.onload = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { // use index json to search console.log(xhr.response); - searchAll(key, xhr.response); + counter = searchAll(key, xhr.response, counter); + console.log(counter); + if (counter === 'notFound') { + infoElements[1].removeAttribute('style'); + } else { + infoElements[0].innerHTML = infoElements[0].innerHTML.replace('[TIME]', counter[0]); + infoElements[0].innerHTML = infoElements[0].innerHTML.replace('[NUM]', counter[1]); + infoElements[0].removeAttribute('style'); + } } else { - console.error(`${xhr.status} ${xhr.statusText}`); + console.error(`Failed to get index.json, ${xhr.status} ${xhr.statusText}`); + infoElements[2].removeAttribute('style'); } } }; |