@fancyapps/ui
Version:
Robust JavaScript UI Component Library
1 lines • 3.44 kB
JavaScript
!/*! License details at fancyapps.com/license */function(t,e){"object"==typeof exports&&"u">typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="u">typeof globalThis?globalThis:t||self).window=t.window||{})}(this,function(t){"use strict";let e,o,n=t=>"object"==typeof t&&null!==t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t),i=!1,r=!1,l=!1,a=!1,s=()=>{let t=new URL(document.URL).hash,e=t.slice(1).split("-"),o=e[e.length-1],n=o&&/^\+?\d+$/.test(o)&&parseInt(e.pop()||"1",10)||1;return{urlHash:t,urlSlug:e.join("-"),urlIndex:n}},c=()=>{let t=e?.getInstance(),o=t?.getState();return!!t&&(0===o||1===o)},u=()=>{if(!e||c())return;let{urlSlug:t,urlIndex:o}=s();if(!t)return;let n=document.querySelector(`[data-slug="${t}"]`);n&&e.fromTriggerEl(n),!c()&&(n=document.querySelectorAll(`[data-fancybox="${t}"]`)[o-1])&&e.fromTriggerEl(n,{startIndex:o-1}),c()&&n&&!n.closest("[inert]")&&n.scrollIntoView({behavior:"instant",block:"center",inline:"center"})},d=t=>{let e=t.getOptions().Hash,o=t.getSlide();return o&&(o.slug||o.fancybox||(n(e)?e.slug:""))||""},f=t=>{let e=d(t),o=t.getSlide();if(!o||!e)return"";let n=parseInt(o.index+"",10)+1,i=o.slug?`#${o.slug}`:`#${e}-${n}`;return 2>(t.getCarousel()?.getPages()?.length||0)&&(i=`#${e}`),i},g=()=>{if(!e||l)return;let t=e?.getInstance(),o=t?.getCarousel(),{urlSlug:i,urlIndex:c}=s(),d=t?.getOptions().Hash;if(!1!==d){if(t&&1===t.getState()&&o){for(let t of o.getSlides()||[])if(t.slug===i||(t.fancybox===i||n(d)&&d.slug===i)&&t.index===c-1){r=!1,o.goTo(t.index);return}a=!0,t.close(),a=!1}u()}},w=()=>{e&&(o=setTimeout(()=>{i=!0,u(),i=!1},300),window.addEventListener("hashchange",g,!1))},h=()=>{let t,e="auto",n="";function c(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;if(i){let e=t.getOptions().sync;e&&e.goTo(t?.getCarousel()?.getPageIndex()||0,{transition:!1,tween:!1})}let o=t.getCarousel();if(!o||!t.getSlide())return;let l=d(t);if(!l)return;let{urlHash:a,urlSlug:c}=s(),g=f(t);a!==g&&(n=a),history.scrollRestoration&&(e=history.scrollRestoration,history.scrollRestoration="manual"),o.on("change",u);let w=l!==c;try{window.history[w?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+g),w&&(r=!0)}catch(t){}}function u(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash||!t.getSlide()||!d(t))return;let e=f(t);l=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+e)}catch(t){}l=!1}function g(){if(t&&t.isTopMost()&&!1!==t.getOptions().Hash&&!a&&d(t)){l=!0;try{r&&!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")}()&&document.activeElement?.nodeName!=="IFRAME"?window.history.back():window.history.replaceState({},document.title,window.location.pathname+window.location.search+n)}catch(t){}l=!1}}return{init:function(e){clearTimeout(o),(t=e).on("ready",c),t.on("close",g)},destroy:function(){t?.off("ready",c),t?.off("close",g);let o=t?.getCarousel();o&&o.off("change",u),t=void 0,history.scrollRestoration&&e&&(history.scrollRestoration=e)}}};h.getInfoFromURL=s,h.startFromUrl=u,h.setup=function(t){!e&&(e=t,"u">typeof window&&window.document&&window.document.createElement&&(/complete|interactive|loaded/.test(document.readyState)?w():document.addEventListener("DOMContentLoaded",w)))},t.Hash=h});