index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>three.js manual</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link rel="shortcut icon" href="../files/favicon_white.ico" media="(prefers-color-scheme: dark)"/>
  8. <link rel="shortcut icon" href="../files/favicon.ico" media="(prefers-color-scheme: light)" />
  9. <link rel="stylesheet" type="text/css" href="../files/main.css">
  10. <!-- console sandbox -->
  11. <script src="../build/three.min.js" async defer></script>
  12. </head>
  13. <body>
  14. <div id="panel">
  15. <div id="header">
  16. <h1><a href="https://threejs.org">three.js</a></h1>
  17. <div id="sections">
  18. <span class="selected">manual</span>
  19. </div>
  20. <div id="expandButton"></div>
  21. </div>
  22. <div id="panelScrim"></div>
  23. <div id="contentWrapper">
  24. <div id="inputWrapper">
  25. <input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
  26. <div id="clearSearchButton"></div>
  27. <select id="language">
  28. <option value="en">en</option>
  29. <option value="fr">fr</option>
  30. <option value="ru">ru</option>
  31. <option value="ko">한국어</option>
  32. <option value="zh">中文</option>
  33. <option value="ja">日本語</option>
  34. </select>
  35. </div>
  36. <br/>
  37. <div id="content"></div>
  38. </div>
  39. </div>
  40. <iframe name="viewer"></iframe>
  41. <script>
  42. const panel = document.querySelector( '#panel' );
  43. const content = document.querySelector( '#content' );
  44. const expandButton = document.querySelector( '#expandButton' );
  45. const clearSearchButton = document.querySelector( '#clearSearchButton' );
  46. const panelScrim = document.querySelector( '#panelScrim' );
  47. const filterInput = document.querySelector( '#filterInput' );
  48. let iframe = document.querySelector( 'iframe' );
  49. const pageProperties = {};
  50. const titles = {};
  51. const categoryElements = [];
  52. let navigation;
  53. init();
  54. async function init() {
  55. const list = await ( await fetch( 'list.json' ) ).json();
  56. const hash = window.location.hash.substring( 1 );
  57. // Localization
  58. let language = 'en';
  59. const hashLanguage = /^(.*?)\//.exec( hash );
  60. if ( hashLanguage ) {
  61. language = hashLanguage[ 1 ];
  62. }
  63. const languageSelect = document.querySelector( '#language' );
  64. languageSelect.value = language;
  65. languageSelect.addEventListener( 'change', function () {
  66. setLanguage( this.value );
  67. } );
  68. function setLanguage( value ) {
  69. language = value;
  70. createNavigation( list, language );
  71. updateFilter();
  72. autoChangeUrlLanguage( language );
  73. }
  74. // Functionality for hamburger button (on small devices)
  75. expandButton.onclick = function ( event ) {
  76. event.preventDefault();
  77. panel.classList.toggle( 'open' );
  78. };
  79. panelScrim.onclick = function ( event ) {
  80. event.preventDefault();
  81. panel.classList.toggle( 'open' );
  82. };
  83. // Functionality for search/filter input field
  84. filterInput.onfocus = function () {
  85. panel.classList.add( 'searchFocused' );
  86. };
  87. filterInput.onblur = function () {
  88. if ( filterInput.value === '' ) {
  89. panel.classList.remove( 'searchFocused' );
  90. }
  91. };
  92. filterInput.oninput = function () {
  93. updateFilter();
  94. };
  95. clearSearchButton.onclick = function () {
  96. filterInput.value = '';
  97. updateFilter();
  98. filterInput.focus();
  99. };
  100. // Activate content and title change on browser navigation
  101. window.onpopstate = function () {
  102. updateNavigation();
  103. createNewIframe();
  104. };
  105. // Create the navigation panel and configure the iframe
  106. createNavigation( list, language );
  107. createNewIframe();
  108. // Handle search query
  109. filterInput.value = extractQuery();
  110. if ( filterInput.value !== '' ) {
  111. panel.classList.add( 'searchFocused' );
  112. updateFilter();
  113. }
  114. }
  115. // Navigation Panel
  116. function createLink( pageName, pageURL ) {
  117. const link = document.createElement( 'a' );
  118. const url = new URL( pageURL, window.location.href );
  119. url.pathname += '.html';
  120. link.href = url.href;
  121. link.textContent = pageName;
  122. link.setAttribute( 'target', 'viewer' );
  123. link.addEventListener( 'click', function ( event ) {
  124. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  125. window.location.hash = pageURL;
  126. panel.classList.remove( 'open' );
  127. updateNavigation();
  128. } );
  129. return link;
  130. }
  131. function createNavigation( list, language ) {
  132. if ( navigation !== undefined ) {
  133. content.removeChild( navigation );
  134. }
  135. // Create the navigation panel using data from list.js
  136. navigation = document.createElement( 'div' );
  137. content.appendChild( navigation );
  138. if ( language === 'ar' ) {
  139. navigation.style.direction = 'rtl';
  140. }
  141. const categories = list[ language ];
  142. const selectedPage = window.location.hash.substring( 1 ).replace( /\.html$/, '' );
  143. for ( const category in categories ) {
  144. // Create categories
  145. const pages = categories[ category ];
  146. const categoryContainer = document.createElement( 'div' );
  147. navigation.appendChild( categoryContainer );
  148. const categoryHead = document.createElement( 'h3' );
  149. categoryHead.textContent = category;
  150. categoryContainer.appendChild( categoryHead );
  151. const categoryContent = document.createElement( 'ul' );
  152. categoryContainer.appendChild( categoryContent );
  153. for ( const pageName in pages ) {
  154. // Create page links
  155. const pageURL = pages[ pageName ];
  156. // Localisation
  157. const listElement = document.createElement( 'li' );
  158. categoryContent.appendChild( listElement );
  159. const linkElement = createLink( pageName, pageURL );
  160. listElement.appendChild( linkElement );
  161. // select current page
  162. if ( pageURL === selectedPage ) {
  163. linkElement.classList.add( 'selected' );
  164. }
  165. // Gather the main properties for the current subpage
  166. pageProperties[ pageName ] = {
  167. category: category,
  168. pageURL: pageURL,
  169. linkElement: linkElement
  170. };
  171. // Gather the document titles (used for easy access on browser navigation)
  172. titles[ pageURL ] = pageName;
  173. }
  174. // Gather the category elements for easy access on filtering
  175. categoryElements.push( categoryContent );
  176. }
  177. }
  178. function updateNavigation() {
  179. const selectedPage = window.location.hash.substring( 1 ).replace( /\.html$/, '' );
  180. content.querySelectorAll( 'a' ).forEach( function ( item ) {
  181. if ( item.href.includes( selectedPage ) ) {
  182. item.classList.add( 'selected' );
  183. } else {
  184. item.classList.remove( 'selected' );
  185. }
  186. } );
  187. }
  188. // Auto change language url. If a reader open a document in English, when they click "zh", the document they read will auto change into Chinese version
  189. function autoChangeUrlLanguage( language ) {
  190. const hash = location.hash;
  191. if ( hash === '' ) return;
  192. const docLink = hash.slice( hash.indexOf( '/' ) );
  193. location.href = '#' + language + docLink;
  194. }
  195. // Filtering
  196. function extractQuery() {
  197. const search = window.location.search;
  198. if ( search.indexOf( '?q=' ) !== - 1 ) {
  199. return decodeURI( search.slice( 3 ) );
  200. }
  201. return '';
  202. }
  203. function updateFilter() {
  204. let v = filterInput.value.trim();
  205. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  206. if ( v !== '' ) {
  207. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  208. } else {
  209. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  210. }
  211. //
  212. const regExp = new RegExp( filterInput.value, 'gi' );
  213. for ( let pageName in pageProperties ) {
  214. const linkElement = pageProperties[ pageName ].linkElement;
  215. const categoryClassList = linkElement.parentElement.classList;
  216. const filterResults = pageName.match( regExp );
  217. if ( filterResults !== null && filterResults.length > 0 ) {
  218. pageName = pageName.replaceAll( regExp, '<b>$&</b>' );
  219. categoryClassList.remove( 'hidden' );
  220. linkElement.innerHTML = pageName;
  221. } else {
  222. // Hide all non-matching page names
  223. categoryClassList.add( 'hidden' );
  224. }
  225. }
  226. displayFilteredPanel();
  227. }
  228. function displayFilteredPanel() {
  229. // Show/hide categories depending on their content
  230. // First check if at least one page in this category is not hidden
  231. categoryElements.forEach( function ( category ) {
  232. const pages = category.children;
  233. const pagesLength = pages.length;
  234. const sectionClassList = category.parentElement.classList;
  235. let hideCategory = true;
  236. for ( let i = 0; i < pagesLength; i ++ ) {
  237. const pageClassList = pages[ i ].classList;
  238. if ( ! pageClassList.contains( 'hidden' ) ) {
  239. hideCategory = false;
  240. }
  241. }
  242. // If and only if all page names are hidden, hide the whole category
  243. if ( hideCategory ) {
  244. sectionClassList.add( 'hidden' );
  245. } else {
  246. sectionClassList.remove( 'hidden' );
  247. }
  248. } );
  249. }
  250. // Routing
  251. function setUrl( href ) { // eslint-disable-line no-undef
  252. // yea I know this is hacky.
  253. const re = /^(\/(?:manual\/|docs\/#?))(.*?)$/;
  254. const url = new URL( href );
  255. if ( url.origin === window.location.origin ) {
  256. const hrefNoOrigin = url.href.slice( url.origin.length );
  257. const m = re.exec( hrefNoOrigin );
  258. const [ , base, path ] = m;
  259. if ( base.includes( 'manual' ) ) {
  260. const newHash = `#${ path.replace( '.html', '' ) }`;
  261. // Only create new iframe if we're actually changing pages.
  262. // We could just be going to an anchor on the same page.
  263. const newPrefix = newHash.split( '#' )[ 1 ];
  264. const oldPrefix = window.location.hash.split( '#' )[ 1 ];
  265. if ( newPrefix === oldPrefix ) {
  266. const newUrl = new URL( window.location.href );
  267. newUrl.hash = newHash;
  268. window.history.pushState( {}, '', newUrl.href );
  269. } else {
  270. window.location.hash = newHash;
  271. updateNavigation();
  272. createNewIframe();
  273. }
  274. return;
  275. }
  276. }
  277. window.location.href = href;
  278. }
  279. function setTitle( title ) { // eslint-disable-line no-undef
  280. document.title = `${title} - three.js manual`;
  281. }
  282. function createNewIframe() {
  283. // Change the content displayed in the iframe
  284. // First separate the member part of the fragment (if existing)
  285. const hash = window.location.hash.substring( 1 );
  286. const splitHash = decomposePageName( hash, '.', '#' );
  287. // Creating a new Iframe instead of assigning a new src is
  288. // a cross-browser solution to allow normal browser navigation;
  289. // - only assigning a new src would result in two history states each time.
  290. // Note: iframe.contentWindow.location.replace(hash) should work, too,
  291. // but it doesn't work in Edge with links from the subpages!
  292. const oldIframe = iframe;
  293. iframe = oldIframe.cloneNode();
  294. if ( hash ) {
  295. // We can have 2 hashes. One for the main page, one for the page it's referencing
  296. // In other words
  297. // #en/somePage#someSectionOfPage
  298. const subHash = splitHash[0].indexOf('#');
  299. let src;
  300. if (subHash >= 0) {
  301. const beforeSubHash = splitHash[0].slice(0, subHash);
  302. const afterSubHash = splitHash[0].slice(subHash);
  303. src = `${beforeSubHash}.html${afterSubHash}${splitHash[ 1 ]}`;
  304. } else {
  305. src = splitHash[ 0 ] + '.html' + splitHash[ 1 ];
  306. }
  307. iframe.src = src; // lgtm[js/client-side-unvalidated-url-redirection]
  308. iframe.style.display = 'unset';
  309. } else {
  310. iframe.src = '';
  311. iframe.style.display = 'none';
  312. }
  313. document.body.replaceChild( iframe, oldIframe );
  314. }
  315. function decomposePageName( pageName, oldDelimiter, newDelimiter ) {
  316. // Helper function for separating the member (if existing) from the pageName
  317. // For example: 'Geometry.morphTarget' can be converted to
  318. // ['Geometry', '.morphTarget'] or ['Geometry', '#morphTarget']
  319. // Note: According RFC 3986 no '#' allowed inside of an URL fragment!
  320. let parts = [];
  321. const dotIndex = pageName.indexOf( oldDelimiter );
  322. if ( dotIndex !== - 1 ) {
  323. parts = pageName.split( oldDelimiter );
  324. parts[ 1 ] = newDelimiter + parts[ 1 ];
  325. } else {
  326. parts[ 0 ] = pageName;
  327. parts[ 1 ] = '';
  328. }
  329. return parts;
  330. }
  331. //
  332. console.log( [
  333. ' __ __',
  334. ' __/ __\\ / __\\__ ____ _____ _____',
  335. '/ __/ /\\/ / /___\\/ ____\\/ _____\\/ _____\\',
  336. '\\/_ __/ / _ / / __/ / __ / / __ /_ __ _____',
  337. '/ / / / / / / / / / / / ___/ / ___/\\ _\\/ __\\/ _____\\',
  338. '\\/__/ \\/__/\\/__/\\/__/ \\/_____/\\/_____/\\/__/ / / / ___/',
  339. ' / __/ / \\__ \\',
  340. ' \\/____/\\/_____/'
  341. ].join( '\n' ) );
  342. </script>
  343. </body>
  344. </html>