Difference between revisions of "MediaWiki:Common.js"

From XilePK - Ragnarok Online Server
Jump to navigation Jump to search
Line 2: Line 2:
  
 
$(document).ready(function() {
 
$(document).ready(function() {
   // Função para copiar texto e mostrar feedback
+
   // Função única para cópia de texto
   function handleCopy(element) {
+
   $(document).on('click', '.warp-copy', function(e) {
     var textToCopy = $(element).attr('data-copy');
+
    e.preventDefault();
 +
     var textToCopy = $(this).attr('data-copy');
 
      
 
      
     // Cria elemento temporário para copiar
+
     // Cria elemento temporário para cópia
 
     var tempInput = document.createElement('textarea');
 
     var tempInput = document.createElement('textarea');
 
     tempInput.value = textToCopy;
 
     tempInput.value = textToCopy;
 
     document.body.appendChild(tempInput);
 
     document.body.appendChild(tempInput);
 
     tempInput.select();
 
     tempInput.select();
 +
    document.execCommand('copy');
 +
    document.body.removeChild(tempInput);
 +
   
 +
    // Adiciona classe para feedback visual
 +
    var $element = $(this);
 +
    $element.addClass('copied');
 +
    setTimeout(function() {
 +
      $element.removeClass('copied');
 +
    }, 2000);
 +
  });
 +
 +
  /* Auto-expand sections when clicking anchored links */
 +
  console.log("Inicializando script para expandir seções com links âncora");
 +
 
 +
  // Handle initial page load with hash
 +
  if (window.location.hash) {
 +
    console.log("Página carregada com hash: " + window.location.hash);
 +
    setTimeout(function() {
 +
      expandSectionForAnchor(window.location.hash);
 +
    }, 300);
 +
  }
 +
 
 +
  // Handle clicks on anchor links
 +
  $(document).on('click', 'a[href^="#"]', function(event) {
 +
    var hash = $(this).attr('href');
 +
    console.log("Clique em link âncora: " + hash);
 +
    event.preventDefault();
 +
   
 +
    if (history.pushState) {
 +
      history.pushState(null, null, hash);
 +
    } else {
 +
      location.hash = hash;
 +
    }
 +
   
 +
    expandSectionForAnchor(hash);
 +
  });
 +
 
 +
  function expandSectionForAnchor(hash) {
 +
    console.log("Procurando e expandindo seção para âncora: " + hash);
 +
    var targetElement = $(hash);
 
      
 
      
     try {
+
     if (targetElement.length) {
       document.execCommand('copy');
+
       console.log("Elemento alvo encontrado");
 
        
 
        
       // Adiciona classe de feedback
+
       var collapsibleSections = targetElement.parents('.mw-collapsible.mw-collapsed');
       $(element).addClass('copied');
+
       var directCollapsible = targetElement.closest('.mw-collapsible.mw-collapsed');
 
        
 
        
       // Remove feedback após 1.5 segundos
+
       if (directCollapsible.length) {
      setTimeout(function() {
+
         collapsibleSections = collapsibleSections.add(directCollapsible);
         $(element).removeClass('copied');
+
       }
       }, 1500);
 
 
        
 
        
    } catch (err) {
+
      console.log("Seções colapsáveis encontradas: " + collapsibleSections.length);
      console.error('Erro ao copiar:', err);
+
     
 +
      if (collapsibleSections.length > 0) {
 +
        collapsibleSections.each(function() {
 +
          var section = $(this);
 +
          console.log("Expandindo seção colapsável");
 +
         
 +
          section.removeClass('mw-collapsed');
 +
          var toggleButton = section.find('.mw-collapsible-toggle').first();
 +
          if (toggleButton.length) {
 +
            console.log("Clicando no botão de expansão");
 +
            toggleButton.click();
 +
          }
 +
         
 +
          if (section.hasClass('wikitable')) {
 +
            console.log("Expandindo tabela wikitable");
 +
            section.find('tr:not(:first-child)').show();
 +
          }
 +
         
 +
          section.find('.mw-collapsible-content').show();
 +
        });
 +
       
 +
        setTimeout(function() {
 +
          scrollToTarget(targetElement);
 +
        }, 400);
 +
      } else {
 +
        scrollToTarget(targetElement);
 +
      }
 +
    } else {
 +
      console.log("Elemento alvo não encontrado para hash: " + hash);
 
     }
 
     }
   
 
    document.body.removeChild(tempInput);
 
 
   }
 
   }
 
+
    
   // Click nos elementos .warp-copy
+
   function scrollToTarget(element) {
   $(document).on('click', '.warp-copy', function() {
+
     console.log("Rolando até o elemento alvo");
     handleCopy(this);
+
     $('html, body').animate({
  });
+
       scrollTop: element.offset().top - 100
 
+
     }, 200);
  // Click nas imagens dos NPCs
 
  function initNpcCopy() {
 
     $('.contents-equipment .tile-top.tile-image a').off('click.npccopy').on('click.npccopy', function(e) {
 
       e.preventDefault();
 
      var npcId = $(this).attr('href').replace('#','');
 
      handleCopy($(this).closest('.contents-equipment').find('.warp-copy')[0]);
 
     });
 
 
   }
 
   }
 
+
    
   // Inicializa quando o documento está pronto
 
  initNpcCopy();
 
 
 
  // Reinicia quando novo conteúdo é carregado
 
 
   if (typeof mw !== 'undefined' && mw.hook) {
 
   if (typeof mw !== 'undefined' && mw.hook) {
 
     mw.hook('wikipage.content').add(function() {
 
     mw.hook('wikipage.content').add(function() {
       initNpcCopy();
+
       console.log("Conteúdo da wiki atualizado");
 +
      if (window.location.hash) {
 +
        setTimeout(function() {
 +
          expandSectionForAnchor(window.location.hash);
 +
        }, 300);
 +
      }
 
     });
 
     });
 
   }
 
   }
 
  // Auto-expandir seções com anchor links
 
  function expandSectionForAnchor(hash) {
 
    var targetElement = $(hash);
 
    if (targetElement.length) {
 
      targetElement.parents('.mw-collapsible.mw-collapsed').removeClass('mw-collapsed')
 
        .find('.mw-collapsible-content').show();
 
      setTimeout(function() {
 
        $('html, body').animate({ scrollTop: targetElement.offset().top - 100 }, 200);
 
      }, 400);
 
    }
 
  }
 
 
  // Trata hash na URL inicial
 
  if (window.location.hash) {
 
    setTimeout(function() { expandSectionForAnchor(window.location.hash); }, 300);
 
  }
 
 
  // Trata clicks em links âncora
 
  $(document).on('click', 'a[href^="#"]', function(e) {
 
    e.preventDefault();
 
    var hash = $(this).attr('href');
 
    history.pushState(null, null, hash);
 
    expandSectionForAnchor(hash);
 
  });
 
 
});
 
});

