MediaWiki:Timeless.js: Difference between revisions

From Game Wiki - VortanMU
No edit summary
Tag: Reverted
No edit summary
Tags: Manual revert Reverted
Line 1: Line 1:
$( function () {
/* MegaMenu Vortan – limpo e com whitelist (Timeless) */
// sidebar-chunk only applies to desktop-small, but the toggles are hidden at
mw.loader.using('jquery').then(function () {
// other resolutions regardless and the css overrides any visible effects.
  if (window.VORTAN_MM) return; window.VORTAN_MM = 1;
var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );


/**
  var NAV = '#site-navigation .sidebar-inner';
* 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.
*/


/**
  // Só estes portlets viram mega-menu. Ajuste se quiser (IDs dos divs .mw-portlet)
* Close all dropdowns
  var MEGA_IDS = [
*/
    'p-Sobre_VortanMU','p-Classes','p-Mapas','p-Items','p-NPCs',
function closeOpen() {
    'p-Eventos','p-Mini-Eventos','p-Quests','p-Chaos_Machine','p-Guias','p-Outros'
$dropdowns.removeClass( 'dropdown-active' );
  ];
}
  // O que NÃO está nessa lista (ex.: p-navigation, p-tb/Wiki tools) fica como está.


/**
  function isMega($portlet){
* Click behaviour
    var id = $portlet.attr('id') || '';
*/
    return MEGA_IDS.indexOf(id) !== -1;
$dropdowns.on( 'click', function ( e ) {
  }
// Check if it's already open so we don't open it again
  function colCount(n){ return n>20?4:n>12?3:n>6?2:1; }
// 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 ) {
  function build(){
// Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
    var $nav = $(NAV); if (!$nav.length) return;
$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>' );


/**
    // limpar execuções anteriores
* Set up borders for experimental overflowing table scrolling
    $nav.find('h3.has-mega').removeClass('has-mega active');
*
    $nav.find('ul.mm-submenu').remove();
* I have no idea what I'm doing.
    $(document).off('.vmm'); $(window).off('.vmm');
*
* @param {jQuery} $table
*/
function setScrollClass( $table ) {
var $tableWrapper = $table.parent(),
// wtf browser rtl implementations
scroll = Math.abs( $tableWrapper.scrollLeft() );


$tableWrapper.parent()
    // largura do botão padrão da sidebar (para o submenu copiar)
// 1 instead of 0 because of weird rtl rounding errors or something
    var $refBtn = $nav.find('li > a').first();
.toggleClass( 'scroll-left', scroll > 1 )
    var btnW = $refBtn.outerWidth(); // inclui padding
.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 );
    var gap  = 12; // mesmo “ritmo” da sidebar
}


$content.find( '.content-table' ).on( 'scroll', function () {
    $nav.find('.mw-portlet').each(function(){
setScrollClass( $( this ).children( 'table' ).first() );
      var $p = $(this);
      if (!isMega($p)) return;


if ( $content.attr( 'dir' ) === 'rtl' ) {
      var $h3 = $p.children('h3').first();
$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' );
      var $ul = $p.children('ul').first();
} else {
      if (!$ul.length) $ul = $p.find('> .mw-portlet-body > ul:first');
$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' );
      if (!$h3.length || !$ul.length) return;
}
} );


/**
      var $links = $ul.find('> li > a');
* Mark overflowed tables for scrolling
      if (!$links.length) return;
*/
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
      var n = $links.length;
$content.find( '.content-table > table > caption' ).each( function () {
      var cols = colCount(n);
var $container, tableHeight,
$table = $( this ).parent(),
$wrapper = $table.parent().parent();


if ( $table.outerWidth() > $wrapper.outerWidth() ) {
      // painel com grid e variáveis inline para casar com a sidebar
$container = $( this ).parents( '.content-table-wrapper' );
      var $panel = $('<ul/>', { 'class':'mm-submenu mm-'+cols })
$( this ).width( $content.width() );
        .css({'--mm-col-w': btnW+'px','--cols': cols, '--mm-gap': gap+'px'})
tableHeight = $container.innerHeight() - $( this ).outerHeight();
        .appendTo($p);


$container.find( '.content-table-left' ).height( tableHeight );
      $links.each(function(){
$container.find( '.content-table-right' ).height( tableHeight );
        $('<li class="mm-item"/>')
}
          .append($(this).clone().addClass('mm-link'))
} );
          .appendTo($panel);
}
      });


unOverflowTables();
      $h3.addClass('has-mega');
$( window ).on( 'resize', unOverflowTables );
      $ul.hide();
      $panel.hide();


/**
      function position(){
* Sticky scrollbars maybe?!
        var left = $nav.outerWidth() + 12;
*/
        var top  = $h3.position().top - 6;
$content.find( '.content-table' ).each( function () {
        $panel.css({ left:left, top:top });
var $table, $tableWrapper, $spoof, $scrollbar;
      }
      function open(){
        $nav.find('h3.has-mega').not($h3).removeClass('active');
        $nav.find('ul.mm-submenu').not($panel).hide();
        $h3.addClass('active'); position(); $panel.stop(true,true).fadeIn(90);
      }
      function close(){ $h3.removeClass('active'); $panel.stop(true,true).fadeOut(90); }


$tableWrapper = $( this );
      var timer=null;
$table = $tableWrapper.children( 'table' ).first();
      $h3.on('mouseenter.vmm', function(){ if (window.innerWidth>=851) open(); })
        .on('mouseleave.vmm', function(){
            if (window.innerWidth>=851){
              timer=setTimeout(function(){ if(!$panel.is(':hover') && !$h3.is(':hover')) close(); },120);
            }
        })
        .on('click.vmm', function(e){
            if (window.innerWidth<851){ e.preventDefault(); $panel.toggle(); position(); }
        });


// Assemble our silly crap and add to page
      $panel.on('mouseenter.vmm', function(){ clearTimeout(timer); if (window.innerWidth>=851) open(); })
$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() );
            .on('mouseleave.vmm', function(){
$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() );
              if (window.innerWidth>=851){
$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) );
                timer=setTimeout(function(){ if(!$h3.is(':hover')) close(); },120);
} );
              }
            });


