UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

1 lines 3.21 kB
!/*! License details at fancyapps.com/license */function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).window=t.window||{})}(this,function(t){"use strict";let e,o,n=!1,i=!1,r=!1,l=!1,a=()=>{let t=new URL(document.URL).hash,e=t.slice(1).split("-"),o=e[e.length-1],n=o&&/^\+?\d+$/.test(o)&&parseInt(e.pop()||"1",10)||1;return{urlHash:t,urlSlug:e.join("-"),urlIndex:n}},s=()=>{let t=e?.getInstance();return!!t&&1==t.getState()},c=()=>{if(!e||s())return;let{urlSlug:t,urlIndex:o}=a();if(!t)return;let n=document.querySelector(`[data-slug="${t}"]`);n&&e.fromTriggerEl(n),!s()&&(n=document.querySelectorAll(`[data-fancybox="${t}"]`)[o-1])&&e.fromTriggerEl(n,{startIndex:o-1}),s()&&n&&!n.closest("[inert]")&&n.scrollIntoView({behavior:"instant",block:"center",inline:"center"})},d=()=>{if(!e||r)return;let t=e?.getInstance(),o=t?.getCarousel();if(t?.getOptions().Hash===!1)return;let{urlSlug:n,urlIndex:s}=a();if(t&&o){for(let t of o.getSlides()||[])if(n===t.slug||n===t.fancybox&&t.index===s-1){i=!1,o.goTo(t.index);return}l=!0,t.close(),l=!1}c()},u=()=>{e&&(o=setTimeout(()=>{n=!0,c(),n=!1},300),window.addEventListener("hashchange",d,!1))},f=()=>{let t,e="auto",s="";function c(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;if(n){let e=t.getOptions().sync;e&&e.goTo(t?.getCarousel()?.getPageIndex()||0,{transition:!1,tween:!1})}let o=t.getCarousel();if(!o)return;let{urlHash:r,urlSlug:l}=a(),c=t.getSlide();if(!c)return;let u=c.slug||c.fancybox||"",f=parseInt(c.index+"",10)+1;if(!u)return;let g=c.slug?`#${c.slug}`:`#${u}-${f}`;2>(t.getCarousel()?.getPages()?.length||0)&&(g=`#${u}`),r!==g&&(s=r),history.scrollRestoration&&(e=history.scrollRestoration,history.scrollRestoration="manual"),o.on("change",d);let w=u!==l;try{window.history[w?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+g),w&&(i=!0)}catch(t){}}function d(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;let e=t.getSlide();if(!e)return;let o=e.slug||e.fancybox||"",n=e.index+1,i=e.slug?`#${e.slug}`:`#${o}-${n}`;r=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+i)}catch(t){}r=!1}function u(){if(l||!t||!t.isTopMost()||!1===t.getOptions().Hash)return;let e=t.getSlide();if(e&&e.fancybox){r=!0;try{i&&!function(){if(window.parent===window)return!1;try{var t=window.frameElement}catch(e){t=null}return null===t?"data:"===location.protocol:t.hasAttribute("sandbox")}()?window.history.back():window.history.replaceState({},document.title,window.location.pathname+window.location.search+s)}catch(t){}r=!1}}return{init:function(e){clearTimeout(o),(t=e).on("ready",c),t.on("close",u)},destroy:function(){t?.off("ready",c),t?.off("close",u);let o=t?.getCarousel();o&&o.off("change",d),t=void 0,history.scrollRestoration&&e&&(history.scrollRestoration=e)}}};f.startFromUrl=c,f.setup=function(t){!e&&(e=t,"undefined"!=typeof window&&window.document&&window.document.createElement&&(/complete|interactive|loaded/.test(document.readyState)?u():document.addEventListener("DOMContentLoaded",u)))},t.Hash=f});