Skip to content

Make docs search results URL-addressable #11760

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

Merged
merged 1 commit into from
Jan 24, 2014
Merged
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
53 changes: 50 additions & 3 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@

$('.js-only').removeClass('js-only');

function getQueryStringParams() {
var params = {};
window.location.search.substring(1).split("&").
map(function(s) {
var pair = s.split("=");
params[decodeURIComponent(pair[0])] =
typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
});
return params;
}

function browserSupportsHistoryApi() {
return window.history && typeof window.history.pushState === "function";
}

function resizeShortBlocks() {
if (resizeTimeout) {
clearTimeout(resizeTimeout);
Expand Down Expand Up @@ -97,10 +112,10 @@
});

function initSearch(searchIndex) {
var currentResults, index;
var currentResults, index, params = getQueryStringParams();

// clear cached values from the search bar
$(".search-input")[0].value = '';
// Populate search bar with query string search term when provided.
$(".search-input")[0].value = params.search || '';

/**
* Executes the query and builds an index of results
Expand Down Expand Up @@ -418,6 +433,7 @@
results = [],
maxResults = 200,
resultIndex;
var params = getQueryStringParams();

query = getQuery();
if (e) {
Expand All @@ -428,6 +444,16 @@
return;
}

// Because searching is incremental by character, only the most recent search query
// is added to the browser history.
if (browserSupportsHistoryApi()) {
if (!history.state && !params.search) {
history.pushState(query, "", "?search=" + encodeURIComponent(query.query));
} else {
history.replaceState(query, "", "?search=" + encodeURIComponent(query.query));
}
}

resultIndex = execQuery(query, 20000, index);
len = resultIndex.length;
for (i = 0; i < len; i += 1) {
Expand Down Expand Up @@ -536,6 +562,27 @@
clearTimeout(keyUpTimeout);
keyUpTimeout = setTimeout(search, 100);
});
// Push and pop states are used to add search results to the browser history.
if (browserSupportsHistoryApi()) {
$(window).on('popstate', function(e) {
var params = getQueryStringParams();
// When browsing back from search results the main page visibility must be reset.
if (!params.search) {
$('#main.content').removeClass('hidden');
$('#search.content').addClass('hidden');
}
// When browsing forward to search results the previous search will be repeated,
// so the currentResults are cleared to ensure the search is successful.
currentResults = null;
// Synchronize search bar with query string state and perform the search.
$('.search-input').val(params.search);
// Some browsers fire 'onpopstate' for every page load (Chrome), while others fire the
// event only when actually popping a state (Firefox), which is why search() is called
// both here and at the end of the startSearch() function.
search();
});
}
search();
}

index = buildIndex(searchIndex);
Expand Down