Only build help popup when it's really needed

This commit is contained in:
Guillaume Gomez 2020-12-12 23:32:44 +01:00
parent b97fd3e5a1
commit 1491496eb0
1 changed files with 19 additions and 12 deletions

View File

@ -374,28 +374,35 @@ function defocusSearchBar() {
} }
} }
function getHelpElement() { function getHelpElement(build) {
if (build !== false) {
buildHelperPopup(); buildHelperPopup();
}
return document.getElementById("help"); return document.getElementById("help");
} }
function displayHelp(display, ev, help) { function displayHelp(display, ev, help) {
help = help ? help : getHelpElement();
if (display === true) { if (display === true) {
help = help ? help : getHelpElement(true);
if (hasClass(help, "hidden")) { if (hasClass(help, "hidden")) {
ev.preventDefault(); ev.preventDefault();
removeClass(help, "hidden"); removeClass(help, "hidden");
addClass(document.body, "blur"); addClass(document.body, "blur");
} }
} else if (hasClass(help, "hidden") === false) { } else {
// No need to build the help popup if we want to hide it in case it hasn't been
// built yet...
help = help ? help : getHelpElement(false);
if (help && hasClass(help, "hidden") === false) {
ev.preventDefault(); ev.preventDefault();
addClass(help, "hidden"); addClass(help, "hidden");
removeClass(document.body, "blur"); removeClass(document.body, "blur");
} }
} }
}
function handleEscape(ev) { function handleEscape(ev) {
var help = getHelpElement(); var help = getHelpElement(false);
var search = getSearchElement(); var search = getSearchElement();
if (hasClass(help, "hidden") === false) { if (hasClass(help, "hidden") === false) {
displayHelp(false, ev, help); displayHelp(false, ev, help);
@ -558,6 +565,7 @@ function defocusSearchBar() {
}()); }());
document.addEventListener("click", function(ev) { document.addEventListener("click", function(ev) {
var helpElem = getHelpElement(false);
if (hasClass(ev.target, "help-button")) { if (hasClass(ev.target, "help-button")) {
displayHelp(true, ev); displayHelp(true, ev);
} else if (hasClass(ev.target, "collapse-toggle")) { } else if (hasClass(ev.target, "collapse-toggle")) {
@ -566,11 +574,10 @@ function defocusSearchBar() {
collapseDocs(ev.target.parentNode, "toggle"); collapseDocs(ev.target.parentNode, "toggle");
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) { } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
handleSourceHighlight(ev); handleSourceHighlight(ev);
} else if (hasClass(getHelpElement(), "hidden") === false) { } else if (helpElem && hasClass(helpElem, "hidden") === false) {
var help = getHelpElement(); var is_inside_help_popup = ev.target !== helpElem && helpElem.contains(ev.target);
var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
if (is_inside_help_popup === false) { if (is_inside_help_popup === false) {
addClass(help, "hidden"); addClass(helpElem, "hidden");
removeClass(document.body, "blur"); removeClass(document.body, "blur");
} }
} else { } else {