Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve current selected item interaction #81

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sphinx_search/static/css/rtd_sphinx_search.css
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
}

.search__result__box .active {
background-color: rgb(245, 245, 245);
background-color: #eee;
}

.search__error__box {
Expand Down Expand Up @@ -238,7 +238,7 @@
}

.outer_div_page_results:hover {
background-color: rgb(245, 245, 245);
background-color: #eee;
}

.br-for-hits {
Expand Down Expand Up @@ -273,7 +273,7 @@
}

.rtd__search__credits a {
color: black;
color: #333;
text-decoration: underline;
}

Expand Down
2 changes: 1 addition & 1 deletion sphinx_search/static/css/rtd_sphinx_search.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 49 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 @@ -519,15 +544,19 @@ const fetchAndGenerateResults = (search_url, projectName) => {
removeAllActive();
});
} else {
let err_div = getErrorDiv("No results found.");
COUNT = 1;
err_div.appendChild(getDefaultSearchLink(1));
removeResults();
let err_div = getErrorDiv("No results found");
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));
removeResults();
search_outer.appendChild(err_div);
}
});
Expand Down Expand Up @@ -558,7 +587,9 @@ const generateAndReturnInitialHtml = () => {
<span class="bar"></span> \
</div> \
<div class="rtd__search__credits"> \
Search by <a href="https://readthedocs.org/">Read the Docs</a> & <a href="https://readthedocs-sphinx-search.readthedocs.io/en/latest/">readthedocs-sphinx-search</a> \
<small> \
Search by <a href="https://readthedocs.org/">Read the Docs</a> & <a href="https://readthedocs-sphinx-search.readthedocs.io/">readthedocs-sphinx-search</a> \
<small> \
<div> \
</div>';

Expand Down Expand Up @@ -623,6 +654,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 @@ -721,19 +765,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.click();
}
}
});
Expand Down
Loading