UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

1 lines 3.01 kB
!/*! License details at fancyapps.com/license */function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).window=t.window||{})}(this,function(t){"use strict";let e,n=!1,o=!1,i=!1,a=!1,r=()=>{let 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{hash:t,slug:e.join("-"),index:o}},l=()=>{if(!e||e.getInstance())return;let{slug:t,index:n}=r();if(!t)return;let o=document.querySelector(`[data-slug="${t}"]`);if(o&&o.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0})),!e||e.getInstance())return;let i=document.querySelectorAll(`[data-fancybox="${t}"]`);i.length&&(o=i[n-1])&&o.dispatchEvent(new CustomEvent("click",{bubbles:!0,cancelable:!0}))},c=()=>{if(!e||i)return;let t=e?.getInstance();if(t?.getOptions().Hash===!1)return;let{slug:n,index:o}=r(),c=t?.getSlide()?.fancybox||t?.getSlide()?.slug||void 0;t&&c&&(n===c?t.getCarousel()?.goTo(o-1):(a=!0,t.close(),a=!1)),l()},s=()=>{e&&setTimeout(()=>{n=!0,l(),n=!1,window.addEventListener("hashchange",c,!1)},300)},d=()=>{let t,e="auto",l="";function c(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;let i=t.getCarousel();if(!i)return;let{hash:a,slug:c}=r(),d=t.getSlide();if(!d)return;let u=d.fancybox||"",f=parseInt(d.index+"",10)+1;if(!u)return;let w=`#${u}-${f}`;if(a!==w&&(l=a),history.scrollRestoration&&(e=history.scrollRestoration,history.scrollRestoration="manual"),i.on("change",s),!n)if(u===c)try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+w)}catch(t){}else try{window.history.pushState({},document.title,window.location.pathname+window.location.search+w),o=!0}catch(t){}}function s(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;let{slug:e}=r(),n=t.getSlide();if(!n)return;let o=n.fancybox||"",a=n.index+1,l=`#${o}-${a}`;if(o===e){i=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+l)}catch(t){}i=!1}}function d(){if(a||!t||!t.isTopMost()||!1===t.getOptions().Hash)return;let e=t.getSlide();if(e&&e.fancybox){i=!0;try{o&&!n&&!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")}()?window.history.back():window.history.replaceState({},document.title,window.location.pathname+window.location.search+l)}catch(t){}i=!1}}return{init:function(e){(t=e).on("ready",c),t.on("close",d)},destroy:function(){t?.off("ready",c),t?.off("close",d);let n=t?.getCarousel();n&&n.off("change",s),t=void 0,history.scrollRestoration&&e&&(history.scrollRestoration=e)}}};d.startFromUrl=l,d.setup=function(t){!e&&(e=t,"undefined"!=typeof window&&window.document&&window.document.createElement&&(/complete|interactive|loaded/.test(document.readyState)?s():document.addEventListener("DOMContentLoaded",s)))},t.Hash=d});