2020-12-30 11:12:35 +01:00
|
|
|
/*
|
|
|
|
* doctools.js
|
|
|
|
* ~~~~~~~~~~~
|
|
|
|
*
|
2022-06-22 20:22:26 +02:00
|
|
|
* Base JavaScript utilities for all Sphinx HTML documentation.
|
2020-12-30 11:12:35 +01:00
|
|
|
*
|
2022-01-19 14:47:53 +01:00
|
|
|
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
|
2020-12-30 11:12:35 +01:00
|
|
|
* :license: BSD, see LICENSE for details.
|
|
|
|
*
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
"use strict";
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
const _ready = (callback) => {
|
|
|
|
if (document.readyState !== "loading") {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
document.addEventListener("DOMContentLoaded", callback);
|
2021-02-17 14:20:55 +01:00
|
|
|
}
|
2020-12-30 11:12:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-06-22 20:22:26 +02:00
|
|
|
* highlight a given string on a node by wrapping it in
|
|
|
|
* span elements with the given class name.
|
2020-12-30 11:12:35 +01:00
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
const _highlight = (node, addItems, text, className) => {
|
|
|
|
if (node.nodeType === Node.TEXT_NODE) {
|
|
|
|
const val = node.nodeValue;
|
|
|
|
const parent = node.parentNode;
|
|
|
|
const pos = val.toLowerCase().indexOf(text);
|
|
|
|
if (
|
|
|
|
pos >= 0 &&
|
|
|
|
!parent.classList.contains(className) &&
|
|
|
|
!parent.classList.contains("nohighlight")
|
|
|
|
) {
|
|
|
|
let span;
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
const closestNode = parent.closest("body, svg, foreignObject");
|
|
|
|
const isInSVG = closestNode && closestNode.matches("svg");
|
|
|
|
if (isInSVG) {
|
|
|
|
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
|
|
|
|
} else {
|
|
|
|
span = document.createElement("span");
|
|
|
|
span.classList.add(className);
|
|
|
|
}
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
span.appendChild(document.createTextNode(val.substr(pos, text.length)));
|
|
|
|
parent.insertBefore(
|
|
|
|
span,
|
|
|
|
parent.insertBefore(
|
2020-12-30 11:12:35 +01:00
|
|
|
document.createTextNode(val.substr(pos + text.length)),
|
2022-06-22 20:22:26 +02:00
|
|
|
node.nextSibling
|
|
|
|
)
|
|
|
|
);
|
|
|
|
node.nodeValue = val.substr(0, pos);
|
|
|
|
|
|
|
|
if (isInSVG) {
|
|
|
|
const rect = document.createElementNS(
|
|
|
|
"http://www.w3.org/2000/svg",
|
|
|
|
"rect"
|
|
|
|
);
|
|
|
|
const bbox = parent.getBBox();
|
|
|
|
rect.x.baseVal.value = bbox.x;
|
|
|
|
rect.y.baseVal.value = bbox.y;
|
|
|
|
rect.width.baseVal.value = bbox.width;
|
|
|
|
rect.height.baseVal.value = bbox.height;
|
|
|
|
rect.setAttribute("class", className);
|
|
|
|
addItems.push({ parent: parent, target: rect });
|
2020-12-30 11:12:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 20:22:26 +02:00
|
|
|
} else if (node.matches && !node.matches("button, select, textarea")) {
|
|
|
|
node.childNodes.forEach((el) => _highlight(el, addItems, text, className));
|
2020-12-30 11:12:35 +01:00
|
|
|
}
|
|
|
|
};
|
2022-06-22 20:22:26 +02:00
|
|
|
const _highlightText = (thisNode, text, className) => {
|
|
|
|
let addItems = [];
|
|
|
|
_highlight(thisNode, addItems, text, className);
|
|
|
|
addItems.forEach((obj) =>
|
|
|
|
obj.parent.insertAdjacentElement("beforebegin", obj.target)
|
|
|
|
);
|
|
|
|
};
|
2020-12-30 11:12:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Small JavaScript module for the documentation.
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
const Documentation = {
|
|
|
|
init: () => {
|
|
|
|
Documentation.highlightSearchWords();
|
|
|
|
Documentation.initDomainIndexTable();
|
|
|
|
Documentation.initOnKeyListeners();
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i18n support
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
TRANSLATIONS: {},
|
|
|
|
PLURAL_EXPR: (n) => (n === 1 ? 0 : 1),
|
|
|
|
LOCALE: "unknown",
|
2020-12-30 11:12:35 +01:00
|
|
|
|
|
|
|
// gettext and ngettext don't access this so that the functions
|
|
|
|
// can safely bound to a different name (_ = Documentation.gettext)
|
2022-06-22 20:22:26 +02:00
|
|
|
gettext: (string) => {
|
|
|
|
const translated = Documentation.TRANSLATIONS[string];
|
|
|
|
switch (typeof translated) {
|
|
|
|
case "undefined":
|
|
|
|
return string; // no translation
|
|
|
|
case "string":
|
|
|
|
return translated; // translation exists
|
|
|
|
default:
|
|
|
|
return translated[0]; // (singular, plural) translation tuple exists
|
|
|
|
}
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
ngettext: (singular, plural, n) => {
|
|
|
|
const translated = Documentation.TRANSLATIONS[singular];
|
|
|
|
if (typeof translated !== "undefined")
|
|
|
|
return translated[Documentation.PLURAL_EXPR(n)];
|
|
|
|
return n === 1 ? singular : plural;
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
addTranslations: (catalog) => {
|
|
|
|
Object.assign(Documentation.TRANSLATIONS, catalog.messages);
|
|
|
|
Documentation.PLURAL_EXPR = new Function(
|
|
|
|
"n",
|
|
|
|
`return (${catalog.plural_expr})`
|
|
|
|
);
|
|
|
|
Documentation.LOCALE = catalog.locale;
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* highlight the search words provided in the url in the text
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
highlightSearchWords: () => {
|
|
|
|
const highlight =
|
|
|
|
new URLSearchParams(window.location.search).get("highlight") || "";
|
|
|
|
const terms = highlight.toLowerCase().split(/\s+/).filter(x => x);
|
|
|
|
if (terms.length === 0) return; // nothing to do
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
// There should never be more than one element matching "div.body"
|
|
|
|
const divBody = document.querySelectorAll("div.body");
|
|
|
|
const body = divBody.length ? divBody[0] : document.querySelector("body");
|
|
|
|
window.setTimeout(() => {
|
|
|
|
terms.forEach((term) => _highlightText(body, term, "highlighted"));
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
const searchBox = document.getElementById("searchbox");
|
|
|
|
if (searchBox === null) return;
|
|
|
|
searchBox.appendChild(
|
|
|
|
document
|
|
|
|
.createRange()
|
|
|
|
.createContextualFragment(
|
|
|
|
'<p class="highlight-link">' +
|
|
|
|
'<a href="javascript:Documentation.hideSearchWords()">' +
|
|
|
|
Documentation.gettext("Hide Search Matches") +
|
|
|
|
"</a></p>"
|
|
|
|
)
|
|
|
|
);
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* helper function to hide the search marks again
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
hideSearchWords: () => {
|
|
|
|
document
|
|
|
|
.querySelectorAll("#searchbox .highlight-link")
|
|
|
|
.forEach((el) => el.remove());
|
|
|
|
document
|
|
|
|
.querySelectorAll("span.highlighted")
|
|
|
|
.forEach((el) => el.classList.remove("highlighted"));
|
|
|
|
const url = new URL(window.location);
|
|
|
|
url.searchParams.delete("highlight");
|
|
|
|
window.history.replaceState({}, "", url);
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
/**
|
2022-04-03 19:47:40 +02:00
|
|
|
* helper function to focus on search bar
|
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
focusSearchBar: () => {
|
|
|
|
document.querySelectorAll("input[name=q]")[0]?.focus();
|
2022-04-03 19:47:40 +02:00
|
|
|
},
|
|
|
|
|
2020-12-30 11:12:35 +01:00
|
|
|
/**
|
2022-06-22 20:22:26 +02:00
|
|
|
* Initialise the domain index toggle buttons
|
2020-12-30 11:12:35 +01:00
|
|
|
*/
|
2022-06-22 20:22:26 +02:00
|
|
|
initDomainIndexTable: () => {
|
|
|
|
const toggler = (el) => {
|
|
|
|
const idNumber = el.id.substr(7);
|
|
|
|
const toggledRows = document.querySelectorAll(`tr.cg-${idNumber}`);
|
|
|
|
if (el.src.substr(-9) === "minus.png") {
|
|
|
|
el.src = `${el.src.substr(0, el.src.length - 9)}plus.png`;
|
|
|
|
toggledRows.forEach((el) => (el.style.display = "none"));
|
|
|
|
} else {
|
|
|
|
el.src = `${el.src.substr(0, el.src.length - 8)}minus.png`;
|
|
|
|
toggledRows.forEach((el) => (el.style.display = ""));
|
|
|
|
}
|
|
|
|
};
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
const togglerElements = document.querySelectorAll("img.toggler");
|
|
|
|
togglerElements.forEach((el) =>
|
|
|
|
el.addEventListener("click", (event) => toggler(event.currentTarget))
|
|
|
|
);
|
|
|
|
togglerElements.forEach((el) => (el.style.display = ""));
|
|
|
|
if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) togglerElements.forEach(toggler);
|
2020-12-30 11:12:35 +01:00
|
|
|
},
|
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
initOnKeyListeners: () => {
|
2022-04-03 19:47:40 +02:00
|
|
|
// only install a listener if it is really needed
|
2022-06-22 20:22:26 +02:00
|
|
|
if (
|
|
|
|
!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&
|
|
|
|
!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS
|
|
|
|
)
|
|
|
|
return;
|
2022-04-03 19:47:40 +02:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
const blacklistedElements = new Set([
|
|
|
|
"TEXTAREA",
|
|
|
|
"INPUT",
|
|
|
|
"SELECT",
|
|
|
|
"BUTTON",
|
|
|
|
]);
|
|
|
|
document.addEventListener("keydown", (event) => {
|
|
|
|
if (blacklistedElements.has(document.activeElement.tagName)) return; // bail for input elements
|
|
|
|
if (event.altKey || event.ctrlKey || event.metaKey) return; // bail with special keys
|
2022-04-03 19:47:40 +02:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
if (!event.shiftKey) {
|
2022-04-03 19:47:40 +02:00
|
|
|
switch (event.key) {
|
2022-06-22 20:22:26 +02:00
|
|
|
case "ArrowLeft":
|
|
|
|
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;
|
|
|
|
|
|
|
|
const prevLink = document.querySelector('link[rel="prev"]');
|
|
|
|
if (prevLink && prevLink.href) {
|
|
|
|
window.location.href = prevLink.href;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ArrowRight":
|
|
|
|
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;
|
|
|
|
|
|
|
|
const nextLink = document.querySelector('link[rel="next"]');
|
|
|
|
if (nextLink && nextLink.href) {
|
|
|
|
window.location.href = nextLink.href;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Escape":
|
|
|
|
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) break;
|
|
|
|
Documentation.hideSearchWords();
|
|
|
|
event.preventDefault();
|
2020-12-30 11:12:35 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 20:22:26 +02:00
|
|
|
|
|
|
|
// some keyboard layouts may need Shift to get /
|
|
|
|
switch (event.key) {
|
|
|
|
case "/":
|
|
|
|
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) break;
|
|
|
|
Documentation.focusSearchBar();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2020-12-30 11:12:35 +01:00
|
|
|
});
|
2022-06-22 20:22:26 +02:00
|
|
|
},
|
2020-12-30 11:12:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// quick alias for translations
|
2022-06-22 20:22:26 +02:00
|
|
|
const _ = Documentation.gettext;
|
2020-12-30 11:12:35 +01:00
|
|
|
|
2022-06-22 20:22:26 +02:00
|
|
|
_ready(Documentation.init);
|