Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 533358b

Browse files
committed
Link to default search
1 parent 285f12d commit 533358b

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

sphinx_search/static/js/rtd_sphinx_search.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ const generateSuggestionsList = (data, projectName) => {
372372
search_result_single.appendChild(content);
373373
search_result_box.appendChild(search_result_single);
374374
}
375+
376+
// Extra element
377+
let search_result_single = createDomNode("div", {
378+
class: "search__result__single"
379+
});
380+
COUNT += 1;
381+
search_result_single.appendChild(getDefaultSearchLink(COUNT));
382+
search_result_box.appendChild(search_result_single);
383+
375384
return search_result_box;
376385
};
377386

@@ -468,6 +477,22 @@ const getErrorDiv = err_msg => {
468477
return err_div;
469478
};
470479

480+
481+
const getDefaultSearchLink = (id) => {
482+
let link = createDomNode("a", {href: "#"});
483+
const content_template =
484+
'<div class="outer_div_page_results" id="<%= id %>"> \
485+
<p>Show all results.</p> \
486+
</div>'
487+
let content = $u.template(content_template, {id: 'hit__' + id});
488+
link.innerHTML = content;
489+
link.onclick = () => {
490+
defaultSearch();
491+
return false;
492+
};
493+
return link;
494+
};
495+
471496
/**
472497
* Fetch the suggestions from search backend,
473498
* and appends the results to <div class="search__outer"> node,
@@ -519,15 +544,19 @@ const fetchAndGenerateResults = (search_url, projectName) => {
519544
removeAllActive();
520545
});
521546
} else {
547+
let err_div = getErrorDiv("No results found.");
548+
COUNT = 1;
549+
err_div.appendChild(getDefaultSearchLink(1));
522550
removeResults();
523-
let err_div = getErrorDiv("No results found");
524551
search_outer.appendChild(err_div);
525552
}
526553
}
527554
},
528555
error: (resp, status_code, error) => {
529-
removeResults();
530556
let err_div = getErrorDiv("There was an error. Please try again.");
557+
COUNT = 1;
558+
err_div.appendChild(getDefaultSearchLink(1));
559+
removeResults();
531560
search_outer.appendChild(err_div);
532561
}
533562
});
@@ -625,6 +654,19 @@ const removeSearchModal = () => {
625654
$(".search__outer__wrapper").fadeOut(ANIMATION_TIME);
626655
};
627656

657+
658+
/**
659+
* Search using the default form.
660+
*/
661+
const defaultSearch = () => {
662+
const input_field = getInputField();
663+
const form = input_field.parentElement;
664+
665+
input_field.value = getSearchTerm();
666+
form.submit();
667+
};
668+
669+
628670
window.addEventListener("DOMContentLoaded", evt => {
629671
// only add event listeners if READTHEDOCS_DATA global
630672
// variable is found.
@@ -723,19 +765,9 @@ window.addEventListener("DOMContentLoaded", evt => {
723765
const current_item = document.querySelector(
724766
".outer_div_page_results.active"
725767
);
726-
// if an item is selected,
727-
// then redirect to its link
768+
// If an item is selected, click it!
728769
if (current_item !== null) {
729-
const link = current_item.parentElement["href"];
730-
window.location.href = link;
731-
} else {
732-
// submit search form if there
733-
// is no active item.
734-
const input_field = getInputField();
735-
const form = input_field.parentElement;
736-
737-
search_bar.value = getSearchTerm();
738-
form.submit();
770+
current_item.parentElement.click();
739771
}
740772
}
741773
});

0 commit comments

Comments
 (0)