Skip to content

Commit

Permalink
Fix librustdoc search events
Browse files Browse the repository at this point in the history
Previously only keyup event was looked at, which meant that pasting, cutting and
otherwise changing the input without typing would not catch any updates to the
search query.
  • Loading branch information
nagisa committed Oct 2, 2015
1 parent 98841d4 commit f38bc2c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/librustdoc/html/static/main.js
Expand Up @@ -515,23 +515,20 @@
var $active = $results.filter('.highlighted');

if (e.which === 38) { // up
e.preventDefault();
if (!$active.length || !$active.prev()) {
return;
}

$active.prev().addClass('highlighted');
$active.removeClass('highlighted');
} else if (e.which === 40) { // down
e.preventDefault();
if (!$active.length) {
$results.first().addClass('highlighted');
} else if ($active.next().length) {
$active.next().addClass('highlighted');
$active.removeClass('highlighted');
}
} else if (e.which === 13) { // return
e.preventDefault();
if ($active.length) {
document.location.href = $active.find('a').prop('href');
}
Expand Down Expand Up @@ -722,20 +719,29 @@
}

function startSearch() {

$(".search-input").on("keyup",function() {
var searchTimeout;
$(".search-input").on("keyup input",function() {
clearTimeout(searchTimeout);
if ($(this).val().length === 0) {
window.history.replaceState("", "std - Rust", "?search=");
$('#main.content').removeClass('hidden');
$('#search.content').addClass('hidden');
} else {
searchTimeout = setTimeout(search, 500);
}
});

var keyUpTimeout;
$('.do-search').on('click', search);
$('.search-input').on('keyup', function() {
clearTimeout(keyUpTimeout);
keyUpTimeout = setTimeout(search, 500);
$('.search-form').on('submit', function(e){
e.preventDefault();
clearTimeout(searchTimeout);
search();
});
$('.search-input').on('change paste', function(e) {
// Do NOT e.preventDefault() here. It will prevent pasting.
clearTimeout(searchTimeout);
// zero-timeout necessary here because at the time of event handler execution the
// pasted content is not in the input field yet. Shouldn’t make any difference for
// change, though.
setTimeout(search, 0);
});

// Push and pop states are used to add search results to the browser
Expand Down

0 comments on commit f38bc2c

Please sign in to comment.