Revision as of 13:15, 30 April 2025

/* Any JavaScript here will be loaded for all users on every page load. */

$(document).ready(function() {
  // Função única para cópia de texto
  $(document).on('click', '.warp-copy', function(e) {
    e.preventDefault();
    var textToCopy = $(this).attr('data-copy');
    
    // Cria elemento temporário para cópia
    var tempInput = document.createElement('textarea');
    tempInput.value = textToCopy;
    document.body.appendChild(tempInput);
    tempInput.select();
    document.execCommand('copy');
    document.body.removeChild(tempInput);
    
    // Adiciona classe para feedback visual
    var $element = $(this);
    $element.addClass('copied');
    setTimeout(function() {
      $element.removeClass('copied');
    }, 2000);
  });

  /* Auto-expand sections when clicking anchored links */
  console.log("Inicializando script para expandir seções com links âncora");
  
  // Handle initial page load with hash
  if (window.location.hash) {
    console.log("Página carregada com hash: " + window.location.hash);
    setTimeout(function() {
      expandSectionForAnchor(window.location.hash);
    }, 300);
  }
  
  // Handle clicks on anchor links
  $(document).on('click', 'a[href^="#"]', function(event) {
    var hash = $(this).attr('href');
    console.log("Clique em link âncora: " + hash);
    event.preventDefault();
    
    if (history.pushState) {
      history.pushState(null, null, hash);
    } else {
      location.hash = hash;
    }
    
    expandSectionForAnchor(hash);
  });
  
  function expandSectionForAnchor(hash) {
    console.log("Procurando e expandindo seção para âncora: " + hash);
    var targetElement = $(hash);
    
    if (targetElement.length) {
      console.log("Elemento alvo encontrado");
      
      var collapsibleSections = targetElement.parents('.mw-collapsible.mw-collapsed');
      var directCollapsible = targetElement.closest('.mw-collapsible.mw-collapsed');
      
      if (directCollapsible.length) {
        collapsibleSections = collapsibleSections.add(directCollapsible);
      }
      
      console.log("Seções colapsáveis encontradas: " + collapsibleSections.length);
      
      if (collapsibleSections.length > 0) {
        collapsibleSections.each(function() {
          var section = $(this);
          console.log("Expandindo seção colapsável");
          
          section.removeClass('mw-collapsed');
          var toggleButton = section.find('.mw-collapsible-toggle').first();
          if (toggleButton.length) {
            console.log("Clicando no botão de expansão");
            toggleButton.click();
          }
          
          if (section.hasClass('wikitable')) {
            console.log("Expandindo tabela wikitable");
            section.find('tr:not(:first-child)').show();
          }
          
          section.find('.mw-collapsible-content').show();
        });
        
        setTimeout(function() {
          scrollToTarget(targetElement);
        }, 400);
      } else {
        scrollToTarget(targetElement);
      }
    } else {
      console.log("Elemento alvo não encontrado para hash: " + hash);
    }
  }
  
  function scrollToTarget(element) {
    console.log("Rolando até o elemento alvo");
    $('html, body').animate({
      scrollTop: element.offset().top - 100
    }, 200);
  }
  
  if (typeof mw !== 'undefined' && mw.hook) {
    mw.hook('wikipage.content').add(function() {
      console.log("Conteúdo da wiki atualizado");
      if (window.location.hash) {
        setTimeout(function() {
          expandSectionForAnchor(window.location.hash);
        }, 300);
      }
    });
  }
});