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