@fancyapps/ui
Version:
Robust JavaScript UI Component Library
1 lines • 3.57 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,i=t=>"object"==typeof t&&null!==t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t),r=!1,l=!1,a=!1,s=!1,c=()=>{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}},d=()=>{let t=e?.getInstance(),o=t?.getState();return!!t&&(0===o||1===o)},u=()=>{if(!e||d())return;let{urlSlug:t,urlIndex:o}=c();if(!t)return;let n=document.querySelector(`[data-slug="${t}"]`);n&&e.fromTriggerEl(n),!d()&&(n=document.querySelectorAll(`[data-fancybox="${t}"]`)[o-1])&&e.fromTriggerEl(n,{startIndex:o-1}),d()&&n&&!n.closest("[inert]")&&n.scrollIntoView({behavior:"instant",block:"center",inline:"center"})},f=t=>{let e=t.getOptions().Hash,o=t.getSlide();return o&&(o.slug||o.fancybox||(i(e)?e.slug:""))||""},g=t=>{let e=f(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},w=()=>{if(!e||a)return;let t=e?.getInstance(),o=t?.getCarousel(),{urlSlug:n,urlIndex:r}=c(),d=t?.getOptions().Hash;if(!1!==d){if(t&&1===t.getState()&&o){for(let t of o.getSlides()||[])if(t.slug===n||(t.fancybox===n||i(d)&&d.slug===n)&&t.index===r-1){l=!1,o.goTo(t.index);return}s=!0,t.close(),s=!1}u()}},h=()=>{e&&(o=setTimeout(()=>{r=!0,u(),r=!1},300),window.addEventListener("hashchange",w,!1))};function p(){history.scrollRestoration&&n&&(history.scrollRestoration=n,n=void 0)}let m=()=>{let t,i="";function d(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash)return;if(r){let e=t.getOptions().sync;e&&e.goTo(t?.getCarousel()?.getPageIndex()||0,{transition:!1,tween:!1})}let e=t.getCarousel();if(!e||!t.getSlide())return;let o=f(t);if(!o)return;let{urlHash:a,urlSlug:s}=c(),d=g(t);a!==d&&(i=a),history.scrollRestoration&&!n&&(n=history.scrollRestoration,history.scrollRestoration="manual",window.addEventListener("beforeunload",p)),e.on("change",u);let w=o!==s;try{window.history[w?"pushState":"replaceState"]({},document.title,window.location.pathname+window.location.search+d),w&&(l=!0)}catch(t){}}function u(){if(!t||!t.isTopMost()||!1===t.getOptions().Hash||!t.getSlide()||!f(t))return;let e=g(t);a=!0;try{window.history.replaceState({},document.title,window.location.pathname+window.location.search+e)}catch(t){}a=!1}function w(){if(t&&t.isTopMost()&&!1!==t.getOptions().Hash&&!s&&f(t)){a=!0;try{l&&!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+i)}catch(t){}a=!1}}return{init:function(e){clearTimeout(o),(t=e).on("ready",d),t.on("close",w)},destroy:function(){t?.off("ready",d),t?.off("close",w);let o=t?.getCarousel();o&&o.off("change",u),t=void 0,e?.getInstance()||(p(),window.removeEventListener("beforeunload",p))}}};m.getInfoFromURL=c,m.startFromUrl=u,m.setup=function(t){!e&&(e=t,"u">typeof window&&window.document&&window.document.createElement&&(/complete|interactive|loaded/.test(document.readyState)?h():document.addEventListener("DOMContentLoaded",h)))},t.Hash=m});