MediaWiki:Timeless.js: Difference between revisions

From Game Wiki - VortanMU
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
mw.loader.using(['jquery']).then(function () {
$( function () {
  try {
// sidebar-chunk only applies to desktop-small, but the toggles are hidden at
    if (window.__VortanCleanJS) return;
// other resolutions regardless and the css overrides any visible effects.
    window.__VortanCleanJS = true;
var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );
 
/**
* Desktop menu click-toggling
*
* We're not even checking if it's desktop because the classes in play have no effect
* on mobile regardless... this may break things at some point, though.
*/
 
/**
* Close all dropdowns
*/
function closeOpen() {
$dropdowns.removeClass( 'dropdown-active' );
}
 
/**
* Click behaviour
*/
$dropdowns.on( 'click', function ( e ) {
// Check if it's already open so we don't open it again
// eslint-disable-next-line no-jquery/no-class-state
if ( $( this ).hasClass( 'dropdown-active' ) ) {
if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) {
// treat reclick on the header as a toggle
closeOpen();
}
// Clicked inside an open menu; don't do anything
} else {
closeOpen();
e.stopPropagation(); // stop hiding it!
$( this ).addClass( 'dropdown-active' );
}
} );
$( document ).on( 'click', function ( e ) {
if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
// Clicked inside an open menu; don't close anything
} else {
closeOpen();
}
} );
} );
 
