swiper
Version:
Most modern mobile touch slider and framework with hardware accelerated transitions
2 lines • 2.03 kB
JavaScript
const History=({swiper:e,extendParams:t,on:a})=>{t({history:{enabled:!1,root:"",replaceState:!1,key:"slides",keepQuery:!1}});let s=!1,r={key:void 0,value:void 0};function i(){return e.params.history}const n=e=>e.toString().replace(/\s+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+/,"").replace(/-+$/,""),l=e=>{let t;t=e?new URL(e):window.location;const a=t.pathname.slice(1).split("/").filter(e=>""!==e),s=a.length;return{key:a[s-2],value:a[s-1]}},o=(t,a)=>{const r=i();if(!s||!r.enabled)return;let l;l=e.params.url?new URL(e.params.url):window.location;const o=!!e.params.virtual?.enabled,d=e.virtual&&o?e.slidesEl.querySelector(`[data-swiper-slide-index="${a}"]`):e.slides[a];if(!d)return;let p=n(d.getAttribute("data-history")||"");const c=r.root;if(c.length>0){p=`${"/"===c[c.length-1]?c.slice(0,c.length-1):c}/${t?`${t}/`:""}${p}`}else l.pathname.includes(t||"")||(p=`${t?`${t}/`:""}${p}`);r.keepQuery&&(p+=l.search);const u=window.history.state;u&&u.value===p||(r.replaceState?window.history.replaceState({value:p},"",p):window.history.pushState({value:p},"",p))},d=(t,a,s)=>{if(a)for(let r=0,i=e.slides.length;r<i;r+=1){const i=e.slides[r];if(n(i.getAttribute("data-history")||"")===a){const a=e.getSlideIndex(i);e.slideTo(a,t,s)}}else e.slideTo(0,t,s)},p=()=>{r=l(e.params.url),d(e.params.speed,r.value,!1)};a("init",()=>{i().enabled&&(()=>{const t=e.params.history;if(t){if(!window.history||!window.history.pushState){t.enabled=!1;const a=e.params.hashNavigation;return void(a&&(a.enabled=!0))}s=!0,r=l(e.params.url),r.key||r.value?(d(0,r.value,e.params.runCallbacksOnInit),t.replaceState||window.addEventListener("popstate",p)):t.replaceState||window.addEventListener("popstate",p)}})()}),a("destroy",()=>{i().enabled&&(i().replaceState||window.removeEventListener("popstate",p))}),a("transitionEnd _freeModeNoMomentumRelease",()=>{s&&o(i().key,e.activeIndex)}),a("slideChange",()=>{s&&e.params.cssMode&&o(i().key,e.activeIndex)})};export{History as default};
//# sourceMappingURL=history.min.mjs.map