Path: blob/main/docs/_static/doctools.js
469 views
/*1* doctools.js2* ~~~~~~~~~~~3*4* Base JavaScript utilities for all Sphinx HTML documentation.5*6* :copyright: Copyright 2007-2024 by the Sphinx team, see AUTHORS.7* :license: BSD, see LICENSE for details.8*9*/10"use strict";11const BLACKLISTED_KEY_CONTROL_ELEMENTS = new Set([12"TEXTAREA",13"INPUT",14"SELECT",15"BUTTON",16]);17const _ready = (callback) => {18if (document.readyState !== "loading") {19callback();20} else {21document.addEventListener("DOMContentLoaded", callback);22}23};24/**25* Small JavaScript module for the documentation.26*/27const Documentation = {28init: () => {29Documentation.initDomainIndexTable();30Documentation.initOnKeyListeners();31},32/**33* i18n support34*/35TRANSLATIONS: {},36PLURAL_EXPR: (n) => (n === 1 ? 0 : 1),37LOCALE: "unknown",38// gettext and ngettext don't access this so that the functions39// can safely bound to a different name (_ = Documentation.gettext)40gettext: (string) => {41const translated = Documentation.TRANSLATIONS[string];42switch (typeof translated) {43case "undefined":44return string; // no translation45case "string":46return translated; // translation exists47default:48return translated[0]; // (singular, plural) translation tuple exists49}50},51ngettext: (singular, plural, n) => {52const translated = Documentation.TRANSLATIONS[singular];53if (typeof translated !== "undefined")54return translated[Documentation.PLURAL_EXPR(n)];55return n === 1 ? singular : plural;56},57addTranslations: (catalog) => {58Object.assign(Documentation.TRANSLATIONS, catalog.messages);59Documentation.PLURAL_EXPR = new Function(60"n",61`return (${catalog.plural_expr})`62);63Documentation.LOCALE = catalog.locale;64},65/**66* helper function to focus on search bar67*/68focusSearchBar: () => {69document.querySelectorAll("input[name=q]")[0]?.focus();70},71/**72* Initialise the domain index toggle buttons73*/74initDomainIndexTable: () => {75const toggler = (el) => {76const idNumber = el.id.substr(7);77const toggledRows = document.querySelectorAll(`tr.cg-${idNumber}`);78if (el.src.substr(-9) === "minus.png") {79el.src = `${el.src.substr(0, el.src.length - 9)}plus.png`;80toggledRows.forEach((el) => (el.style.display = "none"));81} else {82el.src = `${el.src.substr(0, el.src.length - 8)}minus.png`;83toggledRows.forEach((el) => (el.style.display = ""));84}85};86const togglerElements = document.querySelectorAll("img.toggler");87togglerElements.forEach((el) =>88el.addEventListener("click", (event) => toggler(event.currentTarget))89);90togglerElements.forEach((el) => (el.style.display = ""));91if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) togglerElements.forEach(toggler);92},93initOnKeyListeners: () => {94// only install a listener if it is really needed95if (96!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&97!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS98)99return;100document.addEventListener("keydown", (event) => {101// bail for input elements102if (BLACKLISTED_KEY_CONTROL_ELEMENTS.has(document.activeElement.tagName)) return;103// bail with special keys104if (event.altKey || event.ctrlKey || event.metaKey) return;105if (!event.shiftKey) {106switch (event.key) {107case "ArrowLeft":108if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;109const prevLink = document.querySelector('link[rel="prev"]');110if (prevLink && prevLink.href) {111window.location.href = prevLink.href;112event.preventDefault();113}114break;115case "ArrowRight":116if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;117const nextLink = document.querySelector('link[rel="next"]');118if (nextLink && nextLink.href) {119window.location.href = nextLink.href;120event.preventDefault();121}122break;123}124}125// some keyboard layouts may need Shift to get /126switch (event.key) {127case "/":128if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) break;129Documentation.focusSearchBar();130event.preventDefault();131}132});133},134};135// quick alias for translations136const _ = Documentation.gettext;137_ready(Documentation.init);138139140