MediaWiki:Timeless.js

From Game Wiki - VortanMU
Revision as of 08:56, 7 November 2025 by Admin (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['jquery']).then(function () {
  try {
    if (window.__VortanCleanJS) return;
    window.__VortanCleanJS = true;

    // ===== 0) Limpeza de handlers antigos em nossos namespaces =====
    $(document).off('.vt-dd');
    $(window).off('.vt-table');

    // Remove submenus custom antigos, se existirem
    $('.custom-submenu').remove();

    // ===== 1) DROPDOWNS DO HEADER (NÃO TOCA NA SIDEBAR) =====
    // Removido ".sidebar-chunk" daqui para não interferir no Navigation
    var $dropdowns = $('#personal, #p-variants-desktop');

    function closeAll() {
      $dropdowns.removeClass('dropdown-active');
    }

    $dropdowns.on('click.vt-dd', function (e) {
      var $self = $(this);
      // eslint-disable-next-line no-jquery/no-class-state
      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) {
      if ($(e.target).closest($dropdowns).length === 0) {
        closeAll();
      }
    });

    // ===== 2) CONTEÚDO: utilitários de tabela (teu bloco) =====
    mw.hook('wikipage.content').add(function ($content) {
      // Evita duplicar wrappers em navegações Ajax
      $content.find('div > table:not( table table )').each(function () {
        var $t = $(this);
        if (!$t.parent().hasClass('content-table')) {
          $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) {
        var $tableWrapper = $table.parent();
        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 () {
        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');
        }
      });

      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');
          }
        });

        // Sticky captions
        $content.find('.content-table > table > caption').each(function () {
          var $table = $(this).parent(),
              $wrapper = $table.parent().parent();
          if ($table.outerWidth() > $wrapper.outerWidth()) {
            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();
      $(window).off('resize.vt-table-unoverflow').on('resize.vt-table-unoverflow', unOverflowTables);

      // Limpa barras antigas e recria (idempotente)
      $content.find('.content-table-scrollbar').remove();

      $content.find('.content-table').each(function () {
        var $tableWrapper = $(this);
        var $table = $tableWrapper.children('table').first();
        var $scrollbar = $('<div>')
          .addClass('content-table-scrollbar inactive')
          .width($content.width());
        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 () {
        var $mirror = $(this).siblings('.inactive').first();
        $mirror.scrollLeft($(this).scrollLeft());
      });

      $content.find('.content-table-scrollbar').off('scroll.vt-table-sync').on('scroll.vt-table-sync', function () {
        var $mirror = $(this).siblings('.content-table').first();
        if (!$(this).hasClass('inactive')) {
          $mirror.scrollLeft($(this).scrollLeft());
        }
      });

      function determineActiveSpoofScrollbars() {
        $content.find('.overflowed .content-table').each(function () {
          var $scrollbar = $(this).siblings('.content-table-scrollbar').first();
          var captionHeight = $(this).find('caption').outerHeight() || 0;
          if (captionHeight) captionHeight += 8;

          var tableTop = $(this).offset().top;
          var tableBottom = tableTop + $(this).outerHeight();
          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);

      // Mantém os tamanhos corretos ao redimensionar
      $(window).off('resize.vt-table-sizefix').on('resize.vt-table-sizefix', function () {
        $content.find('.content-table-scrollbar').each(function () {
          var $ct = $(this).siblings().first().find('table').first();
          $(this).find('.content-table-spoof').first().width($ct.width());
          $(this).width($content.width());
        });
      });
    });

    // ===== 3) Segurança: garante que Navigation não está oculto =====
    $('#p-navigation .mw-portlet-body > ul').show();

    console.log('[Vortan Clean JS] OK');
  } catch (err) {
    console.error('[Vortan Clean JS] erro', err);
  }
});