mw.hook( 'wikipage.content' ).add( function ( $content ) {
// Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
$content.find( 'div > table:not( table table )' ).wrap( '<div class="content-table-wrapper"><div class="content-table"></div></div>' );
$content.find( '.content-table-wrapper' ).prepend( '<div class="content-table-left"></div><div class="content-table-right"></div>' );
 
/**
* Set up borders for experimental overflowing table scrolling
*
* I have no idea what I'm doing.
*
* @param {jQuery} $table
*/
function setScrollClass( $table ) {
var $tableWrapper = $table.parent(),
// wtf browser rtl implementations
scroll = Math.abs( $tableWrapper.scrollLeft() );
 
$tableWrapper.parent()
// 1 instead of 0 because of weird rtl rounding errors or something
.toggleClass( 'scroll-left', scroll > 1 )
.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 );
}
 
$content.find( '.content-table' ).on( 'scroll', function () {
setScrollClass( $( this ).children( 'table' ).first() );
 
if ( $content.attr( 'dir' ) === 'rtl' ) {
$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' );
} else {
$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' );
}
} );


    // ===== 0) Limpeza de handlers antigos =====
/**
    $(document).off('.vt-dd .vt-mega');
* Mark overflowed tables for scrolling
    $(window).off('.vt-table .vt-mega .vt-table-sizefix .vt-table-unoverflow');
*/
    $('.custom-submenu').remove();
function unOverflowTables() {
$content.find( '.content-table > table' ).each( function () {
var $table = $( this ),
$wrapper = $table.parent().parent();
if ( $table.outerWidth() > $wrapper.outerWidth() ) {
$wrapper.addClass( 'overflowed' );
setScrollClass( $table );
} else {
$wrapper.removeClass( 'overflowed scroll-left scroll-right fixed-scrollbar-container' );
}
} );


    // ===== 1) DROPDOWNS DO HEADER (não toca na sidebar) =====
// Set up sticky captions
    var $dropdowns = $('#personal, #p-variants-desktop');
$content.find( '.content-table > table > caption' ).each( function () {
var $container, tableHeight,
$table = $( this ).parent(),
$wrapper = $table.parent().parent();


    function closeAll() { $dropdowns.removeClass('dropdown-active'); }
if ( $table.outerWidth() > $wrapper.outerWidth() ) {
$container = $( this ).parents( '.content-table-wrapper' );
$( this ).width( $content.width() );
tableHeight = $container.innerHeight() - $( this ).outerHeight();


    $dropdowns.on('click.vt-dd', function (e) {
$container.find( '.content-table-left' ).height( tableHeight );
      var $self = $(this);
$container.find( '.content-table-right' ).height( tableHeight );
      if ($self.hasClass('dropdown-active')) {
}
        if ($(e.target).closest($('h2, #p-variants-desktop h3')).length > 0) closeAll();
} );
      } else {
}
        closeAll();
        e.stopPropagation();
        $self.addClass('dropdown-active');
      }
    });


    $(document).on('click.vt-dd', function (e) {
unOverflowTables();
      if ($(e.target).closest($dropdowns).length === 0) closeAll();
$( window ).on( 'resize', unOverflowTables );
    });


    // ===== 2) Utilitários de tabela (teu bloco, idempotente) =====
/**
    mw.hook('wikipage.content').add(function ($content) {
* Sticky scrollbars maybe?!
      $content.find('div > table:not( table table )').each(function () {
*/
        var $t = $(this);
$content.find( '.content-table' ).each( function () {
        if (!$t.parent().hasClass('content-table')) {
var $table, $tableWrapper, $spoof, $scrollbar;
          $t.wrap('<div class="content-table-wrapper"><div class="content-table"></div></div>');
          $t.closest('.content-table-wrapper')
            .prepend('<div class="content-table-left"></div><div class="content-table-right"></div>');
        }
      });


      function setScrollClass($table) {
$tableWrapper = $( this );
        var $tableWrapper = $table.parent();
$table = $tableWrapper.children( 'table' ).first();
        var scroll = Math.abs($tableWrapper.scrollLeft());
        $tableWrapper.parent()
          .toggleClass('scroll-left', scroll > 1)
          .toggleClass('scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1);
      }


      $content.find('.content-table').off('scroll.vt-table').on('scroll.vt-table', function () {
// Assemble our silly crap and add to page
        setScrollClass($(this).children('table').first());
$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() );
        if ($content.attr('dir') === 'rtl') {
$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() );
          $(this).find('caption').css('margin-right', Math.abs($(this).scrollLeft()) + 'px');
$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) );
        } else {
} );
          $(this).find('caption').css('margin-left', $(this).scrollLeft() + 'px');
        }
      });


      function unOverflowTables() {
/**
        $content.find('.content-table > table').each(function () {
* Scoll table when scrolling scrollbar and visa-versa lololol wut
          var $table = $(this),
*/
              $wrapper = $table.parent().parent();
$content.find( '.content-table' ).on( 'scroll', function () {
          if ($table.outerWidth() > $wrapper.outerWidth()) {
// Only do this here if we're not already mirroring the spoof
            $wrapper.addClass('overflowed');
var $mirror = $( this ).siblings( '.inactive' ).first();
            setScrollClass($table);
          } else {
            $wrapper.removeClass('overflowed scroll-left scroll-right fixed-scrollbar-container');
          }
        });


        $content.find('.content-table > table > caption').each(function () {
$mirror.scrollLeft( $( this ).scrollLeft() );
          var $table = $(this).parent(),
} );
              $wrapper = $table.parent().parent();
$content.find( '.content-table-scrollbar' ).on( 'scroll', function () {
          if ($table.outerWidth() > $wrapper.outerWidth()) {
var $mirror = $( this ).siblings( '.content-table' ).first();
            var $container = $(this).parents('.content-table-wrapper');
            $(this).width($content.width());
            var tableHeight = $container.innerHeight() - $(this).outerHeight();
            $container.find('.content-table-left').height(tableHeight);
            $container.find('.content-table-right').height(tableHeight);
          }
        });
      }


      unOverflowTables();
// Only do this here if we're not already mirroring the table
      $(window).off('resize.vt-table-unoverflow').on('resize.vt-table-unoverflow', unOverflowTables);
// eslint-disable-next-line no-jquery/no-class-state
if ( !$( this ).hasClass( 'inactive' ) ) {
$mirror.scrollLeft( $( this ).scrollLeft() );
}
} );


      // Recria barras “spoof”
/**
      $content.find('.content-table-scrollbar').remove();
* Set active when actually over the table it applies to...
      $content.find('.content-table').each(function () {
*/
        var $tableWrapper = $(this);
