MediaWiki:Common.js: відмінності між версіями
Wiki (обговорення | внесок) Немає опису редагування Мітка: Скасовано |
Wiki (обговорення | внесок) Немає опису редагування Мітка: Скасовано |
||
| Рядок 105: | Рядок 105: | ||
//OVERLAY | //OVERLAY | ||
(function() { | |||
// ===== Отключаем MediaViewer ===== | |||
if (window.mw && mw.config) mw.config.set('wgUseMediaViewer', false); | |||
// ===== CSS для overlay ===== | |||
const style = document.createElement('style'); | |||
style.innerHTML = ` | |||
.custom-overlay { | |||
position: fixed; | |||
top:0; left:0; | |||
width:100%; height:100%; | |||
background: rgba(0,0,0,0.85); | |||
display: flex; | |||
justify-content: center; | |||
align-items: center; | |||
flex-direction: column; | |||
z-index: 9999; | |||
opacity: 0; | |||
transition: opacity 0.3s ease; | |||
} | |||
.custom-overlay.show { opacity: 1; } | |||
.custom-overlay img { | |||
max-width: 90%; | |||
max-height: 90%; | |||
border-radius: 5px; | |||
box-shadow: 0 0 20px rgba(0,0,0,0.5); | |||
} | |||
.custom-overlay button { | |||
margin: 10px; | |||
padding: 8px 15px; | |||
font-size: 16px; | |||
cursor: pointer; | |||
background: #f44336; | |||
color: white; | |||
border: none; | |||
border-radius: 4px; | |||
} | |||
.nav-buttons { display: flex; justify-content: center; } | |||
`; | |||
document.head.appendChild(style); | |||
// ===== Переменные галереи ===== | |||
let images = Array.from(document.querySelectorAll('img')); | |||
let overlay, imgElement, currentIndex = 0; | |||
// ===== Функция показа overlay ===== | |||
function showOverlay(index) { | |||
currentIndex = index; | |||
const url = images[currentIndex].src; | |||
// Удаляем старый overlay | |||
if (overlay) { overlay.remove(); overlay = null; imgElement = null; } | |||
overlay = document.createElement('div'); | |||
overlay.className = 'custom-overlay'; | |||
imgElement = document.createElement('img'); | |||
imgElement.src = url; | |||
overlay.appendChild(imgElement); | |||
// Кнопка Закрити | |||
const closeBtn = document.createElement('button'); | |||
closeBtn.innerText = '✕ Закрити'; | |||
closeBtn.addEventListener('click', () => overlay.remove()); | |||
overlay.appendChild(closeBtn); | |||
// Кнопки навигации | |||
const navDiv = document.createElement('div'); | |||
navDiv.className = 'nav-buttons'; | |||
const prevBtn = document.createElement('button'); | |||
prevBtn.innerText = '← Назад'; | |||
prevBtn.addEventListener('click', (e) => { | |||
e.stopPropagation(); | |||
currentIndex = (currentIndex - 1 + images.length) % images.length; | |||
imgElement.src = images[currentIndex].src; | |||
}); | |||
const nextBtn = document.createElement('button'); | |||
nextBtn.innerText = 'Вперед →'; | |||
nextBtn.addEventListener('click', (e) => { | |||
e.stopPropagation(); | |||
currentIndex = (currentIndex + 1) % images.length; | |||
imgElement.src = images[currentIndex].src; | |||
}); | |||
navDiv.appendChild(prevBtn); | |||
navDiv.appendChild(nextBtn); | |||
overlay.appendChild(navDiv); | |||
// Закрытие кликом по фону | |||
overlay.addEventListener('click', e => { | |||
if (e.target === overlay) overlay.remove(); | |||
}); | |||
document.body.appendChild(overlay); | |||
setTimeout(() => overlay.classList.add('show'), 10); | |||
} | |||
// ===== Функция для открытия overlay по ссылке на страницу файла ===== | |||
async function showFilePageOverlay(url) { | |||
try { | |||
const res = await fetch(url); | |||
const html = await res.text(); | |||
const parser = new DOMParser(); | |||
const doc = parser.parseFromString(html, 'text/html'); | |||
const img = doc.querySelector('.fullMedia img'); | |||
if (img && img.src) { | |||
// Добавляем изображение во временный массив для листания | |||
const tempIndex = images.length; | |||
images.push({src: img.src}); | |||
showOverlay(tempIndex); | |||
} else { | |||
console.warn('Не удалось найти изображение на странице файла:', url); | |||
} | |||
} catch(err) { | |||
console.error('Ошибка при получении файла:', err); | |||
} | |||
} | |||
// ===== Перехват кликов ===== | |||
document.body.addEventListener('click', e => { | |||
const target = e.target; | |||
// Прямое изображение | |||
if (target.tagName === 'IMG') { | |||
const parentLink = target.closest('a[href*="/w/images/"]'); | |||
if (!parentLink) { | |||
e.preventDefault(); | |||
showOverlay(images.indexOf(target)); | |||
} | |||
} | |||
// Ссылка на страницу файла | |||
if (target.tagName === 'A' && target.href.includes('/w/index.php/File:')) { | |||
e.preventDefault(); | |||
showFilePageOverlay(target.href); | |||
} | |||
}); | |||
})(); | |||
(function() { | (function() { | ||