Skip to content

Commit dc58ab9

Browse files
Only load searchindex when needed
1 parent d9d27f3 commit dc58ab9

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/front-end/searcher/searcher.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ window.search = window.search || {};
267267
doc_urls = config.doc_urls;
268268
searchindex = elasticlunr.Index.load(config.index);
269269

270+
searchbar.removeAttribute("disabled");
271+
searchbar.focus();
272+
273+
const searchterm = searchbar.value.trim();
274+
if (searchterm !== "") {
275+
searchbar.classList.add("active");
276+
doSearch(searchterm);
277+
}
278+
}
279+
280+
function initSearchInteractions() {
270281
// Set up events
271282
searchicon.addEventListener('click', () => {
272283
searchIconClickHandler();
@@ -290,6 +301,8 @@ window.search = window.search || {};
290301
doSearchOrMarkFromUrl();
291302
}
292303

304+
initSearchInteractions();
305+
293306
function unfocusSearchbar() {
294307
// hacky, but just focusing a div only works once
295308
const tmp = document.createElement('input');
@@ -396,8 +409,23 @@ window.search = window.search || {};
396409
}
397410
}
398411

412+
function loadScript(url, id) {
413+
if (document.getElementById(id)) {
414+
return;
415+
}
416+
const script = document.createElement('script');
417+
script.src = url;
418+
script.id = id;
419+
script.onload = () => init(window.search);
420+
script.onerror = error => {
421+
console.error(`Failed to load \`${url}\`: ${error}`);
422+
};
423+
document.head.append(script);
424+
}
425+
399426
function showSearch(yes) {
400427
if (yes) {
428+
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
401429
search_wrap.classList.remove('hidden');
402430
searchicon.setAttribute('aria-expanded', 'true');
403431
} else {
@@ -478,16 +506,15 @@ window.search = window.search || {};
478506

479507
function doSearch(searchterm) {
480508
// Don't search the same twice
481-
if (current_searchterm === searchterm) {
509+
if (current_searchterm == searchterm) {
482510
return;
483-
} else {
484-
current_searchterm = searchterm;
485511
}
486-
487-
if (searchindex === null) {
512+
if (searchindex == null) {
488513
return;
489514
}
490515

516+
current_searchterm = searchterm;
517+
491518
// Do the actual search
492519
const results = searchindex.search(searchterm, search_options);
493520
const resultcount = Math.min(results.length, results_options.limit_results);
@@ -508,19 +535,6 @@ window.search = window.search || {};
508535
showResults(true);
509536
}
510537

511-
function loadScript(url, id) {
512-
const script = document.createElement('script');
513-
script.src = url;
514-
script.id = id;
515-
script.onload = () => init(window.search);
516-
script.onerror = error => {
517-
console.error(`Failed to load \`${url}\`: ${error}`);
518-
};
519-
document.head.append(script);
520-
}
521-
522-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
523-
524538
// Exported functions
525539
search.hasFocus = hasFocus;
526540
})(window.search);

src/front-end/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
{{#if search_enabled}}
174174
<div id="search-wrapper" class="hidden">
175175
<form id="searchbar-outer" class="searchbar-outer">
176-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
176+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header" disabled>
177177
</form>
178178
<div id="searchresults-outer" class="searchresults-outer hidden">
179179
<div id="searchresults-header" class="searchresults-header"></div>

0 commit comments

Comments
 (0)