UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

3 lines (2 loc) • 3.39 kB
/*! 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,s=!1,a=!1;const l=()=>{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}},c=()=>{const t=n?.getInstance(),e=t?.getState();return!(!t||0!==e&&1!==e)},u=()=>{if(!n)return;if(c())return;const{urlSlug:t,urlIndex:e}=l();if(!t)return;let o=document.querySelector(`[data-slug="${t}"]`);o&&n.fromTriggerEl(o),c()||(o=document.querySelectorAll(`[data-fancybox="${t}"]`)[e-1],o&&n.fromTriggerEl(o,{startIndex:e-1})),c()&&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:""))||""},g=t=>{const e=d(t),n=t.getSlide();if(!n||!e)return"";let o=parseInt(n.index+"",10)+1,r=n.slug?`#${n.slug}`:`#${e}-${o}`;return(t.getCarousel()?.getPages()?.length||0)<2&&(r=`#${e}`),r},f=()=>{if(!n)return;if(s)return;const t=n?.getInstance(),o=t?.getCarousel(),{urlSlug:r,urlIndex:c}=l(),d=t?.getOptions().Hash;if(!1!==d){if(t&&1===t.getState()&&o){const n=o.getSlides();for(const t of n||[])if(t.slug===r||(t.fancybox===r||e(d)&&d.slug===r)&&t.index===c-1)return i=!1,void o.goTo(t.index);a=!0,t.close(),a=!1}u()}},h=()=>{n&&(o=setTimeout(()=>{r=!0,u(),r=!1},300),window.addEventListener("hashchange",f,!1))};let w;function m(){history.scrollRestoration&&w&&(history.scrollRestoration=w,w=void 0)}const p=()=>{let t,e="";function c(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;if(r){const e=t.getOptions().sync;e&&e.goTo(t?.getCarousel()?.getPageIndex()||0,{transition:!1,tween:!1})}const n=t.getCarousel();if(!n)return;if(!t.getSlide())return;const o=d(t);if(!o)return;const{urlHash:s,urlSlug:a}=l(),c=g(t);s!==c&&(e=s),history.scrollRestoration&&!w&&(w=history.scrollRestoration,history.scrollRestoration="manual",window.addEventListener("beforeunload",m)),n.on("change",u);const f=o!==a;try{window.history[f?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+c),f&&(i=!0)}catch(t){}}function u(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;if(!t.getSlide())return;if(!d(t))return;const e=g(t);s=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+e)}catch(t){}s=!1}function f(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash||a)return;if(d(t)){s=!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"!==document.activeElement?.nodeName?window.history.back():window.history.replaceState({},document.title,window.location.pathname+window.location.search+e)}catch(t){}s=!1}}return{init:function(e){clearTimeout(o),t=e,t.on("ready",c),t.on("close",f)},destroy:function(){t?.off("ready",c),t?.off("close",f);const e=t?.getCarousel();e&&e.off("change",u),t=void 0,n?.getInstance()||(m(),window.removeEventListener("beforeunload",m))}}};p.getInfoFromURL=l,p.startFromUrl=u,p.setup=function(e){n||(n=e,t()&&(/complete|interactive|loaded/.test(document.readyState)?h():document.addEventListener("DOMContentLoaded",h)))};export{p as Hash};