Tab.js 5.4 KB

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