1
Fork 0

librustdoc: generalise handling of keyboard shortcuts

This commit is contained in:
Andreas Tolfsen 2015-07-07 15:15:22 +01:00
parent fd8e175c4e
commit 8792c5c040

View file

@ -76,62 +76,65 @@
highlightSourceLines(null); highlightSourceLines(null);
$(window).on('hashchange', highlightSourceLines); $(window).on('hashchange', highlightSourceLines);
// Helper function for Keyboard events, // Gets the human-readable string for the virtual-key code of the
// Get's the char from the keypress event // given KeyboardEvent, ev.
// //
// This method is used because e.wich === x is not // This function is meant as a polyfill for KeyboardEvent#key,
// compatible with non-english keyboard layouts // since it is not supported in Trident. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
// //
// Note: event.type must be keypress ! // So I guess you could say things are getting pretty interoperable.
function getChar(event) { function getVirtualKey(ev) {
if (event.which == null) { if ("key" in ev && typeof ev.key != "undefined")
return String.fromCharCode(event.keyCode) // IE return ev.key;
} else if (event.which!=0 && event.charCode!=0) {
return String.fromCharCode(event.which) // the rest var c = ev.charCode || ev.keyCode;
} else { if (c == 27)
return null // special key return "Escape";
} return String.fromCharCode(c);
} }
$(document).on('keypress', function handleKeyboardShortcut(e) { function handleShortcut(ev) {
if (document.activeElement.tagName === 'INPUT') { if (document.activeElement.tagName == "INPUT")
return; return;
}
if (getChar(e) === '?') { switch (getVirtualKey(ev)) {
if (e.shiftKey && $('#help').hasClass('hidden')) { case "Escape":
e.preventDefault(); if (!$("#help").hasClass("hidden")) {
$('#help').removeClass('hidden'); ev.preventDefault();
$("#help").addClass("hidden");
} else if (!$("#search").hasClass("hidden")) {
ev.preventDefault();
$("#search").addClass("hidden");
$("#main").removeClass("hidden");
} }
} else if (getChar(e) === 's' || getChar(e) === 'S') { break;
e.preventDefault();
$('.search-input').focus();
}
}).on('keydown', function(e) {
// The escape key event has to be captured with the keydown event.
// Because keypressed has no keycode for the escape key
// (and other special keys in general)...
if (document.activeElement.tagName === 'INPUT') {
return;
}
if (e.keyCode === 27) { // escape key case "s":
if (!$('#help').hasClass('hidden')) { case "S":
e.preventDefault(); ev.preventDefault();
$('#help').addClass('hidden'); $(".search-input").focus();
} else if (!$('#search').hasClass('hidden')) { break;
e.preventDefault();
$('#search').addClass('hidden'); case "?":
$('#main').removeClass('hidden'); if (ev.shiftKey && $("#help").hasClass("hidden")) {
ev.preventDefault();
$("#help").removeClass("hidden");
} }
break;
} }
}).on('click', function(e) { }
if (!$(e.target).closest('#help').length) {
$('#help').addClass('hidden'); $(document).on("keypress", handleShortcut);
$(document).on("keydown", handleShortcut);
$(document).on("click", function(ev) {
if (!$(ev.target).closest("#help").length) {
$("#help").addClass("hidden");
} }
}); });
$('.version-selector').on('change', function() { $('.version-selector').on('change', function() {
var i, match, var i, match,
url = document.location.href, url = document.location.href,
@ -150,6 +153,7 @@
document.location.href = url; document.location.href = url;
}); });
/** /**
* A function to compute the Levenshtein distance between two strings * A function to compute the Levenshtein distance between two strings
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported