UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

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