(function () { 'use strict'; var remoteBase = 'https://cdn.jsdelivr.net/npm/photoswipe@5.4.4/dist/'; var config = window.kalmInnPhotoSwipe || {}; var galleries = Array.prototype.slice.call(document.querySelectorAll('[data-kalm-gallery]')); config.cssUrl = config.cssUrl || remoteBase + 'photoswipe.css'; config.lightboxUrl = config.lightboxUrl || remoteBase + 'photoswipe-lightbox.esm.js'; config.coreUrl = config.coreUrl || remoteBase + 'photoswipe.esm.js'; config.fallbackCssUrl = config.fallbackCssUrl || remoteBase + 'photoswipe.css'; config.fallbackLightboxUrl = config.fallbackLightboxUrl || remoteBase + 'photoswipe-lightbox.esm.js'; config.fallbackCoreUrl = config.fallbackCoreUrl || remoteBase + 'photoswipe.esm.js'; if (!galleries.length) { return; } function normalizeUrl(url) { try { return new URL(url, document.baseURI).href; } catch (error) { return ''; } } function assetHasMime(url, expectedPattern) { if (!url || typeof window.fetch !== 'function') { return Promise.resolve(false); } return window.fetch(url, { method: 'HEAD', credentials: 'same-origin' }).then(function (response) { var contentType = response.headers.get('content-type') || ''; return response.ok && expectedPattern.test(contentType); }).catch(function () { return false; }); } function photoSwipeStylesAreReady() { var probe; var ready; if (!document.body) { return false; } probe = document.createElement('div'); probe.className = 'pswp'; probe.hidden = true; document.body.appendChild(probe); ready = window.getComputedStyle(probe).position === 'fixed'; probe.remove(); return ready; } function loadStylesheet(url) { var normalizedUrl = normalizeUrl(url); if (!normalizedUrl) { return Promise.reject(new Error('Invalid PhotoSwipe stylesheet URL.')); } return new Promise(function (resolve, reject) { var links = Array.prototype.slice.call(document.querySelectorAll('link[rel="stylesheet"]')); var link = links.find(function (candidate) { return normalizeUrl(candidate.href) === normalizedUrl; }); var elapsed = 0; var interval; var timeout = 3000; function cleanup() { window.clearInterval(interval); link.removeEventListener('error', onError); } function onError() { cleanup(); reject(new Error('PhotoSwipe stylesheet could not be loaded.')); } if (!link) { link = document.createElement('link'); link.rel = 'stylesheet'; link.href = normalizedUrl; link.setAttribute('data-kalm-photoswipe-style', 'true'); document.head.appendChild(link); } link.addEventListener('error', onError, { once: true }); interval = window.setInterval(function () { elapsed += 50; if (photoSwipeStylesAreReady()) { cleanup(); resolve(); } else if (elapsed >= timeout) { cleanup(); reject(new Error('PhotoSwipe stylesheet did not become active.')); } }, 50); }); } function ensureStyles() { var primaryUrl = normalizeUrl(config.cssUrl); var fallbackUrl = normalizeUrl(config.fallbackCssUrl); if (photoSwipeStylesAreReady()) { return Promise.resolve(); } function usePrimaryStyles() { if (!config.usesLocalModules) { return loadStylesheet(primaryUrl); } return assetHasMime(primaryUrl, /^text\/css(?:;|$)/i).then(function (validMime) { if (!validMime) { throw new Error('Local PhotoSwipe CSS has an invalid MIME type.'); } return loadStylesheet(primaryUrl); }); } return usePrimaryStyles().catch(function (error) { if (!fallbackUrl || fallbackUrl === primaryUrl) { throw error; } return loadStylesheet(fallbackUrl); }); } function importModules(lightboxUrl, coreUrl) { return Promise.all([ import(lightboxUrl), import(coreUrl) ]); } function loadModules() { var primaryLightbox = normalizeUrl(config.lightboxUrl); var primaryCore = normalizeUrl(config.coreUrl); var fallbackLightbox = normalizeUrl(config.fallbackLightboxUrl); var fallbackCore = normalizeUrl(config.fallbackCoreUrl); function usePrimaryModules() { if (!config.usesLocalModules) { return importModules(primaryLightbox, primaryCore); } return Promise.all([ assetHasMime(primaryLightbox, /(?:java|ecma)script/i), assetHasMime(primaryCore, /(?:java|ecma)script/i) ]).then(function (validMime) { if (!validMime[0] || !validMime[1]) { throw new Error('Local PhotoSwipe modules have an invalid MIME type.'); } return importModules(primaryLightbox, primaryCore); }); } return usePrimaryModules().catch(function (error) { if ( !fallbackLightbox || !fallbackCore || (fallbackLightbox === primaryLightbox && fallbackCore === primaryCore) ) { throw error; } return importModules(fallbackLightbox, fallbackCore); }); } function enableHoverEffect(gallery) { gallery.querySelectorAll('.kalm-gallery__item').forEach(function (item) { if (item.dataset.kalmGalleryHoverReady) { return; } item.dataset.kalmGalleryHoverReady = 'true'; item.addEventListener('mousemove', function (event) { var rect = item.getBoundingClientRect(); var x = ((event.clientX - rect.left) / rect.width - 0.5) * -10; var y = ((event.clientY - rect.top) / rect.height - 0.5) * -10; item.style.setProperty('--kalm-gallery-hover-x', x + 'px'); item.style.setProperty('--kalm-gallery-hover-y', y + 'px'); }); item.addEventListener('mouseleave', function () { item.style.removeProperty('--kalm-gallery-hover-x'); item.style.removeProperty('--kalm-gallery-hover-y'); }); }); } galleries.forEach(enableHoverEffect); Promise.all([ensureStyles(), loadModules()]).then(function (assets) { var modules = assets[1]; var PhotoSwipeLightbox = modules[0] && modules[0].default; var PhotoSwipe = modules[1] && modules[1].default; if (typeof PhotoSwipeLightbox !== 'function' || typeof PhotoSwipe !== 'function') { throw new Error('PhotoSwipe modules do not expose the expected constructors.'); } window.kalmInnPhotoSwipeInstances = window.kalmInnPhotoSwipeInstances || []; galleries.forEach(function (gallery) { var items = gallery.querySelectorAll('.kalm-gallery__item[href][data-pswp-width][data-pswp-height]'); var lightbox; if (!items.length || gallery.dataset.kalmPhotoSwipeReady) { return; } lightbox = new PhotoSwipeLightbox({ gallery: gallery, children: '.kalm-gallery__item[href][data-pswp-width][data-pswp-height]', pswpModule: PhotoSwipe, appendToEl: document.body, mainClass: 'kalm-photoswipe' }); lightbox.init(); gallery.dataset.kalmPhotoSwipeReady = 'true'; window.kalmInnPhotoSwipeInstances.push(lightbox); }); }).catch(function (error) { if (window.console && typeof window.console.warn === 'function') { window.console.warn('[Kalm Inn] PhotoSwipe non disponibile; i link alle immagini restano attivi.', error); } }); }());