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

Commit 1559619

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

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

sphinx_search/static/js/rtd_sphinx_search.js

Lines changed: 46 additions & 15 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,
@@ -504,12 +529,12 @@ const fetchAndGenerateResults = (search_url, projectName) => {
504529
status_code === "success" ||
505530
typeof resp.responseJSON !== "undefined"
506531
) {
532+
removeResults();
507533
if (resp.responseJSON.results.length > 0) {
508534
let search_result_box = generateSuggestionsList(
509535
resp.responseJSON,
510536
projectName
511537
);
512-
removeResults();
513538
search_outer.appendChild(search_result_box);
514539

515540
// remove active classes from all suggestions
@@ -519,15 +544,18 @@ const fetchAndGenerateResults = (search_url, projectName) => {
519544
removeAllActive();
520545
});
521546
} else {
522-
removeResults();
523-
let err_div = getErrorDiv("No results found");
547+
let err_div = getErrorDiv("No results found.");
548+
COUNT = 1;
549+
err_div.appendChild(getDefaultSearchLink(1));
524550
search_outer.appendChild(err_div);
525551
}
526552
}
527553
},
528554
error: (resp, status_code, error) => {
529555
removeResults();
530556
let err_div = getErrorDiv("There was an error. Please try again.");
557+
COUNT = 1;
558+
err_div.appendChild(getDefaultSearchLink(1));
531559
search_outer.appendChild(err_div);
532560
}
533561
});
@@ -625,6 +653,19 @@ const removeSearchModal = () => {
625653
$(".search__outer__wrapper").fadeOut(ANIMATION_TIME);
626654
};
627655

656+
657+
/**
658+
* Search using the default form.
659+
*/
660+
const defaultSearch = () => {
661+
const input_field = getInputField();
662+
const form = input_field.parentElement;
663+
664+
input_field.value = getSearchTerm();
665+
form.submit();
666+
};
667+
668+
628669
window.addEventListener("DOMContentLoaded", evt => {
629670
// only add event listeners if READTHEDOCS_DATA global
630671
// variable is found.
@@ -723,19 +764,9 @@ window.addEventListener("DOMContentLoaded", evt => {
723764
const current_item = document.querySelector(
724765
".outer_div_page_results.active"
725766
);
726-
// if an item is selected,
727-
// then redirect to its link
767+
// If an item is selected, click it!
728768
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();
769+
current_item.parentElement.onclick();
739770
}
740771
}
741772
});

0 commit comments

Comments
 (0)