Skip to content

Commit

Permalink
Link to default search
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Jan 20, 2021
1 parent 285f12d commit 1559619
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
61 changes: 46 additions & 15 deletions sphinx_search/static/js/rtd_sphinx_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ const generateSuggestionsList = (data, projectName) => {
search_result_single.appendChild(content);
search_result_box.appendChild(search_result_single);
}

// Extra element
let search_result_single = createDomNode("div", {
class: "search__result__single"
});
COUNT += 1;
search_result_single.appendChild(getDefaultSearchLink(COUNT));
search_result_box.appendChild(search_result_single);

return search_result_box;
};

Expand Down Expand Up @@ -468,6 +477,22 @@ const getErrorDiv = err_msg => {
return err_div;
};


const getDefaultSearchLink = (id) => {
let link = createDomNode("a", {href: "#"});
const content_template =
'<div class="outer_div_page_results" id="<%= id %>"> \
<p>Show all results.</p> \
</div>'
let content = $u.template(content_template, {id: 'hit__' + id});
link.innerHTML = content;
link.onclick = () => {
defaultSearch();
return false;
};
return link;
};

/**
* Fetch the suggestions from search backend,
* and appends the results to <div class="search__outer"> node,
Expand Down Expand Up @@ -504,12 +529,12 @@ const fetchAndGenerateResults = (search_url, projectName) => {
status_code === "success" ||
typeof resp.responseJSON !== "undefined"
) {
removeResults();
if (resp.responseJSON.results.length > 0) {
let search_result_box = generateSuggestionsList(
resp.responseJSON,
projectName
);
removeResults();
search_outer.appendChild(search_result_box);

// remove active classes from all suggestions
Expand All @@ -519,15 +544,18 @@ const fetchAndGenerateResults = (search_url, projectName) => {
removeAllActive();
});
} else {
removeResults();
let err_div = getErrorDiv("No results found");
let err_div = getErrorDiv("No results found.");
COUNT = 1;
err_div.appendChild(getDefaultSearchLink(1));
search_outer.appendChild(err_div);
}
}
},
error: (resp, status_code, error) => {
removeResults();
let err_div = getErrorDiv("There was an error. Please try again.");
COUNT = 1;
err_div.appendChild(getDefaultSearchLink(1));
search_outer.appendChild(err_div);
}
});
Expand Down Expand Up @@ -625,6 +653,19 @@ const removeSearchModal = () => {
$(".search__outer__wrapper").fadeOut(ANIMATION_TIME);
};


/**
* Search using the default form.
*/
const defaultSearch = () => {
const input_field = getInputField();
const form = input_field.parentElement;

input_field.value = getSearchTerm();
form.submit();
};


window.addEventListener("DOMContentLoaded", evt => {
// only add event listeners if READTHEDOCS_DATA global
// variable is found.
Expand Down Expand Up @@ -723,19 +764,9 @@ window.addEventListener("DOMContentLoaded", evt => {
const current_item = document.querySelector(
".outer_div_page_results.active"
);
// if an item is selected,
// then redirect to its link
// If an item is selected, click it!
if (current_item !== null) {
const link = current_item.parentElement["href"];
window.location.href = link;
} else {
// submit search form if there
// is no active item.
const input_field = getInputField();
const form = input_field.parentElement;

search_bar.value = getSearchTerm();
form.submit();
current_item.parentElement.onclick();
}
}
});
Expand Down
Loading

0 comments on commit 1559619

Please sign in to comment.