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