Tab.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { EventDispatcher } from 'three';
  2. /**
  3. * Tab class
  4. * @param {string} title - The title of the tab
  5. * @param {Object} options - Options for the tab
  6. * @param {boolean} [options.allowDetach=true] - Whether the tab can be detached into a separate window
  7. * @param {boolean} [options.builtin=false] - Whether the tab should appear in the profiler-toggle button
  8. * @param {string} [options.icon] - SVG icon HTML for the builtin button
  9. *
  10. * @example
  11. * // Create a tab that can be detached (default behavior)
  12. * const tab1 = new Tab('My Tab');
  13. *
  14. * // Create a tab that cannot be detached
  15. * const tab2 = new Tab('Fixed Tab', { allowDetach: false });
  16. *
  17. * // Create a builtin tab that appears in the profiler-toggle
  18. * const tab3 = new Tab('Builtin Tab', { builtin: true });
  19. *
  20. * // Create a builtin tab with custom icon
  21. * const tab4 = new Tab('Settings', { builtin: true, icon: '<svg>...</svg>' });
  22. *
  23. * // Control builtin tab visibility
  24. * tab3.showBuiltin(); // Show the builtin button and mini-content
  25. * tab3.hideBuiltin(); // Hide the builtin button and mini-content
  26. */
  27. export class Tab extends EventDispatcher {
  28. constructor( title, options = {} ) {
  29. super();
  30. this.id = title.toLowerCase();
  31. this.button = document.createElement( 'button' );
  32. this.button.className = 'tab-btn';
  33. this.button.textContent = title;
  34. this.content = document.createElement( 'div' );
  35. this.content.id = `${this.id}-content`;
  36. this.content.className = 'profiler-content';
  37. this._isActive = false;
  38. this.isVisible = true;
  39. this.isDetached = false;
  40. this.detachedWindow = null;
  41. this.allowDetach = options.allowDetach !== undefined ? options.allowDetach : true;
  42. this.builtin = options.builtin !== undefined ? options.builtin : false;
  43. this.icon = options.icon || null;
  44. this.builtinButton = null; // Reference to the builtin button in profiler-toggle
  45. this.miniContent = null; // Reference to the mini-panel content container
  46. this.profiler = null; // Reference to the profiler instance
  47. this.onVisibilityChange = null; // Callback for visibility changes
  48. }
  49. get inspector() {
  50. return this.profiler.inspector;
  51. }
  52. get isActive() {
  53. const isProfilerVisible = this.profiler && this.profiler.panel.classList.contains( 'visible' );
  54. if ( ! isProfilerVisible ) return false;
  55. return this.isDetached || this._isActive;
  56. }
  57. set isActive( value ) {
  58. this._isActive = value;
  59. }
  60. init( /*inspector*/ ) { }
  61. update( /*inspector*/ ) { }
  62. setActive( isActive ) {
  63. this.button.classList.toggle( 'active', isActive );
  64. this.content.classList.toggle( 'active', isActive );
  65. this.isActive = isActive;
  66. }
  67. show() {
  68. this.content.style.display = '';
  69. this.button.style.display = '';
  70. this.isVisible = true;
  71. // Show detached window if tab is detached
  72. if ( this.isDetached && this.detachedWindow ) {
  73. this.detachedWindow.panel.style.display = '';
  74. }
  75. // Notify profiler of visibility change
  76. if ( this.onVisibilityChange ) {
  77. this.onVisibilityChange();
  78. }
  79. this.showBuiltin();
  80. }
  81. hide() {
  82. this.content.style.display = 'none';
  83. this.button.style.display = 'none';
  84. this.isVisible = false;
  85. // Hide detached window if tab is detached
  86. if ( this.isDetached && this.detachedWindow ) {
  87. this.detachedWindow.panel.style.display = 'none';
  88. }
  89. // Notify profiler of visibility change
  90. if ( this.onVisibilityChange ) {
  91. this.onVisibilityChange();
  92. }
  93. this.hideBuiltin();
  94. }
  95. showBuiltin() {
  96. if ( ! this.builtin ) return;
  97. // Show the builtin-tabs-container
  98. if ( this.profiler && this.profiler.builtinTabsContainer ) {
  99. this.profiler.builtinTabsContainer.style.display = '';
  100. }
  101. // Show the button
  102. if ( this.builtinButton ) {
  103. this.builtinButton.style.display = '';
  104. }
  105. // Show and activate the mini-panel with content
  106. if ( this.miniContent && this.profiler ) {
  107. // Hide all other mini-panel contents
  108. this.profiler.miniPanel.querySelectorAll( '.mini-panel-content' ).forEach( content => {
  109. content.style.display = 'none';
  110. } );
  111. // Remove active state from all builtin buttons
  112. this.profiler.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ).forEach( btn => {
  113. btn.classList.remove( 'active' );
  114. } );
  115. // Activate this tab's button
  116. if ( this.builtinButton ) {
  117. this.builtinButton.classList.add( 'active' );
  118. }
  119. // Move content to mini-panel if not already there
  120. if ( ! this.miniContent.firstChild ) {
  121. while ( this.content.firstChild ) {
  122. this.miniContent.appendChild( this.content.firstChild );
  123. }
  124. }
  125. // Show the mini-panel and content
  126. this.miniContent.style.display = 'block';
  127. this.profiler.miniPanel.classList.add( 'visible' );
  128. }
  129. }
  130. hideBuiltin() {
  131. if ( ! this.builtin ) return;
  132. // Hide the button
  133. if ( this.builtinButton ) {
  134. this.builtinButton.style.display = 'none';
  135. }
  136. // Hide the mini-panel content
  137. if ( this.miniContent ) {
  138. this.miniContent.style.display = 'none';
  139. // Move content back to main panel
  140. if ( this.miniContent.firstChild ) {
  141. while ( this.miniContent.firstChild ) {
  142. this.content.appendChild( this.miniContent.firstChild );
  143. }
  144. }
  145. }
  146. // Deactivate button
  147. if ( this.builtinButton ) {
  148. this.builtinButton.classList.remove( 'active' );
  149. }
  150. // Hide mini-panel if no content is visible
  151. if ( this.profiler ) {
  152. const hasVisibleContent = Array.from( this.profiler.miniPanel.querySelectorAll( '.mini-panel-content' ) )
  153. .some( content => content.style.display !== 'none' );
  154. if ( ! hasVisibleContent ) {
  155. this.profiler.miniPanel.classList.remove( 'visible' );
  156. }
  157. // Hide the builtin-tabs-container if all builtin buttons are hidden
  158. const hasVisibleBuiltinButtons = Array.from( this.profiler.builtinTabsContainer.querySelectorAll( '.builtin-tab-btn' ) )
  159. .some( btn => btn.style.display !== 'none' );
  160. if ( ! hasVisibleBuiltinButtons ) {
  161. this.profiler.builtinTabsContainer.style.display = 'none';
  162. }
  163. }
  164. }
  165. }
粤ICP备19079148号