Difference between revisions of "MediaWiki:Common.js"

From XilePK - Ragnarok Online Server
Jump to navigation Jump to search
 
(49 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
$(document).ready(function() {
 
$(document).ready(function() {
   // Função para processar o conteúdo da página
+
   // Função única para cópia de texto
  function processWarpTags() {
+
   $(document).on('click', '.warp-copy', function(e) {
    // Seleciona todos os elementos com a classe mw-parser-output
+
    e.preventDefault();
    var $content = $('.mw-parser-output');
 
   
 
    // Obtém o HTML atual
 
    var html = $content.html();
 
    if (!html) return;
 
   
 
    // Substitui os padrões <v ... <c ... <c> v> por spans clicáveis
 
    var newHtml = html.replace(/<v\s+(.*?)<c\s+(.*?)<c>\s*v>/g, function(match, visible, copyText) {
 
      return '<span class="warp-copy" data-copy="' + copyText.trim() + '" style="color:#0066cc;cursor:pointer;border-bottom:1px dotted #0066cc;">' + visible.trim() + '</span>';
 
    });
 
   
 
    // Atualiza o HTML se houve mudanças
 
    if (html !== newHtml) {
 
      $content.html(newHtml);
 
    }
 
  }
 
 
 
  // Adiciona o handler de clique para os elementos
 
   $(document).on('click', '.warp-copy', function() {
 
 
     var textToCopy = $(this).attr('data-copy');
 
     var textToCopy = $(this).attr('data-copy');
 
      
 
      
     // Cria um elemento temporário para a cópia
+
     // Cria elemento temporário para cópia
 
     var tempInput = document.createElement('textarea');
 
     var tempInput = document.createElement('textarea');
    tempInput.style.position = 'absolute';
 
    tempInput.style.left = '-9999px';
 
 
     tempInput.value = textToCopy;
 
     tempInput.value = textToCopy;
 
     document.body.appendChild(tempInput);
 
     document.body.appendChild(tempInput);
Line 36: Line 15:
 
     document.body.removeChild(tempInput);
 
     document.body.removeChild(tempInput);
 
      
 
      
     // Feedback visual
+
     // Adiciona classe para feedback visual
     var originalColor = $(this).css('color');
+
     var $element = $(this);
     $(this).css('color', 'green');
+
     $element.addClass('copied');
    var self = this;
 
 
     setTimeout(function() {
 
     setTimeout(function() {
       $(self).css('color', originalColor);
+
       $element.removeClass('copied');
     }, 500);
+
     }, 2000);
 
   });
 
   });
 +
 +
  // Adicionar funcionalidade às imagens dos NPCs
 +
  function initImageCopy() {
 +
    $('.tile-top.tile-image a').each(function() {
 +
      var $link = $(this);
 +
      if (!$link.hasClass('warp-copy')) {
 +
        var npcId = $link.attr('href').replace('#','');
 +
        $link.addClass('warp-copy')
 +
            .attr('data-copy', '@warp ' + npcId)
 +
            .css('cursor', 'pointer');
 +
      }
 +
    });
 +
   
 +
    // Aplicar tamanhos personalizados para imagens
 +
    $('.tile-top.tile-image img').each(function() {
 +
      var $img = $(this);
 +
      var width = $img.data('width') || $img.attr('width');
 +
      var height = $img.data('height') || $img.attr('height');
 +
     
 +
      if (width || height) {
 +
        $img.addClass('custom-size');
 +
        if (width) {
 +
          $img.css('--custom-width', width + 'px');
 +
        }
 +
        if (height) {
 +
          $img.css('--custom-height', height + 'px');
 +
        }
 +
      }
 +
    });
 +
  }
 +
 +
  // Inicializar ao carregar e em atualizações de conteúdo
 +
  initImageCopy();
 +
  if (typeof mw !== 'undefined' && mw.hook) {
 +
    mw.hook('wikipage.content').add(initImageCopy);
 +
  }
 +
 +
  /* Auto-expand sections when clicking anchored links */
 +
  console.log("Inicializando script para expandir seções com links âncora");
 
    
 
    
   // Processa a página após o carregamento
+
   // Handle initial page load with hash
   processWarpTags();
+
   if (window.location.hash) {
 +
    console.log("Página carregada com hash: " + window.location.hash);
 +
    setTimeout(function() {
 +
      expandSectionForAnchor(window.location.hash);
 +
    }, 300);
 +
  }
 
    
 
    
   // Também processa quando o conteúdo é atualizado via AJAX
+
   // Handle clicks on anchor links
   mw.hook('wikipage.content').add(processWarpTags);
+
  $(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);
 +
      }
 +
    });
 +
  }
 
});
 
});

Latest revision as of 02:07, 6 May 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);
  });

  // Adicionar funcionalidade às imagens dos NPCs
  function initImageCopy() {
    $('.tile-top.tile-image a').each(function() {
      var $link = $(this);
      if (!$link.hasClass('warp-copy')) {
        var npcId = $link.attr('href').replace('#','');
        $link.addClass('warp-copy')
             .attr('data-copy', '@warp ' + npcId)
             .css('cursor', 'pointer');
      }
    });
    
    // Aplicar tamanhos personalizados para imagens
    $('.tile-top.tile-image img').each(function() {
      var $img = $(this);
      var width = $img.data('width') || $img.attr('width');
      var height = $img.data('height') || $img.attr('height');
      
      if (width || height) {
        $img.addClass('custom-size');
        if (width) {
          $img.css('--custom-width', width + 'px');
        }
        if (height) {
          $img.css('--custom-height', height + 'px');
        }
      }
    });
  }

  // Inicializar ao carregar e em atualizações de conteúdo
  initImageCopy();
  if (typeof mw !== 'undefined' && mw.hook) {
    mw.hook('wikipage.content').add(initImageCopy);
  }

  /* 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);
      }
    });
  }
});