1
Fork 0

Add crate filtering

This commit is contained in:
Guillaume Gomez 2018-10-01 00:47:54 +02:00
parent b76ee83254
commit dd717deccb
6 changed files with 131 additions and 26 deletions

View file

@ -81,11 +81,16 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
<nav class=\"sub\">\ <nav class=\"sub\">\
<form class=\"search-form js-only\">\ <form class=\"search-form js-only\">\
<div class=\"search-container\">\ <div class=\"search-container\">\
<input class=\"search-input\" name=\"search\" \ <div>\
autocomplete=\"off\" \ <select id=\"crate-search\">\
spellcheck=\"false\" \ <option value=\"All crates\">All crates</option>\
placeholder=\"Click or press S to search, ? for more options…\" \ </select>\
type=\"search\">\ <input class=\"search-input\" name=\"search\" \
autocomplete=\"off\" \
spellcheck=\"false\" \
placeholder=\"Click or press S to search, ? for more options…\" \
type=\"search\">\
</div>\
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\ <a id=\"settings-menu\" href=\"{root_path}settings.html\">\
<img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\ <img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
</a>\ </a>\

View file

@ -983,7 +983,7 @@ themePicker.onblur = handleThemeButtonsBlur;
&[(minifier::js::Keyword::Null, "N")]), &[(minifier::js::Keyword::Null, "N")]),
&dst); &dst);
} }
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst); try_err!(writeln!(&mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
if options.enable_index_page { if options.enable_index_page {
if let Some(index_page) = options.index_page.clone() { if let Some(index_page) = options.index_page.clone() {

View file

@ -254,12 +254,14 @@
// //
// So I guess you could say things are getting pretty interoperable. // So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) { function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined") if ("key" in ev && typeof ev.key != "undefined") {
return ev.key; return ev.key;
}
var c = ev.charCode || ev.keyCode; var c = ev.charCode || ev.keyCode;
if (c == 27) if (c == 27) {
return "Escape"; return "Escape";
}
return String.fromCharCode(c); return String.fromCharCode(c);
} }
@ -467,12 +469,13 @@
/** /**
* Executes the query and builds an index of results * Executes the query and builds an index of results
* @param {[Object]} query [The user query] * @param {[Object]} query [The user query]
* @param {[type]} searchWords [The list of search words to query * @param {[type]} searchWords [The list of search words to query
* against] * against]
* @return {[type]} [A search index of results] * @param {[type]} filterCrates [Crate to search in if defined]
* @return {[type]} [A search index of results]
*/ */
function execQuery(query, searchWords) { function execQuery(query, searchWords, filterCrates) {
function itemTypeFromName(typename) { function itemTypeFromName(typename) {
for (var i = 0; i < itemTypes.length; ++i) { for (var i = 0; i < itemTypes.length; ++i) {
if (itemTypes[i] === typename) { if (itemTypes[i] === typename) {
@ -848,6 +851,9 @@
{ {
val = extractGenerics(val.substr(1, val.length - 2)); val = extractGenerics(val.substr(1, val.length - 2));
for (var i = 0; i < nSearchWords; ++i) { for (var i = 0; i < nSearchWords; ++i) {
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
continue;
}
var in_args = findArg(searchIndex[i], val, true); var in_args = findArg(searchIndex[i], val, true);
var returned = checkReturned(searchIndex[i], val, true); var returned = checkReturned(searchIndex[i], val, true);
var ty = searchIndex[i]; var ty = searchIndex[i];
@ -902,6 +908,9 @@
var output = extractGenerics(parts[1]); var output = extractGenerics(parts[1]);
for (var i = 0; i < nSearchWords; ++i) { for (var i = 0; i < nSearchWords; ++i) {
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
continue;
}
var type = searchIndex[i].type; var type = searchIndex[i].type;
var ty = searchIndex[i]; var ty = searchIndex[i];
if (!type) { if (!type) {
@ -973,11 +982,11 @@
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1); var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
for (j = 0; j < nSearchWords; ++j) { for (j = 0; j < nSearchWords; ++j) {
var lev_distance;
var ty = searchIndex[j]; var ty = searchIndex[j];
if (!ty) { if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
continue; continue;
} }
var lev_distance;
var lev_add = 0; var lev_add = 0;
if (paths.length > 1) { if (paths.length > 1) {
var lev = checkPath(contains, paths[paths.length - 1], ty); var lev = checkPath(contains, paths[paths.length - 1], ty);
@ -1353,7 +1362,7 @@
return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>'; return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>';
} }
function showResults(results) { function showResults(results, filterCrates) {
if (results['others'].length === 1 && if (results['others'].length === 1 &&
getCurrentValue('rustdoc-go-to-only-result') === "true") { getCurrentValue('rustdoc-go-to-only-result') === "true") {
var elem = document.createElement('a'); var elem = document.createElement('a');
@ -1371,8 +1380,13 @@
var ret_in_args = addTab(results['in_args'], query, false); var ret_in_args = addTab(results['in_args'], query, false);
var ret_returned = addTab(results['returned'], query, false); var ret_returned = addTab(results['returned'], query, false);
var filter = "";
if (filterCrates !== undefined) {
filter = " (in <b>" + filterCrates + "</b> crate)";
}
var output = '<h1>Results for ' + escape(query.query) + var output = '<h1>Results for ' + escape(query.query) +
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' + (query.type ? ' (type: ' + escape(query.type) + ')' : '') + filter + '</h1>' +
'<div id="titles">' + '<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]) +
@ -1401,7 +1415,7 @@
printTab(currentTab); printTab(currentTab);
} }
function execSearch(query, searchWords) { function execSearch(query, searchWords, filterCrates) {
var queries = query.raw.split(","); var queries = query.raw.split(",");
var results = { var results = {
'in_args': [], 'in_args': [],
@ -1412,7 +1426,7 @@
for (var i = 0; i < queries.length; ++i) { for (var i = 0; i < queries.length; ++i) {
var query = queries[i].trim(); var query = queries[i].trim();
if (query.length !== 0) { if (query.length !== 0) {
var tmp = execQuery(getQuery(query), searchWords); var tmp = execQuery(getQuery(query), searchWords, filterCrates);
results['in_args'].push(tmp['in_args']); results['in_args'].push(tmp['in_args']);
results['returned'].push(tmp['returned']); results['returned'].push(tmp['returned']);
@ -1474,7 +1488,16 @@
} }
} }
function search(e) { function getFilterCrates() {
var elem = document.getElementById("crate-search");
if (elem && elem.value !== "All crates" && rawSearchIndex.hasOwnProperty(elem.value)) {
return elem.value;
}
return undefined;
}
function search(e, forced) {
var params = getQueryStringParams(); var params = getQueryStringParams();
var query = getQuery(search_input.value.trim()); var query = getQuery(search_input.value.trim());
@ -1482,7 +1505,10 @@
e.preventDefault(); e.preventDefault();
} }
if (query.query.length === 0 || query.id === currentResults) { if (query.query.length === 0) {
return;
}
if (forced !== true && query.id === currentResults) {
if (query.query.length > 0) { if (query.query.length > 0) {
putBackSearch(search_input); putBackSearch(search_input);
} }
@ -1502,7 +1528,8 @@
} }
} }
showResults(execSearch(query, index)); var filterCrates = getFilterCrates();
showResults(execSearch(query, index, filterCrates), filterCrates);
} }
function buildIndex(rawSearchIndex) { function buildIndex(rawSearchIndex) {
@ -1602,6 +1629,13 @@
}; };
search_input.onpaste = search_input.onchange; search_input.onpaste = search_input.onchange;
var selectCrate = document.getElementById('crate-search');
if (selectCrate) {
selectCrate.onchange = function() {
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 (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
@ -2350,6 +2384,39 @@
if (window.location.hash && window.location.hash.length > 0) { if (window.location.hash && window.location.hash.length > 0) {
expandSection(window.location.hash.replace(/^#/, '')); expandSection(window.location.hash.replace(/^#/, ''));
} }
function addSearchOptions(crates) {
var elem = document.getElementById('crate-search');
if (!elem) {
return;
}
var crates_text = [];
for (var crate in crates) {
if (crates.hasOwnProperty(crate)) {
crates_text.push(crate);
}
}
crates_text.sort(function(a, b) {
var lower_a = a.toLowerCase();
var lower_b = b.toLowerCase();
if (lower_a < lower_b) {
return -1;
} else if (lower_a > lower_b) {
return 1;
}
return 0;
});
for (var i = 0; i < crates_text.length; ++i) {
var option = document.createElement("option");
option.value = crates_text[i];
option.innerText = crates_text[i];
elem.appendChild(option);
}
}
window.addSearchOptions = addSearchOptions;
}()); }());
// Sets the focus on the search bar at the top of the page // Sets the focus on the search bar at the top of the page

View file

@ -614,13 +614,32 @@ a {
.search-container { .search-container {
position: relative; position: relative;
} }
.search-container > div {
display: inline-flex;
width: calc(100% - 34px);
}
#crate-search {
margin-top: 5px;
padding: 6px;
padding-right: 12px;
border: 0;
border-right: 0;
border-radius: 4px 0 0 4px;
outline: none;
cursor: pointer;
border-right: 1px solid;
-moz-appearance: none;
-webkit-appearance: none;
/* Removes default arrow from firefox */
text-indent: 0.01px;
text-overflow: "";
}
.search-container > .top-button { .search-container > .top-button {
position: absolute; position: absolute;
right: 0; right: 0;
top: 10px; top: 10px;
} }
.search-input { .search-input {
width: calc(100% - 34px);
/* Override Normalize.css: we have margins and do /* Override Normalize.css: we have margins and do
not want to overflow - the `moz` attribute is necessary not want to overflow - the `moz` attribute is necessary
until Firefox 29, too early to drop at this point */ until Firefox 29, too early to drop at this point */
@ -628,13 +647,14 @@ a {
box-sizing: border-box !important; box-sizing: border-box !important;
outline: none; outline: none;
border: none; border: none;
border-radius: 1px; border-radius: 0 1px 1px 0;
margin-top: 5px; margin-top: 5px;
padding: 10px 16px; padding: 10px 16px;
font-size: 17px; font-size: 17px;
transition: border-color 300ms ease; transition: border-color 300ms ease;
transition: border-radius 300ms ease-in-out; transition: border-radius 300ms ease-in-out;
transition: box-shadow 300ms ease-in-out; transition: box-shadow 300ms ease-in-out;
width: 100%;
} }
.search-input:focus { .search-input:focus {

View file

@ -182,9 +182,15 @@ a.test-arrow {
color: #999; color: #999;
} }
#crate-search {
color: #111;
background-color: #f0f0f0;
border-color: #000;
}
.search-input { .search-input {
color: #111; color: #111;
box-shadow: 0 0 0 1px #000, 0 0 0 2px transparent; box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
background-color: #f0f0f0; background-color: #f0f0f0;
} }

View file

@ -182,9 +182,16 @@ a.test-arrow {
color: #999; color: #999;
} }
#crate-search {
color: #555;
background-color: white;
border-color: #e0e0e0;
box-shadow: 0px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
}
.search-input { .search-input {
color: #555; color: #555;
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent; box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
background-color: white; background-color: white;
} }