1 // Local js definitions:
2 /* global addClass, getSettingValue, hasClass, searchState */
3 /* global onEach, onEachLazy, removeClass */
7 // Get a value from the rustdoc-vars div, which is used to convey data from
8 // Rust to the JS. If there is no such element, return null.
9 function getVar(name) {
10 const el = document.getElementById("rustdoc-vars");
12 return el.attributes["data-" + name].value;
18 // Given a basename (e.g. "storage") and an extension (e.g. ".js"), return a URL
19 // for a resource under the root-path, with the resource-suffix.
20 function resourcePath(basename, extension) {
21 return getVar("root-path") + basename + getVar("resource-suffix") + extension;
25 addClass(document.getElementById(MAIN_ID), "hidden");
29 removeClass(document.getElementById(MAIN_ID), "hidden");
32 function elemIsInParent(elem, parent) {
33 while (elem && elem !== document.body) {
34 if (elem === parent) {
37 elem = elem.parentElement;
42 function blurHandler(event, parentElem, hideCallback) {
43 if (!elemIsInParent(document.activeElement, parentElem) &&
44 !elemIsInParent(event.relatedTarget, parentElem)
50 window.rootPath = getVar("root-path");
51 window.currentCrate = getVar("current-crate");
53 function setMobileTopbar() {
54 // FIXME: It would be nicer to generate this text content directly in HTML,
55 // but with the current code it's hard to get the right information in the right place.
56 const mobileLocationTitle = document.querySelector(".mobile-topbar h2");
57 const locationTitle = document.querySelector(".sidebar h2.location");
58 if (mobileLocationTitle && locationTitle) {
59 mobileLocationTitle.innerHTML = locationTitle.innerHTML;
63 // Gets the human-readable string for the virtual-key code of the
64 // given KeyboardEvent, ev.
66 // This function is meant as a polyfill for KeyboardEvent#key,
67 // since it is not supported in IE 11 or Chrome for Android. We also test for
68 // KeyboardEvent#keyCode because the handleShortcut handler is
69 // also registered for the keydown event, because Blink doesn't fire
70 // keypress on hitting the Escape key.
72 // So I guess you could say things are getting pretty interoperable.
73 function getVirtualKey(ev) {
74 if ("key" in ev && typeof ev.key !== "undefined") {
78 const c = ev.charCode || ev.keyCode;
82 return String.fromCharCode(c);
85 const MAIN_ID = "main-content";
86 const SETTINGS_BUTTON_ID = "settings-menu";
87 const ALTERNATIVE_DISPLAY_ID = "alternative-display";
88 const NOT_DISPLAYED_ID = "not-displayed";
89 const HELP_BUTTON_ID = "help-button";
91 function getSettingsButton() {
92 return document.getElementById(SETTINGS_BUTTON_ID);
95 function getHelpButton() {
96 return document.getElementById(HELP_BUTTON_ID);
99 // Returns the current URL without any query parameter or hash.
100 function getNakedUrl() {
101 return window.location.href.split("?")[0].split("#")[0];
105 * This function inserts `newNode` after `referenceNode`. It doesn't work if `referenceNode`
106 * doesn't have a parent node.
108 * @param {HTMLElement} newNode
109 * @param {HTMLElement} referenceNode
111 function insertAfter(newNode, referenceNode) {
112 referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
116 * This function creates a new `<section>` with the given `id` and `classes` if it doesn't already
119 * More information about this in `switchDisplayedElement` documentation.
122 * @param {string} classes
124 function getOrCreateSection(id, classes) {
125 let el = document.getElementById(id);
128 el = document.createElement("section");
130 el.className = classes;
131 insertAfter(el, document.getElementById(MAIN_ID));
137 * Returns the `<section>` element which contains the displayed element.
139 * @return {HTMLElement}
141 function getAlternativeDisplayElem() {
142 return getOrCreateSection(ALTERNATIVE_DISPLAY_ID, "content hidden");
146 * Returns the `<section>` element which contains the not-displayed elements.
148 * @return {HTMLElement}
150 function getNotDisplayedElem() {
151 return getOrCreateSection(NOT_DISPLAYED_ID, "hidden");
155 * To nicely switch between displayed "extra" elements (such as search results or settings menu)
156 * and to alternate between the displayed and not displayed elements, we hold them in two different
157 * `<section>` elements. They work in pair: one holds the hidden elements while the other
158 * contains the displayed element (there can be only one at the same time!). So basically, we switch
159 * elements between the two `<section>` elements.
161 * @param {HTMLElement} elemToDisplay
163 function switchDisplayedElement(elemToDisplay) {
164 const el = getAlternativeDisplayElem();
166 if (el.children.length > 0) {
167 getNotDisplayedElem().appendChild(el.firstElementChild);
169 if (elemToDisplay === null) {
170 addClass(el, "hidden");
174 el.appendChild(elemToDisplay);
176 removeClass(el, "hidden");
179 function browserSupportsHistoryApi() {
180 return window.history && typeof window.history.pushState === "function";
183 // eslint-disable-next-line no-unused-vars
184 function loadCss(cssUrl) {
185 const link = document.createElement("link");
187 link.rel = "stylesheet";
188 document.getElementsByTagName("head")[0].appendChild(link);
192 const isHelpPage = window.location.pathname.endsWith("/help.html");
194 function loadScript(url) {
195 const script = document.createElement("script");
197 document.head.append(script);
200 getSettingsButton().onclick = event => {
201 if (event.ctrlKey || event.altKey || event.metaKey) {
204 window.hideAllModals(false);
205 addClass(getSettingsButton(), "rotate");
206 event.preventDefault();
207 // Sending request for the CSS and the JS files at the same time so it will
208 // hopefully be loaded when the JS will generate the settings content.
209 loadCss(getVar("static-root-path") + getVar("settings-css"));
210 loadScript(getVar("static-root-path") + getVar("settings-js"));
213 window.searchState = {
214 loadingText: "Loading search results...",
215 input: document.getElementsByClassName("search-input")[0],
216 outputElement: () => {
217 let el = document.getElementById("search");
219 el = document.createElement("section");
221 getNotDisplayedElem().appendChild(el);
225 title: document.title,
226 titleBeforeSearch: document.title,
228 // On the search screen, so you remain on the last tab you opened.
231 // 1 for "In Parameters"
232 // 2 for "In Return Types"
234 // tab and back preserves the element that was focused.
235 focusedByTab: [null, null, null],
236 clearInputTimeout: () => {
237 if (searchState.timeout !== null) {
238 clearTimeout(searchState.timeout);
239 searchState.timeout = null;
242 isDisplayed: () => searchState.outputElement().parentElement.id === ALTERNATIVE_DISPLAY_ID,
243 // Sets the focus on the search bar at the top of the page
245 searchState.input.focus();
247 // Removes the focus from the search bar.
249 searchState.input.blur();
251 showResults: search => {
252 if (search === null || typeof search === "undefined") {
253 search = searchState.outputElement();
255 switchDisplayedElement(search);
256 searchState.mouseMovedAfterSearch = false;
257 document.title = searchState.title;
260 switchDisplayedElement(null);
261 document.title = searchState.titleBeforeSearch;
262 // We also remove the query parameter from the URL.
263 if (browserSupportsHistoryApi()) {
264 history.replaceState(null, window.currentCrate + " - Rust",
265 getNakedUrl() + window.location.hash);
268 getQueryStringParams: () => {
270 window.location.search.substring(1).split("&").
272 const pair = s.split("=");
273 params[decodeURIComponent(pair[0])] =
274 typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
279 const search_input = searchState.input;
280 if (!searchState.input) {
283 let searchLoaded = false;
284 function loadSearch() {
287 loadScript(getVar("static-root-path") + getVar("search-js"));
288 loadScript(resourcePath("search-index", ".js"));
292 search_input.addEventListener("focus", () => {
293 search_input.origPlaceholder = search_input.placeholder;
294 search_input.placeholder = "Type your search here.";
298 if (search_input.value !== "") {
302 const params = searchState.getQueryStringParams();
303 if (params.search !== undefined) {
304 searchState.setLoadingSearch();
308 setLoadingSearch: () => {
309 const search = searchState.outputElement();
310 search.innerHTML = "<h3 class=\"search-loading\">" + searchState.loadingText + "</h3>";
311 searchState.showResults(search);
315 function getPageId() {
316 if (window.location.hash) {
317 const tmp = window.location.hash.replace(/^#/, "");
318 if (tmp.length > 0) {
325 const toggleAllDocsId = "toggle-all-docs";
328 function handleHashes(ev) {
329 if (ev !== null && searchState.isDisplayed() && ev.newURL) {
330 // This block occurs when clicking on an element in the navbar while
332 switchDisplayedElement(null);
333 const hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
334 if (browserSupportsHistoryApi()) {
335 // `window.location.search`` contains all the query parameters, not just `search`.
336 history.replaceState(null, "",
337 getNakedUrl() + window.location.search + "#" + hash);
339 const elem = document.getElementById(hash);
341 elem.scrollIntoView();
344 // This part is used in case an element is not visible.
345 if (savedHash !== window.location.hash) {
346 savedHash = window.location.hash;
347 if (savedHash.length === 0) {
350 expandSection(savedHash.slice(1)); // we remove the '#'
354 function onHashChange(ev) {
355 // If we're in mobile mode, we should hide the sidebar in any case.
360 function openParentDetails(elem) {
362 if (elem.tagName === "DETAILS") {
365 elem = elem.parentNode;
369 function expandSection(id) {
370 openParentDetails(document.getElementById(id));
373 function handleEscape(ev) {
374 searchState.clearInputTimeout();
375 switchDisplayedElement(null);
376 if (browserSupportsHistoryApi()) {
377 history.replaceState(null, window.currentCrate + " - Rust",
378 getNakedUrl() + window.location.hash);
381 searchState.defocus();
382 window.hideAllModals(true); // true = reset focus for notable traits
385 function handleShortcut(ev) {
386 // Don't interfere with browser shortcuts
387 const disableShortcuts = getSettingValue("disable-shortcuts") === "true";
388 if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts) {
392 if (document.activeElement.tagName === "INPUT" &&
393 document.activeElement.type !== "checkbox") {
394 switch (getVirtualKey(ev)) {
400 switch (getVirtualKey(ev)) {
430 document.addEventListener("keypress", handleShortcut);
431 document.addEventListener("keydown", handleShortcut);
433 function addSidebarItems() {
434 if (!window.SIDEBAR_ITEMS) {
437 const sidebar = document.getElementsByClassName("sidebar-elems")[0];
440 * Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
442 * @param {string} shortty - A short type name, like "primitive", "mod", or "macro"
443 * @param {string} id - The HTML id of the corresponding section on the module page.
444 * @param {string} longty - A long, capitalized, plural name, like "Primitive Types",
445 * "Modules", or "Macros".
447 function block(shortty, id, longty) {
448 const filtered = window.SIDEBAR_ITEMS[shortty];
453 const h3 = document.createElement("h3");
454 h3.innerHTML = `<a href="index.html#${id}">${longty}</a>`;
455 const ul = document.createElement("ul");
456 ul.className = "block " + shortty;
458 for (const item of filtered) {
459 const name = item[0];
460 const desc = item[1]; // can be null
463 if (shortty === "mod") {
464 path = name + "/index.html";
466 path = shortty + "." + name + ".html";
468 const current_page = document.location.href.split("/").pop();
469 const link = document.createElement("a");
472 if (path === current_page) {
473 link.className = "current";
475 link.textContent = name;
476 const li = document.createElement("li");
477 li.appendChild(link);
480 sidebar.appendChild(h3);
481 sidebar.appendChild(ul);
485 block("primitive", "primitives", "Primitive Types");
486 block("mod", "modules", "Modules");
487 block("macro", "macros", "Macros");
488 block("struct", "structs", "Structs");
489 block("enum", "enums", "Enums");
490 block("union", "unions", "Unions");
491 block("constant", "constants", "Constants");
492 block("static", "static", "Statics");
493 block("trait", "traits", "Traits");
494 block("fn", "functions", "Functions");
495 block("type", "types", "Type Definitions");
496 block("foreigntype", "foreign-types", "Foreign Types");
497 block("keyword", "keywords", "Keywords");
498 block("traitalias", "trait-aliases", "Trait Aliases");
502 window.register_implementors = imp => {
503 const implementors = document.getElementById("implementors-list");
504 const synthetic_implementors = document.getElementById("synthetic-implementors-list");
505 const inlined_types = new Set();
508 const SYNTHETIC_IDX = 1;
511 if (synthetic_implementors) {
512 // This `inlined_types` variable is used to avoid having the same implementation
513 // showing up twice. For example "String" in the "Sync" doc page.
515 // By the way, this is only used by and useful for traits implemented automatically
516 // (like "Send" and "Sync").
517 onEachLazy(synthetic_implementors.getElementsByClassName("impl"), el => {
518 const aliases = el.getAttribute("data-aliases");
522 aliases.split(",").forEach(alias => {
523 inlined_types.add(alias);
528 let currentNbImpls = implementors.getElementsByClassName("impl").length;
529 const traitName = document.querySelector("h1.fqn > .trait").textContent;
530 const baseIdName = "impl-" + traitName + "-";
531 const libs = Object.getOwnPropertyNames(imp);
532 // We don't want to include impls from this JS file, when the HTML already has them.
533 // The current crate should always be ignored. Other crates that should also be
534 // ignored are included in the attribute `data-ignore-extern-crates`.
535 const script = document
536 .querySelector("script[data-ignore-extern-crates]");
537 const ignoreExternCrates = script ? script.getAttribute("data-ignore-extern-crates") : "";
538 for (const lib of libs) {
539 if (lib === window.currentCrate || ignoreExternCrates.indexOf(lib) !== -1) {
542 const structs = imp[lib];
545 for (const struct of structs) {
546 const list = struct[SYNTHETIC_IDX] ? synthetic_implementors : implementors;
548 // The types list is only used for synthetic impls.
549 // If this changes, `main.js` and `write_shared.rs` both need changed.
550 if (struct[SYNTHETIC_IDX]) {
551 for (const struct_type of struct[TYPES_IDX]) {
552 if (inlined_types.has(struct_type)) {
553 continue struct_loop;
555 inlined_types.add(struct_type);
559 const code = document.createElement("h3");
560 code.innerHTML = struct[TEXT_IDX];
561 addClass(code, "code-header");
563 onEachLazy(code.getElementsByTagName("a"), elem => {
564 const href = elem.getAttribute("href");
566 if (href && !/^(?:[a-z+]+:)?\/\//.test(href)) {
567 elem.setAttribute("href", window.rootPath + href);
571 const currentId = baseIdName + currentNbImpls;
572 const anchor = document.createElement("a");
573 anchor.href = "#" + currentId;
574 addClass(anchor, "anchor");
576 const display = document.createElement("div");
577 display.id = currentId;
578 addClass(display, "impl");
579 display.appendChild(anchor);
580 display.appendChild(code);
581 list.appendChild(display);
586 if (window.pending_implementors) {
587 window.register_implementors(window.pending_implementors);
590 function addSidebarCrates() {
591 if (!window.ALL_CRATES) {
594 const sidebarElems = document.getElementsByClassName("sidebar-elems")[0];
598 // Draw a convenient sidebar of known crates if we have a listing
599 const h3 = document.createElement("h3");
600 h3.innerHTML = "Crates";
601 const ul = document.createElement("ul");
602 ul.className = "block crate";
604 for (const crate of window.ALL_CRATES) {
605 const link = document.createElement("a");
606 link.href = window.rootPath + crate + "/index.html";
607 if (window.rootPath !== "./" && crate === window.currentCrate) {
608 link.className = "current";
610 link.textContent = crate;
612 const li = document.createElement("li");
613 li.appendChild(link);
616 sidebarElems.appendChild(h3);
617 sidebarElems.appendChild(ul);
620 function expandAllDocs() {
621 const innerToggle = document.getElementById(toggleAllDocsId);
622 removeClass(innerToggle, "will-expand");
623 onEachLazy(document.getElementsByClassName("toggle"), e => {
624 if (!hasClass(e, "type-contents-toggle") && !hasClass(e, "more-examples-toggle")) {
628 innerToggle.title = "collapse all docs";
629 innerToggle.children[0].innerText = "\u2212"; // "\u2212" is "−" minus sign
632 function collapseAllDocs() {
633 const innerToggle = document.getElementById(toggleAllDocsId);
634 addClass(innerToggle, "will-expand");
635 onEachLazy(document.getElementsByClassName("toggle"), e => {
636 if (e.parentNode.id !== "implementations-list" ||
637 (!hasClass(e, "implementors-toggle") &&
638 !hasClass(e, "type-contents-toggle"))
643 innerToggle.title = "expand all docs";
644 innerToggle.children[0].innerText = "+";
647 function toggleAllDocs() {
648 const innerToggle = document.getElementById(toggleAllDocsId);
652 if (hasClass(innerToggle, "will-expand")) {
660 const toggles = document.getElementById(toggleAllDocsId);
662 toggles.onclick = toggleAllDocs;
665 const hideMethodDocs = getSettingValue("auto-hide-method-docs") === "true";
666 const hideImplementations = getSettingValue("auto-hide-trait-implementations") === "true";
667 const hideLargeItemContents = getSettingValue("auto-hide-large-items") !== "false";
669 function setImplementorsTogglesOpen(id, open) {
670 const list = document.getElementById(id);
672 onEachLazy(list.getElementsByClassName("implementors-toggle"), e => {
678 if (hideImplementations) {
679 setImplementorsTogglesOpen("trait-implementations-list", false);
680 setImplementorsTogglesOpen("blanket-implementations-list", false);
683 onEachLazy(document.getElementsByClassName("toggle"), e => {
684 if (!hideLargeItemContents && hasClass(e, "type-contents-toggle")) {
687 if (hideMethodDocs && hasClass(e, "method-toggle")) {
693 const pageId = getPageId();
694 if (pageId !== null) {
695 expandSection(pageId);
699 window.rustdoc_add_line_numbers_to_examples = () => {
700 onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
701 const parent = x.parentNode;
702 const line_numbers = parent.querySelectorAll(".example-line-numbers");
703 if (line_numbers.length > 0) {
706 const count = x.textContent.split("\n").length;
708 for (let i = 0; i < count; ++i) {
711 const node = document.createElement("pre");
712 addClass(node, "example-line-numbers");
713 node.innerHTML = elems.join("\n");
714 parent.insertBefore(node, x);
718 window.rustdoc_remove_line_numbers_from_examples = () => {
719 onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
720 const parent = x.parentNode;
721 const line_numbers = parent.querySelectorAll(".example-line-numbers");
722 for (const node of line_numbers) {
723 parent.removeChild(node);
728 if (getSettingValue("line-numbers") === "true") {
729 window.rustdoc_add_line_numbers_to_examples();
732 let oldSidebarScrollPosition = null;
734 // Scroll locking used both here and in source-script.js
736 window.rustdocMobileScrollLock = function() {
737 const mobile_topbar = document.querySelector(".mobile-topbar");
738 if (window.innerWidth <= window.RUSTDOC_MOBILE_BREAKPOINT) {
739 // This is to keep the scroll position on mobile.
740 oldSidebarScrollPosition = window.scrollY;
741 document.body.style.width = `${document.body.offsetWidth}px`;
742 document.body.style.position = "fixed";
743 document.body.style.top = `-${oldSidebarScrollPosition}px`;
745 mobile_topbar.style.top = `${oldSidebarScrollPosition}px`;
746 mobile_topbar.style.position = "relative";
749 oldSidebarScrollPosition = null;
753 window.rustdocMobileScrollUnlock = function() {
754 const mobile_topbar = document.querySelector(".mobile-topbar");
755 if (oldSidebarScrollPosition !== null) {
756 // This is to keep the scroll position on mobile.
757 document.body.style.width = "";
758 document.body.style.position = "";
759 document.body.style.top = "";
761 mobile_topbar.style.top = "";
762 mobile_topbar.style.position = "";
764 // The scroll position is lost when resetting the style, hence why we store it in
765 // `oldSidebarScrollPosition`.
766 window.scrollTo(0, oldSidebarScrollPosition);
767 oldSidebarScrollPosition = null;
771 function showSidebar() {
772 window.hideAllModals(false);
773 window.rustdocMobileScrollLock();
774 const sidebar = document.getElementsByClassName("sidebar")[0];
775 addClass(sidebar, "shown");
778 function hideSidebar() {
779 window.rustdocMobileScrollUnlock();
780 const sidebar = document.getElementsByClassName("sidebar")[0];
781 removeClass(sidebar, "shown");
784 window.addEventListener("resize", () => {
785 if (window.innerWidth > window.RUSTDOC_MOBILE_BREAKPOINT &&
786 oldSidebarScrollPosition !== null) {
787 // If the user opens the sidebar in "mobile" mode, and then grows the browser window,
788 // we need to switch away from mobile mode and make the main content area scrollable.
791 if (window.CURRENT_NOTABLE_ELEMENT) {
792 // As a workaround to the behavior of `contains: layout` used in doc togglers, the
793 // notable traits popup is positioned using javascript.
795 // This means when the window is resized, we need to redo the layout.
796 const base = window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE;
797 const force_visible = base.NOTABLE_FORCE_VISIBLE;
801 base.NOTABLE_FORCE_VISIBLE = true;
806 function handleClick(id, f) {
807 const elem = document.getElementById(id);
809 elem.addEventListener("click", f);
812 handleClick(MAIN_ID, () => {
816 onEachLazy(document.querySelectorAll("a[href^='#']"), el => {
817 // For clicks on internal links (<A> tags with a hash property), we expand the section we're
818 // jumping to *before* jumping there. We can't do this in onHashChange, because it changes
819 // the height of the document so we wind up scrolled to the wrong place.
820 el.addEventListener("click", () => {
821 expandSection(el.hash.slice(1));
826 onEachLazy(document.querySelectorAll(".toggle > summary:not(.hideme)"), el => {
827 el.addEventListener("click", e => {
828 if (e.target.tagName !== "SUMMARY" && e.target.tagName !== "A") {
834 function showNotable(e) {
835 if (!window.NOTABLE_TRAITS) {
836 const data = document.getElementById("notable-traits-data");
838 window.NOTABLE_TRAITS = JSON.parse(data.innerText);
840 throw new Error("showNotable() called on page without any notable traits!");
843 if (window.CURRENT_NOTABLE_ELEMENT && window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE === e) {
844 // Make this function idempotent.
847 window.hideAllModals(false);
848 const ty = e.getAttribute("data-ty");
849 const wrapper = document.createElement("div");
850 wrapper.innerHTML = "<div class=\"docblock\">" + window.NOTABLE_TRAITS[ty] + "</div>";
851 wrapper.className = "notable popover";
852 const focusCatcher = document.createElement("div");
853 focusCatcher.setAttribute("tabindex", "0");
854 focusCatcher.onfocus = hideNotable;
855 wrapper.appendChild(focusCatcher);
856 const pos = e.getBoundingClientRect();
857 // 5px overlap so that the mouse can easily travel from place to place
858 wrapper.style.top = (pos.top + window.scrollY + pos.height) + "px";
859 wrapper.style.left = 0;
860 wrapper.style.right = "auto";
861 wrapper.style.visibility = "hidden";
862 const body = document.getElementsByTagName("body")[0];
863 body.appendChild(wrapper);
864 const wrapperPos = wrapper.getBoundingClientRect();
865 // offset so that the arrow points at the center of the "(i)"
866 const finalPos = pos.left + window.scrollX - wrapperPos.width + 24;
868 wrapper.style.left = finalPos + "px";
870 wrapper.style.setProperty(
871 "--popover-arrow-offset",
872 (wrapperPos.right - pos.right + 4) + "px"
875 wrapper.style.visibility = "";
876 window.CURRENT_NOTABLE_ELEMENT = wrapper;
877 window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE = e;
878 wrapper.onpointerleave = function(ev) {
879 // If this is a synthetic touch event, ignore it. A click event will be along shortly.
880 if (ev.pointerType !== "mouse") {
883 if (!e.NOTABLE_FORCE_VISIBLE && !elemIsInParent(event.relatedTarget, e)) {
889 function notableBlurHandler(event) {
890 if (window.CURRENT_NOTABLE_ELEMENT &&
891 !elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT) &&
892 !elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT) &&
893 !elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE) &&
894 !elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE)
896 // Work around a difference in the focus behaviour between Firefox, Chrome, and Safari.
897 // When I click the button on an already-opened notable trait popover, Safari
898 // hides the popover and then immediately shows it again, while everyone else hides it
899 // and it stays hidden.
901 // To work around this, make sure the click finishes being dispatched before
902 // hiding the popover. Since `hideNotable()` is idempotent, this makes Safari behave
903 // consistently with the other two.
904 setTimeout(() => hideNotable(false), 0);
908 function hideNotable(focus) {
909 if (window.CURRENT_NOTABLE_ELEMENT) {
910 if (window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE) {
912 window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.focus();
914 window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE = false;
916 const body = document.getElementsByTagName("body")[0];
917 body.removeChild(window.CURRENT_NOTABLE_ELEMENT);
918 window.CURRENT_NOTABLE_ELEMENT = null;
922 onEachLazy(document.getElementsByClassName("notable-traits"), e => {
923 e.onclick = function() {
924 this.NOTABLE_FORCE_VISIBLE = this.NOTABLE_FORCE_VISIBLE ? false : true;
925 if (window.CURRENT_NOTABLE_ELEMENT && !this.NOTABLE_FORCE_VISIBLE) {
929 window.CURRENT_NOTABLE_ELEMENT.setAttribute("tabindex", "0");
930 window.CURRENT_NOTABLE_ELEMENT.focus();
931 window.CURRENT_NOTABLE_ELEMENT.onblur = notableBlurHandler;
935 e.onpointerenter = function(ev) {
936 // If this is a synthetic touch event, ignore it. A click event will be along shortly.
937 if (ev.pointerType !== "mouse") {
942 e.onpointerleave = function(ev) {
943 // If this is a synthetic touch event, ignore it. A click event will be along shortly.
944 if (ev.pointerType !== "mouse") {
947 if (!this.NOTABLE_FORCE_VISIBLE &&
948 !elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT)) {
954 const sidebar_menu_toggle = document.getElementsByClassName("sidebar-menu-toggle")[0];
955 if (sidebar_menu_toggle) {
956 sidebar_menu_toggle.addEventListener("click", () => {
957 const sidebar = document.getElementsByClassName("sidebar")[0];
958 if (!hasClass(sidebar, "shown")) {
966 function helpBlurHandler(event) {
967 blurHandler(event, getHelpButton(), window.hidePopoverMenus);
970 function buildHelpMenu() {
971 const book_info = document.createElement("span");
972 book_info.className = "top";
973 book_info.innerHTML = "You can find more information in \
974 <a href=\"https://doc.rust-lang.org/rustdoc/\">the rustdoc book</a>.";
977 ["?", "Show this help dialog"],
978 ["S", "Focus the search field"],
979 ["↑", "Move up in search results"],
980 ["↓", "Move down in search results"],
981 ["← / →", "Switch result tab (when results focused)"],
982 ["⏎", "Go to active search result"],
983 ["+", "Expand all sections"],
984 ["-", "Collapse all sections"],
987 .map((y, index) => ((index & 1) === 0 ? "<kbd>" + y + "</kbd>" : " " + y + " "))
988 .join("") + "</dt><dd>" + x[1] + "</dd>").join("");
989 const div_shortcuts = document.createElement("div");
990 addClass(div_shortcuts, "shortcuts");
991 div_shortcuts.innerHTML = "<h2>Keyboard Shortcuts</h2><dl>" + shortcuts + "</dl></div>";
994 "Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to \
995 restrict the search to a given item kind.",
996 "Accepted kinds are: <code>fn</code>, <code>mod</code>, <code>struct</code>, \
997 <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, \
998 and <code>const</code>.",
999 "Search functions by type signature (e.g., <code>vec -> usize</code> or \
1000 <code>-> vec</code>)",
1001 "Search multiple things at once by splitting your query with comma (e.g., \
1002 <code>str,u8</code> or <code>String,struct:Vec,test</code>)",
1003 "You can look for items with an exact name by putting double quotes around \
1004 your request: <code>\"string\"</code>",
1005 "Look for items inside another one by searching for a path: <code>vec::Vec</code>",
1006 ].map(x => "<p>" + x + "</p>").join("");
1007 const div_infos = document.createElement("div");
1008 addClass(div_infos, "infos");
1009 div_infos.innerHTML = "<h2>Search Tricks</h2>" + infos;
1011 const rustdoc_version = document.createElement("span");
1012 rustdoc_version.className = "bottom";
1013 const rustdoc_version_code = document.createElement("code");
1014 rustdoc_version_code.innerText = "rustdoc " + getVar("rustdoc-version");
1015 rustdoc_version.appendChild(rustdoc_version_code);
1017 const container = document.createElement("div");
1019 container.className = "popover";
1021 container.id = "help";
1022 container.style.display = "none";
1024 const side_by_side = document.createElement("div");
1025 side_by_side.className = "side-by-side";
1026 side_by_side.appendChild(div_shortcuts);
1027 side_by_side.appendChild(div_infos);
1029 container.appendChild(book_info);
1030 container.appendChild(side_by_side);
1031 container.appendChild(rustdoc_version);
1034 const help_section = document.createElement("section");
1035 help_section.appendChild(container);
1036 document.getElementById("main-content").appendChild(help_section);
1037 container.style.display = "block";
1039 const help_button = getHelpButton();
1040 help_button.appendChild(container);
1042 container.onblur = helpBlurHandler;
1043 help_button.onblur = helpBlurHandler;
1044 help_button.children[0].onblur = helpBlurHandler;
1051 * Hide popover menus, notable trait tooltips, and the sidebar (if applicable).
1053 * Pass "true" to reset focus for notable traits.
1055 window.hideAllModals = function(switchFocus) {
1057 window.hidePopoverMenus();
1058 hideNotable(switchFocus);
1062 * Hide all the popover menus.
1064 window.hidePopoverMenus = function() {
1065 onEachLazy(document.querySelectorAll(".search-form .popover"), elem => {
1066 elem.style.display = "none";
1071 * Returns the help menu element (not the button).
1073 * @param {boolean} buildNeeded - If this argument is `false`, the help menu element won't be
1074 * built if it doesn't exist.
1076 * @return {HTMLElement}
1078 function getHelpMenu(buildNeeded) {
1079 let menu = getHelpButton().querySelector(".popover");
1080 if (!menu && buildNeeded) {
1081 menu = buildHelpMenu();
1087 * Show the help popup menu.
1089 function showHelp() {
1090 const menu = getHelpMenu(true);
1091 if (menu.style.display === "none") {
1092 window.hideAllModals();
1093 menu.style.display = "";
1099 document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
1100 // Already on the help page, make help button a no-op.
1101 const target = event.target;
1102 if (target.tagName !== "A" ||
1103 target.parentElement.id !== HELP_BUTTON_ID ||
1109 event.preventDefault();
1112 document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
1113 // By default, have help button open docs in a popover.
1114 // If user clicks with a moderator, though, use default browser behavior,
1115 // probably opening in a new window or tab.
1116 const target = event.target;
1117 if (target.tagName !== "A" ||
1118 target.parentElement.id !== HELP_BUTTON_ID ||
1124 event.preventDefault();
1125 const menu = getHelpMenu(true);
1126 const shouldShowHelp = menu.style.display === "none";
1127 if (shouldShowHelp) {
1130 window.hidePopoverMenus();
1139 window.addEventListener("hashchange", onHashChange);
1140 searchState.setup();
1144 let reset_button_timeout = null;
1146 window.copy_path = but => {
1147 const parent = but.parentElement;
1150 onEach(parent.childNodes, child => {
1151 if (child.tagName === "A") {
1152 path.push(child.textContent);
1156 const el = document.createElement("textarea");
1157 el.value = path.join("::");
1158 el.setAttribute("readonly", "");
1159 // To not make it appear on the screen.
1160 el.style.position = "absolute";
1161 el.style.left = "-9999px";
1163 document.body.appendChild(el);
1165 document.execCommand("copy");
1166 document.body.removeChild(el);
1168 // There is always one children, but multiple childNodes.
1169 but.children[0].style.display = "none";
1172 if (but.childNodes.length < 2) {
1173 tmp = document.createTextNode("✓");
1174 but.appendChild(tmp);
1176 onEachLazy(but.childNodes, e => {
1177 if (e.nodeType === Node.TEXT_NODE) {
1182 tmp.textContent = "✓";
1185 if (reset_button_timeout !== null) {
1186 window.clearTimeout(reset_button_timeout);
1189 function reset_button() {
1190 tmp.textContent = "";
1191 reset_button_timeout = null;
1192 but.children[0].style.display = "";
1195 reset_button_timeout = window.setTimeout(reset_button, 1000);