page.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const panel = document.getElementById( 'panel' );
  2. const panelScrim = document.getElementById( 'panelScrim' );
  3. const expandButton = document.getElementById( 'expandButton' );
  4. const clearSearchButton = document.getElementById( 'clearSearchButton' );
  5. const filterInput = document.getElementById( 'filterInput' );
  6. // code copy buttons
  7. const elements = document.getElementsByTagName( 'pre' );
  8. for ( let i = 0; i < elements.length; i ++ ) {
  9. const element = elements[ i ];
  10. if ( element.classList.contains( 'linenums' ) === false ) {
  11. addCopyButton( element );
  12. }
  13. }
  14. function addCopyButton( element ) {
  15. const copyButton = document.createElement( 'button' );
  16. copyButton.className = 'copy-btn';
  17. element.appendChild( copyButton );
  18. copyButton.addEventListener( 'click', function () {
  19. const codeContent = element.textContent;
  20. navigator.clipboard.writeText( codeContent ).then( () => {
  21. copyButton.classList.add( 'copied' );
  22. setTimeout( () => {
  23. copyButton.classList.remove( 'copied' );
  24. }, 1000 );
  25. } );
  26. } );
  27. }
  28. // Functionality for hamburger button (on small devices)
  29. expandButton.onclick = function ( event ) {
  30. event.preventDefault();
  31. panel.classList.toggle( 'open' );
  32. };
  33. panelScrim.onclick = function ( event ) {
  34. event.preventDefault();
  35. panel.classList.toggle( 'open' );
  36. };
  37. // Functionality for search/filter input field
  38. filterInput.onfocus = function () {
  39. panel.classList.add( 'searchFocused' );
  40. };
  41. filterInput.onblur = function () {
  42. if ( filterInput.value === '' ) {
  43. panel.classList.remove( 'searchFocused' );
  44. }
  45. };
  46. filterInput.oninput = function () {
  47. const term = filterInput.value.trim();
  48. // eslint-disable-next-line no-undef
  49. search( term ); // defined in search.js
  50. };
  51. clearSearchButton.onclick = function () {
  52. filterInput.value = '';
  53. filterInput.focus();
  54. // eslint-disable-next-line no-undef
  55. hideSearch(); // defined in search.js
  56. };
  57. //
  58. window.addEventListener( 'DOMContentLoaded', updateNavigation );
  59. window.addEventListener( 'hashchange', updateNavigation );
  60. function updateNavigation() {
  61. // unselected elements
  62. const selected = document.querySelectorAll( 'nav a.selected' );
  63. selected.forEach( link => link.classList.remove( 'selected' ) );
  64. // determine target
  65. const filename = window.location.pathname.split( '/' ).pop();
  66. const pagename = filename.split( '.' )[ 0 ];
  67. let target = pagename.replace( 'module-', '' );
  68. if ( pagename === 'global' ) {
  69. target = window.location.hash.split( '#' ).pop();
  70. }
  71. if ( target === '' ) return;
  72. // select target and move into view
  73. const liElement = document.querySelector( `li[data-name="${target}"]` );
  74. if ( liElement !== null ) {
  75. const aElement = liElement.firstChild;
  76. aElement.scrollIntoView( { block: 'center' } );
  77. aElement.classList.add( 'selected' );
  78. }
  79. }
  80. // eslint-disable-next-line no-undef
  81. prettyPrint();
  82. console.log( [
  83. ' __ __',
  84. ' __/ __\\ / __\\__ ____ _____ _____',
  85. '/ __/ /\\/ / /___\\/ ____\\/ _____\\/ _____\\',
  86. '\\/_ __/ / _ / / __/ / __ / / __ /_ __ _____',
  87. '/ / / / / / / / / / / / ___/ / ___/\\ _\\/ __\\/ _____\\',
  88. '\\/__/ \\/__/\\/__/\\/__/ \\/_____/\\/_____/\\/__/ / / / ___/',
  89. ' / __/ / \\__ \\',
  90. ' \\/____/\\/_____/'
  91. ].join( '\n' ) );
粤ICP备19079148号