UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

3 lines (2 loc) 3.11 kB
/*! License details at fancyapps.com/license */ import{canUseDOM as t}from"../utils/canUseDOM.js";let n,o=!1,e=!1,i=!1,l=!1;const r=()=>{const t=new URL(document.URL).hash,n=t.slice(1).split("-"),o=n[n.length-1],e=o&&/^\+?\d+$/.test(o)&&parseInt(n.pop()||"1",10)||1;return{hash:t,slug:n.join("-"),index:e}},a=()=>{if(!n||n.getInstance())return;const{slug:t,index:o}=r();if(!t)return;let e=document.querySelector(`[data-slug="${t}"]`);if(e&&e.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0})),!n||n.getInstance())return;const i=document.querySelectorAll(`[data-fancybox="${t}"]`);i.length&&(e=i[o-1],e&&e.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0})))},s=()=>{var t,o,e;if(!n)return;if(i)return;const s=null==n?void 0:n.getInstance();if(!1===(null==s?void 0:s.getOptions().Hash))return;const{slug:c,index:u}=r(),d=(null===(t=null==s?void 0:s.getSlide())||void 0===t?void 0:t.fancybox)||(null===(o=null==s?void 0:s.getSlide())||void 0===o?void 0:o.slug)||void 0;s&&d&&(c===d?null===(e=s.getCarousel())||void 0===e||e.goTo(u-1):(l=!0,s.close(),l=!1)),a()},c=()=>{n&&setTimeout((()=>{o=!0,a(),o=!1,window.addEventListener("hashchange",s,!1)}),300)},u=()=>{let t,n="auto",a="";function s(){if(!(null==t?void 0:t.isTopMost()))return;if(!1===(null==t?void 0:t.getOptions().Hash))return;const i=null==t?void 0:t.getCarousel();if(!i)return;const{hash:l,slug:s}=r(),u=null==t?void 0:t.getSlide();if(!u)return;let d=u.fancybox||"",h=parseInt(u.index+"",10)+1;if(!d)return;let f=`#${d}-${h}`;if(l!==f&&(a=l),history.scrollRestoration&&(n=history.scrollRestoration,history.scrollRestoration="manual"),i.on("change",c),!o)if(d===s)try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+f)}catch(t){}else try{window.history.pushState({},document.title,window.location.pathname+window.location.search+f),e=!0}catch(t){}}function c(){if(!(null==t?void 0:t.isTopMost()))return;const{slug:n}=r(),o=null==t?void 0:t.getSlide();if(!o)return;let e=o.fancybox||"",l=`#${e}-${o.index+1}`;if(e===n){i=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+l)}catch(t){}i=!1}}function u(){if(!(null==t?void 0:t.isTopMost()))return;if(!1===(null==t?void 0:t.getOptions().Hash))return;const n=null==t?void 0:t.getSlide();if(!n)return;if(n.fancybox&&!l){i=!0;try{!e||o||function(){if(window.parent===window)return!1;try{var t=window.frameElement}catch(n){t=null}return null===t?"data:"===location.protocol:t.hasAttribute("sandbox")}()?window.history.replaceState({},document.title,window.location.pathname+window.location.search+a):window.history.back()}catch(t){}i=!1}}return{init:function(n){t=n,t.on("ready",s),t.on("close",u)},destroy:function(){null==t||t.off("ready",s),null==t||t.off("close",u);const o=null==t?void 0:t.getCarousel();o&&o.off("change",c),t=void 0,history.scrollRestoration&&n&&(history.scrollRestoration=n)}}};u.startFromUrl=a,u.setup=function(o){n||(n=o,t()&&(/complete|interactive|loaded/.test(document.readyState)?c():document.addEventListener("DOMContentLoaded",c)))};export{u as Hash};