PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / lightbox-gallery / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/lightbox-gallery/frontend.js

86 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* Lightbox Gallery — frontend.js */
2 ( function () {
3 function initGallery( wrap ) {
4 const items = Array.from( wrap.querySelectorAll( '.bklg-item[data-src]' ) );
5 const showCaptions = wrap.dataset.captions === '1';
6 if ( ! items.length ) return;
7
8 const images = items.map( item => ({ src: item.dataset.src, caption: item.dataset.caption || '' }) );
9 let current = 0;
10 let lb = null;
11
12 function openLightbox( idx ) {
13 current = idx;
14 lb = document.createElement('div');
15 lb.className = 'bklg-lightbox';
16 lb.setAttribute('role', 'dialog');
17 lb.setAttribute('aria-modal', 'true');
18
19 const inner = document.createElement('div'); inner.className = 'bklg-lb-inner';
20 const img = document.createElement('img'); img.className = 'bklg-lb-img';
21 const counter = document.createElement('div'); counter.className = 'bklg-lb-counter';
22 const close = document.createElement('button'); close.className = 'bklg-lb-close'; close.innerHTML = '×'; close.setAttribute('aria-label', 'Close');
23 const prev = document.createElement('button'); prev.className = 'bklg-lb-prev'; prev.innerHTML = '‹'; prev.setAttribute('aria-label', 'Previous');
24 const next = document.createElement('button'); next.className = 'bklg-lb-next'; next.innerHTML = '›'; next.setAttribute('aria-label', 'Next');
25
26 function show( i ) {
27 current = ( i + images.length ) % images.length;
28 img.src = images[current].src;
29 counter.textContent = ( current + 1 ) + ' / ' + images.length;
30 caption.textContent = images[current].caption || '';
31 }
32
33 const caption = document.createElement('p'); caption.className = 'bklg-lb-caption';
34
35 inner.appendChild(img);
36 if ( showCaptions ) inner.appendChild(caption);
37 lb.appendChild(inner);
38 lb.appendChild(close);
39 if ( images.length > 1 ) { lb.appendChild(prev); lb.appendChild(next); }
40 lb.appendChild(counter);
41 document.body.appendChild(lb);
42 document.body.style.overflow = 'hidden';
43 show(idx);
44
45 close.addEventListener('click', closeLightbox);
46 lb.addEventListener('click', function(e){ if(e.target===lb) closeLightbox(); });
47 prev.addEventListener('click', function(e){ e.stopPropagation(); show(current-1); });
48 next.addEventListener('click', function(e){ e.stopPropagation(); show(current+1); });
49
50 document.addEventListener('keydown', onKey);
51
52 // Touch swipe
53 let tx = 0;
54 lb.addEventListener('touchstart', e=>{ tx=e.changedTouches[0].clientX; }, {passive:true});
55 lb.addEventListener('touchend', e=>{ const dx=e.changedTouches[0].clientX-tx; if(Math.abs(dx)>50) show(dx<0?current+1:current-1); }, {passive:true});
56 }
57
58 function closeLightbox() {
59 if (lb) { lb.remove(); lb=null; }
60 document.body.style.overflow = '';
61 document.removeEventListener('keydown', onKey);
62 }
63
64 function onKey(e) {
65 if (e.key === 'Escape') closeLightbox();
66 if (e.key === 'ArrowLeft') { const p = lb && lb.querySelector('.bklg-lb-prev'); p && p.click(); }
67 if (e.key === 'ArrowRight') { const n = lb && lb.querySelector('.bklg-lb-next'); n && n.click(); }
68 }
69
70 items.forEach( function(item, i) {
71 item.querySelector('.bklg-thumb').addEventListener('click', function() { openLightbox(i); });
72 item.querySelector('.bklg-thumb').style.cursor = 'zoom-in';
73 });
74 }
75
76 function init() {
77 document.querySelectorAll('.bklg-wrap[data-lightbox]').forEach(initGallery);
78 }
79
80 if ( document.readyState === 'loading' ) {
81 document.addEventListener('DOMContentLoaded', init);
82 } else {
83 init();
84 }
85 }() );
86