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