function determineActiveSpoofScrollbars() {
        var $table = $tableWrapper.children('table').first();
$content.find( '.overflowed .content-table' ).each( function () {
        var $scrollbar = $('<div>').addClass('content-table-scrollbar inactive').width($content.width());
var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first();
        var $spoof = $('<div>').addClass('content-table-spoof').width($table.outerWidth());
        $tableWrapper.parent().prepend($scrollbar.prepend($spoof));
      });


      $content.find('.content-table').off('scroll.vt-table-sync').on('scroll.vt-table-sync', function () {
// Skip caption
        $(this).siblings('.inactive').first().scrollLeft($(this).scrollLeft());
var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0;
      });
if ( captionHeight ) {
      $content.find('.content-table-scrollbar').off('scroll.vt-table-sync').on('scroll.vt-table-sync', function () {
// Pad slightly for reasons
        if (!$(this).hasClass('inactive')) {
captionHeight += 8;
          $(this).siblings('.content-table').first().scrollLeft($(this).scrollLeft());
}
        }
      });


      function determineActiveSpoofScrollbars() {
var tableTop = $( this ).offset().top,
        $content.find('.overflowed .content-table').each(function () {
tableBottom = tableTop + $( this ).outerHeight(),
          var $scrollbar = $(this).siblings('.content-table-scrollbar').first();
viewBottom = window.scrollY + document.documentElement.clientHeight,
          var captionHeight = $(this).find('caption').outerHeight() || 0;
active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom;
          if (captionHeight) captionHeight += 8;
$scrollbar.toggleClass( 'inactive', !active );
} );
}


          var tableTop = $(this).offset().top;
determineActiveSpoofScrollbars();
          var tableBottom = tableTop + $(this).outerHeight();
$( window ).on( 'scroll resize', determineActiveSpoofScrollbars );
          var viewBottom = window.scrollY + document.documentElement.clientHeight;
          var active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom;
          $scrollbar.toggleClass('inactive', !active);
        });
      }


      determineActiveSpoofScrollbars();
      $(window).off('scroll.vt-table resize.vt-table').on('scroll.vt-table resize.vt-table', determineActiveSpoofScrollbars);


      $(window).off('resize.vt-table-sizefix').on('resize.vt-table-sizefix', function () {
function showContent(id) {
        $content.find('.content-table-scrollbar').each(function () {
const conteudos = document.querySelectorAll('.nav-content');
          var $ct = $(this).siblings().first().find('table').first();
conteudos.forEach((div) => {
          $(this).find('.content-table-spoof').first().width($ct.width());
div.classList.remove('show-content');
          $(this).width($content.width());
});
        });
document.getElementById(id).classList.add('show-content');
      });
}
    });


    // ===== 3) Garante que Navigation/Wiki tools continuam como lista =====
