Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

doctools.js 4.4 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...