MediaWiki:Gadget-AbsyFancyBoxy.js: различия между версиями
Ланселот (обсуждение | вклад) Нет описания правки |
Ланселот (обсуждение | вклад) Нет описания правки |
||
| Строка 2: | Строка 2: | ||
* Гаджет: AbsyFancyBoxy для Absurdopedia.wiki | * Гаджет: AbsyFancyBoxy для Absurdopedia.wiki | ||
* Локальный лайтбокс с листанием галереи и подписями. | * Локальный лайтбокс с листанием галереи и подписями. | ||
* | * Без внешних библиотек. Не конфликтует с TimedMediaHandler. | ||
*/ | */ | ||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
// ---------- Константы ---------- | |||
var MEDIA_EXT = /\.(webm|ogv|ogg|oga|mp4|mp3|flac|opus|wav)$/i; | var MEDIA_EXT = /\.(webm|ogv|ogg|oga|mp4|mp3|flac|opus|wav)$/i; | ||
var SAFE_URL = /^(https?:)?\/\//i; | |||
var CAN_HOVER = window.matchMedia && window.matchMedia( '(hover: hover)' ).matches; | |||
// ---------- Утилиты ---------- | |||
function isSafeUrl( url ) { | |||
return url && ( SAFE_URL.test( url ) || url.charAt( 0 ) === '/' ); | |||
} | |||
function getFullImageUrl( thumbSrc ) { | function getFullImageUrl( thumbSrc ) { | ||
if ( !thumbSrc || thumbSrc.indexOf( '/thumb/' ) === -1 ) { | if ( !thumbSrc || thumbSrc.indexOf( '/thumb/' ) === -1 ) { | ||
| Строка 21: | Строка 28: | ||
} | } | ||
function getCaption( link, img ) { | function getCaption( link, img ) { | ||
var galleryItem = link.closest( '.gallerybox, li.gallerybox' ); | var galleryItem = link.closest( '.gallerybox, li.gallerybox' ); | ||
| Строка 40: | Строка 46: | ||
} | } | ||
// | function isMediaLink( link ) { | ||
var href = link.getAttribute( 'href' ) || ''; | |||
if ( MEDIA_EXT.test( href ) ) return true; | |||
if ( link.querySelector( 'video, audio, .mw-tmh-player, .oo-ui-widget' ) ) return true; | |||
if ( link.closest( '.mw-tmh-player' ) ) return true; | |||
var container = link.closest( 'figure, .thumb, .mw-halign-left, .mw-halign-right, .mw-halign-center, .mw-halign-none' ); | |||
if ( container && container.querySelector( '.mw-tmh-player, video, audio' ) ) return true; | |||
return false; | |||
} | |||
// ---------- Сбор галереи ---------- | |||
function collectGallery( clickedLink ) { | function collectGallery( clickedLink ) { | ||
var items = []; | var items = []; | ||
| Строка 49: | Строка 65: | ||
var links = root.querySelectorAll( 'a.image, a.mw-file-description' ); | var links = root.querySelectorAll( 'a.image, a.mw-file-description' ); | ||
links.forEach( function ( link ) { | links.forEach( function ( link ) { | ||
if ( isMediaLink( link ) ) return; | |||
if ( | |||
var img = link.querySelector( 'img' ); | var img = link.querySelector( 'img' ); | ||
if ( !img ) return; | if ( !img ) return; | ||
var thumbSrc = img.getAttribute( 'src' ); | var thumbSrc = img.getAttribute( 'src' ); | ||
if ( !thumbSrc ) return; | if ( !isSafeUrl( thumbSrc ) ) return; | ||
var fullSrc = getFullImageUrl( thumbSrc ); | |||
if ( !isSafeUrl( fullSrc ) ) return; | |||
items.push( { | items.push( { | ||
link: link, | link: link, | ||
src: fullSrc, | |||
src: | |||
caption: getCaption( link, img ) | caption: getCaption( link, img ) | ||
} ); | } ); | ||
| Строка 69: | Строка 85: | ||
} | } | ||
// | // ---------- Ленивое создание оверлея ---------- | ||
var overlay = document.createElement( 'div' ); | var overlay = null; | ||
var imgElement = null; | |||
var captionElement = null; | |||
var counterElement = null; | |||
var prevBtn = null; | |||
var nextBtn = null; | |||
var closeBtn = null; | |||
var gallery = []; | |||
var currentIndex = 0; | |||
' | |||
function buildOverlay() { | |||
overlay = document.createElement( 'div' ); | |||
overlay.id = 'absyfancyboxy-overlay'; | |||
overlay.setAttribute( 'role', 'dialog' ); | |||
overlay.setAttribute( 'aria-modal', 'true' ); | |||
overlay.setAttribute( 'aria-label', 'Просмотр изображения' ); | |||
var backdrop = document.createElement( 'div' ); | |||
backdrop.className = 'absyfancyboxy-backdrop'; | |||
backdrop.addEventListener( 'click', closeLightbox ); | |||
overlay.appendChild( backdrop ); | |||
var stage = document.createElement( 'div' ); | |||
stage.className = 'absyfancyboxy-stage'; | |||
overlay.appendChild( stage ); | |||
imgElement = document.createElement( 'img' ); | |||
imgElement.id = 'absyfancyboxy-image'; | |||
imgElement.alt = ''; | |||
imgElement.addEventListener( 'error', function () { | |||
' | captionElement.textContent = 'Не удалось загрузить изображение'; | ||
} ); | |||
stage.appendChild( imgElement ); | |||
captionElement = document.createElement( 'div' ); | |||
captionElement.id = 'absyfancyboxy-caption'; | |||
stage.appendChild( captionElement ); | |||
counterElement = document.createElement( 'div' ); | |||
counterElement.id = 'absyfancyboxy-counter'; | |||
stage.appendChild( counterElement ); | |||
closeBtn = document.createElement( 'div' ); | |||
closeBtn.id = 'absyfancyboxy-close'; | |||
closeBtn.textContent = '×'; | |||
closeBtn.setAttribute( 'role', 'button' ); | |||
closeBtn.setAttribute( 'tabindex', '0' ); | |||
closeBtn.setAttribute( 'aria-label', 'Закрыть' ); | |||
closeBtn.addEventListener( 'click', function ( e ) { | |||
' | e.stopPropagation(); | ||
' | closeLightbox(); | ||
} ); | |||
closeBtn.addEventListener( 'keydown', function ( e ) { | |||
' | if ( e.key === 'Enter' || e.key === ' ' ) { | ||
e.preventDefault(); | |||
closeLightbox(); | |||
} | |||
} ); | |||
overlay.appendChild( closeBtn ); | |||
prevBtn = makeArrow( 'prev', '‹', 'left' ); | |||
function makeArrow( dir, symbol | nextBtn = makeArrow( 'next', '›', 'right' ); | ||
stage.appendChild( prevBtn ); | |||
stage.appendChild( nextBtn ); | |||
if ( CAN_HOVER ) { | |||
setArrowsVisible( false ); | |||
overlay.addEventListener( 'mouseenter', function () { setArrowsVisible( true ); } ); | |||
overlay.addEventListener( 'mouseleave', function () { setArrowsVisible( false ); } ); | |||
} | |||
stage.addEventListener( 'click', function ( e ) { | |||
if ( e.target === stage ) closeLightbox(); | |||
} ); | |||
var touchStartX = null; | |||
overlay.addEventListener( 'touchstart', function ( e ) { | |||
if ( e.touches.length === 1 ) touchStartX = e.touches[ 0 ].clientX; | |||
}, { passive: true } ); | |||
overlay.addEventListener( 'touchend', function ( e ) { | |||
if ( touchStartX === null || !e.changedTouches.length ) return; | |||
var dx = e.changedTouches[ 0 ].clientX - touchStartX; | |||
if ( Math.abs( dx ) > 50 ) { | |||
if ( dx < 0 ) next(); else prev(); | |||
} | |||
touchStartX = null; | |||
} ); | |||
document.body.appendChild( overlay ); | |||
} | |||
function makeArrow( dir, symbol ) { | |||
var btn = document.createElement( 'div' ); | var btn = document.createElement( 'div' ); | ||
btn.className = 'absyfancyboxy-arrow'; | btn.className = 'absyfancyboxy-arrow'; | ||
btn.dataset.dir = dir; | btn.dataset.dir = dir; | ||
btn.textContent = symbol; | btn.textContent = symbol; | ||
btn.setAttribute( 'role', 'button' ); | |||
btn.setAttribute( 'tabindex', '0' ); | |||
btn.setAttribute( 'aria-label', dir === 'prev' ? 'Предыдущее' : 'Следующее' ); | btn.setAttribute( 'aria-label', dir === 'prev' ? 'Предыдущее' : 'Следующее' ); | ||
btn. | btn.addEventListener( 'click', function ( e ) { | ||
e.stopPropagation(); | |||
if ( dir === 'prev' ) prev(); else next(); | |||
' | } ); | ||
btn.addEventListener( 'keydown', function ( e ) { | |||
if ( e.key === 'Enter' || e.key === ' ' ) { | |||
e.preventDefault(); | |||
' | if ( dir === 'prev' ) prev(); else next(); | ||
} | |||
} ); | |||
return btn; | return btn; | ||
} | } | ||
function setArrowsVisible( visible ) { | |||
if ( !prevBtn || !nextBtn ) return; | |||
[ prevBtn, nextBtn ].forEach( function ( btn ) { | |||
btn.style.opacity = visible ? '0.85' : '0'; | |||
btn.style.pointerEvents = visible ? 'auto' : 'none'; | |||
} ); | |||
} | |||
// ---------- Рендер и управление ---------- | |||
function render() { | function render() { | ||
if ( !gallery.length ) return; | if ( !gallery.length ) return; | ||
var item = gallery[ currentIndex ]; | var item = gallery[ currentIndex ]; | ||
imgElement.src = item.src; | imgElement.src = item.src; | ||
imgElement.alt = item.caption || ''; | |||
captionElement.textContent = item.caption || ''; | captionElement.textContent = item.caption || ''; | ||
counterElement.textContent = ( currentIndex + 1 ) + ' / ' + gallery.length; | counterElement.textContent = ( currentIndex + 1 ) + ' / ' + gallery.length; | ||
var multiple = gallery.length > 1; | var multiple = gallery.length > 1; | ||
prevBtn.style.display = multiple ? 'block' : 'none'; | prevBtn.style.display = multiple ? 'block' : 'none'; | ||
nextBtn.style.display = multiple ? 'block' : 'none'; | nextBtn.style.display = multiple ? 'block' : 'none'; | ||
counterElement.style.display = multiple ? 'block' : 'none'; | counterElement.style.display = multiple ? 'block' : 'none'; | ||
if ( CAN_HOVER && multiple ) setArrowsVisible( false ); | |||
} | } | ||
function openLightbox( items, index ) { | function openLightbox( items, index ) { | ||
if ( !overlay ) buildOverlay(); | |||
gallery = items; | gallery = items; | ||
currentIndex = index || 0; | currentIndex = index || 0; | ||
overlay. | overlay.classList.add( 'is-open' ); | ||
document.body.style.overflow = 'hidden'; | document.body.style.overflow = 'hidden'; | ||
render(); | render(); | ||
if ( closeBtn ) closeBtn.focus(); | |||
} | } | ||
function closeLightbox() { | function closeLightbox() { | ||
overlay. | if ( !overlay ) return; | ||
overlay.classList.remove( 'is-open' ); | |||
imgElement.src = ''; | imgElement.src = ''; | ||
imgElement.alt = ''; | |||
captionElement.textContent = ''; | captionElement.textContent = ''; | ||
counterElement.textContent = ''; | counterElement.textContent = ''; | ||
| Строка 251: | Строка 258: | ||
} | } | ||
// | // ---------- Клавиатура ---------- | ||
document.addEventListener( 'keydown', function ( e ) { | document.addEventListener( 'keydown', function ( e ) { | ||
if ( overlay. | if ( !overlay || !overlay.classList.contains( 'is-open' ) ) return; | ||
if ( e.key === 'Escape' ) closeLightbox(); | if ( e.key === 'Escape' ) closeLightbox(); | ||
else if ( e.key === 'ArrowRight' ) next(); | else if ( e.key === 'ArrowRight' ) next(); | ||
| Строка 275: | Строка 266: | ||
} ); | } ); | ||
// | // ---------- Перехват клика ---------- | ||
document.addEventListener( 'click', function ( e ) { | document.addEventListener( 'click', function ( e ) { | ||
var link = e.target.closest( 'a.image, a.mw-file-description' ); | var link = e.target.closest( 'a.image, a.mw-file-description' ); | ||
if ( !link ) return; | if ( !link ) return; | ||
if ( !link.closest( '.mw-parser-output' ) ) return; | if ( !link.closest( '.mw-parser-output' ) ) return; | ||
if ( isMediaLink( link ) ) return; | |||
if ( | |||
var items = collectGallery( link ); | var items = collectGallery( link ); | ||
| Строка 335: | Строка 283: | ||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | e.stopPropagation(); | ||
e.stopImmediatePropagation(); | |||
openLightbox( items, idx ); | openLightbox( items, idx ); | ||
}, true ); | }, true ); | ||
window. | // ---------- Сброс при AJAX-навигации ---------- | ||
if ( window.mw && mw.hook ) { | |||
mw.hook( 'wikipage.content' ).add( function () { | |||
if ( overlay && overlay.classList.contains( 'is-open' ) ) closeLightbox(); | |||
} ); | |||
} | |||
} )(); | } )(); | ||