<script type="module">
import PhotoSwipeLightbox from 'https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.2/photoswipe-lightbox.esm.min.js';
async function initLightbox() {
const imageData = [
{%- set photo = profile|avatar -%}
{%- if photo and photo.path -%}
{
src: '{{ responsive_asset(photo.path, 'profile_media', "fullsize", "jpg") }}',
alt: 'Avatar'
},
{%- endif -%}
{%- if profile.photos is defined and profile.photos|length > 0 -%}
{%- for photo in profile.photos -%}
{%- if photo.path -%}
{
src: '{{ responsive_asset(photo.path, "profile_media", "fullsize", "jpg") }}',
alt: 'Фото профиля {{loop.index}}'
},
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if profile.selfies is defined -%}
{%- for photo in profile.selfies -%}
{%- if photo.path -%}
{
src: '{{ responsive_asset(photo.path, "profile_media", "fullsize", "jpg") }}',
alt: 'Селфи профиля {{ loop.index }}'
}{% if not loop.last %},{% endif %}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
];
const dataSource = [];
for (const item of imageData) {
const img = new Image();
img.src = item.src;
await new Promise(resolve => {
img.onload = resolve;
img.onerror = resolve;
});
dataSource.push({
src: item.src,
width: img.naturalWidth || 1,
height: img.naturalHeight || 1,
alt: item.alt
});
}
const lightbox = new PhotoSwipeLightbox({
dataSource,
showHideAnimationType: 'none',
pswpModule: () => import('https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.2/photoswipe.esm.min.js'),
preload: [1, 2],
});
lightbox.init();
document.querySelectorAll('a[data-gallery]').forEach(el => {
el.addEventListener('click', e => {
e.preventDefault();
const id = +e.currentTarget.getAttribute('data-index');
lightbox.loadAndOpen(id); // defines start slide index
});
});
}
initLightbox();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.2/photoswipe.min.css" integrity="sha512-LFWtdAXHQuwUGH9cImO9blA3a3GfQNkpF2uRlhaOpSbDevNyK1rmAjs13mtpjvWyi+flP7zYWboqY+8Mkd42xA==" crossorigin="anonymous" referrerpolicy="no-referrer" />