Skip to content

Commit

Permalink
Merge all popover hide functions into one
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 22, 2022
1 parent 3eb9e1a commit e4b2b41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
33 changes: 13 additions & 20 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ function getNakedUrl() {
return window.location.href.split("?")[0].split("#")[0];
}

window.hideSettings = () => {
// Does nothing by default.
};

/**
* This function inserts `newNode` after `referenceNode`. It doesn't work if `referenceNode`
* doesn't have a parent node.
Expand Down Expand Up @@ -413,8 +409,7 @@ function loadCss(cssFileName) {
}
ev.preventDefault();
searchState.defocus();
window.hideSettings();
hideHelp();
window.hidePopoverMenus();
}

const disableShortcuts = getSettingValue("disable-shortcuts") === "true";
Expand Down Expand Up @@ -824,7 +819,7 @@ function loadCss(cssFileName) {
}

function helpBlurHandler(event) {
blurHandler(event, getHelpButton(), hideHelp);
blurHandler(event, getHelpButton(), window.hidePopoverMenus);
}

function buildHelpMenu() {
Expand Down Expand Up @@ -900,6 +895,15 @@ function loadCss(cssFileName) {
return container;
}

/**
* Hide all the popover menus.
*/
window.hidePopoverMenus = function() {
onEachLazy(document.querySelectorAll(".search-container .popover"), elem => {
elem.style.display = "none";
});
};

/**
* Returns the help menu element (not the button).
*
Expand All @@ -926,25 +930,14 @@ function loadCss(cssFileName) {
}
}

/**
* Hide the help popup menu.
*/
function hideHelp() {
const menu = getHelpMenu(false);
if (menu && menu.style.display !== "none") {
menu.style.display = "none";
}
}

document.querySelector(`#${HELP_BUTTON_ID} > button`).addEventListener("click", event => {
const target = event.target;
if (target.tagName !== "BUTTON" || target.parentElement.id !== HELP_BUTTON_ID) {
return;
}
const menu = getHelpMenu(true);
if (menu.style.display !== "none") {
hideHelp();
} else {
const shouldShowHelp = menu.style.display === "none";
if (shouldShowHelp) {
showHelp();
}
});
Expand Down
12 changes: 5 additions & 7 deletions src/librustdoc/html/static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
}

function settingsBlurHandler(event) {
blurHandler(event, getSettingsButton(), window.hideSettings);
blurHandler(event, getSettingsButton(), window.hidePopoverMenus);
}

if (isSettingsPage) {
Expand All @@ -240,17 +240,15 @@
// We replace the existing "onclick" callback.
const settingsButton = getSettingsButton();
const settingsMenu = document.getElementById("settings");
window.hideSettings = function() {
settingsMenu.style.display = "none";
};
settingsButton.onclick = function(event) {
if (elemIsInParent(event.target, settingsMenu)) {
return;
}
event.preventDefault();
if (settingsMenu.style.display !== "none") {
window.hideSettings();
} else {
const shouldDisplaySettings = settingsMenu.style.display === "none";

window.hidePopoverMenus();
if (shouldDisplaySettings) {
displaySettings();
}
};
Expand Down

0 comments on commit e4b2b41

Please sign in to comment.