Rollup merge of #64312 - GuillaumeGomez:rustdoc-better-esc-handling, r=Mark-Simulacrum
Unify escape usage Fixes #63443. I chose to keep the search text when pressing escape so when we focus on the search bar, we got the results again without needing to load them again. I also unified a bit a few things (maybe I should have done it in another commit, sorry...). r? @Mark-Simulacrum
This commit is contained in:
commit
f7ee13040b
1 changed files with 29 additions and 18 deletions
|
@ -39,6 +39,14 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSearchInput() {
|
||||||
|
return document.getElementsByClassName("search-input")[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSearchElement() {
|
||||||
|
return document.getElementById("search");
|
||||||
|
}
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
@ -71,7 +79,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
"derive",
|
"derive",
|
||||||
"traitalias"];
|
"traitalias"];
|
||||||
|
|
||||||
var search_input = document.getElementsByClassName("search-input")[0];
|
var search_input = getSearchInput();
|
||||||
|
|
||||||
// On the search screen, so you remain on the last tab you opened.
|
// On the search screen, so you remain on the last tab you opened.
|
||||||
//
|
//
|
||||||
|
@ -158,7 +166,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
// If we're in mobile mode, we should add the sidebar in any case.
|
// If we're in mobile mode, we should add the sidebar in any case.
|
||||||
hideSidebar();
|
hideSidebar();
|
||||||
var elem;
|
var elem;
|
||||||
var search = document.getElementById("search");
|
var search = getSearchElement();
|
||||||
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
|
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
|
||||||
if (match) {
|
if (match) {
|
||||||
from = parseInt(match[1], 10);
|
from = parseInt(match[1], 10);
|
||||||
|
@ -250,7 +258,12 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
return String.fromCharCode(c);
|
return String.fromCharCode(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getHelpElement() {
|
||||||
|
return document.getElementById("help");
|
||||||
|
}
|
||||||
|
|
||||||
function displayHelp(display, ev, help) {
|
function displayHelp(display, ev, help) {
|
||||||
|
var help = help ? help : getHelpElement();
|
||||||
if (display === true) {
|
if (display === true) {
|
||||||
if (hasClass(help, "hidden")) {
|
if (hasClass(help, "hidden")) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
|
@ -264,9 +277,10 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleEscape(ev, help) {
|
function handleEscape(ev) {
|
||||||
|
var help = getHelpElement();
|
||||||
|
var search = getSearchElement();
|
||||||
hideModal();
|
hideModal();
|
||||||
var search = document.getElementById("search");
|
|
||||||
if (hasClass(help, "hidden") === false) {
|
if (hasClass(help, "hidden") === false) {
|
||||||
displayHelp(false, ev, help);
|
displayHelp(false, ev, help);
|
||||||
} else if (hasClass(search, "hidden") === false) {
|
} else if (hasClass(search, "hidden") === false) {
|
||||||
|
@ -284,22 +298,21 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var help = document.getElementById("help");
|
|
||||||
if (document.activeElement.tagName === "INPUT") {
|
if (document.activeElement.tagName === "INPUT") {
|
||||||
switch (getVirtualKey(ev)) {
|
switch (getVirtualKey(ev)) {
|
||||||
case "Escape":
|
case "Escape":
|
||||||
handleEscape(ev, help);
|
handleEscape(ev);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch (getVirtualKey(ev)) {
|
switch (getVirtualKey(ev)) {
|
||||||
case "Escape":
|
case "Escape":
|
||||||
handleEscape(ev, help);
|
handleEscape(ev);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "s":
|
case "s":
|
||||||
case "S":
|
case "S":
|
||||||
displayHelp(false, ev, help);
|
displayHelp(false, ev);
|
||||||
hideModal();
|
hideModal();
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
focusSearchBar();
|
focusSearchBar();
|
||||||
|
@ -314,7 +327,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
case "?":
|
case "?":
|
||||||
if (ev.shiftKey) {
|
if (ev.shiftKey) {
|
||||||
hideModal();
|
hideModal();
|
||||||
displayHelp(true, ev, help);
|
displayHelp(true, ev);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1281,9 +1294,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
} else if (e.which === 16) { // shift
|
} else if (e.which === 16) { // shift
|
||||||
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
|
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
|
||||||
} else if (e.which === 27) { // escape
|
} else if (e.which === 27) { // escape
|
||||||
removeClass(actives[currentTab][0], "highlighted");
|
handleEscape(e);
|
||||||
search_input.value = "";
|
|
||||||
defocusSearchBar();
|
|
||||||
} else if (actives[currentTab].length > 0) {
|
} else if (actives[currentTab].length > 0) {
|
||||||
removeClass(actives[currentTab][0], "highlighted");
|
removeClass(actives[currentTab][0], "highlighted");
|
||||||
}
|
}
|
||||||
|
@ -1434,7 +1445,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
|
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
|
||||||
|
|
||||||
addClass(main, "hidden");
|
addClass(main, "hidden");
|
||||||
var search = document.getElementById("search");
|
var search = getSearchElement();
|
||||||
removeClass(search, "hidden");
|
removeClass(search, "hidden");
|
||||||
search.innerHTML = output;
|
search.innerHTML = output;
|
||||||
var tds = search.getElementsByTagName("td");
|
var tds = search.getElementsByTagName("td");
|
||||||
|
@ -1644,7 +1655,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
if (hasClass(main, "content")) {
|
if (hasClass(main, "content")) {
|
||||||
removeClass(main, "hidden");
|
removeClass(main, "hidden");
|
||||||
}
|
}
|
||||||
var search_c = document.getElementById("search");
|
var search_c = getSearchElement();
|
||||||
if (hasClass(search_c, "content")) {
|
if (hasClass(search_c, "content")) {
|
||||||
addClass(search_c, "hidden");
|
addClass(search_c, "hidden");
|
||||||
}
|
}
|
||||||
|
@ -1691,7 +1702,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
if (hasClass(main, "content")) {
|
if (hasClass(main, "content")) {
|
||||||
removeClass(main, "hidden");
|
removeClass(main, "hidden");
|
||||||
}
|
}
|
||||||
var search_c = document.getElementById("search");
|
var search_c = getSearchElement();
|
||||||
if (hasClass(search_c, "content")) {
|
if (hasClass(search_c, "content")) {
|
||||||
addClass(search_c, "hidden");
|
addClass(search_c, "hidden");
|
||||||
}
|
}
|
||||||
|
@ -2460,7 +2471,7 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
var params = getQueryStringParams();
|
var params = getQueryStringParams();
|
||||||
if (params && params.search) {
|
if (params && params.search) {
|
||||||
addClass(main, "hidden");
|
addClass(main, "hidden");
|
||||||
var search = document.getElementById("search");
|
var search = getSearchElement();
|
||||||
removeClass(search, "hidden");
|
removeClass(search, "hidden");
|
||||||
search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
|
search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
|
||||||
}
|
}
|
||||||
|
@ -2545,10 +2556,10 @@ if (!DOMTokenList.prototype.remove) {
|
||||||
|
|
||||||
// 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
|
||||||
function focusSearchBar() {
|
function focusSearchBar() {
|
||||||
document.getElementsByClassName("search-input")[0].focus();
|
getSearchInput().focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes the focus from the search bar
|
// Removes the focus from the search bar
|
||||||
function defocusSearchBar() {
|
function defocusSearchBar() {
|
||||||
document.getElementsByClassName("search-input")[0].blur();
|
getSearchInput().blur();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue