diff --git a/src/front-end/css/chrome.css b/src/front-end/css/chrome.css index 7a95c2441b..a5cea8e81a 100644 --- a/src/front-end/css/chrome.css +++ b/src/front-end/css/chrome.css @@ -344,9 +344,34 @@ mark.fade-out { max-width: var(--content-max-width); } +#searchbar-outer.searching #searchbar { + padding-right: 30px; +} +#searchbar-outer .spinner-wrapper { + display: none; +} +#searchbar-outer.searching .spinner-wrapper { + display: block; +} + +.search-wrapper { + position: relative; +} + +.spinner-wrapper { + --spinner-margin: 2px; + position: absolute; + margin-block-start: calc(var(--searchbar-margin-block-start) + var(--spinner-margin)); + right: var(--spinner-margin); + top: 0; + bottom: var(--spinner-margin); + padding: 6px; + background-color: var(--bg); +} + #searchbar { width: 100%; - margin-block-start: 5px; + margin-block-start: var(--searchbar-margin-block-start); margin-block-end: 0; margin-inline-start: auto; margin-inline-end: auto; diff --git a/src/front-end/css/variables.css b/src/front-end/css/variables.css index 6a692ae7b0..cac3213434 100644 --- a/src/front-end/css/variables.css +++ b/src/front-end/css/variables.css @@ -10,6 +10,7 @@ --menu-bar-height: 50px; --mono-font: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace; --code-font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */ + --searchbar-margin-block-start: 5px; } /* Themes */ diff --git a/src/front-end/searcher/searcher.js b/src/front-end/searcher/searcher.js index 4d18b138c8..fe89a239a0 100644 --- a/src/front-end/searcher/searcher.js +++ b/src/front-end/searcher/searcher.js @@ -22,6 +22,7 @@ window.search = window.search || {}; } const search_wrap = document.getElementById('search-wrapper'), + searchbar_outer = document.getElementById('searchbar-outer'), searchbar = document.getElementById('searchbar'), searchresults = document.getElementById('searchresults'), searchresults_outer = document.getElementById('searchresults-outer'), @@ -267,6 +268,18 @@ window.search = window.search || {}; doc_urls = config.doc_urls; searchindex = elasticlunr.Index.load(config.index); + searchbar_outer.classList.remove('searching'); + + searchbar.focus(); + + const searchterm = searchbar.value.trim(); + if (searchterm !== '') { + searchbar.classList.add('active'); + doSearch(searchterm); + } + } + + function initSearchInteractions() { // Set up events searchicon.addEventListener('click', () => { searchIconClickHandler(); @@ -290,6 +303,8 @@ window.search = window.search || {}; doSearchOrMarkFromUrl(); } + initSearchInteractions(); + function unfocusSearchbar() { // hacky, but just focusing a div only works once const tmp = document.createElement('input'); @@ -396,8 +411,25 @@ window.search = window.search || {}; } } + function loadSearchScript(url, id) { + if (document.getElementById(id)) { + return; + } + searchbar_outer.classList.add('searching'); + + const script = document.createElement('script'); + script.src = url; + script.id = id; + script.onload = () => init(window.search); + script.onerror = error => { + console.error(`Failed to load \`${url}\`: ${error}`); + }; + document.head.append(script); + } + function showSearch(yes) { if (yes) { + loadSearchScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index'); search_wrap.classList.remove('hidden'); searchicon.setAttribute('aria-expanded', 'true'); } else { @@ -480,14 +512,14 @@ window.search = window.search || {}; // Don't search the same twice if (current_searchterm === searchterm) { return; - } else { - current_searchterm = searchterm; } - + searchbar_outer.classList.add('searching'); if (searchindex === null) { return; } + current_searchterm = searchterm; + // Do the actual search const results = searchindex.search(searchterm, search_options); const resultcount = Math.min(results.length, results_options.limit_results); @@ -506,21 +538,9 @@ window.search = window.search || {}; // Display results showResults(true); + searchbar_outer.classList.remove('searching'); } - function loadScript(url, id) { - const script = document.createElement('script'); - script.src = url; - script.id = id; - script.onload = () => init(window.search); - script.onerror = error => { - console.error(`Failed to load \`${url}\`: ${error}`); - }; - document.head.append(script); - } - - loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index'); - // Exported functions search.hasFocus = hasFocus; })(window.search); diff --git a/src/front-end/templates/index.hbs b/src/front-end/templates/index.hbs index 98f3b886c9..892098a0c1 100644 --- a/src/front-end/templates/index.hbs +++ b/src/front-end/templates/index.hbs @@ -173,7 +173,12 @@ {{#if search_enabled}}