index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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="exitSearchButton"></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 exitSearchButton = document.querySelector( '#exitSearchButton' );
  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. exitSearchButton.onclick = function () {
  96. filterInput.value = '';
  97. updateFilter();
  98. panel.classList.remove( 'searchFocused' );
  99. };
  100. // Activate content and title change on browser navigation
  101. window.onpopstate = createNewIframe;
  102. // Create the navigation panel and configure the iframe
  103. createNavigation( list, language );
  104. createNewIframe();
  105. // Handle search query
  106. filterInput.value = extractQuery();
  107. if ( filterInput.value !== '' ) {
  108. panel.classList.add( 'searchFocused' );
  109. updateFilter();
  110. }
  111. }
  112. // Navigation Panel
  113. function createLink( pageName, pageURL ) {
  114. const link = document.createElement( 'a' );
  115. const url = new URL( pageURL, window.location.href );
  116. url.pathname += '.html';
  117. link.href = url.href;
  118. link.textContent = pageName;
  119. link.setAttribute( 'target', 'viewer' );
  120. link.addEventListener( 'click', function ( event ) {
  121. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  122. window.location.hash = pageURL;
  123. panel.classList.remove( 'open' );
  124. content.querySelectorAll( 'a' ).forEach( function ( item ) {
  125. item.classList.remove( 'selected' );
  126. } );
  127. link.classList.add( 'selected' );
  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 );
  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. // Auto change language url. If a reader open a document in English, when he click "zh", the document he read will auto change into Chinese version
  179. function autoChangeUrlLanguage( language ) {
  180. const hash = location.hash;
  181. if ( hash === '' ) return;
  182. const docLink = hash.substr( hash.indexOf( '/' ) );
  183. location.href = '#' + language + docLink;
  184. }
  185. // Filtering
  186. function extractQuery() {
  187. const search = window.location.search;
  188. if ( search.indexOf( '?q=' ) !== - 1 ) {
  189. return decodeURI( search.substr( 3 ) );
  190. }
  191. return '';
  192. }
  193. function updateFilter() {
  194. let v = filterInput.value.trim();
  195. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  196. if ( v !== '' ) {
  197. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  198. } else {
  199. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  200. }
  201. //
  202. const regExp = new RegExp( filterInput.value, 'gi' );
  203. for ( let pageName in pageProperties ) {
  204. const linkElement = pageProperties[ pageName ].linkElement;
  205. const categoryClassList = linkElement.parentElement.classList;
  206. const filterResults = pageName.match( regExp );
  207. if ( filterResults !== null && filterResults.length > 0 ) {
  208. pageName = pageName.replaceAll( regExp, '<b>$&</b>' );
  209. categoryClassList.remove( 'hidden' );
  210. linkElement.innerHTML = pageName;
  211. } else {
  212. // Hide all non-matching page names
  213. categoryClassList.add( 'hidden' );
  214. }
  215. }
  216. displayFilteredPanel();
  217. }
  218. function displayFilteredPanel() {
  219. // Show/hide categories depending on their content
  220. // First check if at least one page in this category is not hidden
  221. categoryElements.forEach( function ( category ) {
  222. const pages = category.children;
  223. const pagesLength = pages.length;
  224. const sectionClassList = category.parentElement.classList;
  225. let hideCategory = true;
  226. for ( let i = 0; i < pagesLength; i ++ ) {
  227. const pageClassList = pages[ i ].classList;
  228. if ( ! pageClassList.contains( 'hidden' ) ) {
  229. hideCategory = false;
  230. }
  231. }
  232. // If and only if all page names are hidden, hide the whole category
  233. if ( hideCategory ) {
  234. sectionClassList.add( 'hidden' );
  235. } else {
  236. sectionClassList.remove( 'hidden' );
  237. }
  238. } );
  239. }
  240. // Routing
  241. function setUrl( href ) { // eslint-disable-line no-undef
  242. // yea I know this is hacky.
  243. const re = /^(\/(?:manual\/|docs\/#?))(.*?)$/;
  244. const url = new URL( href );
  245. if ( url.origin === window.location.origin ) {
  246. const hrefNoOrigin = url.href.substr( url.origin.length );
  247. const m = re.exec( hrefNoOrigin );
  248. const [ , base, path ] = m;
  249. if ( base.includes( 'manual' ) ) {
  250. const newHash = `#${ path.replace( '.html', '' ) }`;
  251. // Only create new iframe if we're actually changing pages.
  252. // We could just be going to an anchor on the same page.
  253. const newPrefix = newHash.split( '#' )[ 1 ];
  254. const oldPrefix = window.location.hash.split( '#' )[ 1 ];
  255. if ( newPrefix === oldPrefix ) {
  256. const newUrl = new URL( window.location.href );
  257. newUrl.hash = newHash;
  258. window.history.pushState( {}, '', newUrl.href );
  259. } else {
  260. window.location.hash = newHash;
  261. createNewIframe();
  262. }
  263. return;
  264. }
  265. }
  266. window.location.href = href;
  267. }
  268. function setTitle( title ) { // eslint-disable-line no-undef
  269. document.title = `${title} - three.js manual`;
  270. }
  271. function createNewIframe() {
  272. // Change the content displayed in the iframe
  273. // First separate the member part of the fragment (if existing)
  274. const hash = window.location.hash.substring( 1 );
  275. const splitHash = decomposePageName( hash, '.', '#' );
  276. // Creating a new Iframe instead of assigning a new src is
  277. // a cross-browser solution to allow normal browser navigation;
  278. // - only assigning a new src would result in two history states each time.
  279. // Note: iframe.contentWindow.location.replace(hash) should work, too,
  280. // but it doesn't work in Edge with links from the subpages!
  281. const oldIframe = iframe;
  282. iframe = oldIframe.cloneNode();
  283. if ( hash ) {
  284. // We can have 2 hashes. One for the main page, one for the page it's referencing
  285. // In other words
  286. // #en/somePage#someSectionOfPage
  287. const subHash = splitHash[0].indexOf('#');
  288. let src;
  289. if (subHash >= 0) {
  290. const beforeSubHash = splitHash[0].substr(0, subHash);
  291. const afterSubHash = splitHash[0].substr(subHash);
  292. src = `${beforeSubHash}.html${afterSubHash}${splitHash[ 1 ]}`;
  293. } else {
  294. src = splitHash[ 0 ] + '.html' + splitHash[ 1 ];
  295. }
  296. iframe.src = src; // lgtm[js/client-side-unvalidated-url-redirection]
  297. iframe.style.display = 'unset';
  298. } else {
  299. iframe.src = '';
  300. iframe.style.display = 'none';
  301. }
  302. document.body.replaceChild( iframe, oldIframe );
  303. }
  304. function decomposePageName( pageName, oldDelimiter, newDelimiter ) {
  305. // Helper function for separating the member (if existing) from the pageName
  306. // For example: 'Geometry.morphTarget' can be converted to
  307. // ['Geometry', '.morphTarget'] or ['Geometry', '#morphTarget']
  308. // Note: According RFC 3986 no '#' allowed inside of an URL fragment!
  309. let parts = [];
  310. const dotIndex = pageName.indexOf( oldDelimiter );
  311. if ( dotIndex !== - 1 ) {
  312. parts = pageName.split( oldDelimiter );
  313. parts[ 1 ] = newDelimiter + parts[ 1 ];
  314. } else {
  315. parts[ 0 ] = pageName;
  316. parts[ 1 ] = '';
  317. }
  318. return parts;
  319. }
  320. //
  321. console.log( [
  322. ' __ __',
  323. ' __/ __\\ / __\\__ ____ _____ _____',
  324. '/ __/ /\\/ / /___\\/ ____\\/ _____\\/ _____\\',
  325. '\\/_ __/ / _ / / __/ / __ / / __ /_ __ _____',
  326. '/ / / / / / / / / / / / ___/ / ___/\\ _\\/ __\\/ _____\\',
  327. '\\/__/ \\/__/\\/__/\\/__/ \\/_____/\\/_____/\\/__/ / / / ___/',
  328. ' / __/ / \\__ \\',
  329. ' \\/____/\\/_____/'
  330. ].join( '\n' ) );
  331. </script>
  332. </body>
  333. </html>