Jump to content

MediaWiki:Common.js

From Vampiro
Revision as of 19:29, 31 August 2026 by Fernando (talk | contribs) (Script de agrupamento dinâmico de seções H2 para animação em cascata sincronizada)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
/* ==========================================================================
   VAMPIRO: A MÁSCARA — SCRIPTS DE ANIMAÇÃO & CASCATA PROGRESSIVA
   Agrupa dinamicamente as seções H2 e seus conteúdos para animação fluida
   ========================================================================== */

(function () {
  'use strict';

  function initSectionAnimation() {
    var content = document.querySelector('.mw-parser-output');
    if (!content || content.dataset.vtmAnimated === 'true') return;

    // Respeita preferência por movimento reduzido
    if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
      return;
    }

    // Marca como animado para evitar re-execução
    content.dataset.vtmAnimated = 'true';

    var children = Array.from(content.children);
    var sections = [];
    var currentSection = [];

    children.forEach(function (child) {
      // Ignora caixas de categorias, scripts e painéis de edição
      if (
        child.classList.contains('catlinks') ||
        child.classList.contains('mw-category-generated') ||
        child.tagName === 'SCRIPT' ||
        child.tagName === 'STYLE'
      ) {
        if (currentSection.length > 0) {
          sections.push(currentSection);
          currentSection = [];
        }
        return;
      }

      var isH2 = child.tagName === 'H2' || child.classList.contains('mw-heading2');
      if (isH2 && currentSection.length > 0) {
        sections.push(currentSection);
        currentSection = [];
      }
      currentSection.push(child);
    });

    if (currentSection.length > 0) {
      sections.push(currentSection);
    }

    // Intervalo marcado de 420ms entre cada seção completa
    sections.forEach(function (sectionElements, index) {
      var wrapper = document.createElement('div');
      wrapper.className = 'vtm-section-block';
      var delay = (0.12 + index * 0.42).toFixed(2);
      wrapper.style.animationDelay = delay + 's';

      var firstElem = sectionElements[0];
      if (firstElem && firstElem.parentNode) {
        firstElem.parentNode.insertBefore(wrapper, firstElem);
        sectionElements.forEach(function (el) {
          wrapper.appendChild(el);
        });
      }
    });
  }

  // Executa no carregamento do DOM
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initSectionAnimation);
  } else {
    initSectionAnimation();
  }

  // Suporte a carregamento assíncrono do MediaWiki (ex: navegação via AJAX / VisualEditor)
  if (window.mw && window.mw.hook) {
    window.mw.hook('wikipage.content').add(function () {
      initSectionAnimation();
    });
  }
})();