Move crate drop-down to search results page

This reduces clutter on doc pages.
This commit is contained in:
Jacob Hoffman-Andrews 2022-01-01 23:48:34 -05:00
parent f3fe91278c
commit 8abb4bb698
6 changed files with 40 additions and 35 deletions

View file

@ -862,18 +862,24 @@ h2.small-section-header > .anchor {
display: inline-flex; display: inline-flex;
width: calc(100% - 63px); width: calc(100% - 63px);
} }
.search-results-title {
display: inline;
}
#search-settings {
font-size: 1.5rem;
font-weight: 500;
margin-bottom: 20px;
}
#crate-search { #crate-search {
min-width: 115px; min-width: 115px;
margin-top: 5px; margin-top: 5px;
padding: 6px; margin-left: 0.2em;
padding-right: 19px; padding-left: 0.3em;
flex: none; padding-right: 23px;
border: 0; border: 0;
border-right: 0; border-radius: 4px;
border-radius: 4px 0 0 4px;
outline: none; outline: none;
cursor: pointer; cursor: pointer;
border-right: 1px solid;
-moz-appearance: none; -moz-appearance: none;
-webkit-appearance: none; -webkit-appearance: none;
/* Removes default arrow from firefox */ /* Removes default arrow from firefox */

View file

@ -1085,7 +1085,7 @@ window.initSearch = function(rawSearchIndex) {
return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>"; return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
} }
function showResults(results, go_to_first) { function showResults(results, go_to_first, filterCrates) {
var search = searchState.outputElement(); var search = searchState.outputElement();
if (go_to_first || (results.others.length === 1 if (go_to_first || (results.others.length === 1
&& getSettingValue("go-to-only-result") === "true" && getSettingValue("go-to-only-result") === "true"
@ -1126,9 +1126,16 @@ window.initSearch = function(rawSearchIndex) {
} }
} }
var output = "<h1>Results for " + escape(query.query) + let crates = `<select id="crate-search"><option value="All crates">All crates</option>`;
for (let c of window.ALL_CRATES) {
crates += `<option value="${c}" ${c == filterCrates && "selected"}>${c}</option>`;
}
crates += `</select>`;
var output = `<div id="search-settings">
<h1 class="search-results-title">Results for ${escape(query.query)} ` +
(query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" + (query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
"<div id=\"titles\">" + ` in ${crates} ` +
`</div><div id="titles">` +
makeTabHeader(0, "In Names", ret_others[1]) + makeTabHeader(0, "In Names", ret_others[1]) +
makeTabHeader(1, "In Parameters", ret_in_args[1]) + makeTabHeader(1, "In Parameters", ret_in_args[1]) +
makeTabHeader(2, "In Return Types", ret_returned[1]) + makeTabHeader(2, "In Return Types", ret_returned[1]) +
@ -1141,6 +1148,7 @@ window.initSearch = function(rawSearchIndex) {
resultsElem.appendChild(ret_returned[0]); resultsElem.appendChild(ret_returned[0]);
search.innerHTML = output; search.innerHTML = output;
document.getElementById("crate-search").addEventListener("input", updateCrate);
search.appendChild(resultsElem); search.appendChild(resultsElem);
// Reset focused elements. // Reset focused elements.
searchState.focusedByTab = [null, null, null]; searchState.focusedByTab = [null, null, null];
@ -1316,7 +1324,8 @@ window.initSearch = function(rawSearchIndex) {
} }
var filterCrates = getFilterCrates(); var filterCrates = getFilterCrates();
showResults(execSearch(query, searchWords, filterCrates), params["go_to_first"]); showResults(execSearch(query, searchWords, filterCrates),
params["go_to_first"], filterCrates);
} }
function buildIndex(rawSearchIndex) { function buildIndex(rawSearchIndex) {
@ -1552,19 +1561,6 @@ window.initSearch = function(rawSearchIndex) {
} }
}); });
var selectCrate = document.getElementById("crate-search");
if (selectCrate) {
selectCrate.onchange = function() {
updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
// In case you "cut" the entry from the search input, then change the crate filter
// before paste back the previous search, you get the old search results without
// the filter. To prevent this, we need to remove the previous results.
currentResults = null;
search(undefined, true);
};
}
// Push and pop states are used to add search results to the browser // Push and pop states are used to add search results to the browser
// history. // history.
if (searchState.browserSupportsHistoryApi()) { if (searchState.browserSupportsHistoryApi()) {
@ -1616,6 +1612,15 @@ window.initSearch = function(rawSearchIndex) {
}; };
} }
function updateCrate(ev) {
updateLocalStorage("rustdoc-saved-filter-crate", ev.target.value);
// In case you "cut" the entry from the search input, then change the crate filter
// before paste back the previous search, you get the old search results without
// the filter. To prevent this, we need to remove the previous results.
currentResults = null;
search(undefined, true);
}
searchWords = buildIndex(rawSearchIndex); searchWords = buildIndex(rawSearchIndex);
registerSearchEvents(); registerSearchEvents();
// If there's a search term in the URL, execute the search now. // If there's a search term in the URL, execute the search now.

