Skip to content

Commit 1fb91d6

Browse files
Only load searchindex when needed
1 parent 1b046e5 commit 1fb91d6

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/front-end/searcher/searcher.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ window.search = window.search || {};
262262
doc_urls = config.doc_urls;
263263
searchindex = elasticlunr.Index.load(config.index);
264264

265+
searchbar.removeAttribute("disabled");
266+
searchbar.focus();
267+
268+
const searchterm = searchbar.value.trim();
269+
if (searchterm !== "") {
270+
searchbar.classList.add("active");
271+
doSearch(searchterm);
272+
}
273+
}
274+
275+
function initSearchInteractions(config) {
265276
// Set up events
266277
searchicon.addEventListener('click', () => {
267278
searchIconClickHandler();
@@ -288,6 +299,8 @@ window.search = window.search || {};
288299
config.hasFocus = hasFocus;
289300
}
290301

302+
initSearchInteractions(window.search);
303+
291304
function unfocusSearchbar() {
292305
// hacky, but just focusing a div only works once
293306
const tmp = document.createElement('input');
@@ -401,8 +414,23 @@ window.search = window.search || {};
401414
}
402415
}
403416

417+
function loadScript(url, id) {
418+
if (document.getElementById(id)) {
419+
return;
420+
}
421+
const script = document.createElement('script');
422+
script.src = url;
423+
script.id = id;
424+
script.onload = () => init(window.search);
425+
script.onerror = error => {
426+
console.error(`Failed to load \`${url}\`: ${error}`);
427+
};
428+
document.head.append(script);
429+
}
430+
404431
function showSearch(yes) {
405432
if (yes) {
433+
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
406434
search_wrap.classList.remove('hidden');
407435
searchicon.setAttribute('aria-expanded', 'true');
408436
} else {
@@ -483,16 +511,15 @@ window.search = window.search || {};
483511

484512
function doSearch(searchterm) {
485513
// Don't search the same twice
486-
if (current_searchterm === searchterm) {
514+
if (current_searchterm == searchterm) {
487515
return;
488-
} else {
489-
current_searchterm = searchterm;
490516
}
491-
492-
if (searchindex === null) {
517+
if (searchindex == null) {
493518
return;
494519
}
495520

521+
current_searchterm = searchterm;
522+
496523
// Do the actual search
497524
const results = searchindex.search(searchterm, search_options);
498525
const resultcount = Math.min(results.length, results_options.limit_results);
@@ -513,17 +540,6 @@ window.search = window.search || {};
513540
showResults(true);
514541
}
515542

516-
function loadScript(url, id) {
517-
const script = document.createElement('script');
518-
script.src = url;
519-
script.id = id;
520-
script.onload = () => init(window.search);
521-
script.onerror = error => {
522-
console.error(`Failed to load \`${url}\`: ${error}`);
523-
};
524-
document.head.append(script);
525-
}
526-
527-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
528-
543+
// Exported functions
544+
search.hasFocus = hasFocus;
529545
})(window.search);

src/front-end/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
{{#if search_enabled}}
187187
<div id="search-wrapper" class="hidden">
188188
<form id="searchbar-outer" class="searchbar-outer">
189-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
189+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header" disabled>
190190
</form>
191191
<div id="searchresults-outer" class="searchresults-outer hidden">
192192
<div id="searchresults-header" class="searchresults-header"></div>

0 commit comments

Comments
 (0)