@fancyapps/ui
Version:
Robust JavaScript UI Component Library
3 lines (2 loc) • 3.67 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:""))||""},g=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},f=()=>{if(!n)return;if(l)return;const t=null==n?void 0:n.getInstance(),o=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()&&o){const n=o.getSlides();for(const t of n||[])if(t.slug===r||(t.fancybox===r||e(d)&&d.slug===r)&&t.index===u-1)return i=!1,void o.goTo(t.index);s=!0,t.close(),s=!1}c()}},h=()=>{n&&(o=setTimeout(()=>{r=!0,c(),r=!1},300),window.addEventListener("hashchange",f,!1))};let w;function v(){history.scrollRestoration&&w&&(history.scrollRestoration=w,w=void 0)}const m=()=>{let t,e="";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 o=t.getCarousel();if(!o)return;if(!t.getSlide())return;const l=d(t);if(!l)return;const{urlHash:s,urlSlug:u}=a(),f=g(t);s!==f&&(e=s),history.scrollRestoration&&!w&&(w=history.scrollRestoration,history.scrollRestoration="manual",window.addEventListener("beforeunload",v)),o.on("change",c);const h=l!==u;try{window.history[h?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+f),h&&(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 n;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===(n=document.activeElement)||void 0===n?void 0:n.nodeName)?window.history.back():window.history.replaceState({},document.title,window.location.pathname+window.location.search+e)}catch(t){}l=!1}}return{init:function(e){clearTimeout(o),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 e=null==t?void 0:t.getCarousel();e&&e.off("change",c),t=void 0,(null==n?void 0:n.getInstance())||(v(),window.removeEventListener("beforeunload",v))}}};m.getInfoFromURL=a,m.startFromUrl=c,m.setup=function(e){n||(n=e,t()&&(/complete|interactive|loaded/.test(document.readyState)?h():document.addEventListener("DOMContentLoaded",h)))};export{m as Hash};