Skip to content

Commit 0414dfa

Browse files
committed
Auto merge of #62941 - GuillaumeGomez:save-crate-filter, r=Mark-Simulacrum
Save crate filtering on rustdoc Fixes #62929. I added a hashmap and a hash encoding for the current crate list in case you have multiple crates handling on a same website (who talked about docs.rs?!). Like that, for each context, you have the filter crate selected. r? @QuietMisdreavus
2 parents 17e73e8 + 06228d3 commit 0414dfa

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/librustdoc/html/static/main.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ if (!DOMTokenList.prototype.remove) {
445445
var OUTPUT_DATA = 1;
446446
var params = getQueryStringParams();
447447

448+
// Set the crate filter from saved storage, if the current page has the saved crate filter.
449+
//
450+
// If not, ignore the crate filter -- we want to support filtering for crates on sites like
451+
// doc.rust-lang.org where the crates may differ from page to page while on the same domain.
452+
var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
453+
if (savedCrate !== null) {
454+
onEachLazy(document.getElementById("crate-search").getElementsByTagName("option"),
455+
function(e) {
456+
if (e.value === savedCrate) {
457+
document.getElementById("crate-search").value = e.value;
458+
return true;
459+
}
460+
});
461+
}
462+
448463
// Populate search bar with query string search term when provided,
449464
// but only if the input bar is empty. This avoid the obnoxious issue
450465
// where you start trying to do a search, and the index loads, and
@@ -1658,9 +1673,10 @@ if (!DOMTokenList.prototype.remove) {
16581673
};
16591674
search_input.onpaste = search_input.onchange;
16601675

1661-
var selectCrate = document.getElementById('crate-search');
1676+
var selectCrate = document.getElementById("crate-search");
16621677
if (selectCrate) {
16631678
selectCrate.onchange = function() {
1679+
updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
16641680
search(undefined, true);
16651681
};
16661682
}
@@ -2496,7 +2512,7 @@ if (!DOMTokenList.prototype.remove) {
24962512
}
24972513

24982514
function addSearchOptions(crates) {
2499-
var elem = document.getElementById('crate-search');
2515+
var elem = document.getElementById("crate-search");
25002516

25012517
if (!elem) {
25022518
return;

src/librustdoc/html/static/storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function onEachLazy(lazyArray, func, reversed) {
5757

5858
function usableLocalStorage() {
5959
// Check if the browser supports localStorage at all:
60-
if (typeof(Storage) === "undefined") {
60+
if (typeof Storage === "undefined") {
6161
return false;
6262
}
6363
// Check if we can access it; this access will fail if the browser

0 commit comments

Comments
 (0)