/**
    $('#p-navigation .mw-portlet-body > ul, #p-tb .mw-portlet-body > ul').show();
* Make sure tablespoofs remain correctly-sized?
*/
$( window ).on( 'resize', function () {
$content.find( '.content-table-scrollbar' ).each( function () {
var width = $( this ).siblings().first().find( 'table' ).first().width();
$( this ).find( '.content-table-spoof' ).first().width( width );
$( this ).width( $content.width() );
} );
} );
} );
/* ===== Mega-menu estável – Timeless (.mw-portlet) ===== */
mw.loader.using(['jquery']).then(function () {
  try {
    if (window.__megaMenuStable) return;
    window.__megaMenuStable = true;


    // ===== 4) Mega-menu SÓ para os portlets da esquerda (exclui Navigation/Wiki tools) =====
     var NAV_SEL = '#site-navigation .sidebar-inner';
     var NAV_SEL = '#site-navigation .sidebar-inner';
     var EXCLUDE = '#p-navigation, #p-tb'; // acrescente aqui se tiver outro que deva ficar como lista
     var isDesktop = function () { return window.innerWidth >= 851; };


     function buildMega() {
     function build() {
       var $container = $(NAV_SEL);
       var $container = $(NAV_SEL);
       if (!$container.length) return;
       if (!$container.length) return;


       // limpa resquícios
       // limpa qualquer resíduo de execuções anteriores
       $container.find('h3.vt-hasmega').removeClass('vt-hasmega active');
       $container.find('h3.has-mega').removeClass('has-mega active');
       $container.find('ul.submenu').remove();
       $container.find('ul.submenu').remove();
       $(document).off('.vt-mega');
       $(document).off('.megamenu');
       $(window).off('.vt-mega');
       $(window).off('.megamenu');


       $container.find('.mw-portlet').not(EXCLUDE).each(function () {
      // para cada seção (portlet)
       $container.find('.mw-portlet').each(function () {
         var $portlet = $(this);
         var $portlet = $(this);
         var $h3 = $portlet.children('h3').first();
         var $h3 = $portlet.children('h3').first();
         var $ul = $portlet.find('> .mw-portlet-body > ul').first();
         var $ul = $portlet.children('ul').first();
        if (!$ul.length) $ul = $portlet.find('> .mw-portlet-body > ul').first();
         if (!$h3.length || !$ul.length) return;
         if (!$h3.length || !$ul.length) return;


Line 156: Line 222:
         if (!$links.length) return;
         if (!$links.length) return;


         $h3.addClass('vt-hasmega');
        // marca o título como "tem mega"
         $h3.addClass('has-mega');


        // define colunas dinamicamente
         var n = $links.length;
         var n = $links.length;
         var cls = 'submenu';
         var cls = 'submenu';
Line 164: Line 232:
         else if (n > 6) cls += ' submenu-2-columns';
         else if (n > 6) cls += ' submenu-2-columns';


        // cria o painel
         var $submenu = $('<ul/>', { 'class': cls }).appendTo($portlet);
         var $submenu = $('<ul/>', { 'class': cls }).appendTo($portlet);
         $links.each(function () { $('<li/>').append($(this).clone()).appendTo($submenu); });
         $links.each(function () {
          $('<li/>').append($(this).clone()).appendTo($submenu);
        });


         // esconde a lista original: agora o portlet mostra só o título
         // esconde a lista original e o painel
         $ul.hide();
         $ul.hide();
        $submenu.hide();


        // posiciona ao lado da barra e alinhado ao título
         function reposition() {
         function reposition() {
           var left = $container.outerWidth() + 12;
           var left = $container.outerWidth() + 12;         // distância da sidebar
           var top = $h3.position().top - 6;
           var top = $h3.position().top - 6;                 // alinhado pela altura do h3
           $submenu.css({ left: left, top: top });
           $submenu.css({ left: left, top: top });
         }
         }


         function open() {
         function open() {
           $container.find('h3.vt-hasmega').not($h3).removeClass('active');
          // fecha outros
           $container.find('h3.has-mega').not($h3).removeClass('active');
           $container.find('ul.submenu').not($submenu).hide();
           $container.find('ul.submenu').not($submenu).hide();
           $h3.addClass('active');
           $h3.addClass('active');
           reposition();
           reposition();
Line 189: Line 264:
         }
         }


         function toggle() { ($submenu.is(':visible')) ? close() : open(); }
         function toggle() {
          ($submenu.is(':visible')) ? close() : open();
        }


        // hover/click com delay pra não sumir no caminho do mouse
         var closeTimer = null;
         var closeTimer = null;


         $h3.on('mouseenter.vt-mega', function () {
         $h3.on('mouseenter.megamenu', function () {
           if (window.innerWidth >= 851) open();
           if (isDesktop()) open();
         }).on('mouseleave.vt-mega', function () {
         }).on('mouseleave.megamenu', function () {
           if (window.innerWidth >= 851) {
           if (isDesktop()) {
             closeTimer = setTimeout(function () {
             closeTimer = setTimeout(function () {
               if (!$submenu.is(':hover') && !$h3.is(':hover')) close();
               if (!$submenu.is(':hover') && !$h3.is(':hover')) close();
             }, 120);
             }, 120);
           }
           }
         }).on('click.vt-mega', function (e) {
         }).on('click.megamenu', function (e) {
           if (window.innerWidth < 851) { e.preventDefault(); toggle(); }
           if (!isDesktop()) { e.preventDefault(); toggle(); }
         });
         });


         $submenu.on('mouseenter.vt-mega', function () {
         $submenu.on('mouseenter.megamenu', function () {
           if (window.innerWidth >= 851) { clearTimeout(closeTimer); open(); }
           if (isDesktop()) { clearTimeout(closeTimer); open(); }
         }).on('mouseleave.vt-mega', function () {
         }).on('mouseleave.megamenu', function () {
           if (window.innerWidth >= 851) {
           if (isDesktop()) {
             closeTimer = setTimeout(function () {
             closeTimer = setTimeout(function () {
               if (!$h3.is(':hover')) close();
               if (!$h3.is(':hover')) close();
Line 215: Line 293:
         });
         });


         $(window).on('resize.vt-mega', reposition);
         $(window).on('resize.megamenu', reposition);
       });
       });


       $(document).on('click.vt-mega', function (e) {
      // fecha clicando fora
       $(document).on('click.megamenu', function (e) {
         if ($(e.target).closest(NAV_SEL).length === 0) {
         if ($(e.target).closest(NAV_SEL).length === 0) {
           var $c = $(NAV_SEL);
           var $c = $(NAV_SEL);
           $c.find('h3.vt-hasmega').removeClass('active');
           $c.find('h3.has-mega').removeClass('active');
           $c.find('ul.submenu').hide();
           $c.find('ul.submenu').hide();
         }
         }
Line 227: Line 306:
     }
     }


    $(buildMega);
     mw.hook('wikipage.content').add(build);
     mw.hook('wikipage.content').add(buildMega);
    $(build);
 
     console.log('[MegaMenu] OK');
     console.log('[Vortan Clean JS + Mega (sidebar)] OK');
   } catch (err) {
   } catch (err) {
     console.error('[Vortan Clean JS] erro', err);
     console.error('[MegaMenu] erro', err);
   }
   }
});
});

Latest revision as of 09:01, 17 November 2025

$( function () {
	// sidebar-chunk only applies to desktop-small, but the toggles are hidden at
	// other resolutions regardless and the css overrides any visible effects.
	var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );

	/**
	 * Desktop menu click-toggling
	 *
	 * We're not even checking if it's desktop because the classes in play have no effect
	 * on mobile regardless... this may break things at some point, though.
	 */

	/**
	 * Close all dropdowns
	 */
	function closeOpen() {
		$dropdowns.removeClass( 'dropdown-active' );
	}

	/**
	 * Click behaviour
	 */
	$dropdowns.on( 'click', function ( e ) {
		// Check if it's already open so we don't open it again
		// eslint-disable-next-line no-jquery/no-class-state
		if ( $( this ).hasClass( 'dropdown-active' ) ) {
			if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) {
				// treat reclick on the header as a toggle
				closeOpen();
			}
			// Clicked inside an open menu; don't do anything
		} else {
			closeOpen();
			e.stopPropagation(); // stop hiding it!
			$( this ).addClass( 'dropdown-active' );
		}
	} );
	$( document ).on( 'click', function ( e ) {
		if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
			// Clicked inside an open menu; don't close anything
		} else {
			closeOpen();
		}
	} );
} );

mw.hook( 'wikipage.content' ).add( function ( $content ) {
	// Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
	$content.find( 'div > table:not( table table )' ).wrap( '<div class="content-table-wrapper"><div class="content-table"></div></div>' );
	$content.find( '.content-table-wrapper' ).prepend( '<div class="content-table-left"></div><div class="content-table-right"></div>' );

	/**
	 * Set up borders for experimental overflowing table scrolling
	 *
	 * I have no idea what I'm doing.
	 *
	 * @param {jQuery} $table
	 */
	function setScrollClass( $table ) {
		var $tableWrapper = $table.parent(),
			// wtf browser rtl implementations
			scroll = Math.abs( $tableWrapper.scrollLeft() );

		$tableWrapper.parent()
			// 1 instead of 0 because of weird rtl rounding errors or something
			.toggleClass( 'scroll-left', scroll > 1 )
			.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 );
	}

	$content.find( '.content-table' ).on( 'scroll', function () {
		setScrollClass( $( this ).children( 'table' ).first() );

		if ( $content.attr( 'dir' ) === 'rtl' ) {
			$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' );
		} else {
			$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' );
		}
	} );

	/**
	 * Mark overflowed tables for scrolling
	 */
	function unOverflowTables() {
		$content.find( '.content-table > table' ).each( function () {
			var $table = $( this ),
				$wrapper = $table.parent().parent();
			if ( $table.outerWidth() > $wrapper.outerWidth() ) {
				$wrapper.addClass( 'overflowed' );
				setScrollClass( $table );
			} else {
				$wrapper.removeClass( 'overflowed scroll-left scroll-right fixed-scrollbar-container' );
			}
		} );

		// Set up sticky captions
		$content.find( '.content-table > table > caption' ).each( function () {
			var $container, tableHeight,
				$table = $( this ).parent(),
				$wrapper = $table.parent().parent();

			if ( $table.outerWidth() > $wrapper.outerWidth() ) {
				$container = $( this ).parents( '.content-table-wrapper' );
				$( this ).width( $content.width() );
				tableHeight = $container.innerHeight() - $( this ).outerHeight();

				$container.find( '.content-table-left' ).height( tableHeight );
				$container.find( '.content-table-right' ).height( tableHeight );
			}
		} );
	}

	unOverflowTables();
	$( window ).on( 'resize', unOverflowTables );

	/**
	 * Sticky scrollbars maybe?!
	 */
	$content.find( '.content-table' ).each( function () {
		var $table, $tableWrapper, $spoof, $scrollbar;

		$tableWrapper = $( this );
		$table = $tableWrapper.children( 'table' ).first();

		// Assemble our silly crap and add to page
		$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() );
		$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() );
		$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) );
	} );

	/**
	 * Scoll table when scrolling scrollbar and visa-versa lololol wut
	 */
	$content.find( '.content-table' ).on( 'scroll', function () {
		// Only do this here if we're not already mirroring the spoof
		var $mirror = $( this ).siblings( '.inactive' ).first();

		$mirror.scrollLeft( $( this ).scrollLeft() );
	} );
	$content.find( '.content-table-scrollbar' ).on( 'scroll', function () {
		var $mirror = $( this ).siblings( '.content-table' ).first();

		// Only do this here if we're not already mirroring the table
		// eslint-disable-next-line no-jquery/no-class-state
		if ( !$( this ).hasClass( 'inactive' ) ) {
			$mirror.scrollLeft( $( this ).scrollLeft() );
		}
	} );

	/**
	 * Set active when actually over the table it applies to...
	 */
	function determineActiveSpoofScrollbars() {
		$content.find( '.overflowed .content-table' ).each( function () {
			var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first();

			// Skip caption
			var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0;
			if ( captionHeight ) {
				// Pad slightly for reasons
				captionHeight += 8;
			}

			var tableTop = $( this ).offset().top,
				tableBottom = tableTop + $( this ).outerHeight(),
				viewBottom = window.scrollY + document.documentElement.clientHeight,
				active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom;
			$scrollbar.toggleClass( 'inactive', !active );
		} );
	}

	determineActiveSpoofScrollbars();
	$( window ).on( 'scroll resize', determineActiveSpoofScrollbars );


	function showContent(id) {
		const conteudos = document.querySelectorAll('.nav-content');
		conteudos.forEach((div) => {
			div.classList.remove('show-content');
		});
		document.getElementById(id).classList.add('show-content');
	}

	/**
	 * Make sure tablespoofs remain correctly-sized?
	 */
	$( window ).on( 'resize', function () {
		$content.find( '.content-table-scrollbar' ).each( function () {
			var width = $( this ).siblings().first().find( 'table' ).first().width();
			$( this ).find( '.content-table-spoof' ).first().width( width );
			$( this ).width( $content.width() );
		} );
	} );
} );
/* ===== Mega-menu estável – Timeless (.mw-portlet) ===== */
mw.loader.using(['jquery']).then(function () {
  try {
    if (window.__megaMenuStable) return;
    window.__megaMenuStable = true;

    var NAV_SEL = '#site-navigation .sidebar-inner';
    var isDesktop = function () { return window.innerWidth >= 851; };

    function build() {
      var $container = $(NAV_SEL);
      if (!$container.length) return;

      // limpa qualquer resíduo de execuções anteriores
      $container.find('h3.has-mega').removeClass('has-mega active');
      $container.find('ul.submenu').remove();
      $(document).off('.megamenu');
      $(window).off('.megamenu');

      // para cada seção (portlet)
      $container.find('.mw-portlet').each(function () {
        var $portlet = $(this);
        var $h3 = $portlet.children('h3').first();
        var $ul = $portlet.children('ul').first();
        if (!$ul.length) $ul = $portlet.find('> .mw-portlet-body > ul').first();
        if (!$h3.length || !$ul.length) return;

        var $links = $ul.find('> li > a');
        if (!$links.length) return;

        // marca o título como "tem mega"
        $h3.addClass('has-mega');

        // define colunas dinamicamente
        var n = $links.length;
        var cls = 'submenu';
        if (n > 20) cls += ' submenu-4-columns';
        else if (n > 12) cls += ' submenu-3-columns';
        else if (n > 6) cls += ' submenu-2-columns';

        // cria o painel
        var $submenu = $('<ul/>', { 'class': cls }).appendTo($portlet);
        $links.each(function () {
          $('<li/>').append($(this).clone()).appendTo($submenu);
        });

        // esconde a lista original e o painel
        $ul.hide();
        $submenu.hide();

        // posiciona ao lado da barra e alinhado ao título
        function reposition() {
          var left = $container.outerWidth() + 12;          // distância da sidebar
          var top  = $h3.position().top - 6;                 // alinhado pela altura do h3
          $submenu.css({ left: left, top: top });
        }

        function open() {
          // fecha outros
          $container.find('h3.has-mega').not($h3).removeClass('active');
          $container.find('ul.submenu').not($submenu).hide();

          $h3.addClass('active');
          reposition();
          $submenu.stop(true, true).fadeIn(90);
        }

        function close() {
          $h3.removeClass('active');
          $submenu.stop(true, true).fadeOut(90);
        }

        function toggle() {
          ($submenu.is(':visible')) ? close() : open();
        }

        // hover/click com delay pra não sumir no caminho do mouse
        var closeTimer = null;

        $h3.on('mouseenter.megamenu', function () {
          if (isDesktop()) open();
        }).on('mouseleave.megamenu', function () {
          if (isDesktop()) {
            closeTimer = setTimeout(function () {
              if (!$submenu.is(':hover') && !$h3.is(':hover')) close();
            }, 120);
          }
        }).on('click.megamenu', function (e) {
          if (!isDesktop()) { e.preventDefault(); toggle(); }
        });

        $submenu.on('mouseenter.megamenu', function () {
          if (isDesktop()) { clearTimeout(closeTimer); open(); }
        }).on('mouseleave.megamenu', function () {
          if (isDesktop()) {
            closeTimer = setTimeout(function () {
              if (!$h3.is(':hover')) close();
            }, 120);
          }
        });

        $(window).on('resize.megamenu', reposition);
      });

      // fecha clicando fora
      $(document).on('click.megamenu', function (e) {
        if ($(e.target).closest(NAV_SEL).length === 0) {
          var $c = $(NAV_SEL);
          $c.find('h3.has-mega').removeClass('active');
          $c.find('ul.submenu').hide();
        }
      });
    }

    mw.hook('wikipage.content').add(build);
    $(build);
    console.log('[MegaMenu] OK');
  } catch (err) {
    console.error('[MegaMenu] erro', err);
  }
});