/**
      $(window).on('resize.vmm scroll.vmm', position);
* 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() );
    // fechar clicando fora
} );
    $(document).on('click.vmm', function(e){
$content.find( '.content-table-scrollbar' ).on( 'scroll', function () {
      if ($(e.target).closest(NAV).length === 0) {
var $mirror = $( this ).siblings( '.content-table' ).first();
        var $n = $(NAV);
        $n.find('h3.has-mega').removeClass('active');
        $n.find('ul.mm-submenu').hide();
      }
    });
  }


// Only do this here if we're not already mirroring the table
  mw.hook('wikipage.content').add(build);
// eslint-disable-next-line no-jquery/no-class-state
  $(build);
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() );
} );
} );
} );

Revision as of 07:48, 7 November 2025

/* MegaMenu Vortan – limpo e com whitelist (Timeless) */
mw.loader.using('jquery').then(function () {
  if (window.VORTAN_MM) return; window.VORTAN_MM = 1;

  var NAV = '#site-navigation .sidebar-inner';

  // Só estes portlets viram mega-menu. Ajuste se quiser (IDs dos divs .mw-portlet)
  var MEGA_IDS = [
    'p-Sobre_VortanMU','p-Classes','p-Mapas','p-Items','p-NPCs',
    'p-Eventos','p-Mini-Eventos','p-Quests','p-Chaos_Machine','p-Guias','p-Outros'
  ];
  // O que NÃO está nessa lista (ex.: p-navigation, p-tb/Wiki tools) fica como está.

  function isMega($portlet){
    var id = $portlet.attr('id') || '';
    return MEGA_IDS.indexOf(id) !== -1;
  }
  function colCount(n){ return n>20?4:n>12?3:n>6?2:1; }

  function build(){
    var $nav = $(NAV); if (!$nav.length) return;

    // limpar execuções anteriores
    $nav.find('h3.has-mega').removeClass('has-mega active');
    $nav.find('ul.mm-submenu').remove();
    $(document).off('.vmm'); $(window).off('.vmm');

    // largura do botão padrão da sidebar (para o submenu copiar)
    var $refBtn = $nav.find('li > a').first();
    var btnW = $refBtn.outerWidth(); // inclui padding
    var gap  = 12; // mesmo “ritmo” da sidebar

    $nav.find('.mw-portlet').each(function(){
      var $p = $(this);
      if (!isMega($p)) return;

      var $h3 = $p.children('h3').first();
      var $ul = $p.children('ul').first();
      if (!$ul.length) $ul = $p.find('> .mw-portlet-body > ul:first');
      if (!$h3.length || !$ul.length) return;

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

      var n = $links.length;
      var cols = colCount(n);

      // painel com grid e variáveis inline para casar com a sidebar
      var $panel = $('<ul/>', { 'class':'mm-submenu mm-'+cols })
        .css({'--mm-col-w': btnW+'px','--cols': cols, '--mm-gap': gap+'px'})
        .appendTo($p);

      $links.each(function(){
        $('<li class="mm-item"/>')
          .append($(this).clone().addClass('mm-link'))
          .appendTo($panel);
      });

      $h3.addClass('has-mega');
      $ul.hide();
      $panel.hide();

      function position(){
        var left = $nav.outerWidth() + 12;
        var top  = $h3.position().top - 6;
        $panel.css({ left:left, top:top });
      }
      function open(){
        $nav.find('h3.has-mega').not($h3).removeClass('active');
        $nav.find('ul.mm-submenu').not($panel).hide();
        $h3.addClass('active'); position(); $panel.stop(true,true).fadeIn(90);
      }
      function close(){ $h3.removeClass('active'); $panel.stop(true,true).fadeOut(90); }

      var timer=null;
      $h3.on('mouseenter.vmm', function(){ if (window.innerWidth>=851) open(); })
         .on('mouseleave.vmm', function(){
            if (window.innerWidth>=851){
              timer=setTimeout(function(){ if(!$panel.is(':hover') && !$h3.is(':hover')) close(); },120);
            }
         })
         .on('click.vmm', function(e){
            if (window.innerWidth<851){ e.preventDefault(); $panel.toggle(); position(); }
         });

      $panel.on('mouseenter.vmm', function(){ clearTimeout(timer); if (window.innerWidth>=851) open(); })
            .on('mouseleave.vmm', function(){
              if (window.innerWidth>=851){
                timer=setTimeout(function(){ if(!$h3.is(':hover')) close(); },120);
              }
            });

      $(window).on('resize.vmm scroll.vmm', position);
    });

    // fechar clicando fora
    $(document).on('click.vmm', function(e){
      if ($(e.target).closest(NAV).length === 0) {
        var $n = $(NAV);
        $n.find('h3.has-mega').removeClass('active');
        $n.find('ul.mm-submenu').hide();
      }
    });
  }

  mw.hook('wikipage.content').add(build);
  $(build);
});