UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

3 lines (2 loc) 3.28 kB
/*! License details at fancyapps.com/license */ import{canUseDOM as t}from"../utils/canUseDOM.js";let e,n,o=!1,r=!1,i=!1,l=!1;const s=()=>{const t=new URL(document.URL).hash,e=t.slice(1).split("-"),n=e[e.length-1],o=n&&/^\+?\d+$/.test(n)&&parseInt(e.pop()||"1",10)||1;return{urlHash:t,urlSlug:e.join("-"),urlIndex:o}},a=()=>{const t=null==e?void 0:e.getInstance();return!(!t||1!=t.getState())},u=()=>{if(!e)return;if(a())return;const{urlSlug:t,urlIndex:n}=s();if(!t)return;let o=document.querySelector(`[data-slug="${t}"]`);o&&e.fromTriggerEl(o),a()||(o=document.querySelectorAll(`[data-fancybox="${t}"]`)[n-1],o&&e.fromTriggerEl(o,{startIndex:n-1})),a()&&o&&!o.closest("[inert]")&&o.scrollIntoView({behavior:"instant",block:"center",inline:"center"})},c=()=>{if(!e)return;if(i)return;const t=null==e?void 0:e.getInstance(),n=null==t?void 0:t.getCarousel();if(!1===(null==t?void 0:t.getOptions().Hash))return;const{urlSlug:o,urlIndex:a}=s();if(t&&n){const e=n.getSlides();for(const t of e||[])if(o===t.slug||o===t.fancybox&&t.index===a-1)return r=!1,void n.goTo(t.index);l=!0,t.close(),l=!1}u()},d=()=>{e&&(n=setTimeout((()=>{o=!0,u(),o=!1}),300),window.addEventListener("hashchange",c,!1))},f=()=>{let t,e="auto",a="";function u(){var n,i,l;if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;if(o){const e=t.getOptions().sync;e&&e.goTo((null===(n=null==t?void 0:t.getCarousel())||void 0===n?void 0:n.getPageIndex())||0,{transition:!1,tween:!1})}const u=t.getCarousel();if(!u)return;const{urlHash:d,urlSlug:f}=s(),g=t.getSlide();if(!g)return;let h=g.slug||g.fancybox||"",w=parseInt(g.index+"",10)+1;if(!h)return;let p=g.slug?`#${g.slug}`:`#${h}-${w}`;((null===(l=null===(i=t.getCarousel())||void 0===i?void 0:i.getPages())||void 0===l?void 0:l.length)||0)<2&&(p=`#${h}`),d!==p&&(a=d),history.scrollRestoration&&(e=history.scrollRestoration,history.scrollRestoration="manual"),u.on("change",c);const y=h!==f;try{window.history[y?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+p),y&&(r=!0)}catch(t){}}function c(){if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;const e=t.getSlide();if(!e)return;let n=e.slug||e.fancybox||"",o=e.index+1,r=e.slug?`#${e.slug}`:`#${n}-${o}`;i=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+r)}catch(t){}i=!1}function d(){if(l)return;if(!t||!t.isTopMost())return;if(!1===t.getOptions().Hash)return;const e=t.getSlide();if(!e)return;if(e.fancybox||""){i=!0;try{r&&!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+a)}catch(t){}i=!1}}return{init:function(e){clearTimeout(n),t=e,t.on("ready",u),t.on("close",d)},destroy:function(){null==t||t.off("ready",u),null==t||t.off("close",d);const n=null==t?void 0:t.getCarousel();n&&n.off("change",c),t=void 0,history.scrollRestoration&&e&&(history.scrollRestoration=e)}}};f.startFromUrl=u,f.setup=function(n){e||(e=n,t()&&(/complete|interactive|loaded/.test(document.readyState)?d():document.addEventListener("DOMContentLoaded",d)))};export{f as Hash};