View file

@ -105,11 +105,7 @@
</div> {#- -#} </div> {#- -#}
<form class="search-form"> {#- -#} <form class="search-form"> {#- -#}
<div class="search-container"> {#- -#} <div class="search-container"> {#- -#}
<div>{%- if layout.generate_search_filter -%} <div>
<select id="crate-search"> {#- -#}
<option value="All crates">All crates</option> {#- -#}
</select> {#- -#}
{%- endif -%}
<input {# -#} <input {# -#}
class="search-input" {# -#} class="search-input" {# -#}
name="search" {# -#} name="search" {# -#}

View file

@ -1,7 +1,7 @@
goto: file://|DOC_PATH|/test_docs/index.html goto: file://|DOC_PATH|/test_docs/index.html
// First, we check that the search results are hidden when the Escape key is pressed. // First, we check that the search results are hidden when the Escape key is pressed.
write: (".search-input", "test") write: (".search-input", "test")
wait-for: "#search > h1" // The search element is empty before the first search wait-for: "#search h1" // The search element is empty before the first search
assert-attribute: ("#search", {"class": "content"}) assert-attribute: ("#search", {"class": "content"})
assert-attribute: ("#main-content", {"class": "content hidden"}) assert-attribute: ("#main-content", {"class": "content hidden"})
press-key: "Escape" press-key: "Escape"

View file

@ -5,14 +5,12 @@ write: (".search-input", "test")
wait-for: "#titles" wait-for: "#titles"
assert-text: ("#results .externcrate", "test_docs") assert-text: ("#results .externcrate", "test_docs")
goto: file://|DOC_PATH|/test_docs/index.html wait-for: "#crate-search"
// We now want to change the crate filter. // We now want to change the crate filter.
click: "#crate-search" click: "#crate-search"
// We select "lib2" option then press enter to change the filter. // We select "lib2" option then press enter to change the filter.
press-key: "ArrowDown" press-key: "ArrowDown"
press-key: "Enter" press-key: "Enter"
// We now make the search again.
write: (".search-input", "test")
// Waiting for the search results to appear... // Waiting for the search results to appear...
wait-for: "#titles" wait-for: "#titles"
// We check that there is no more "test_docs" appearing. // We check that there is no more "test_docs" appearing.

View file

@ -1,12 +1,12 @@
goto: file://|DOC_PATH|/test_docs/struct.Foo.html goto: file://|DOC_PATH|/test_docs/struct.Foo.html
size: (433, 600) size: (433, 600)
assert-attribute: (".top-doc", {"open": ""}) assert-attribute: (".top-doc", {"open": ""})
click: (4, 280) // This is the position of the top doc comment toggle click: (4, 240) // This is the position of the top doc comment toggle
assert-attribute-false: (".top-doc", {"open": ""}) assert-attribute-false: (".top-doc", {"open": ""})
click: (4, 280) click: (4, 240)
assert-attribute: (".top-doc", {"open": ""}) assert-attribute: (".top-doc", {"open": ""})
// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked. // To ensure that the toggle isn't over the text, we check that the toggle isn't clicked.
click: (3, 280) click: (3, 240)
assert-attribute: (".top-doc", {"open": ""}) assert-attribute: (".top-doc", {"open": ""})
// Assert the position of the toggle on the top doc block. // Assert the position of the toggle on the top doc block.