custom.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Set this to `true` when the `latest` branch is significantly incompatible with the
  2. // current `stable` branch, which can lead to confusion for users that land on
  3. // `latest` instead of `stable`.
  4. const inDev = true;
  5. // Handle page scroll and adjust sidebar accordingly.
  6. // Each page has two scrolls: the main scroll, which is moving the content of the page;
  7. // and the sidebar scroll, which is moving the navigation in the sidebar.
  8. // We want the logo to gradually disappear as the main content is scrolled, giving
  9. // more room to the navigation on the left. This means adjusting the height
  10. // available to the navigation on the fly. There is also a banner below the navigation
  11. // that must be dealt with simultaneously.
  12. const registerOnScrollEvent = (function(){
  13. // Configuration.
  14. // The number of pixels the user must scroll by before the logo is completely hidden.
  15. const scrollTopPixels = 84;
  16. // The target margin to be applied to the navigation bar when the logo is hidden.
  17. const menuTopMargin = 88;
  18. // The max-height offset when the logo is completely visible.
  19. const menuHeightOffset_default = 180;
  20. // The max-height offset when the logo is completely hidden.
  21. const menuHeightOffset_fixed = 98;
  22. // The distance between the two max-height offset values above; used for intermediate values.
  23. const menuHeightOffset_diff = (menuHeightOffset_default - menuHeightOffset_fixed);
  24. // Media query handler.
  25. return function(mediaQuery) {
  26. // We only apply this logic to the "desktop" resolution (defined by a media query at the bottom).
  27. // This handler is executed when the result of the query evaluation changes, which means that
  28. // the page has moved between "desktop" and "mobile" states.
  29. // When entering the "desktop" state, we register scroll events and adjust elements on the page.
  30. // When entering the "mobile" state, we clean up any registered events and restore elements on the page
  31. // to their initial state.
  32. const $window = $(window);
  33. const $sidebar = $('.wy-side-scroll');
  34. const $search = $sidebar.children('.wy-side-nav-search');
  35. const $menu = $sidebar.children('.wy-menu-vertical');
  36. const $ethical = $sidebar.children('.ethical-rtd');
  37. // This padding is needed to correctly adjust the height of the scrollable area in the sidebar.
  38. // It has to have the same height as the ethical block, if there is one.
  39. let $menuPadding = $menu.children('.wy-menu-ethical-padding');
  40. if ($menuPadding.length == 0) {
  41. $menuPadding = $('<div class="wy-menu-ethical-padding"></div>');
  42. $menu.append($menuPadding);
  43. }
  44. if (mediaQuery.matches) {
  45. // Entering the "desktop" state.
  46. // The main scroll event handler.
  47. // Executed as the page is scrolled and once immediately as the page enters this state.
  48. const handleMainScroll = (currentScroll) => {
  49. if (currentScroll >= scrollTopPixels) {
  50. // After the page is scrolled below the threshold, we fix everything in place.
  51. $search.css('margin-top', `-${scrollTopPixels}px`);
  52. $menu.css('margin-top', `${menuTopMargin}px`);
  53. $menu.css('max-height', `calc(100% - ${menuHeightOffset_fixed}px)`);
  54. }
  55. else {
  56. // Between the top of the page and the threshold we calculate intermediate values
  57. // to guarantee a smooth transition.
  58. $search.css('margin-top', `-${currentScroll}px`);
  59. $menu.css('margin-top', `${menuTopMargin + (scrollTopPixels - currentScroll)}px`);
  60. if (currentScroll > 0) {
  61. const scrolledPercent = (scrollTopPixels - currentScroll) / scrollTopPixels;
  62. const offsetValue = menuHeightOffset_fixed + menuHeightOffset_diff * scrolledPercent;
  63. $menu.css('max-height', `calc(100% - ${offsetValue}px)`);
  64. } else {
  65. $menu.css('max-height', `calc(100% - ${menuHeightOffset_default}px)`);
  66. }
  67. }
  68. };
  69. // The sidebar scroll event handler.
  70. // Executed as the sidebar is scrolled as well as after the main scroll. This is needed
  71. // because the main scroll can affect the scrollable area of the sidebar.
  72. const handleSidebarScroll = () => {
  73. const menuElement = $menu.get(0);
  74. const menuScrollTop = $menu.scrollTop();
  75. const menuScrollBottom = menuElement.scrollHeight - (menuScrollTop + menuElement.offsetHeight);
  76. // As the navigation is scrolled we add a shadow to the top bar hanging over it.
  77. if (menuScrollTop > 0) {
  78. $search.addClass('fixed-and-scrolled');
  79. } else {
  80. $search.removeClass('fixed-and-scrolled');
  81. }
  82. // Near the bottom we start moving the sidebar banner into view.
  83. if (menuScrollBottom < ethicalOffsetBottom) {
  84. $ethical.css('display', 'block');
  85. $ethical.css('margin-top', `-${ethicalOffsetBottom - menuScrollBottom}px`);
  86. } else {
  87. $ethical.css('display', 'none');
  88. $ethical.css('margin-top', '0px');
  89. }
  90. };
  91. $search.addClass('fixed');
  92. $ethical.addClass('fixed');
  93. // Adjust the inner height of navigation so that the banner can be overlaid there later.
  94. const ethicalOffsetBottom = $ethical.height() || 0;
  95. if (ethicalOffsetBottom) {
  96. $menuPadding.css('height', `${ethicalOffsetBottom}px`);
  97. } else {
  98. $menuPadding.css('height', `0px`);
  99. }
  100. $window.scroll(function() {
  101. handleMainScroll(window.scrollY);
  102. handleSidebarScroll();
  103. });
  104. $menu.scroll(function() {
  105. handleSidebarScroll();
  106. });
  107. handleMainScroll(window.scrollY);
  108. handleSidebarScroll();
  109. } else {
  110. // Entering the "mobile" state.
  111. $window.unbind('scroll');
  112. $menu.unbind('scroll');
  113. $search.removeClass('fixed');
  114. $ethical.removeClass('fixed');
  115. $search.css('margin-top', `0px`);
  116. $menu.css('margin-top', `0px`);
  117. $menu.css('max-height', 'initial');
  118. $menuPadding.css('height', `0px`);
  119. $ethical.css('margin-top', '0px');
  120. $ethical.css('display', 'block');
  121. }
  122. };
  123. })();
  124. // Subscribe to DOM changes in the sidebar container, because there is a
  125. // banner that gets added at a later point, that we might not catch otherwise.
  126. const registerSidebarObserver = (function(){
  127. return function(callback) {
  128. const sidebarContainer = document.querySelector('.wy-side-scroll');
  129. let sidebarEthical = null;
  130. const registerEthicalObserver = () => {
  131. if (sidebarEthical) {
  132. // Do it only once.
  133. return;
  134. }
  135. sidebarEthical = sidebarContainer.querySelector('.ethical-rtd');
  136. if (!sidebarEthical) {
  137. // Do it only after we have the element there.
  138. return;
  139. }
  140. // This observer watches over the ethical block in sidebar, and all of its subtree.
  141. const ethicalObserverConfig = { childList: true, subtree: true };
  142. const ethicalObserverCallback = (mutationsList, observer) => {
  143. for (let mutation of mutationsList) {
  144. if (mutation.type !== 'childList') {
  145. continue;
  146. }
  147. callback();
  148. }
  149. };
  150. const ethicalObserver = new MutationObserver(ethicalObserverCallback);
  151. ethicalObserver.observe(sidebarEthical, ethicalObserverConfig);
  152. };
  153. registerEthicalObserver();
  154. // This observer watches over direct children of the main sidebar container.
  155. const observerConfig = { childList: true };
  156. const observerCallback = (mutationsList, observer) => {
  157. for (let mutation of mutationsList) {
  158. if (mutation.type !== 'childList') {
  159. continue;
  160. }
  161. callback();
  162. registerEthicalObserver();
  163. }
  164. };
  165. const observer = new MutationObserver(observerCallback);
  166. observer.observe(sidebarContainer, observerConfig);
  167. // Default TOC tree has links that immediately navigate to the selected page. Our
  168. // theme adds an extra button to fold and unfold the tree without navigating away.
  169. // But that means that the buttons are added after the initial load, so we need to
  170. // improvise to detect clicks on these buttons.
  171. const scrollElement = document.querySelector('.wy-menu-vertical');
  172. const registerLinkHandler = (linkChildren) => {
  173. linkChildren.forEach(it => {
  174. if (it.nodeType === Node.ELEMENT_NODE && it.classList.contains('toctree-expand')) {
  175. it.addEventListener('click', () => {
  176. // Toggling a different item will close the currently opened one,
  177. // which may shift the clicked item out of the view. We correct for that.
  178. const menuItem = it.parentNode;
  179. const baseScrollOffset = scrollElement.scrollTop + scrollElement.offsetTop;
  180. const maxScrollOffset = baseScrollOffset + scrollElement.offsetHeight;
  181. if (menuItem.offsetTop < baseScrollOffset || menuItem.offsetTop > maxScrollOffset) {
  182. menuItem.scrollIntoView();
  183. }
  184. callback();
  185. });
  186. }
  187. });
  188. }
  189. const navigationSections = document.querySelectorAll('.wy-menu-vertical ul');
  190. navigationSections.forEach(it => {
  191. if (it.previousSibling == null || typeof it.previousSibling === 'undefined' || it.previousSibling.tagName != 'A') {
  192. return;
  193. }
  194. const navigationLink = it.previousSibling;
  195. registerLinkHandler(navigationLink.childNodes);
  196. const linkObserverConfig = { childList: true };
  197. const linkObserverCallback = (mutationsList, observer) => {
  198. for (let mutation of mutationsList) {
  199. registerLinkHandler(mutation.addedNodes);
  200. }
  201. };
  202. const linkObserver = new MutationObserver(linkObserverCallback);
  203. linkObserver.observe(navigationLink, linkObserverConfig);
  204. });
  205. };
  206. })();
  207. $(document).ready(() => {
  208. // Remove the search match highlights from the page, and adjust the URL in the
  209. // navigation history.
  210. const url = new URL(location.href);
  211. if (url.searchParams.has('highlight')) {
  212. Documentation.hideSearchWords();
  213. }
  214. // Initialize handlers for page scrolling and our custom sidebar.
  215. const mediaQuery = window.matchMedia('only screen and (min-width: 769px)');
  216. registerOnScrollEvent(mediaQuery);
  217. mediaQuery.addListener(registerOnScrollEvent);
  218. registerSidebarObserver(() => {
  219. registerOnScrollEvent(mediaQuery);
  220. });
  221. // Add line-break suggestions to the sidebar navigation items in the class reference section.
  222. //
  223. // Some class reference pages have very long PascalCase names, such as
  224. // VisualShaderNodeCurveXYZTexture
  225. // Those create issues for our layout, as we can neither make them wrap with CSS without
  226. // breaking normal article titles, nor is it good to have them overflow their containers.
  227. // So we use a <wbr> tag to insert mid-word suggestions for appropriate splits, so the browser
  228. // knows that it's okay to split it like
  229. // Visual Shader Node Curve XYZTexture
  230. // and add a new line at an opportune moment.
  231. const classReferenceLinks = document.querySelectorAll('.wy-menu-vertical > ul:last-of-type .reference.internal');
  232. for (const linkItem of classReferenceLinks) {
  233. let textNode = null;
  234. linkItem.childNodes.forEach(it => {
  235. if (it.nodeType === Node.TEXT_NODE) {
  236. // If this is a text node and if it needs to be updated, store a reference.
  237. let text = it.textContent;
  238. if (!(text.includes(" ") || text.length < 10)) {
  239. textNode = it;
  240. }
  241. }
  242. });
  243. if (textNode != null) {
  244. let text = textNode.textContent;
  245. // Add suggested line-breaks and replace the original text.
  246. // The regex looks for a lowercase letter followed by a number or an uppercase
  247. // letter. We avoid splitting at the last character in the name, though.
  248. text = text.replace(/([a-z])([A-Z0-9](?!$))/gm, '$1<wbr>$2');
  249. linkItem.removeChild(textNode);
  250. linkItem.insertAdjacentHTML('beforeend', text);
  251. }
  252. }
  253. if (inDev) {
  254. // Add a compatibility notice using JavaScript so it doesn't end up in the
  255. // automatically generated `meta description` tag.
  256. const baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
  257. // These lines only work as expected in the production environment, can't test this locally.
  258. const fallbackUrl = baseUrl.replace('/latest/', '/stable/');
  259. const homeUrl = baseUrl.split('/latest/')[0] + '/stable/';
  260. const searchUrl = homeUrl + 'search.html?q=';
  261. // Insert the base notice with a placeholder to display as we're making a request.
  262. document.querySelector('div[itemprop="articleBody"]').insertAdjacentHTML('afterbegin', `
  263. <div class="admonition attention latest-notice">
  264. <p class="first admonition-title">Attention</p>
  265. <p>
  266. You are reading the <code class="docutils literal notranslate"><span class="pre">latest</span></code>
  267. (unstable) version of this documentation, which may document features not available
  268. or compatible with Godot 3.x.
  269. </p>
  270. <p class="last latest-notice-link">
  271. Checking the <a class="reference" href="${homeUrl}">stable version</a>
  272. of the documentation...
  273. </p>
  274. </div>
  275. `);
  276. const noticeLink = document.querySelector('.latest-notice-link');
  277. // Make a HEAD request to the possible stable URL to check if the page exists.
  278. fetch(fallbackUrl, { method: 'HEAD' })
  279. .then((res) => {
  280. // We only check the HTTP status, which should tell us if the link is valid or not.
  281. if (res.status === 200) {
  282. noticeLink.innerHTML = `
  283. See the <a class="reference" href="${fallbackUrl}">stable version</a>
  284. of this documentation page instead.
  285. `;
  286. } else {
  287. // Err, just to fallthrough to catch.
  288. throw Error('Bad request');
  289. }
  290. })
  291. .catch((err) => {
  292. let message = `
  293. This page does not exist in the <a class="reference" href="${homeUrl}">stable version</a>
  294. of the documentation.
  295. `;
  296. // Also suggest a search query using the page's title. It should work with translations as well.
  297. // Note that we can't use the title tag as it has a permanent suffix. OG title doesn't, though.
  298. const titleMeta = document.querySelector('meta[property="og:title"]');
  299. if (typeof titleMeta !== 'undefined') {
  300. const pageTitle = titleMeta.getAttribute('content');
  301. message += `
  302. You can try searching for "<a class="reference" href="${searchUrl + encodeURIComponent(pageTitle)}">${pageTitle}</a>" instead.
  303. `;
  304. }
  305. noticeLink.innerHTML = message;
  306. });
  307. }
  308. // Load instant.page to prefetch pages upon hovering. This makes navigation feel
  309. // snappier. The script is dynamically appended as Read the Docs doesn't have
  310. // a way to add scripts with a "module" attribute.
  311. const instantPageScript = document.createElement('script');
  312. instantPageScript.toggleAttribute('module');
  313. /*! instant.page v5.1.0 - (C) 2019-2020 Alexandre Dieulot - https://instant.page/license */
  314. instantPageScript.innerText = 'let t,e;const n=new Set,o=document.createElement("link"),i=o.relList&&o.relList.supports&&o.relList.supports("prefetch")&&window.IntersectionObserver&&"isIntersecting"in IntersectionObserverEntry.prototype,s="instantAllowQueryString"in document.body.dataset,a="instantAllowExternalLinks"in document.body.dataset,r="instantWhitelist"in document.body.dataset,c="instantMousedownShortcut"in document.body.dataset,d=1111;let l=65,u=!1,f=!1,m=!1;if("instantIntensity"in document.body.dataset){const t=document.body.dataset.instantIntensity;if("mousedown"==t.substr(0,"mousedown".length))u=!0,"mousedown-only"==t&&(f=!0);else if("viewport"==t.substr(0,"viewport".length))navigator.connection&&(navigator.connection.saveData||navigator.connection.effectiveType&&navigator.connection.effectiveType.includes("2g"))||("viewport"==t?document.documentElement.clientWidth*document.documentElement.clientHeight<45e4&&(m=!0):"viewport-all"==t&&(m=!0));else{const e=parseInt(t);isNaN(e)||(l=e)}}if(i){const n={capture:!0,passive:!0};if(f||document.addEventListener("touchstart",function(t){e=performance.now();const n=t.target.closest("a");if(!h(n))return;v(n.href)},n),u?c||document.addEventListener("mousedown",function(t){const e=t.target.closest("a");if(!h(e))return;v(e.href)},n):document.addEventListener("mouseover",function(n){if(performance.now()-e<d)return;const o=n.target.closest("a");if(!h(o))return;o.addEventListener("mouseout",p,{passive:!0}),t=setTimeout(()=>{v(o.href),t=void 0},l)},n),c&&document.addEventListener("mousedown",function(t){if(performance.now()-e<d)return;const n=t.target.closest("a");if(t.which>1||t.metaKey||t.ctrlKey)return;if(!n)return;n.addEventListener("click",function(t){1337!=t.detail&&t.preventDefault()},{capture:!0,passive:!1,once:!0});const o=new MouseEvent("click",{view:window,bubbles:!0,cancelable:!1,detail:1337});n.dispatchEvent(o)},n),m){let t;(t=window.requestIdleCallback?t=>{requestIdleCallback(t,{timeout:1500})}:t=>{t()})(()=>{const t=new IntersectionObserver(e=>{e.forEach(e=>{if(e.isIntersecting){const n=e.target;t.unobserve(n),v(n.href)}})});document.querySelectorAll("a").forEach(e=>{h(e)&&t.observe(e)})})}}function p(e){e.relatedTarget&&e.target.closest("a")==e.relatedTarget.closest("a")||t&&(clearTimeout(t),t=void 0)}function h(t){if(t&&t.href&&(!r||"instant"in t.dataset)&&(a||t.origin==location.origin||"instant"in t.dataset)&&["http:","https:"].includes(t.protocol)&&("http:"!=t.protocol||"https:"!=location.protocol)&&(s||!t.search||"instant"in t.dataset)&&!(t.hash&&t.pathname+t.search==location.pathname+location.search||"noInstant"in t.dataset))return!0}function v(t){if(n.has(t))return;const e=document.createElement("link");e.rel="prefetch",e.href=t,document.head.appendChild(e),n.add(t)}';
  315. document.head.appendChild(instantPageScript);
  316. // Make sections in the sidebar togglable.
  317. let hasCurrent = false;
  318. let menuHeaders = document.querySelectorAll('.wy-menu-vertical .caption[role=heading]');
  319. menuHeaders.forEach(it => {
  320. let connectedMenu = it.nextElementSibling;
  321. // Enable toggling.
  322. it.addEventListener('click', () => {
  323. if (connectedMenu.classList.contains('active')) {
  324. connectedMenu.classList.remove('active');
  325. it.classList.remove('active');
  326. } else {
  327. connectedMenu.classList.add('active');
  328. it.classList.add('active');
  329. }
  330. // Hide other sections.
  331. menuHeaders.forEach(ot => {
  332. if (ot !== it && ot.classList.contains('active')) {
  333. ot.nextElementSibling.classList.remove('active');
  334. ot.classList.remove('active');
  335. }
  336. });
  337. registerOnScrollEvent(mediaQuery);
  338. }, true);
  339. // Set the default state, expand our current section.
  340. if (connectedMenu.classList.contains('current')) {
  341. connectedMenu.classList.add('active');
  342. it.classList.add('active');
  343. hasCurrent = true;
  344. }
  345. });
  346. // Unfold the first (general information) section on the home page.
  347. if (!hasCurrent && menuHeaders.length > 0) {
  348. menuHeaders[0].classList.add('active');
  349. menuHeaders[0].nextElementSibling.classList.add('active');
  350. registerOnScrollEvent(mediaQuery);
  351. }
  352. });
  353. // Override the default implementation from doctools.js to avoid this behavior.
  354. Documentation.highlightSearchWords = function() {
  355. // Nope.
  356. }