Difference between revisions of "MediaWiki:Common.js"
Jump to navigation
Jump to search
| Line 1: | Line 1: | ||
| − | /* Função melhorada para a funcionalidade de cópia | + | /* Any JavaScript here will be loaded for all users on every page load. */ |
| − | function initImageCopy() { | + | |
| − | + | $(document).ready(function() { | |
| − | + | // Função melhorada para a funcionalidade de cópia | |
| − | + | function initImageCopy() { | |
| − | + | // Adiciona a funcionalidade warp-copy para links de imagem | |
| − | + | $('.tile-top.tile-image a').each(function() { | |
| − | + | var $link = $(this); | |
| − | + | if (!$link.hasClass('warp-copy')) { | |
| − | + | var href = $link.attr('href') || ''; | |
| − | + | var npcId = href.replace('#',''); | |
| − | + | if (npcId) { | |
| − | + | $link.addClass('warp-copy') | |
| − | + | .attr('data-copy', '@warp ' + npcId) | |
| − | + | .css('cursor', 'pointer'); | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | // Remove a classe após 2 segundos | + | // Adiciona eventos de click específicos para garantir o feedback visual |
| − | + | $link.off('click').on('click', function(e) { | |
| − | + | e.preventDefault(); | |
| − | + | e.stopPropagation(); | |
| − | + | ||
| − | + | 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'); | ||
| + | console.log("Texto copiado da imagem: " + textToCopy); | ||
| + | |||
| + | // Remove a classe após 2 segundos | ||
| + | setTimeout(function() { | ||
| + | $element.removeClass('copied'); | ||
| + | }, 2000); | ||
| + | |||
| + | return false; | ||
| + | }); | ||
| + | } | ||
} | } | ||
| − | + | }); | |
| − | + | } | |
| − | } | ||
| − | |||
| − | |||
// Função para texto | // Função para texto | ||
$(document).on('click', '.warp-copy:not(.tile-top.tile-image a)', function(e) { | $(document).on('click', '.warp-copy:not(.tile-top.tile-image a)', function(e) { | ||
| Line 68: | Line 69: | ||
}, 2000); | }, 2000); | ||
}); | }); | ||
| − | + | ||
| + | // Função corrigida para aplicar larguras e alturas aos containers, não às imagens | ||
| + | function fixNpcContainerWidths() { | ||
| + | $('.contents-equipment.tile-halves').each(function() { | ||
| + | var $container = $(this); | ||
| + | var width = $container.data('width'); | ||
| + | |||
| + | if (width && width > 0) { | ||
| + | $container.css({ | ||
| + | '--container-width': width + 'px' | ||
| + | }); | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | // Aplicar altura aos containers de imagem, não às próprias imagens | ||
| + | $('.tile-top.tile-image').each(function() { | ||
| + | var $imageContainer = $(this); | ||
| + | var height = $imageContainer.css('--image-height'); | ||
| + | |||
| + | if (height && height !== 'auto') { | ||
| + | $imageContainer.css('height', height); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | |||
// Inicializa a funcionalidade para imagens | // Inicializa a funcionalidade para imagens | ||
setTimeout(function() { | setTimeout(function() { | ||
initImageCopy(); | initImageCopy(); | ||
| + | fixNpcContainerWidths(); | ||
}, 300); | }, 300); | ||
| Line 79: | Line 105: | ||
setTimeout(function() { | setTimeout(function() { | ||
initImageCopy(); | initImageCopy(); | ||
| + | fixNpcContainerWidths(); | ||
}, 300); | }, 300); | ||
}); | }); | ||
| + | } | ||
| + | |||
| + | /* 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); | ||
} | } | ||
}); | }); | ||
Revision as of 16:06, 9 May 2025
/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function() {
// Função melhorada para a funcionalidade de cópia
function initImageCopy() {
// Adiciona a funcionalidade warp-copy para links de imagem
$('.tile-top.tile-image a').each(function() {
var $link = $(this);
if (!$link.hasClass('warp-copy')) {
var href = $link.attr('href') || '';
var npcId = href.replace('#','');
if (npcId) {
$link.addClass('warp-copy')
.attr('data-copy', '@warp ' + npcId)
.css('cursor', 'pointer');
// Adiciona eventos de click específicos para garantir o feedback visual
$link.off('click').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
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');
console.log("Texto copiado da imagem: " + textToCopy);
// Remove a classe após 2 segundos
setTimeout(function() {
$element.removeClass('copied');
}, 2000);
return false;
});
}
}
});
}
// Função para texto
$(document).on('click', '.warp-copy:not(.tile-top.tile-image a)', 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');
console.log("Texto copiado do texto: " + textToCopy);
setTimeout(function() {
$element.removeClass('copied');
}, 2000);
});
// Função corrigida para aplicar larguras e alturas aos containers, não às imagens
function fixNpcContainerWidths() {
$('.contents-equipment.tile-halves').each(function() {
var $container = $(this);
var width = $container.data('width');
if (width && width > 0) {
$container.css({
'--container-width': width + 'px'
});
}
});
// Aplicar altura aos containers de imagem, não às próprias imagens
$('.tile-top.tile-image').each(function() {
var $imageContainer = $(this);
var height = $imageContainer.css('--image-height');
if (height && height !== 'auto') {
$imageContainer.css('height', height);
}
});
}
// Inicializa a funcionalidade para imagens
setTimeout(function() {
initImageCopy();
fixNpcContainerWidths();
}, 300);
// Reinicializa ao atualizar conteúdo wiki
if (typeof mw !== 'undefined' && mw.hook) {
mw.hook('wikipage.content').add(function() {
setTimeout(function() {
initImageCopy();
fixNpcContainerWidths();
}, 300);
});
}
/* 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);
}
});