From 469d9d88cd35c62b7f399e0b0dfdc339bc3f0055 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Wed, 17 Jul 2019 17:56:12 +0200 Subject: Show progressbar while loading lunr and data --- assets/search.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'assets/search.js') diff --git a/assets/search.js b/assets/search.js index eb38c1d..3cc9702 100644 --- a/assets/search.js +++ b/assets/search.js @@ -3,19 +3,20 @@ (function() { const input = document.querySelector("#book-search-input"); const results = document.querySelector("#book-search-results"); - const dummy = document.querySelector("#book-search-dummy"); input.addEventListener("focus", init); input.addEventListener("keyup", search); function init() { - loadScript("{{ "lunr.min.js" | relURL }}") + input.removeEventListener("focus", init); //init once + input.required = true; + + loadScript("{{ "lunr.min.js" | relURL }}"); loadScript("{{ $searchData.RelPermalink }}", function() { input.readOnly = false; + input.required = false; search(); }); - - input.removeEventListener("focus", init); } function search() { @@ -23,18 +24,18 @@ results.removeChild(results.firstChild); } - if (!input.value || !window.bookSearch) { - return + if (!input.value) { + return; } const terms = lunr.tokenizer(input.value); const searchHits = window.bookSearch.idx.query(function(query) { query.term(terms, { - boost: 100, + boost: 100 }); query.term(terms, { boost: 10, - wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING, + wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING }); query.term(terms, { editDistance: 2 @@ -43,8 +44,8 @@ searchHits.slice(0, 10).forEach(function(hit) { const page = window.bookSearch.pages[hit.ref]; - const li = dummy.querySelector("li").cloneNode(true), - a = li.querySelector("a"); + const li = document.createElement("li"), + a = li.appendChild(document.createElement("a")); a.href = page.href; a.textContent = page.title; -- cgit v1.2.3