templates/ProfilePreview/components/media.js.twig line 1

Open in your IDE?
  1. <script type="module">
  2.     import PhotoSwipeLightbox from 'https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.2/photoswipe-lightbox.esm.min.js';
  3.     async function initLightbox() {
  4.         const imageData = [
  5.             {%- set photo = profile|avatar -%}
  6.             {%- if photo and photo.path -%}
  7.                 {
  8.                     src: '{{ responsive_asset(photo.path, 'profile_media', "fullsize", "jpg") }}',
  9.                     alt: 'Avatar'
  10.                 },
  11.             {%- endif -%}
  12.             {%- if profile.photos is defined and profile.photos|length > 0 -%}
  13.                 {%- for photo in profile.photos -%}
  14.                     {%- if photo.path -%}
  15.                         {
  16.                             src: '{{ responsive_asset(photo.path, "profile_media", "fullsize", "jpg") }}',
  17.                             alt: 'Фото профиля {{loop.index}}'
  18.                         },
  19.                     {%- endif -%}
  20.                 {%- endfor -%}
  21.             {%- endif -%}
  22.             {%- if profile.selfies is defined -%}
  23.                 {%- for photo in profile.selfies -%}
  24.                     {%- if photo.path -%}
  25.                         {
  26.                             src: '{{ responsive_asset(photo.path, "profile_media", "fullsize", "jpg") }}',
  27.                             alt: 'Селфи профиля {{ loop.index }}'
  28.                         }{% if not loop.last %},{% endif %}
  29.                     {%- endif -%}
  30.                 {%- endfor -%}
  31.             {%- endif -%}
  32.         ];
  33.         const dataSource = [];
  34.         for (const item of imageData) {
  35.             const img = new Image();
  36.             img.src = item.src;
  37.             await new Promise(resolve => {
  38.                 img.onload = resolve;
  39.                 img.onerror = resolve;
  40.             });
  41.             dataSource.push({
  42.                 src: item.src,
  43.                 width: img.naturalWidth || 1,
  44.                 height: img.naturalHeight || 1,
  45.                 alt: item.alt
  46.             });
  47.         }
  48.         const lightbox = new PhotoSwipeLightbox({
  49.             dataSource,
  50.             showHideAnimationType: 'none',
  51.             pswpModule: () => import('https://cdnjs.cloudflare.com/ajax/libs/photoswipe/5.4.2/photoswipe.esm.min.js'),
  52.             preload: [1, 2],
  53.         });
  54.         lightbox.init();
  55.         document.querySelectorAll('a[data-gallery]').forEach(el => {
  56.             el.addEventListener('click', e => {
  57.                 e.preventDefault();
  58.                 const id = +e.currentTarget.getAttribute('data-index');
  59.                 lightbox.loadAndOpen(id); // defines start slide index
  60.             });
  61.         });
  62.     }
  63.     initLightbox();
  64. </script>
  65. <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" />