Difference between revisions of "MediaWiki:Common.js"

From XilePK - Ragnarok Online Server
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
$(document).ready(function() {
 
$(document).ready(function() {
   $(document).on('click', '.warp-copy', function() {
+
  // Função única para cópia de texto
 +
   $(document).on('click', '.warp-copy', function(e) {
 +
    e.preventDefault();
 
     var textToCopy = $(this).attr('data-copy');
 
     var textToCopy = $(this).attr('data-copy');
 
      
 
      
 +
    // Cria elemento temporário para cópia
 
     var tempInput = document.createElement('textarea');
 
     var tempInput = document.createElement('textarea');
 
     tempInput.value = textToCopy;
 
     tempInput.value = textToCopy;
Line 12: Line 15:
 
     document.body.removeChild(tempInput);
 
     document.body.removeChild(tempInput);
 
      
 
      
     // Store original color
+
     // Adiciona classe para feedback visual
     var originalColor = $(this).css('color');
+
     var $element = $(this);
   
+
     $element.addClass('copied');
    // Add "copied" class for tooltip feedback
 
     $(this).addClass('copied');
 
   
 
    // Change color to green
 
    $(this).css('color', 'green');
 
   
 
    var self = this;
 
 
     setTimeout(function() {
 
     setTimeout(function() {
      // Reset color to original
+
       $element.removeClass('copied');
      $(self).css('color', originalColor);
+
     }, 2000);
      // Remove copied class
 
       $(self).removeClass('copied');
 
     }, 1500);
 
 
   });
 
   });
});
 
  
/* Auto-expand sections when clicking anchored links */
+
  // Adicionar funcionalidade às imagens dos NPCs
$(document).ready(function() {
+
  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");
 
   console.log("Inicializando script para expandir seções com links âncora");
 
    
 
    
Line 38: Line 65:
 
   if (window.location.hash) {
 
   if (window.location.hash) {
 
     console.log("Página carregada com hash: " + window.location.hash);
 
     console.log("Página carregada com hash: " + window.location.hash);
    // Pequeno delay para garantir que o DOM esteja completamente carregado
 
 
     setTimeout(function() {
 
     setTimeout(function() {
 
       expandSectionForAnchor(window.location.hash);
 
       expandSectionForAnchor(window.location.hash);
Line 44: Line 70:
 
   }
 
   }
 
    
 
    
   // Handle clicks on anchor links within the page
+
   // Handle clicks on anchor links
 
   $(document).on('click', 'a[href^="#"]', function(event) {
 
   $(document).on('click', 'a[href^="#"]', function(event) {
 
     var hash = $(this).attr('href');
 
     var hash = $(this).attr('href');
 
     console.log("Clique em link âncora: " + hash);
 
     console.log("Clique em link âncora: " + hash);
    // Previne o comportamento padrão para tratar manualmente
 
 
     event.preventDefault();
 
     event.preventDefault();
 
      
 
      
    // Atualiza a URL sem recarregar a página
 
 
     if (history.pushState) {
 
     if (history.pushState) {
 
       history.pushState(null, null, hash);
 
       history.pushState(null, null, hash);
Line 61: Line 85:
 
   });
 
   });
 
    
 
    
  // Function to expand section containing an anchor
 
 
   function expandSectionForAnchor(hash) {
 
   function expandSectionForAnchor(hash) {
 
     console.log("Procurando e expandindo seção para âncora: " + hash);
 
     console.log("Procurando e expandindo seção para âncora: " + hash);
    // Try to find the anchor element
 
 
     var targetElement = $(hash);
 
     var targetElement = $(hash);
 
      
 
      
Line 70: Line 92:
 
       console.log("Elemento alvo encontrado");
 
       console.log("Elemento alvo encontrado");
 
        
 
        
      // Encontrar todas as seções colapsáveis que contêm o elemento
 
      // Primeiro os pais diretos
 
 
       var collapsibleSections = targetElement.parents('.mw-collapsible.mw-collapsed');
 
       var collapsibleSections = targetElement.parents('.mw-collapsible.mw-collapsed');
 +
      var directCollapsible = targetElement.closest('.mw-collapsible.mw-collapsed');
 
        
 
        
      // Depois verifica se o próprio elemento está em uma seção colapsável
 
      var directCollapsible = targetElement.closest('.mw-collapsible.mw-collapsed');
 
 
       if (directCollapsible.length) {
 
       if (directCollapsible.length) {
 
         collapsibleSections = collapsibleSections.add(directCollapsible);
 
         collapsibleSections = collapsibleSections.add(directCollapsible);
Line 82: Line 101:
 
       console.log("Seções colapsáveis encontradas: " + collapsibleSections.length);
 
       console.log("Seções colapsáveis encontradas: " + collapsibleSections.length);
 
        
 
        
      // Expandir cada seção encontrada
 
 
       if (collapsibleSections.length > 0) {
 
       if (collapsibleSections.length > 0) {
 
         collapsibleSections.each(function() {
 
         collapsibleSections.each(function() {
Line 88: Line 106:
 
           console.log("Expandindo seção colapsável");
 
           console.log("Expandindo seção colapsável");
 
            
 
            
          // Remover a classe collapsed
 
 
           section.removeClass('mw-collapsed');
 
           section.removeClass('mw-collapsed');
         
 
          // Tentar clicar no botão de expansão
 
 
           var toggleButton = section.find('.mw-collapsible-toggle').first();
 
           var toggleButton = section.find('.mw-collapsible-toggle').first();
 
           if (toggleButton.length) {
 
           if (toggleButton.length) {
Line 98: Line 113:
 
           }
 
           }
 
            
 
            
          // Para tabelas, garantir que as linhas sejam mostradas
 
 
           if (section.hasClass('wikitable')) {
 
           if (section.hasClass('wikitable')) {
 
             console.log("Expandindo tabela wikitable");
 
             console.log("Expandindo tabela wikitable");
Line 104: Line 118:
 
           }
 
           }
 
            
 
            
          // Forçar exibição do conteúdo
 
 
           section.find('.mw-collapsible-content').show();
 
           section.find('.mw-collapsible-content').show();
 
         });
 
         });
 
          
 
          
        // Aguardar a expansão antes de rolar
 
 
         setTimeout(function() {
 
         setTimeout(function() {
 
           scrollToTarget(targetElement);
 
           scrollToTarget(targetElement);
 
         }, 400);
 
         }, 400);
 
       } else {
 
       } else {
        // Se não houver seções colapsáveis, apenas role até o elemento
 
 
         scrollToTarget(targetElement);
 
         scrollToTarget(targetElement);
 
       }
 
       }
Line 121: Line 132:
 
   }
 
   }
 
    
 
    
  // Função auxiliar para rolar até o elemento alvo
 
 
   function scrollToTarget(element) {
 
   function scrollToTarget(element) {
 
     console.log("Rolando até o elemento alvo");
 
     console.log("Rolando até o elemento alvo");
Line 129: Line 139:
 
   }
 
   }
 
    
 
    
  // Adicionar hook para quando o conteúdo da wiki for atualizado
 
 
   if (typeof mw !== 'undefined' && mw.hook) {
 
   if (typeof mw !== 'undefined' && mw.hook) {
 
     mw.hook('wikipage.content').add(function() {
 
     mw.hook('wikipage.content').add(function() {
Line 138: Line 147:
 
         }, 300);
 
         }, 300);
 
       }
 
       }
    });
 
  }
 
});
 
 
/* Script para adicionar funcionalidade de cópia às imagens dos NPCs */
 
$(function() {
 
  // Espera o carregamento completo do conteúdo da wiki
 
  function initNpcCopyLinks() {
 
    console.log("Inicializando links de cópia para NPCs");
 
    // Identifica os links dentro dos containers de imagem
 
    $('.contents-equipment .tile-top.tile-image a').each(function() {
 
      var $link = $(this);
 
      var linkHref = $link.attr('href') || '';
 
     
 
      // Extrai o ID do NPC do link (remove o # do início)
 
      var npcId = linkHref.startsWith('#') ? linkHref.substring(1) : linkHref;
 
     
 
      if (npcId) {
 
        // Remove handlers anteriores para evitar duplicação
 
        $link.off('mousedown.npccopy');
 
       
 
        // Adiciona um handler de clique que irá copiar o texto
 
        $link.on('mousedown.npccopy', function(e) {
 
          // Texto que será copiado - usa o ID específico do NPC
 
          var textToCopy = "@warp " + npcId;
 
         
 
          // Cria um elemento temporário para copiar o texto
 
          var tempInput = document.createElement('textarea');
 
          tempInput.value = textToCopy;
 
          document.body.appendChild(tempInput);
 
          tempInput.select();
 
         
 
          try {
 
            // Executa o comando de cópia
 
            document.execCommand('copy');
 
           
 
            // Mostrar feedback visual temporário
 
            $('<div class="copy-notification" style="position:fixed;bottom:20px;right:20px;background:#4CAF50;color:white;padding:10px;border-radius:5px;z-index:9999;">Copiado: ' + textToCopy + '</div>')
 
              .appendTo('body')
 
              .delay(1500)
 
              .fadeOut(300, function() { $(this).remove(); });
 
             
 
          } catch (err) {
 
            console.error('Erro ao copiar texto: ', err);
 
          }
 
         
 
          // Remove o elemento temporário
 
          document.body.removeChild(tempInput);
 
         
 
          // Permite que o evento continue normalmente
 
          return true;
 
        });
 
      }
 
    });
 
  }
 
 
 
  // Inicializa na carga da página
 
  initNpcCopyLinks();
 
 
 
  // Também inicializa quando o conteúdo da wiki é atualizado
 
  if (typeof mw !== 'undefined' && mw.hook) {
 
    mw.hook('wikipage.content').add(function() {
 
      initNpcCopyLinks();
 
 
     });
 
     });
 
   }
 
   }
 
});
 
});

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