Comparing version 4.0.0-rc.23 to 4.0.0-rc.24
@@ -1,2 +0,2 @@ | ||
import t from"delegate-it";import{match as e}from"path-to-regexp";function i(){return i=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(t[s]=i[s])}return t},i.apply(this,arguments)}const s=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",n=({hash:t}={})=>location.pathname+location.search+(t?location.hash:""),o=(t,e={})=>{const s=i({url:t=t||n({hash:!0}),random:Math.random(),source:"swup"},e);history.pushState(s,"",t)},r=(t=null,e={})=>{t=t||n({hash:!0});const s=i({},history.state,{url:t,random:Math.random(),source:"swup"},e);history.replaceState(s,"",t)},a=(e,s,n,o)=>{const r=new AbortController;return o=i({},o,{signal:r.signal}),t(e,s,n,o),{destroy:()=>r.abort()}};class h extends URL{constructor(t,e=document.baseURI){super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new h(e)}static fromUrl(t){return new h(t)}}const c=(t,i)=>{try{return e(t,i)}catch(e){throw new Error(`[swup] Error parsing path "${t}":\n${e}`)}};class l{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){e=i({},e,{url:t=this.resolve(t)}),this.pages.set(t,e),this.swup.hooks.triggerSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e=i({},this.get(t),e,{url:t}),this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.triggerSync("cache:clear")}prune(t){this.pages.forEach((e,i)=>{t(i,e)&&this.delete(i)})}resolve(t){const{url:e}=h.fromUrl(t);return this.swup.resolveUrl(e)}}const u=(t,e=document)=>e.querySelector(t),p=(t,e=document)=>Array.from(e.querySelectorAll(t)),g=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function d(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}function m(t,e=[],i={}){return new Promise((s,n)=>{const o=t.apply(i,e);d(o)?o.then(s,n):s(o)})}function f(t){var e;return null==(e=t=t||document.body)?void 0:e.offsetHeight}const w=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,v=t=>1e3*Number(t.slice(0,-1).replace(",","."));class y{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?p(this.selector):[]}add(...t){this.targets.forEach(e=>e.classList.add(...t))}remove(...t){this.targets.forEach(e=>e.classList.remove(...t))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function k({to:t,from:e=this.currentPageUrl,hash:i,animate:s=!0,animation:n,el:o,event:r,action:a="push",resetScroll:h=!0}){return{from:{url:e},to:{url:t},containers:this.options.containers,animation:{animate:s,wait:!1,name:n,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:o,event:r},history:{action:a,popstate:!1,direction:void 0},scroll:{reset:h,target:i}}}class x{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:end","animation:in:start","animation:in:end","animation:skip","animation:await","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,s={}){const n=this.get(t);if(!n)return console.warn(`Hook '${t}' not found.`),()=>{};const o=i({},s,{id:n.size+1,hook:t,handler:e});return n.set(e,o),()=>this.off(t,e)}before(t,e,s={}){return this.on(t,e,i({},s,{before:!0}))}replace(t,e,s={}){return this.on(t,e,i({},s,{replace:!0}))}once(t,e,s={}){return this.on(t,e,i({},s,{once:!0}))}off(t,e){const i=this.get(t);i&&e?i.delete(e)||console.warn(`Handler for hook '${t}' not found.`):i&&i.clear()}async trigger(t,e,i){const{before:s,handler:n,after:o,replaced:r}=this.getHandlers(t,i);await this.execute(s,e);const[a]=await this.execute(n,e,r?i:void 0);return await this.execute(o,e),this.dispatchDomEvent(t,e),a}triggerSync(t,e,i){const{before:s,after:n,handler:o,replaced:r}=this.getHandlers(t,i);this.executeSync(s,e);const[a]=this.executeSync(o,e,r?i:void 0);return this.executeSync(n,e),this.dispatchDomEvent(t,e),a}async execute(t,e,i){const s=[];for(const{hook:n,handler:o,once:r}of t){const t=await m(o,[this.swup.context,e,i]);s.push(t),r&&this.off(n,o)}return s}executeSync(t,e,i){const s=[];for(const{hook:n,handler:o,once:r}of t){const t=o(this.swup.context,e,i);s.push(t),d(t)&&console.warn(`Promise returned from handler for synchronous hook '${n}'.Swup will not wait for it to resolve.`),r&&this.off(n,o)}return s}getHandlers(t,e){const i=this.get(t);if(!i)return{found:!1,before:[],handler:[],after:[],replaced:!1};const s=this.sortRegistrations,n=Array.from(i.values()),o=n.filter(({before:t,replace:e})=>t&&!e).sort(s),r=n.filter(({replace:t})=>t).sort(s),a=n.filter(({before:t,replace:e})=>!t&&!e).sort(s),h=r.length>0;let c=[];return h?c=[{id:0,hook:t,handler:r[0].handler}]:e&&(c=[{id:0,hook:t,handler:e}]),{found:!0,before:o,handler:c,after:a,replaced:h}}sortRegistrations(t,e){var i,s;return(null!=(i=t.priority)?i:0)-(null!=(s=e.priority)?s:0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const S=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let i=document.getElementById(t)||document.getElementById(e)||u(`a[name='${w(t)}']`)||u(`a[name='${w(e)}']`);return i||"top"!==t||(i=document.body),i},b="transition",U="animation";async function E({elements:t,selector:e}){if(!1===e&&!t)return;let i=[];if(t)i=Array.from(t);else if(e&&(i=p(e,document.body),!i.length))return void console.warn(`[swup] No elements found matching animationSelector \`${e}\``);const s=i.map(t=>function(t){const{type:e,timeout:i,propCount:s}=function(t,e){const i=window.getComputedStyle(t),s=P(i,`${b}Delay`),n=P(i,`${b}Duration`),o=C(s,n),r=P(i,`${U}Delay`),a=P(i,`${U}Duration`),h=C(r,a);let c=null,l=0,u=0;return l=Math.max(o,h),c=l>0?o>h?b:U:null,u=c?c===b?n.length:a.length:0,{type:c,timeout:l,propCount:u}}(t);return!(!e||!i)&&new Promise(n=>{const o=`${e}end`,r=performance.now();let a=0;const h=()=>{t.removeEventListener(o,c),n()},c=e=>{if(e.target===t){if(!function(t){return[`${b}end`,`${U}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-r)/1e3<e.elapsedTime||++a>=s&&h()}};setTimeout(()=>{a<s&&h()},i+1),t.addEventListener(o,c)})}(t));s.filter(Boolean).length>0?await Promise.all(s):e&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${e}\``)}function P(t,e){return(t[e]||"").split(", ")}function C(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,i)=>v(e)+v(t[i])))}function H(t,e={},s={}){if(this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:n,hash:o}=h.fromUrl(t);this.context=this.createContext(i({},s,{to:n,hash:o})),this.performVisit(n,e)}async function $(t,e={}){if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:i}=this.context.trigger;this.context.to.url=h.fromUrl(t).url,e.referrer=e.referrer||this.currentPageUrl,!1===e.animate&&(this.context.animation.animate=!1),this.context.animation.animate||this.classes.clear();const s=e.history||(null==i?void 0:i.getAttribute("data-swup-history"))||void 0;s&&["push","replace"].includes(s)&&(this.context.history.action=s);const a=e.animation||(null==i?void 0:i.getAttribute("data-swup-animation"))||void 0;a&&(this.context.animation.name=a);try{await this.hooks.trigger("visit:start");const i=this.hooks.trigger("page:request",{url:this.context.to.url,options:e},async(t,{options:e})=>await this.fetchPage(t.to.url,e));if(!this.context.history.popstate){const e=t+(this.context.scroll.target||"");"replace"===this.context.history.action?r(e):o(e,{index:this.currentHistoryIndex+1})}if(this.currentPageUrl=n(),this.context.animation.wait){const{html:t}=await i;this.context.to.html=t}const s=this.animatePageOut(),[a]=await Promise.all([i,s]);await this.renderPage(this.context.to.url,a),await this.animatePageIn(),await this.hooks.trigger("visit:end",void 0,()=>this.classes.clear())}catch(t){if(!t)return;console.error(t),this.options.skipPopStateHandling=()=>(window.location.href=this.context.to.url,!0),window.history.go(-1)}}class A extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}async function q(t,e={}){if(t=h.fromUrl(t).url,this.cache.has(t)){const i=this.cache.get(t);return!1!==e.triggerHooks&&await this.hooks.trigger("page:load",{page:i,cache:!0}),i}const s=i({},this.options.requestHeaders,e.headers);e=i({},e,{headers:s});const n=await this.hooks.trigger("fetch:request",{url:t,options:e},(t,{url:e,options:i})=>fetch(e,i)),{status:o,url:r}=n,a=await n.text();if(500===o)throw this.hooks.trigger("fetch:error",{status:o,response:n,url:r}),new A(`Server error: ${r}`,{status:o,url:r});if(!a)throw new A(`Empty response: ${r}`,{status:o,url:r});const{url:c}=h.fromUrl(r),l={url:c,html:a};return t===c&&this.cache.set(l.url,l),!1!==e.triggerHooks&&await this.hooks.trigger("page:load",{page:l,cache:!1}),l}const R=async function(){this.context.animation.animate?(await this.hooks.trigger("animation:out:start",void 0,()=>{this.classes.add("is-changing","is-leaving","is-animating"),this.context.history.popstate&&this.classes.add("is-popstate"),this.context.animation.name&&this.classes.add(`to-${s(this.context.animation.name)}`)}),await this.hooks.trigger("animation:await",{direction:"out"},async(t,{direction:e})=>{await this.awaitAnimations({selector:t.animation.selector,direction:e})}),await this.hooks.trigger("animation:out:end")):await this.hooks.trigger("animation:skip")},I=function({html:t},{containers:e}=this.options){var i;const s=(new DOMParser).parseFromString(t,"text/html"),n=(null==(i=s.querySelector("title"))?void 0:i.innerText)||"";return document.title=n,e.map(t=>{const e=document.querySelector(t),i=s.querySelector(t);return e&&i?(e.replaceWith(i),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),i||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===e.length},D=async function(){if(!this.context.animation.animate)return;const t=this.hooks.trigger("animation:await",{direction:"in"},async(t,{direction:e})=>{await this.awaitAnimations({selector:t.animation.selector,direction:e})});await g(),await this.hooks.trigger("animation:in:start",void 0,()=>{this.classes.remove("is-animating")}),await t,await this.hooks.trigger("animation:in:end")},L=async function(t,e){const{url:i,html:o}=e;this.classes.remove("is-leaving"),this.isSameResolvedUrl(n(),t)&&(this.isSameResolvedUrl(n(),i)||(r(i),this.currentPageUrl=n(),this.context.to.url=this.currentPageUrl),this.context.animation.animate&&this.classes.add("is-rendering"),this.context.to.html=o,await this.hooks.trigger("content:replace",{page:e},(t,{page:e})=>{if(!this.replaceContent(e,{containers:t.containers}))throw new Error("[swup] Container mismatch, aborting");this.context.animation.animate&&(this.classes.add("is-animating","is-changing","is-rendering"),this.context.animation.name&&this.classes.add(`to-${s(this.context.animation.name)}`))}),await this.hooks.trigger("content:scroll",{options:{behavior:"auto"}},(t,{options:e})=>{if(this.context.scroll.target){const t=this.getAnchorElement(this.context.scroll.target);if(t)return void t.scrollIntoView(e)}this.context.scroll.reset&&window.scrollTo(0,0)}),await this.hooks.trigger("page:view",{url:this.currentPageUrl,title:document.title}),this.options.cache||this.cache.clear())},V=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function M(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function N(t){return this.plugins.find(e=>e===t||e.name===t)}class O{constructor(t={}){this.version="4.0.0-rc.23",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=n(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=H,this.performVisit=$,this.animatePageOut=R,this.renderPage=L,this.replaceContent=I,this.animatePageIn=D,this.delegateEvent=a,this.fetchPage=q,this.awaitAnimations=E,this.getAnchorElement=S,this.use=V,this.unuse=M,this.findPlugin=N,this.getCurrentUrl=n,this.createContext=k,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:(t,{el:e}={})=>!(null==e||!e.closest("[data-no-swup]")),linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options=i({},this.defaults,t),this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new l(this),this.classes=new y(this),this.hooks=new x(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}async enable(){const{linkSelector:t}=this.options;this.clickDelegate=this.delegateEvent(t,"click",this.linkClickHandler),window.addEventListener("popstate",this.popStateHandler),this.options.plugins.forEach(t=>this.use(t)),r(null,{index:1}),await g(),await this.hooks.trigger("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")}),await this.hooks.trigger("page:view",{url:this.currentPageUrl,title:document.title})}async destroy(){this.clickDelegate.destroy(),window.removeEventListener("popstate",this.popStateHandler),this.cache.clear(),this.options.plugins.forEach(t=>this.unuse(t)),await this.hooks.trigger("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")}),this.hooks.clear()}shouldIgnoreVisit(t,{el:e,event:i}={}){const{origin:s,url:n,hash:o}=h.fromUrl(t);return s!==window.location.origin||!(!e||!this.triggerWillOpenNewWindow(e))||!!this.options.ignoreVisit(n+o,{el:e,event:i})}linkClickHandler(t){const e=t.delegateTarget,{href:i,url:s,hash:n}=h.fromElement(e);this.shouldIgnoreVisit(i,{el:e,event:t})||(this.context=this.createContext({to:s,hash:n,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.trigger("link:newtab",{href:i}):0===t.button&&this.hooks.triggerSync("link:click",{el:e,event:t},()=>{var e;const i=null!=(e=this.context.from.url)?e:"";t.preventDefault(),s&&s!==i?this.isSameResolvedUrl(s,i)||this.performVisit(s):n?(r(s+n),this.hooks.triggerSync("link:anchor",{hash:n,options:{behavior:"auto"}},(t,{hash:e,options:i})=>{const s=this.getAnchorElement(e);s&&s.scrollIntoView(i)})):this.hooks.triggerSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var e,i,s;const o=null!=(e=null==(i=t.state)?void 0:i.url)?e:location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(n(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(o,{event:t}))return;const{url:r,hash:a}=h.fromUrl(o);this.context=this.createContext({to:r,hash:a,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(s=t.state)?void 0:s.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.triggerSync("history:popstate",{event:t},()=>{this.performVisit(r)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}export{h as Location,s as classify,o as createHistoryRecord,O as default,a as delegateEvent,w as escapeCssIdentifier,f as forceReflow,n as getCurrentUrl,d as isPromise,c as matchPath,g as nextTick,u as query,p as queryAll,m as runAsPromise,v as toMs,r as updateHistoryRecord}; | ||
import t from"delegate-it";import{match as e}from"path-to-regexp";function s(){return s=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(t[i]=s[i])}return t},s.apply(this,arguments)}const i=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",n=({hash:t}={})=>location.pathname+location.search+(t?location.hash:""),o=(t,e={})=>{const i=s({url:t=t||n({hash:!0}),random:Math.random(),source:"swup"},e);history.pushState(i,"",t)},r=(t=null,e={})=>{t=t||n({hash:!0});const i=s({},history.state,{url:t,random:Math.random(),source:"swup"},e);history.replaceState(i,"",t)},a=(e,i,n,o)=>{const r=new AbortController;return o=s({},o,{signal:r.signal}),t(e,i,n,o),{destroy:()=>r.abort()}};class l extends URL{constructor(t,e=document.baseURI){super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new l(e)}static fromUrl(t){return new l(t)}}const c=(t,s)=>{try{return e(t,s)}catch(e){throw new Error(`[swup] Error parsing path "${t}":\n${e}`)}};class h{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){e=s({},e,{url:t=this.resolve(t)}),this.pages.set(t,e),this.swup.hooks.callSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e=s({},this.get(t),e,{url:t}),this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.callSync("cache:clear")}prune(t){this.pages.forEach((e,s)=>{t(s,e)&&this.delete(s)})}resolve(t){const{url:e}=l.fromUrl(t);return this.swup.resolveUrl(e)}}const u=(t,e=document)=>e.querySelector(t),p=(t,e=document)=>Array.from(e.querySelectorAll(t)),d=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function m(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}function g(t,e=[],s={}){return new Promise((i,n)=>{const o=t.apply(s,e);m(o)?o.then(i,n):i(o)})}function f(t){var e;return null==(e=t=t||document.body)?void 0:e.offsetHeight}const w=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,v=t=>1e3*Number(t.slice(0,-1).replace(",","."));class y{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?p(this.selector):[]}add(...t){this.targets.forEach(e=>e.classList.add(...t))}remove(...t){this.targets.forEach(e=>e.classList.remove(...t))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function k({to:t,from:e=this.currentPageUrl,hash:s,animate:i=!0,animation:n,el:o,event:r,action:a="push",resetScroll:l=!0}){return{from:{url:e},to:{url:t},containers:this.options.containers,animation:{animate:i,wait:!1,name:n,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:o,event:r},history:{action:a,popstate:!1,direction:void 0},scroll:{reset:l,target:s}}}class x{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:await","animation:out:end","animation:in:start","animation:in:await","animation:in:end","animation:skip","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,i={}){const n=this.get(t);if(!n)return console.warn(`Hook '${t}' not found.`),()=>{};const o=s({},i,{id:n.size+1,hook:t,handler:e});return n.set(e,o),()=>this.off(t,e)}before(t,e,i={}){return this.on(t,e,s({},i,{before:!0}))}replace(t,e,i={}){return this.on(t,e,s({},i,{replace:!0}))}once(t,e,i={}){return this.on(t,e,s({},i,{once:!0}))}off(t,e){const s=this.get(t);s&&e?s.delete(e)||console.warn(`Handler for hook '${t}' not found.`):s&&s.clear()}async call(t,e,s){const{before:i,handler:n,after:o}=this.getHandlers(t,s);await this.run(i,e);const[r]=await this.run(n,e);return await this.run(o,e),this.dispatchDomEvent(t,e),r}callSync(t,e,s){const{before:i,handler:n,after:o}=this.getHandlers(t,s);this.runSync(i,e);const[r]=this.runSync(n,e);return this.runSync(o,e),this.dispatchDomEvent(t,e),r}async run(t,e){const s=[];for(const{hook:i,handler:n,defaultHandler:o,once:r}of t){const t=await g(n,[this.swup.context,e,o]);s.push(t),r&&this.off(i,n)}return s}runSync(t,e){const s=[];for(const{hook:i,handler:n,defaultHandler:o,once:r}of t){const t=n(this.swup.context,e,o);s.push(t),m(t)&&console.warn(`Promise returned from handler for synchronous hook '${i}'.Swup will not wait for it to resolve.`),r&&this.off(i,n)}return s}getHandlers(t,e){const s=this.get(t);if(!s)return{found:!1,before:[],handler:[],after:[],replaced:!1};const i=this.sortRegistrations,n=Array.from(s.values()),o=n.filter(({before:t,replace:e})=>t&&!e).sort(i),r=n.filter(({replace:t})=>t).sort(i),a=n.filter(({before:t,replace:e})=>!t&&!e).sort(i),l=r.length>0;let c=[];if(e&&(c=[{id:0,hook:t,handler:e}],l)){const s=r.length-1,i=t=>{const s=r[t-1];return s?(e,n)=>s.handler(e,n,i(t-1)):e};c=[{id:0,hook:t,handler:r[s].handler,defaultHandler:i(s)}]}return{found:!0,before:o,handler:c,after:a,replaced:l}}sortRegistrations(t,e){var s,i;return(null!=(s=t.priority)?s:0)-(null!=(i=e.priority)?i:0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const S=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let s=document.getElementById(t)||document.getElementById(e)||u(`a[name='${w(t)}']`)||u(`a[name='${w(e)}']`);return s||"top"!==t||(s=document.body),s},b="transition",U="animation";async function E({elements:t,selector:e}){if(!1===e&&!t)return;let s=[];if(t)s=Array.from(t);else if(e&&(s=p(e,document.body),!s.length))return void console.warn(`[swup] No elements found matching animationSelector \`${e}\``);const i=s.map(t=>function(t){const{type:e,timeout:s,propCount:i}=function(t,e){const s=window.getComputedStyle(t),i=P(s,`${b}Delay`),n=P(s,`${b}Duration`),o=H(i,n),r=P(s,`${U}Delay`),a=P(s,`${U}Duration`),l=H(r,a);let c=null,h=0,u=0;return h=Math.max(o,l),c=h>0?o>l?b:U:null,u=c?c===b?n.length:a.length:0,{type:c,timeout:h,propCount:u}}(t);return!(!e||!s)&&new Promise(n=>{const o=`${e}end`,r=performance.now();let a=0;const l=()=>{t.removeEventListener(o,c),n()},c=e=>{if(e.target===t){if(!function(t){return[`${b}end`,`${U}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-r)/1e3<e.elapsedTime||++a>=i&&l()}};setTimeout(()=>{a<i&&l()},s+1),t.addEventListener(o,c)})}(t));i.filter(Boolean).length>0?await Promise.all(i):e&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${e}\``)}function P(t,e){return(t[e]||"").split(", ")}function H(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,s)=>v(e)+v(t[s])))}function C(t,e={},i={}){if(this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:n,hash:o}=l.fromUrl(t);this.context=this.createContext(s({},i,{to:n,hash:o})),this.performVisit(n,e)}async function $(t,e={}){if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:s}=this.context.trigger;this.context.to.url=l.fromUrl(t).url,e.referrer=e.referrer||this.currentPageUrl,!1===e.animate&&(this.context.animation.animate=!1),this.context.animation.animate||this.classes.clear();const i=e.history||(null==s?void 0:s.getAttribute("data-swup-history"))||void 0;i&&["push","replace"].includes(i)&&(this.context.history.action=i);const a=e.animation||(null==s?void 0:s.getAttribute("data-swup-animation"))||void 0;a&&(this.context.animation.name=a);try{await this.hooks.call("visit:start");const s=this.hooks.call("page:request",{url:this.context.to.url,options:e},async(t,{options:e})=>await this.fetchPage(t.to.url,e));if(!this.context.history.popstate){const e=t+(this.context.scroll.target||"");"replace"===this.context.history.action?r(e):o(e,{index:this.currentHistoryIndex+1})}if(this.currentPageUrl=n(),this.context.animation.wait){const{html:t}=await s;this.context.to.html=t}const i=this.animatePageOut(),[a]=await Promise.all([s,i]);await this.renderPage(this.context.to.url,a),await this.animatePageIn(),await this.hooks.call("visit:end",void 0,()=>this.classes.clear())}catch(t){if(!t)return;console.error(t),this.options.skipPopStateHandling=()=>(window.location.href=this.context.to.url,!0),window.history.go(-1)}}class A extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}async function q(t,e={}){if(t=l.fromUrl(t).url,this.cache.has(t)){const s=this.cache.get(t);return!1!==e.triggerHooks&&await this.hooks.call("page:load",{page:s,cache:!0}),s}const i=s({},this.options.requestHeaders,e.headers);e=s({},e,{headers:i});const n=await this.hooks.call("fetch:request",{url:t,options:e},(t,{url:e,options:s})=>fetch(e,s)),{status:o,url:r}=n,a=await n.text();if(500===o)throw this.hooks.call("fetch:error",{status:o,response:n,url:r}),new A(`Server error: ${r}`,{status:o,url:r});if(!a)throw new A(`Empty response: ${r}`,{status:o,url:r});const{url:c}=l.fromUrl(r),h={url:c,html:a};return t===c&&this.cache.set(h.url,h),!1!==e.triggerHooks&&await this.hooks.call("page:load",{page:h,cache:!1}),h}const R=async function(){this.context.animation.animate?(await this.hooks.call("animation:out:start",void 0,()=>{this.classes.add("is-changing","is-leaving","is-animating"),this.context.history.popstate&&this.classes.add("is-popstate"),this.context.animation.name&&this.classes.add(`to-${i(this.context.animation.name)}`)}),await this.hooks.call("animation:out:await",{skip:!1},async(t,{skip:e})=>{e||await this.awaitAnimations({selector:t.animation.selector})}),await this.hooks.call("animation:out:end")):await this.hooks.call("animation:skip")},I=function({html:t},{containers:e}=this.options){var s;const i=(new DOMParser).parseFromString(t,"text/html"),n=(null==(s=i.querySelector("title"))?void 0:s.innerText)||"";return document.title=n,e.map(t=>{const e=document.querySelector(t),s=i.querySelector(t);return e&&s?(e.replaceWith(s),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),s||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===e.length},D=async function(){if(!this.context.animation.animate)return;const t=this.hooks.call("animation:in:await",{skip:!1},async(t,{skip:e})=>{e||await this.awaitAnimations({selector:t.animation.selector})});await d(),await this.hooks.call("animation:in:start",void 0,()=>{this.classes.remove("is-animating")}),await t,await this.hooks.call("animation:in:end")},L=async function(t,e){const{url:s,html:o}=e;this.classes.remove("is-leaving"),this.isSameResolvedUrl(n(),t)&&(this.isSameResolvedUrl(n(),s)||(r(s),this.currentPageUrl=n(),this.context.to.url=this.currentPageUrl),this.context.animation.animate&&this.classes.add("is-rendering"),this.context.to.html=o,await this.hooks.call("content:replace",{page:e},(t,{page:e})=>{if(!this.replaceContent(e,{containers:t.containers}))throw new Error("[swup] Container mismatch, aborting");this.context.animation.animate&&(this.classes.add("is-animating","is-changing","is-rendering"),this.context.animation.name&&this.classes.add(`to-${i(this.context.animation.name)}`))}),await this.hooks.call("content:scroll",{options:{behavior:"auto"}},(t,{options:e})=>{if(this.context.scroll.target){const t=this.getAnchorElement(this.context.scroll.target);if(t)return void t.scrollIntoView(e)}this.context.scroll.reset&&window.scrollTo(0,0)}),await this.hooks.call("page:view",{url:this.currentPageUrl,title:document.title}),this.options.cache||this.cache.clear())},V=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function M(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function N(t){return this.plugins.find(e=>e===t||e.name===t)}class O{constructor(t={}){this.version="4.0.0-rc.24",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=n(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=C,this.performVisit=$,this.animatePageOut=R,this.renderPage=L,this.replaceContent=I,this.animatePageIn=D,this.delegateEvent=a,this.fetchPage=q,this.awaitAnimations=E,this.getAnchorElement=S,this.use=V,this.unuse=M,this.findPlugin=N,this.getCurrentUrl=n,this.createContext=k,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:(t,{el:e}={})=>!(null==e||!e.closest("[data-no-swup]")),linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options=s({},this.defaults,t),this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new h(this),this.classes=new y(this),this.hooks=new x(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}async enable(){const{linkSelector:t}=this.options;this.clickDelegate=this.delegateEvent(t,"click",this.linkClickHandler),window.addEventListener("popstate",this.popStateHandler),this.options.plugins.forEach(t=>this.use(t)),r(null,{index:1}),await d(),await this.hooks.call("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")}),await this.hooks.call("page:view",{url:this.currentPageUrl,title:document.title})}async destroy(){this.clickDelegate.destroy(),window.removeEventListener("popstate",this.popStateHandler),this.cache.clear(),this.options.plugins.forEach(t=>this.unuse(t)),await this.hooks.call("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")}),this.hooks.clear()}shouldIgnoreVisit(t,{el:e,event:s}={}){const{origin:i,url:n,hash:o}=l.fromUrl(t);return i!==window.location.origin||!(!e||!this.triggerWillOpenNewWindow(e))||!!this.options.ignoreVisit(n+o,{el:e,event:s})}linkClickHandler(t){const e=t.delegateTarget,{href:s,url:i,hash:n}=l.fromElement(e);this.shouldIgnoreVisit(s,{el:e,event:t})||(this.context=this.createContext({to:i,hash:n,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.call("link:newtab",{href:s}):0===t.button&&this.hooks.callSync("link:click",{el:e,event:t},()=>{var e;const s=null!=(e=this.context.from.url)?e:"";t.preventDefault(),i&&i!==s?this.isSameResolvedUrl(i,s)||this.performVisit(i):n?(r(i+n),this.hooks.callSync("link:anchor",{hash:n,options:{behavior:"auto"}},(t,{hash:e,options:s})=>{const i=this.getAnchorElement(e);i&&i.scrollIntoView(s)})):this.hooks.callSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var e,s,i;const o=null!=(e=null==(s=t.state)?void 0:s.url)?e:location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(n(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(o,{event:t}))return;const{url:r,hash:a}=l.fromUrl(o);this.context=this.createContext({to:r,hash:a,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(i=t.state)?void 0:i.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.callSync("history:popstate",{event:t},()=>{this.performVisit(r)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}export{l as Location,i as classify,o as createHistoryRecord,O as default,a as delegateEvent,w as escapeCssIdentifier,f as forceReflow,n as getCurrentUrl,m as isPromise,c as matchPath,d as nextTick,u as query,p as queryAll,g as runAsPromise,v as toMs,r as updateHistoryRecord}; | ||
//# sourceMappingURL=Swup.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import t from"delegate-it";import{match as e}from"path-to-regexp";const n=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",r=function(t){let{hash:e}=void 0===t?{}:t;return location.pathname+location.search+(e?location.hash:"")},o=function(t,e){void 0===e&&(e={});const n={url:t=t||r({hash:!0}),random:Math.random(),source:"swup",...e};history.pushState(n,"",t)},i=function(t,e){void 0===t&&(t=null),void 0===e&&(e={}),t=t||r({hash:!0});const n={...history.state,url:t,random:Math.random(),source:"swup",...e};history.replaceState(n,"",t)},s=(e,n,r,o)=>{const i=new AbortController;return o={...o,signal:i.signal},t(e,n,r,o),{destroy:()=>i.abort()}};class a extends URL{constructor(t,e){void 0===e&&(e=document.baseURI),super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new a(e)}static fromUrl(t){return new a(t)}}const c=(t,n)=>{try{return e(t,n)}catch(e){throw new Error(`[swup] Error parsing path "${t}":\n${e}`)}};class l{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){t=this.resolve(t),e={...e,url:t},this.pages.set(t,e),this.swup.hooks.triggerSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e={...this.get(t),...e,url:t},this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.triggerSync("cache:clear")}prune(t){this.pages.forEach((e,n)=>{t(n,e)&&this.delete(n)})}resolve(t){const{url:e}=a.fromUrl(t);return this.swup.resolveUrl(e)}}const u=function(t,e){return void 0===e&&(e=document),e.querySelector(t)},h=function(t,e){return void 0===e&&(e=document),Array.from(e.querySelectorAll(t))},m=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function d(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}function f(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new Promise((r,o)=>{const i=t.apply(n,e);d(i)?i.then(r,o):r(i)})}function p(t){var e;return null==(e=t=t||document.body)?void 0:e.offsetHeight}const g=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,v=t=>1e3*Number(t.slice(0,-1).replace(",","."));class w{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?h(this.selector):[]}add(){this.targets.forEach(t=>t.classList.add(...[].slice.call(arguments)))}remove(){this.targets.forEach(t=>t.classList.remove(...[].slice.call(arguments)))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function y(t){let{to:e,from:n=this.currentPageUrl,hash:r,animate:o=!0,animation:i,el:s,event:a,action:c="push",resetScroll:l=!0}=t;return{from:{url:n},to:{url:e},containers:this.options.containers,animation:{animate:o,wait:!1,name:i,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:s,event:a},history:{action:c,popstate:!1,direction:void 0},scroll:{reset:l,target:r}}}const P="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function k(t,e,n){if(!t.s){if(n instanceof x){if(!n.s)return void(n.o=k.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(k.bind(null,t,e),k.bind(null,t,2));t.s=e,t.v=n;const r=t.o;r&&r(t)}}const x=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const r=new t,o=this.s;if(o){const t=1&o?e:n;if(t){try{k(r,1,t(this.v))}catch(t){k(r,2,t)}return r}return this}return this.o=function(t){try{const o=t.v;1&t.s?k(r,1,e?e(o):o):n?k(r,1,n(o)):k(r,2,o)}catch(t){k(r,2,t)}},r},t}();function S(t){return t instanceof x&&1&t.s}class b{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:end","animation:in:start","animation:in:end","animation:skip","animation:await","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,n){void 0===n&&(n={});const r=this.get(t);if(!r)return console.warn(`Hook '${t}' not found.`),()=>{};const o=r.size+1,i={...n,id:o,hook:t,handler:e};return r.set(e,i),()=>this.off(t,e)}before(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,before:!0})}replace(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,replace:!0})}once(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,once:!0})}off(t,e){const n=this.get(t);n&&e?n.delete(e)||console.warn(`Handler for hook '${t}' not found.`):n&&n.clear()}trigger(t,e,n){try{const r=this,{before:o,handler:i,after:s,replaced:a}=r.getHandlers(t,n);return Promise.resolve(r.execute(o,e)).then(function(){return Promise.resolve(r.execute(i,e,a?n:void 0)).then(function(n){let[o]=n;return Promise.resolve(r.execute(s,e)).then(function(){return r.dispatchDomEvent(t,e),o})})})}catch(t){return Promise.reject(t)}}triggerSync(t,e,n){const{before:r,after:o,handler:i,replaced:s}=this.getHandlers(t,n);this.executeSync(r,e);const[a]=this.executeSync(i,e,s?n:void 0);return this.executeSync(o,e),this.dispatchDomEvent(t,e),a}execute(t,e,n){try{const r=this,o=[],i=function(t,e,n){if("function"==typeof t[P]){var r,o,i,s=t[P]();if(function t(n){try{for(;!(r=s.next()).done;)if((n=e(r.value))&&n.then){if(!S(n))return void n.then(t,i||(i=k.bind(null,o=new x,2)));n=n.v}o?k(o,1,n):o=n}catch(t){k(o||(o=new x),2,t)}}(),s.return){var a=function(t){try{r.done||s.return()}catch(t){}return t};if(o&&o.then)return o.then(a,function(t){throw a(t)});a()}return o}if(!("length"in t))throw new TypeError("Object is not iterable");for(var c=[],l=0;l<t.length;l++)c.push(t[l]);return function(t,e,n){var r,o,i=-1;return function n(s){try{for(;++i<t.length;)if((s=e(i))&&s.then){if(!S(s))return void s.then(n,o||(o=k.bind(null,r=new x,2)));s=s.v}r?k(r,1,s):r=s}catch(t){k(r||(r=new x),2,t)}}(),r}(c,function(t){return e(c[t])})}(t,function(t){let{hook:i,handler:s,once:a}=t;return Promise.resolve(f(s,[r.swup.context,e,n])).then(function(t){o.push(t),a&&r.off(i,s)})});return Promise.resolve(i&&i.then?i.then(function(){return o}):o)}catch(t){return Promise.reject(t)}}executeSync(t,e,n){const r=[];for(const{hook:o,handler:i,once:s}of t){const t=i(this.swup.context,e,n);r.push(t),d(t)&&console.warn(`Promise returned from handler for synchronous hook '${o}'.Swup will not wait for it to resolve.`),s&&this.off(o,i)}return r}getHandlers(t,e){const n=this.get(t);if(!n)return{found:!1,before:[],handler:[],after:[],replaced:!1};const r=this.sortRegistrations,o=Array.from(n.values()),i=o.filter(t=>{let{before:e,replace:n}=t;return e&&!n}).sort(r),s=o.filter(t=>{let{replace:e}=t;return e}).sort(r),a=o.filter(t=>{let{before:e,replace:n}=t;return!e&&!n}).sort(r),c=s.length>0;let l=[];return c?l=[{id:0,hook:t,handler:s[0].handler}]:e&&(l=[{id:0,hook:t,handler:e}]),{found:!0,before:i,handler:l,after:a,replaced:c}}sortRegistrations(t,e){return(t.priority??0)-(e.priority??0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const U=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let n=document.getElementById(t)||document.getElementById(e)||u(`a[name='${g(t)}']`)||u(`a[name='${g(e)}']`);return n||"top"!==t||(n=document.body),n},E=function(t){let{elements:e,selector:n}=t;try{if(!1===n&&!e)return Promise.resolve();let t=[];if(e)t=Array.from(e);else if(n&&(t=h(n,document.body),!t.length))return console.warn(`[swup] No elements found matching animationSelector \`${n}\``),Promise.resolve();const r=t.map(t=>function(t){const{type:e,timeout:n,propCount:r}=function(t,e){const n=window.getComputedStyle(t),r=$(n,`${C}Delay`),o=$(n,`${C}Duration`),i=A(r,o),s=$(n,`${H}Delay`),a=$(n,`${H}Duration`),c=A(s,a);let l=null,u=0,h=0;return e===C?i>0&&(l=C,u=i,h=o.length):e===H?c>0&&(l=H,u=c,h=a.length):(u=Math.max(i,c),l=u>0?i>c?C:H:null,h=l?l===C?o.length:a.length:0),{type:l,timeout:u,propCount:h}}(t);return!(!e||!n)&&new Promise(o=>{const i=`${e}end`,s=performance.now();let a=0;const c=()=>{t.removeEventListener(i,l),o()},l=e=>{if(e.target===t){if(!function(t){return[`${C}end`,`${H}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-s)/1e3<e.elapsedTime||++a>=r&&c()}};setTimeout(()=>{a<r&&c()},n+1),t.addEventListener(i,l)})}(t));return r.filter(Boolean).length>0?Promise.resolve(Promise.all(r)).then(function(){}):(n&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${n}\``),Promise.resolve())}catch(t){return Promise.reject(t)}},C="transition",H="animation";function $(t,e){return(t[e]||"").split(", ")}function A(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,n)=>v(e)+v(t[n])))}const q=function(t,e){void 0===e&&(e={});try{const n=this;if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:s}=n.context.trigger;n.context.to.url=a.fromUrl(t).url,e.referrer=e.referrer||n.currentPageUrl,!1===e.animate&&(n.context.animation.animate=!1),n.context.animation.animate||n.classes.clear();const c=e.history||(null==s?void 0:s.getAttribute("data-swup-history"))||void 0;c&&["push","replace"].includes(c)&&(n.context.history.action=c);const l=e.animation||(null==s?void 0:s.getAttribute("data-swup-animation"))||void 0;return l&&(n.context.animation.name=l),Promise.resolve(function(s,a){try{var c=Promise.resolve(n.hooks.trigger("visit:start")).then(function(){function s(){const t=n.animatePageOut();return Promise.resolve(Promise.all([a,t])).then(function(t){let[e]=t;return Promise.resolve(n.renderPage(n.context.to.url,e)).then(function(){return Promise.resolve(n.animatePageIn()).then(function(){return Promise.resolve(n.hooks.trigger("visit:end",void 0,()=>n.classes.clear())).then(function(){})})})})}const a=n.hooks.trigger("page:request",{url:n.context.to.url,options:e},function(t,e){let{options:r}=e;try{return Promise.resolve(n.fetchPage(t.to.url,r))}catch(t){return Promise.reject(t)}});if(!n.context.history.popstate){const e=t+(n.context.scroll.target||"");"replace"===n.context.history.action?i(e):o(e,{index:n.currentHistoryIndex+1})}n.currentPageUrl=r();const c=function(){if(n.context.animation.wait)return Promise.resolve(a).then(function(t){let{html:e}=t;n.context.to.html=e})}();return c&&c.then?c.then(s):s()})}catch(t){return a(t)}return c&&c.then?c.then(void 0,a):c}(0,function(t){t&&(console.error(t),n.options.skipPopStateHandling=()=>(window.location.href=n.context.to.url,!0),window.history.go(-1))}))}catch(t){return Promise.reject(t)}};function j(t,e,n){if(void 0===e&&(e={}),void 0===n&&(n={}),this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:r,hash:o}=a.fromUrl(t);this.context=this.createContext({...n,to:r,hash:o}),this.performVisit(r,e)}const R=function(t,e){void 0===e&&(e={});try{let r;const o=this;function n(n){if(r)return n;const i={...o.options.requestHeaders,...e.headers};return e={...e,headers:i},Promise.resolve(o.hooks.trigger("fetch:request",{url:t,options:e},(t,e)=>{let{url:n,options:r}=e;return fetch(n,r)})).then(function(n){const{status:r,url:i}=n;return Promise.resolve(n.text()).then(function(s){if(500===r)throw o.hooks.trigger("fetch:error",{status:r,response:n,url:i}),new I(`Server error: ${i}`,{status:r,url:i});if(!s)throw new I(`Empty response: ${i}`,{status:r,url:i});const{url:c}=a.fromUrl(i),l={url:c,html:s};t===c&&o.cache.set(l.url,l);const u=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.trigger("page:load",{page:l,cache:!1})).then(function(){})}();return u&&u.then?u.then(function(){return l}):l})})}t=a.fromUrl(t).url;const i=function(){if(o.cache.has(t)){function n(){return r=1,i}const i=o.cache.get(t),s=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.trigger("page:load",{page:i,cache:!0})).then(function(){})}();return s&&s.then?s.then(n):n()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(s){return Promise.reject(s)}};class I extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}const D=function(){try{let e;const r=this;function t(t){return e?t:Promise.resolve(r.hooks.trigger("animation:out:start",void 0,()=>{r.classes.add("is-changing","is-leaving","is-animating"),r.context.history.popstate&&r.classes.add("is-popstate"),r.context.animation.name&&r.classes.add(`to-${n(r.context.animation.name)}`)})).then(function(){return Promise.resolve(r.hooks.trigger("animation:await",{direction:"out"},function(t,e){let{direction:n}=e;try{return Promise.resolve(r.awaitAnimations({selector:t.animation.selector,direction:n})).then(function(){})}catch(t){return Promise.reject(t)}})).then(function(){return Promise.resolve(r.hooks.trigger("animation:out:end")).then(function(){})})})}const o=function(){if(!r.context.animation.animate)return Promise.resolve(r.hooks.trigger("animation:skip")).then(function(){e=1})}();return Promise.resolve(o&&o.then?o.then(t):t(o))}catch(i){return Promise.reject(i)}},L=function(t,e){var n;let{html:r}=t,{containers:o}=void 0===e?this.options:e;const i=(new DOMParser).parseFromString(r,"text/html"),s=(null==(n=i.querySelector("title"))?void 0:n.innerText)||"";return document.title=s,o.map(t=>{const e=document.querySelector(t),n=i.querySelector(t);return e&&n?(e.replaceWith(n),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),n||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===o.length},V=function(){try{const t=this;if(!t.context.animation.animate)return Promise.resolve();const e=t.hooks.trigger("animation:await",{direction:"in"},function(e,n){let{direction:r}=n;try{return Promise.resolve(t.awaitAnimations({selector:e.animation.selector,direction:r})).then(function(){})}catch(t){return Promise.reject(t)}});return Promise.resolve(m()).then(function(){return Promise.resolve(t.hooks.trigger("animation:in:start",void 0,()=>{t.classes.remove("is-animating")})).then(function(){return Promise.resolve(e).then(function(){return Promise.resolve(t.hooks.trigger("animation:in:end")).then(function(){})})})})}catch(t){return Promise.reject(t)}},M=function(t,e){try{const o=this,{url:s,html:a}=e;return o.classes.remove("is-leaving"),o.isSameResolvedUrl(r(),t)?(o.isSameResolvedUrl(r(),s)||(i(s),o.currentPageUrl=r(),o.context.to.url=o.currentPageUrl),o.context.animation.animate&&o.classes.add("is-rendering"),o.context.to.html=a,Promise.resolve(o.hooks.trigger("content:replace",{page:e},(t,e)=>{let{page:r}=e;if(!o.replaceContent(r,{containers:t.containers}))throw new Error("[swup] Container mismatch, aborting");o.context.animation.animate&&(o.classes.add("is-animating","is-changing","is-rendering"),o.context.animation.name&&o.classes.add(`to-${n(o.context.animation.name)}`))})).then(function(){return Promise.resolve(o.hooks.trigger("content:scroll",{options:{behavior:"auto"}},(t,e)=>{let{options:n}=e;if(o.context.scroll.target){const t=o.getAnchorElement(o.context.scroll.target);if(t)return void t.scrollIntoView(n)}o.context.scroll.reset&&window.scrollTo(0,0)})).then(function(){return Promise.resolve(o.hooks.trigger("page:view",{url:o.currentPageUrl,title:document.title})).then(function(){o.options.cache||o.cache.clear()})})})):Promise.resolve()}catch(t){return Promise.reject(t)}},N=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function W(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function _(t){return this.plugins.find(e=>e===t||e.name===t)}class B{constructor(t){void 0===t&&(t={}),this.version="4.0.0-rc.23",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=r(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=j,this.performVisit=q,this.animatePageOut=D,this.renderPage=M,this.replaceContent=L,this.animatePageIn=V,this.delegateEvent=s,this.fetchPage=R,this.awaitAnimations=E,this.getAnchorElement=U,this.use=N,this.unuse=W,this.findPlugin=_,this.getCurrentUrl=r,this.createContext=y,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:function(t,e){let{el:n}=void 0===e?{}:e;return!(null==n||!n.closest("[data-no-swup]"))},linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options={...this.defaults,...t},this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new l(this),this.classes=new w(this),this.hooks=new b(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}enable(){try{const t=this,{linkSelector:e}=t.options;return t.clickDelegate=t.delegateEvent(e,"click",t.linkClickHandler),window.addEventListener("popstate",t.popStateHandler),t.options.plugins.forEach(e=>t.use(e)),i(null,{index:1}),Promise.resolve(m()).then(function(){return Promise.resolve(t.hooks.trigger("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")})).then(function(){return Promise.resolve(t.hooks.trigger("page:view",{url:t.currentPageUrl,title:document.title})).then(function(){})})})}catch(t){return Promise.reject(t)}}destroy(){try{const t=this;return t.clickDelegate.destroy(),window.removeEventListener("popstate",t.popStateHandler),t.cache.clear(),t.options.plugins.forEach(e=>t.unuse(e)),Promise.resolve(t.hooks.trigger("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")})).then(function(){t.hooks.clear()})}catch(t){return Promise.reject(t)}}shouldIgnoreVisit(t,e){let{el:n,event:r}=void 0===e?{}:e;const{origin:o,url:i,hash:s}=a.fromUrl(t);return o!==window.location.origin||!(!n||!this.triggerWillOpenNewWindow(n))||!!this.options.ignoreVisit(i+s,{el:n,event:r})}linkClickHandler(t){const e=t.delegateTarget,{href:n,url:r,hash:o}=a.fromElement(e);this.shouldIgnoreVisit(n,{el:e,event:t})||(this.context=this.createContext({to:r,hash:o,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.trigger("link:newtab",{href:n}):0===t.button&&this.hooks.triggerSync("link:click",{el:e,event:t},()=>{const e=this.context.from.url??"";t.preventDefault(),r&&r!==e?this.isSameResolvedUrl(r,e)||this.performVisit(r):o?(i(r+o),this.hooks.triggerSync("link:anchor",{hash:o,options:{behavior:"auto"}},(t,e)=>{let{hash:n,options:r}=e;const o=this.getAnchorElement(n);o&&o.scrollIntoView(r)})):this.hooks.triggerSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var e,n;const o=(null==(e=t.state)?void 0:e.url)??location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(r(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(o,{event:t}))return;const{url:i,hash:s}=a.fromUrl(o);this.context=this.createContext({to:i,hash:s,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(n=t.state)?void 0:n.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.triggerSync("history:popstate",{event:t},()=>{this.performVisit(i)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}export{a as Location,n as classify,o as createHistoryRecord,B as default,s as delegateEvent,g as escapeCssIdentifier,p as forceReflow,r as getCurrentUrl,d as isPromise,c as matchPath,m as nextTick,u as query,h as queryAll,f as runAsPromise,v as toMs,i as updateHistoryRecord}; | ||
import t from"delegate-it";import{match as e}from"path-to-regexp";const n=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",r=function(t){let{hash:e}=void 0===t?{}:t;return location.pathname+location.search+(e?location.hash:"")},o=function(t,e){void 0===e&&(e={});const n={url:t=t||r({hash:!0}),random:Math.random(),source:"swup",...e};history.pushState(n,"",t)},i=function(t,e){void 0===t&&(t=null),void 0===e&&(e={}),t=t||r({hash:!0});const n={...history.state,url:t,random:Math.random(),source:"swup",...e};history.replaceState(n,"",t)},s=(e,n,r,o)=>{const i=new AbortController;return o={...o,signal:i.signal},t(e,n,r,o),{destroy:()=>i.abort()}};class a extends URL{constructor(t,e){void 0===e&&(e=document.baseURI),super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new a(e)}static fromUrl(t){return new a(t)}}const c=(t,n)=>{try{return e(t,n)}catch(e){throw new Error(`[swup] Error parsing path "${t}":\n${e}`)}};class l{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){t=this.resolve(t),e={...e,url:t},this.pages.set(t,e),this.swup.hooks.callSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e={...this.get(t),...e,url:t},this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.callSync("cache:clear")}prune(t){this.pages.forEach((e,n)=>{t(n,e)&&this.delete(n)})}resolve(t){const{url:e}=a.fromUrl(t);return this.swup.resolveUrl(e)}}const u=function(t,e){return void 0===e&&(e=document),e.querySelector(t)},h=function(t,e){return void 0===e&&(e=document),Array.from(e.querySelectorAll(t))},m=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function d(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}function f(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new Promise((r,o)=>{const i=t.apply(n,e);d(i)?i.then(r,o):r(i)})}function p(t){var e;return null==(e=t=t||document.body)?void 0:e.offsetHeight}const g=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,v=t=>1e3*Number(t.slice(0,-1).replace(",","."));class w{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?h(this.selector):[]}add(){this.targets.forEach(t=>t.classList.add(...[].slice.call(arguments)))}remove(){this.targets.forEach(t=>t.classList.remove(...[].slice.call(arguments)))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function y(t){let{to:e,from:n=this.currentPageUrl,hash:r,animate:o=!0,animation:i,el:s,event:a,action:c="push",resetScroll:l=!0}=t;return{from:{url:n},to:{url:e},containers:this.options.containers,animation:{animate:o,wait:!1,name:i,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:s,event:a},history:{action:c,popstate:!1,direction:void 0},scroll:{reset:l,target:r}}}const P="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function k(t,e,n){if(!t.s){if(n instanceof x){if(!n.s)return void(n.o=k.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(k.bind(null,t,e),k.bind(null,t,2));t.s=e,t.v=n;const r=t.o;r&&r(t)}}const x=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const r=new t,o=this.s;if(o){const t=1&o?e:n;if(t){try{k(r,1,t(this.v))}catch(t){k(r,2,t)}return r}return this}return this.o=function(t){try{const o=t.v;1&t.s?k(r,1,e?e(o):o):n?k(r,1,n(o)):k(r,2,o)}catch(t){k(r,2,t)}},r},t}();function S(t){return t instanceof x&&1&t.s}class b{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:await","animation:out:end","animation:in:start","animation:in:await","animation:in:end","animation:skip","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,n){void 0===n&&(n={});const r=this.get(t);if(!r)return console.warn(`Hook '${t}' not found.`),()=>{};const o=r.size+1,i={...n,id:o,hook:t,handler:e};return r.set(e,i),()=>this.off(t,e)}before(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,before:!0})}replace(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,replace:!0})}once(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,once:!0})}off(t,e){const n=this.get(t);n&&e?n.delete(e)||console.warn(`Handler for hook '${t}' not found.`):n&&n.clear()}call(t,e,n){try{const r=this,{before:o,handler:i,after:s}=r.getHandlers(t,n);return Promise.resolve(r.run(o,e)).then(function(){return Promise.resolve(r.run(i,e)).then(function(n){let[o]=n;return Promise.resolve(r.run(s,e)).then(function(){return r.dispatchDomEvent(t,e),o})})})}catch(t){return Promise.reject(t)}}callSync(t,e,n){const{before:r,handler:o,after:i}=this.getHandlers(t,n);this.runSync(r,e);const[s]=this.runSync(o,e);return this.runSync(i,e),this.dispatchDomEvent(t,e),s}run(t,e){try{const n=this,r=[],o=function(t,e,n){if("function"==typeof t[P]){var r,o,i,s=t[P]();if(function t(n){try{for(;!(r=s.next()).done;)if((n=e(r.value))&&n.then){if(!S(n))return void n.then(t,i||(i=k.bind(null,o=new x,2)));n=n.v}o?k(o,1,n):o=n}catch(t){k(o||(o=new x),2,t)}}(),s.return){var a=function(t){try{r.done||s.return()}catch(t){}return t};if(o&&o.then)return o.then(a,function(t){throw a(t)});a()}return o}if(!("length"in t))throw new TypeError("Object is not iterable");for(var c=[],l=0;l<t.length;l++)c.push(t[l]);return function(t,e,n){var r,o,i=-1;return function n(s){try{for(;++i<t.length;)if((s=e(i))&&s.then){if(!S(s))return void s.then(n,o||(o=k.bind(null,r=new x,2)));s=s.v}r?k(r,1,s):r=s}catch(t){k(r||(r=new x),2,t)}}(),r}(c,function(t){return e(c[t])})}(t,function(t){let{hook:o,handler:i,defaultHandler:s,once:a}=t;return Promise.resolve(f(i,[n.swup.context,e,s])).then(function(t){r.push(t),a&&n.off(o,i)})});return Promise.resolve(o&&o.then?o.then(function(){return r}):r)}catch(t){return Promise.reject(t)}}runSync(t,e){const n=[];for(const{hook:r,handler:o,defaultHandler:i,once:s}of t){const t=o(this.swup.context,e,i);n.push(t),d(t)&&console.warn(`Promise returned from handler for synchronous hook '${r}'.Swup will not wait for it to resolve.`),s&&this.off(r,o)}return n}getHandlers(t,e){const n=this.get(t);if(!n)return{found:!1,before:[],handler:[],after:[],replaced:!1};const r=this.sortRegistrations,o=Array.from(n.values()),i=o.filter(t=>{let{before:e,replace:n}=t;return e&&!n}).sort(r),s=o.filter(t=>{let{replace:e}=t;return e}).sort(r),a=o.filter(t=>{let{before:e,replace:n}=t;return!e&&!n}).sort(r),c=s.length>0;let l=[];if(e&&(l=[{id:0,hook:t,handler:e}],c)){const n=s.length-1,r=t=>{const n=s[t-1];return n?(e,o)=>n.handler(e,o,r(t-1)):e};l=[{id:0,hook:t,handler:s[n].handler,defaultHandler:r(n)}]}return{found:!0,before:i,handler:l,after:a,replaced:c}}sortRegistrations(t,e){return(t.priority??0)-(e.priority??0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const U=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let n=document.getElementById(t)||document.getElementById(e)||u(`a[name='${g(t)}']`)||u(`a[name='${g(e)}']`);return n||"top"!==t||(n=document.body),n},E=function(t){let{elements:e,selector:n}=t;try{if(!1===n&&!e)return Promise.resolve();let t=[];if(e)t=Array.from(e);else if(n&&(t=h(n,document.body),!t.length))return console.warn(`[swup] No elements found matching animationSelector \`${n}\``),Promise.resolve();const r=t.map(t=>function(t){const{type:e,timeout:n,propCount:r}=function(t,e){const n=window.getComputedStyle(t),r=$(n,`${H}Delay`),o=$(n,`${H}Duration`),i=A(r,o),s=$(n,`${C}Delay`),a=$(n,`${C}Duration`),c=A(s,a);let l=null,u=0,h=0;return e===H?i>0&&(l=H,u=i,h=o.length):e===C?c>0&&(l=C,u=c,h=a.length):(u=Math.max(i,c),l=u>0?i>c?H:C:null,h=l?l===H?o.length:a.length:0),{type:l,timeout:u,propCount:h}}(t);return!(!e||!n)&&new Promise(o=>{const i=`${e}end`,s=performance.now();let a=0;const c=()=>{t.removeEventListener(i,l),o()},l=e=>{if(e.target===t){if(!function(t){return[`${H}end`,`${C}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-s)/1e3<e.elapsedTime||++a>=r&&c()}};setTimeout(()=>{a<r&&c()},n+1),t.addEventListener(i,l)})}(t));return r.filter(Boolean).length>0?Promise.resolve(Promise.all(r)).then(function(){}):(n&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${n}\``),Promise.resolve())}catch(t){return Promise.reject(t)}},H="transition",C="animation";function $(t,e){return(t[e]||"").split(", ")}function A(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,n)=>v(e)+v(t[n])))}const q=function(t,e){void 0===e&&(e={});try{const n=this;if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:s}=n.context.trigger;n.context.to.url=a.fromUrl(t).url,e.referrer=e.referrer||n.currentPageUrl,!1===e.animate&&(n.context.animation.animate=!1),n.context.animation.animate||n.classes.clear();const c=e.history||(null==s?void 0:s.getAttribute("data-swup-history"))||void 0;c&&["push","replace"].includes(c)&&(n.context.history.action=c);const l=e.animation||(null==s?void 0:s.getAttribute("data-swup-animation"))||void 0;return l&&(n.context.animation.name=l),Promise.resolve(function(s,a){try{var c=Promise.resolve(n.hooks.call("visit:start")).then(function(){function s(){const t=n.animatePageOut();return Promise.resolve(Promise.all([a,t])).then(function(t){let[e]=t;return Promise.resolve(n.renderPage(n.context.to.url,e)).then(function(){return Promise.resolve(n.animatePageIn()).then(function(){return Promise.resolve(n.hooks.call("visit:end",void 0,()=>n.classes.clear())).then(function(){})})})})}const a=n.hooks.call("page:request",{url:n.context.to.url,options:e},function(t,e){let{options:r}=e;try{return Promise.resolve(n.fetchPage(t.to.url,r))}catch(t){return Promise.reject(t)}});if(!n.context.history.popstate){const e=t+(n.context.scroll.target||"");"replace"===n.context.history.action?i(e):o(e,{index:n.currentHistoryIndex+1})}n.currentPageUrl=r();const c=function(){if(n.context.animation.wait)return Promise.resolve(a).then(function(t){let{html:e}=t;n.context.to.html=e})}();return c&&c.then?c.then(s):s()})}catch(t){return a(t)}return c&&c.then?c.then(void 0,a):c}(0,function(t){t&&(console.error(t),n.options.skipPopStateHandling=()=>(window.location.href=n.context.to.url,!0),window.history.go(-1))}))}catch(t){return Promise.reject(t)}};function j(t,e,n){if(void 0===e&&(e={}),void 0===n&&(n={}),this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:r,hash:o}=a.fromUrl(t);this.context=this.createContext({...n,to:r,hash:o}),this.performVisit(r,e)}const R=function(t,e){void 0===e&&(e={});try{let r;const o=this;function n(n){if(r)return n;const i={...o.options.requestHeaders,...e.headers};return e={...e,headers:i},Promise.resolve(o.hooks.call("fetch:request",{url:t,options:e},(t,e)=>{let{url:n,options:r}=e;return fetch(n,r)})).then(function(n){const{status:r,url:i}=n;return Promise.resolve(n.text()).then(function(s){if(500===r)throw o.hooks.call("fetch:error",{status:r,response:n,url:i}),new I(`Server error: ${i}`,{status:r,url:i});if(!s)throw new I(`Empty response: ${i}`,{status:r,url:i});const{url:c}=a.fromUrl(i),l={url:c,html:s};t===c&&o.cache.set(l.url,l);const u=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.call("page:load",{page:l,cache:!1})).then(function(){})}();return u&&u.then?u.then(function(){return l}):l})})}t=a.fromUrl(t).url;const i=function(){if(o.cache.has(t)){function n(){return r=1,i}const i=o.cache.get(t),s=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.call("page:load",{page:i,cache:!0})).then(function(){})}();return s&&s.then?s.then(n):n()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(s){return Promise.reject(s)}};class I extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}const D=function(){try{let e;const r=this;function t(t){return e?t:Promise.resolve(r.hooks.call("animation:out:start",void 0,()=>{r.classes.add("is-changing","is-leaving","is-animating"),r.context.history.popstate&&r.classes.add("is-popstate"),r.context.animation.name&&r.classes.add(`to-${n(r.context.animation.name)}`)})).then(function(){return Promise.resolve(r.hooks.call("animation:out:await",{skip:!1},function(t,e){let{skip:n}=e;try{return n?Promise.resolve():Promise.resolve(r.awaitAnimations({selector:t.animation.selector})).then(function(){})}catch(t){return Promise.reject(t)}})).then(function(){return Promise.resolve(r.hooks.call("animation:out:end")).then(function(){})})})}const o=function(){if(!r.context.animation.animate)return Promise.resolve(r.hooks.call("animation:skip")).then(function(){e=1})}();return Promise.resolve(o&&o.then?o.then(t):t(o))}catch(i){return Promise.reject(i)}},L=function(t,e){var n;let{html:r}=t,{containers:o}=void 0===e?this.options:e;const i=(new DOMParser).parseFromString(r,"text/html"),s=(null==(n=i.querySelector("title"))?void 0:n.innerText)||"";return document.title=s,o.map(t=>{const e=document.querySelector(t),n=i.querySelector(t);return e&&n?(e.replaceWith(n),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),n||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===o.length},V=function(){try{const t=this;if(!t.context.animation.animate)return Promise.resolve();const e=t.hooks.call("animation:in:await",{skip:!1},function(e,n){let{skip:r}=n;try{return r?Promise.resolve():Promise.resolve(t.awaitAnimations({selector:e.animation.selector})).then(function(){})}catch(t){return Promise.reject(t)}});return Promise.resolve(m()).then(function(){return Promise.resolve(t.hooks.call("animation:in:start",void 0,()=>{t.classes.remove("is-animating")})).then(function(){return Promise.resolve(e).then(function(){return Promise.resolve(t.hooks.call("animation:in:end")).then(function(){})})})})}catch(t){return Promise.reject(t)}},M=function(t,e){try{const o=this,{url:s,html:a}=e;return o.classes.remove("is-leaving"),o.isSameResolvedUrl(r(),t)?(o.isSameResolvedUrl(r(),s)||(i(s),o.currentPageUrl=r(),o.context.to.url=o.currentPageUrl),o.context.animation.animate&&o.classes.add("is-rendering"),o.context.to.html=a,Promise.resolve(o.hooks.call("content:replace",{page:e},(t,e)=>{let{page:r}=e;if(!o.replaceContent(r,{containers:t.containers}))throw new Error("[swup] Container mismatch, aborting");o.context.animation.animate&&(o.classes.add("is-animating","is-changing","is-rendering"),o.context.animation.name&&o.classes.add(`to-${n(o.context.animation.name)}`))})).then(function(){return Promise.resolve(o.hooks.call("content:scroll",{options:{behavior:"auto"}},(t,e)=>{let{options:n}=e;if(o.context.scroll.target){const t=o.getAnchorElement(o.context.scroll.target);if(t)return void t.scrollIntoView(n)}o.context.scroll.reset&&window.scrollTo(0,0)})).then(function(){return Promise.resolve(o.hooks.call("page:view",{url:o.currentPageUrl,title:document.title})).then(function(){o.options.cache||o.cache.clear()})})})):Promise.resolve()}catch(t){return Promise.reject(t)}},N=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function W(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function _(t){return this.plugins.find(e=>e===t||e.name===t)}class B{constructor(t){void 0===t&&(t={}),this.version="4.0.0-rc.24",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=r(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=j,this.performVisit=q,this.animatePageOut=D,this.renderPage=M,this.replaceContent=L,this.animatePageIn=V,this.delegateEvent=s,this.fetchPage=R,this.awaitAnimations=E,this.getAnchorElement=U,this.use=N,this.unuse=W,this.findPlugin=_,this.getCurrentUrl=r,this.createContext=y,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:function(t,e){let{el:n}=void 0===e?{}:e;return!(null==n||!n.closest("[data-no-swup]"))},linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options={...this.defaults,...t},this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new l(this),this.classes=new w(this),this.hooks=new b(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}enable(){try{const t=this,{linkSelector:e}=t.options;return t.clickDelegate=t.delegateEvent(e,"click",t.linkClickHandler),window.addEventListener("popstate",t.popStateHandler),t.options.plugins.forEach(e=>t.use(e)),i(null,{index:1}),Promise.resolve(m()).then(function(){return Promise.resolve(t.hooks.call("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")})).then(function(){return Promise.resolve(t.hooks.call("page:view",{url:t.currentPageUrl,title:document.title})).then(function(){})})})}catch(t){return Promise.reject(t)}}destroy(){try{const t=this;return t.clickDelegate.destroy(),window.removeEventListener("popstate",t.popStateHandler),t.cache.clear(),t.options.plugins.forEach(e=>t.unuse(e)),Promise.resolve(t.hooks.call("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")})).then(function(){t.hooks.clear()})}catch(t){return Promise.reject(t)}}shouldIgnoreVisit(t,e){let{el:n,event:r}=void 0===e?{}:e;const{origin:o,url:i,hash:s}=a.fromUrl(t);return o!==window.location.origin||!(!n||!this.triggerWillOpenNewWindow(n))||!!this.options.ignoreVisit(i+s,{el:n,event:r})}linkClickHandler(t){const e=t.delegateTarget,{href:n,url:r,hash:o}=a.fromElement(e);this.shouldIgnoreVisit(n,{el:e,event:t})||(this.context=this.createContext({to:r,hash:o,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.call("link:newtab",{href:n}):0===t.button&&this.hooks.callSync("link:click",{el:e,event:t},()=>{const e=this.context.from.url??"";t.preventDefault(),r&&r!==e?this.isSameResolvedUrl(r,e)||this.performVisit(r):o?(i(r+o),this.hooks.callSync("link:anchor",{hash:o,options:{behavior:"auto"}},(t,e)=>{let{hash:n,options:r}=e;const o=this.getAnchorElement(n);o&&o.scrollIntoView(r)})):this.hooks.callSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var e,n;const o=(null==(e=t.state)?void 0:e.url)??location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(r(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(o,{event:t}))return;const{url:i,hash:s}=a.fromUrl(o);this.context=this.createContext({to:i,hash:s,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(n=t.state)?void 0:n.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.callSync("history:popstate",{event:t},()=>{this.performVisit(i)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}export{a as Location,n as classify,o as createHistoryRecord,B as default,s as delegateEvent,g as escapeCssIdentifier,p as forceReflow,r as getCurrentUrl,d as isPromise,c as matchPath,m as nextTick,u as query,h as queryAll,f as runAsPromise,v as toMs,i as updateHistoryRecord}; | ||
//# sourceMappingURL=Swup.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).Swup=e()}(this,function(){const t=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",e=function(t){let{hash:e}=void 0===t?{}:t;return location.pathname+location.search+(e?location.hash:"")},n=function(t,n){void 0===n&&(n={});const r={url:t=t||e({hash:!0}),random:Math.random(),source:"swup",...n};history.pushState(r,"",t)},r=function(t,n){void 0===t&&(t=null),void 0===n&&(n={}),t=t||e({hash:!0});const r={...history.state,url:t,random:Math.random(),source:"swup",...n};history.replaceState(r,"",t)},o=new WeakMap;function i(t,e,n,r){if(!t&&!o.has(e))return!1;const i=o.get(e)??new WeakMap;o.set(e,i);const s=i.get(n)??new Set;i.set(n,s);const a=s.has(r);return t?s.add(r):s.delete(r),a&&t}const s=(t,e,n,r)=>{const o=new AbortController;return function(t,e,n,r={}){const{signal:o,base:s=document}=r;if(o?.aborted)return;const{once:a,...c}=r,l=s instanceof Document?s.documentElement:s,u=Boolean("object"==typeof r?r.capture:r),h=r=>{const o=function(t,e){let n=t.target;if(n instanceof Text&&(n=n.parentElement),n instanceof Element&&t.currentTarget instanceof Element){const r=n.closest(e);if(r&&t.currentTarget.contains(r))return r}}(r,t);if(o){const t=Object.assign(r,{delegateTarget:o});n.call(l,t),a&&(l.removeEventListener(e,h,c),i(!1,l,n,d))}},d=JSON.stringify({selector:t,type:e,capture:u});i(!0,l,n,d)||l.addEventListener(e,h,c),o?.addEventListener("abort",()=>{i(!1,l,n,d)})}(t,e,n,r={...r,signal:o.signal}),{destroy:()=>o.abort()}};class a extends URL{constructor(t,e){void 0===e&&(e=document.baseURI),super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new a(e)}static fromUrl(t){return new a(t)}}class c{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){t=this.resolve(t),e={...e,url:t},this.pages.set(t,e),this.swup.hooks.triggerSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e={...this.get(t),...e,url:t},this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.triggerSync("cache:clear")}prune(t){this.pages.forEach((e,n)=>{t(n,e)&&this.delete(n)})}resolve(t){const{url:e}=a.fromUrl(t);return this.swup.resolveUrl(e)}}const l=function(t,e){return void 0===e&&(e=document),e.querySelector(t)},u=function(t,e){return void 0===e&&(e=document),Array.from(e.querySelectorAll(t))},h=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function d(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}const f=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,m=t=>1e3*Number(t.slice(0,-1).replace(",","."));class p{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?u(this.selector):[]}add(){this.targets.forEach(t=>t.classList.add(...[].slice.call(arguments)))}remove(){this.targets.forEach(t=>t.classList.remove(...[].slice.call(arguments)))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function g(t){let{to:e,from:n=this.currentPageUrl,hash:r,animate:o=!0,animation:i,el:s,event:a,action:c="push",resetScroll:l=!0}=t;return{from:{url:n},to:{url:e},containers:this.options.containers,animation:{animate:o,wait:!1,name:i,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:s,event:a},history:{action:c,popstate:!1,direction:void 0},scroll:{reset:l,target:r}}}const v="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function w(t,e,n){if(!t.s){if(n instanceof y){if(!n.s)return void(n.o=w.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(w.bind(null,t,e),w.bind(null,t,2));t.s=e,t.v=n;const r=t.o;r&&r(t)}}const y=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const r=new t,o=this.s;if(o){const t=1&o?e:n;if(t){try{w(r,1,t(this.v))}catch(t){w(r,2,t)}return r}return this}return this.o=function(t){try{const o=t.v;1&t.s?w(r,1,e?e(o):o):n?w(r,1,n(o)):w(r,2,o)}catch(t){w(r,2,t)}},r},t}();function P(t){return t instanceof y&&1&t.s}class k{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:end","animation:in:start","animation:in:end","animation:skip","animation:await","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,n){void 0===n&&(n={});const r=this.get(t);if(!r)return console.warn(`Hook '${t}' not found.`),()=>{};const o=r.size+1,i={...n,id:o,hook:t,handler:e};return r.set(e,i),()=>this.off(t,e)}before(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,before:!0})}replace(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,replace:!0})}once(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,once:!0})}off(t,e){const n=this.get(t);n&&e?n.delete(e)||console.warn(`Handler for hook '${t}' not found.`):n&&n.clear()}trigger(t,e,n){try{const r=this,{before:o,handler:i,after:s,replaced:a}=r.getHandlers(t,n);return Promise.resolve(r.execute(o,e)).then(function(){return Promise.resolve(r.execute(i,e,a?n:void 0)).then(function(n){let[o]=n;return Promise.resolve(r.execute(s,e)).then(function(){return r.dispatchDomEvent(t,e),o})})})}catch(t){return Promise.reject(t)}}triggerSync(t,e,n){const{before:r,after:o,handler:i,replaced:s}=this.getHandlers(t,n);this.executeSync(r,e);const[a]=this.executeSync(i,e,s?n:void 0);return this.executeSync(o,e),this.dispatchDomEvent(t,e),a}execute(t,e,n){try{const r=this,o=[],i=function(t,e,n){if("function"==typeof t[v]){var r,o,i,s=t[v]();if(function t(n){try{for(;!(r=s.next()).done;)if((n=e(r.value))&&n.then){if(!P(n))return void n.then(t,i||(i=w.bind(null,o=new y,2)));n=n.v}o?w(o,1,n):o=n}catch(t){w(o||(o=new y),2,t)}}(),s.return){var a=function(t){try{r.done||s.return()}catch(t){}return t};if(o&&o.then)return o.then(a,function(t){throw a(t)});a()}return o}if(!("length"in t))throw new TypeError("Object is not iterable");for(var c=[],l=0;l<t.length;l++)c.push(t[l]);return function(t,e,n){var r,o,i=-1;return function n(s){try{for(;++i<t.length;)if((s=e(i))&&s.then){if(!P(s))return void s.then(n,o||(o=w.bind(null,r=new y,2)));s=s.v}r?w(r,1,s):r=s}catch(t){w(r||(r=new y),2,t)}}(),r}(c,function(t){return e(c[t])})}(t,function(t){let{hook:i,handler:s,once:a}=t;return Promise.resolve(function(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new Promise((r,o)=>{const i=t.apply(n,e);d(i)?i.then(r,o):r(i)})}(s,[r.swup.context,e,n])).then(function(t){o.push(t),a&&r.off(i,s)})});return Promise.resolve(i&&i.then?i.then(function(){return o}):o)}catch(t){return Promise.reject(t)}}executeSync(t,e,n){const r=[];for(const{hook:o,handler:i,once:s}of t){const t=i(this.swup.context,e,n);r.push(t),d(t)&&console.warn(`Promise returned from handler for synchronous hook '${o}'.Swup will not wait for it to resolve.`),s&&this.off(o,i)}return r}getHandlers(t,e){const n=this.get(t);if(!n)return{found:!1,before:[],handler:[],after:[],replaced:!1};const r=this.sortRegistrations,o=Array.from(n.values()),i=o.filter(t=>{let{before:e,replace:n}=t;return e&&!n}).sort(r),s=o.filter(t=>{let{replace:e}=t;return e}).sort(r),a=o.filter(t=>{let{before:e,replace:n}=t;return!e&&!n}).sort(r),c=s.length>0;let l=[];return c?l=[{id:0,hook:t,handler:s[0].handler}]:e&&(l=[{id:0,hook:t,handler:e}]),{found:!0,before:i,handler:l,after:a,replaced:c}}sortRegistrations(t,e){return(t.priority??0)-(e.priority??0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const x=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let n=document.getElementById(t)||document.getElementById(e)||l(`a[name='${f(t)}']`)||l(`a[name='${f(e)}']`);return n||"top"!==t||(n=document.body),n},S=function(t){let{elements:e,selector:n}=t;try{if(!1===n&&!e)return Promise.resolve();let t=[];if(e)t=Array.from(e);else if(n&&(t=u(n,document.body),!t.length))return console.warn(`[swup] No elements found matching animationSelector \`${n}\``),Promise.resolve();const r=t.map(t=>function(t){const{type:e,timeout:n,propCount:r}=function(t,e){const n=window.getComputedStyle(t),r=U(n,`${b}Delay`),o=U(n,`${b}Duration`),i=C(r,o),s=U(n,`${E}Delay`),a=U(n,`${E}Duration`),c=C(s,a);let l=null,u=0,h=0;return e===b?i>0&&(l=b,u=i,h=o.length):e===E?c>0&&(l=E,u=c,h=a.length):(u=Math.max(i,c),l=u>0?i>c?b:E:null,h=l?l===b?o.length:a.length:0),{type:l,timeout:u,propCount:h}}(t);return!(!e||!n)&&new Promise(o=>{const i=`${e}end`,s=performance.now();let a=0;const c=()=>{t.removeEventListener(i,l),o()},l=e=>{if(e.target===t){if(!function(t){return[`${b}end`,`${E}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-s)/1e3<e.elapsedTime||++a>=r&&c()}};setTimeout(()=>{a<r&&c()},n+1),t.addEventListener(i,l)})}(t));return r.filter(Boolean).length>0?Promise.resolve(Promise.all(r)).then(function(){}):(n&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${n}\``),Promise.resolve())}catch(t){return Promise.reject(t)}},b="transition",E="animation";function U(t,e){return(t[e]||"").split(", ")}function C(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,n)=>m(e)+m(t[n])))}const H=function(t,o){void 0===o&&(o={});try{const i=this;if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:s}=i.context.trigger;i.context.to.url=a.fromUrl(t).url,o.referrer=o.referrer||i.currentPageUrl,!1===o.animate&&(i.context.animation.animate=!1),i.context.animation.animate||i.classes.clear();const c=o.history||(null==s?void 0:s.getAttribute("data-swup-history"))||void 0;c&&["push","replace"].includes(c)&&(i.context.history.action=c);const l=o.animation||(null==s?void 0:s.getAttribute("data-swup-animation"))||void 0;return l&&(i.context.animation.name=l),Promise.resolve(function(s,a){try{var c=Promise.resolve(i.hooks.trigger("visit:start")).then(function(){function s(){const t=i.animatePageOut();return Promise.resolve(Promise.all([a,t])).then(function(t){let[e]=t;return Promise.resolve(i.renderPage(i.context.to.url,e)).then(function(){return Promise.resolve(i.animatePageIn()).then(function(){return Promise.resolve(i.hooks.trigger("visit:end",void 0,()=>i.classes.clear())).then(function(){})})})})}const a=i.hooks.trigger("page:request",{url:i.context.to.url,options:o},function(t,e){let{options:n}=e;try{return Promise.resolve(i.fetchPage(t.to.url,n))}catch(t){return Promise.reject(t)}});if(!i.context.history.popstate){const e=t+(i.context.scroll.target||"");"replace"===i.context.history.action?r(e):n(e,{index:i.currentHistoryIndex+1})}i.currentPageUrl=e();const c=function(){if(i.context.animation.wait)return Promise.resolve(a).then(function(t){let{html:e}=t;i.context.to.html=e})}();return c&&c.then?c.then(s):s()})}catch(t){return a(t)}return c&&c.then?c.then(void 0,a):c}(0,function(t){t&&(console.error(t),i.options.skipPopStateHandling=()=>(window.location.href=i.context.to.url,!0),window.history.go(-1))}))}catch(t){return Promise.reject(t)}};function $(t,e,n){if(void 0===e&&(e={}),void 0===n&&(n={}),this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:r,hash:o}=a.fromUrl(t);this.context=this.createContext({...n,to:r,hash:o}),this.performVisit(r,e)}const A=function(t,e){void 0===e&&(e={});try{let r;const o=this;function n(n){if(r)return n;const i={...o.options.requestHeaders,...e.headers};return e={...e,headers:i},Promise.resolve(o.hooks.trigger("fetch:request",{url:t,options:e},(t,e)=>{let{url:n,options:r}=e;return fetch(n,r)})).then(function(n){const{status:r,url:i}=n;return Promise.resolve(n.text()).then(function(s){if(500===r)throw o.hooks.trigger("fetch:error",{status:r,response:n,url:i}),new j(`Server error: ${i}`,{status:r,url:i});if(!s)throw new j(`Empty response: ${i}`,{status:r,url:i});const{url:c}=a.fromUrl(i),l={url:c,html:s};t===c&&o.cache.set(l.url,l);const u=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.trigger("page:load",{page:l,cache:!1})).then(function(){})}();return u&&u.then?u.then(function(){return l}):l})})}t=a.fromUrl(t).url;const i=function(){if(o.cache.has(t)){function n(){return r=1,i}const i=o.cache.get(t),s=function(){if(!1!==e.triggerHooks)return Promise.resolve(o.hooks.trigger("page:load",{page:i,cache:!0})).then(function(){})}();return s&&s.then?s.then(n):n()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(s){return Promise.reject(s)}};class j extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}const q=function(){try{let n;const r=this;function e(e){return n?e:Promise.resolve(r.hooks.trigger("animation:out:start",void 0,()=>{r.classes.add("is-changing","is-leaving","is-animating"),r.context.history.popstate&&r.classes.add("is-popstate"),r.context.animation.name&&r.classes.add(`to-${t(r.context.animation.name)}`)})).then(function(){return Promise.resolve(r.hooks.trigger("animation:await",{direction:"out"},function(t,e){let{direction:n}=e;try{return Promise.resolve(r.awaitAnimations({selector:t.animation.selector,direction:n})).then(function(){})}catch(t){return Promise.reject(t)}})).then(function(){return Promise.resolve(r.hooks.trigger("animation:out:end")).then(function(){})})})}const o=function(){if(!r.context.animation.animate)return Promise.resolve(r.hooks.trigger("animation:skip")).then(function(){n=1})}();return Promise.resolve(o&&o.then?o.then(e):e(o))}catch(i){return Promise.reject(i)}},R=function(t,e){var n;let{html:r}=t,{containers:o}=void 0===e?this.options:e;const i=(new DOMParser).parseFromString(r,"text/html"),s=(null==(n=i.querySelector("title"))?void 0:n.innerText)||"";return document.title=s,o.map(t=>{const e=document.querySelector(t),n=i.querySelector(t);return e&&n?(e.replaceWith(n),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),n||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===o.length},I=function(){try{const t=this;if(!t.context.animation.animate)return Promise.resolve();const e=t.hooks.trigger("animation:await",{direction:"in"},function(e,n){let{direction:r}=n;try{return Promise.resolve(t.awaitAnimations({selector:e.animation.selector,direction:r})).then(function(){})}catch(t){return Promise.reject(t)}});return Promise.resolve(h()).then(function(){return Promise.resolve(t.hooks.trigger("animation:in:start",void 0,()=>{t.classes.remove("is-animating")})).then(function(){return Promise.resolve(e).then(function(){return Promise.resolve(t.hooks.trigger("animation:in:end")).then(function(){})})})})}catch(t){return Promise.reject(t)}},L=function(n,o){try{const i=this,{url:s,html:a}=o;return i.classes.remove("is-leaving"),i.isSameResolvedUrl(e(),n)?(i.isSameResolvedUrl(e(),s)||(r(s),i.currentPageUrl=e(),i.context.to.url=i.currentPageUrl),i.context.animation.animate&&i.classes.add("is-rendering"),i.context.to.html=a,Promise.resolve(i.hooks.trigger("content:replace",{page:o},(e,n)=>{let{page:r}=n;if(!i.replaceContent(r,{containers:e.containers}))throw new Error("[swup] Container mismatch, aborting");i.context.animation.animate&&(i.classes.add("is-animating","is-changing","is-rendering"),i.context.animation.name&&i.classes.add(`to-${t(i.context.animation.name)}`))})).then(function(){return Promise.resolve(i.hooks.trigger("content:scroll",{options:{behavior:"auto"}},(t,e)=>{let{options:n}=e;if(i.context.scroll.target){const t=i.getAnchorElement(i.context.scroll.target);if(t)return void t.scrollIntoView(n)}i.context.scroll.reset&&window.scrollTo(0,0)})).then(function(){return Promise.resolve(i.hooks.trigger("page:view",{url:i.currentPageUrl,title:document.title})).then(function(){i.options.cache||i.cache.clear()})})})):Promise.resolve()}catch(t){return Promise.reject(t)}},D=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function M(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function T(t){return this.plugins.find(e=>e===t||e.name===t)}return class{constructor(t){void 0===t&&(t={}),this.version="4.0.0-rc.23",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=e(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=$,this.performVisit=H,this.animatePageOut=q,this.renderPage=L,this.replaceContent=R,this.animatePageIn=I,this.delegateEvent=s,this.fetchPage=A,this.awaitAnimations=S,this.getAnchorElement=x,this.use=D,this.unuse=M,this.findPlugin=T,this.getCurrentUrl=e,this.createContext=g,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:function(t,e){let{el:n}=void 0===e?{}:e;return!(null==n||!n.closest("[data-no-swup]"))},linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options={...this.defaults,...t},this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new c(this),this.classes=new p(this),this.hooks=new k(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}enable(){try{const t=this,{linkSelector:e}=t.options;return t.clickDelegate=t.delegateEvent(e,"click",t.linkClickHandler),window.addEventListener("popstate",t.popStateHandler),t.options.plugins.forEach(e=>t.use(e)),r(null,{index:1}),Promise.resolve(h()).then(function(){return Promise.resolve(t.hooks.trigger("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")})).then(function(){return Promise.resolve(t.hooks.trigger("page:view",{url:t.currentPageUrl,title:document.title})).then(function(){})})})}catch(t){return Promise.reject(t)}}destroy(){try{const t=this;return t.clickDelegate.destroy(),window.removeEventListener("popstate",t.popStateHandler),t.cache.clear(),t.options.plugins.forEach(e=>t.unuse(e)),Promise.resolve(t.hooks.trigger("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")})).then(function(){t.hooks.clear()})}catch(t){return Promise.reject(t)}}shouldIgnoreVisit(t,e){let{el:n,event:r}=void 0===e?{}:e;const{origin:o,url:i,hash:s}=a.fromUrl(t);return o!==window.location.origin||!(!n||!this.triggerWillOpenNewWindow(n))||!!this.options.ignoreVisit(i+s,{el:n,event:r})}linkClickHandler(t){const e=t.delegateTarget,{href:n,url:o,hash:i}=a.fromElement(e);this.shouldIgnoreVisit(n,{el:e,event:t})||(this.context=this.createContext({to:o,hash:i,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.trigger("link:newtab",{href:n}):0===t.button&&this.hooks.triggerSync("link:click",{el:e,event:t},()=>{const e=this.context.from.url??"";t.preventDefault(),o&&o!==e?this.isSameResolvedUrl(o,e)||this.performVisit(o):i?(r(o+i),this.hooks.triggerSync("link:anchor",{hash:i,options:{behavior:"auto"}},(t,e)=>{let{hash:n,options:r}=e;const o=this.getAnchorElement(n);o&&o.scrollIntoView(r)})):this.hooks.triggerSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var n,r;const o=(null==(n=t.state)?void 0:n.url)??location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(e(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(o,{event:t}))return;const{url:i,hash:s}=a.fromUrl(o);this.context=this.createContext({to:i,hash:s,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(r=t.state)?void 0:r.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.triggerSync("history:popstate",{event:t},()=>{this.performVisit(i)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).Swup=e()}(this,function(){const t=(t,e)=>String(t).toLowerCase().replace(/[\s/_.]+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-").replace(/^-+|-+$/g,"")||e||"",e=function(t){let{hash:e}=void 0===t?{}:t;return location.pathname+location.search+(e?location.hash:"")},n=function(t,n){void 0===n&&(n={});const o={url:t=t||e({hash:!0}),random:Math.random(),source:"swup",...n};history.pushState(o,"",t)},o=function(t,n){void 0===t&&(t=null),void 0===n&&(n={}),t=t||e({hash:!0});const o={...history.state,url:t,random:Math.random(),source:"swup",...n};history.replaceState(o,"",t)},r=new WeakMap;function i(t,e,n,o){if(!t&&!r.has(e))return!1;const i=r.get(e)??new WeakMap;r.set(e,i);const s=i.get(n)??new Set;i.set(n,s);const a=s.has(o);return t?s.add(o):s.delete(o),a&&t}const s=(t,e,n,o)=>{const r=new AbortController;return function(t,e,n,o={}){const{signal:r,base:s=document}=o;if(r?.aborted)return;const{once:a,...c}=o,l=s instanceof Document?s.documentElement:s,u=Boolean("object"==typeof o?o.capture:o),h=o=>{const r=function(t,e){let n=t.target;if(n instanceof Text&&(n=n.parentElement),n instanceof Element&&t.currentTarget instanceof Element){const o=n.closest(e);if(o&&t.currentTarget.contains(o))return o}}(o,t);if(r){const t=Object.assign(o,{delegateTarget:r});n.call(l,t),a&&(l.removeEventListener(e,h,c),i(!1,l,n,f))}},f=JSON.stringify({selector:t,type:e,capture:u});i(!0,l,n,f)||l.addEventListener(e,h,c),r?.addEventListener("abort",()=>{i(!1,l,n,f)})}(t,e,n,o={...o,signal:r.signal}),{destroy:()=>r.abort()}};class a extends URL{constructor(t,e){void 0===e&&(e=document.baseURI),super(t.toString(),e)}get url(){return this.pathname+this.search}static fromElement(t){const e=t.getAttribute("href")||t.getAttribute("xlink:href")||"";return new a(e)}static fromUrl(t){return new a(t)}}class c{constructor(t){this.swup=void 0,this.pages=new Map,this.swup=t}get size(){return this.pages.size}get all(){return this.pages}has(t){return this.pages.has(this.resolve(t))}get(t){return this.pages.get(this.resolve(t))}set(t,e){t=this.resolve(t),e={...e,url:t},this.pages.set(t,e),this.swup.hooks.callSync("cache:set",{page:e})}update(t,e){t=this.resolve(t),e={...this.get(t),...e,url:t},this.pages.set(t,e)}delete(t){this.pages.delete(this.resolve(t))}clear(){this.pages.clear(),this.swup.hooks.callSync("cache:clear")}prune(t){this.pages.forEach((e,n)=>{t(n,e)&&this.delete(n)})}resolve(t){const{url:e}=a.fromUrl(t);return this.swup.resolveUrl(e)}}const l=function(t,e){return void 0===e&&(e=document),e.querySelector(t)},u=function(t,e){return void 0===e&&(e=document),Array.from(e.querySelectorAll(t))},h=()=>new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(()=>{t()})})});function f(t){return!!t&&("object"==typeof t||"function"==typeof t)&&"function"==typeof t.then}const m=t=>window.CSS&&window.CSS.escape?CSS.escape(t):t,d=t=>1e3*Number(t.slice(0,-1).replace(",","."));class p{constructor(t){this.swup=void 0,this.swupClasses=["to-","is-changing","is-rendering","is-popstate","is-animating"],this.swup=t}get selectors(){const{scope:t}=this.swup.context.animation;return"containers"===t?this.swup.context.containers:"html"===t?["html"]:Array.isArray(t)?t:[]}get selector(){return this.selectors.join(",")}get targets(){return this.selector.trim()?u(this.selector):[]}add(){this.targets.forEach(t=>t.classList.add(...[].slice.call(arguments)))}remove(){this.targets.forEach(t=>t.classList.remove(...[].slice.call(arguments)))}clear(){this.targets.forEach(t=>{const e=t.className.split(" ").filter(t=>this.isSwupClass(t));t.classList.remove(...e)})}isSwupClass(t){return this.swupClasses.some(e=>t.startsWith(e))}}function g(t){let{to:e,from:n=this.currentPageUrl,hash:o,animate:r=!0,animation:i,el:s,event:a,action:c="push",resetScroll:l=!0}=t;return{from:{url:n},to:{url:e},containers:this.options.containers,animation:{animate:r,wait:!1,name:i,scope:this.options.animationScope,selector:this.options.animationSelector},trigger:{el:s,event:a},history:{action:c,popstate:!1,direction:void 0},scroll:{reset:l,target:o}}}const v="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function w(t,e,n){if(!t.s){if(n instanceof y){if(!n.s)return void(n.o=w.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(w.bind(null,t,e),w.bind(null,t,2));t.s=e,t.v=n;const o=t.o;o&&o(t)}}const y=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){const o=new t,r=this.s;if(r){const t=1&r?e:n;if(t){try{w(o,1,t(this.v))}catch(t){w(o,2,t)}return o}return this}return this.o=function(t){try{const r=t.v;1&t.s?w(o,1,e?e(r):r):n?w(o,1,n(r)):w(o,2,r)}catch(t){w(o,2,t)}},o},t}();function P(t){return t instanceof y&&1&t.s}class k{constructor(t){this.swup=void 0,this.registry=new Map,this.hooks=["animation:out:start","animation:out:await","animation:out:end","animation:in:start","animation:in:await","animation:in:end","animation:skip","cache:clear","cache:set","content:replace","content:scroll","enable","disable","fetch:request","fetch:error","history:popstate","link:click","link:self","link:anchor","link:newtab","page:request","page:load","page:view","visit:start","visit:end"],this.swup=t,this.init()}init(){this.hooks.forEach(t=>this.create(t))}create(t){this.registry.has(t)||this.registry.set(t,new Map)}has(t){return this.registry.has(t)}get(t){const e=this.registry.get(t);if(e)return e;console.error(`Unknown hook '${t}'`)}clear(){this.registry.forEach(t=>t.clear())}on(t,e,n){void 0===n&&(n={});const o=this.get(t);if(!o)return console.warn(`Hook '${t}' not found.`),()=>{};const r=o.size+1,i={...n,id:r,hook:t,handler:e};return o.set(e,i),()=>this.off(t,e)}before(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,before:!0})}replace(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,replace:!0})}once(t,e,n){return void 0===n&&(n={}),this.on(t,e,{...n,once:!0})}off(t,e){const n=this.get(t);n&&e?n.delete(e)||console.warn(`Handler for hook '${t}' not found.`):n&&n.clear()}call(t,e,n){try{const o=this,{before:r,handler:i,after:s}=o.getHandlers(t,n);return Promise.resolve(o.run(r,e)).then(function(){return Promise.resolve(o.run(i,e)).then(function(n){let[r]=n;return Promise.resolve(o.run(s,e)).then(function(){return o.dispatchDomEvent(t,e),r})})})}catch(t){return Promise.reject(t)}}callSync(t,e,n){const{before:o,handler:r,after:i}=this.getHandlers(t,n);this.runSync(o,e);const[s]=this.runSync(r,e);return this.runSync(i,e),this.dispatchDomEvent(t,e),s}run(t,e){try{const n=this,o=[],r=function(t,e,n){if("function"==typeof t[v]){var o,r,i,s=t[v]();if(function t(n){try{for(;!(o=s.next()).done;)if((n=e(o.value))&&n.then){if(!P(n))return void n.then(t,i||(i=w.bind(null,r=new y,2)));n=n.v}r?w(r,1,n):r=n}catch(t){w(r||(r=new y),2,t)}}(),s.return){var a=function(t){try{o.done||s.return()}catch(t){}return t};if(r&&r.then)return r.then(a,function(t){throw a(t)});a()}return r}if(!("length"in t))throw new TypeError("Object is not iterable");for(var c=[],l=0;l<t.length;l++)c.push(t[l]);return function(t,e,n){var o,r,i=-1;return function n(s){try{for(;++i<t.length;)if((s=e(i))&&s.then){if(!P(s))return void s.then(n,r||(r=w.bind(null,o=new y,2)));s=s.v}o?w(o,1,s):o=s}catch(t){w(o||(o=new y),2,t)}}(),o}(c,function(t){return e(c[t])})}(t,function(t){let{hook:r,handler:i,defaultHandler:s,once:a}=t;return Promise.resolve(function(t,e,n){return void 0===e&&(e=[]),void 0===n&&(n={}),new Promise((o,r)=>{const i=t.apply(n,e);f(i)?i.then(o,r):o(i)})}(i,[n.swup.context,e,s])).then(function(t){o.push(t),a&&n.off(r,i)})});return Promise.resolve(r&&r.then?r.then(function(){return o}):o)}catch(t){return Promise.reject(t)}}runSync(t,e){const n=[];for(const{hook:o,handler:r,defaultHandler:i,once:s}of t){const t=r(this.swup.context,e,i);n.push(t),f(t)&&console.warn(`Promise returned from handler for synchronous hook '${o}'.Swup will not wait for it to resolve.`),s&&this.off(o,r)}return n}getHandlers(t,e){const n=this.get(t);if(!n)return{found:!1,before:[],handler:[],after:[],replaced:!1};const o=this.sortRegistrations,r=Array.from(n.values()),i=r.filter(t=>{let{before:e,replace:n}=t;return e&&!n}).sort(o),s=r.filter(t=>{let{replace:e}=t;return e}).sort(o),a=r.filter(t=>{let{before:e,replace:n}=t;return!e&&!n}).sort(o),c=s.length>0;let l=[];if(e&&(l=[{id:0,hook:t,handler:e}],c)){const n=s.length-1,o=t=>{const n=s[t-1];return n?(e,r)=>n.handler(e,r,o(t-1)):e};l=[{id:0,hook:t,handler:s[n].handler,defaultHandler:o(n)}]}return{found:!0,before:i,handler:l,after:a,replaced:c}}sortRegistrations(t,e){return(t.priority??0)-(e.priority??0)||t.id-e.id||0}dispatchDomEvent(t,e){document.dispatchEvent(new CustomEvent(`swup:${t}`,{detail:{hook:t,args:e,context:this.swup.context}}))}}const x=t=>{if(t&&"#"===t.charAt(0)&&(t=t.substring(1)),!t)return null;const e=decodeURIComponent(t);let n=document.getElementById(t)||document.getElementById(e)||l(`a[name='${m(t)}']`)||l(`a[name='${m(e)}']`);return n||"top"!==t||(n=document.body),n},S=function(t){let{elements:e,selector:n}=t;try{if(!1===n&&!e)return Promise.resolve();let t=[];if(e)t=Array.from(e);else if(n&&(t=u(n,document.body),!t.length))return console.warn(`[swup] No elements found matching animationSelector \`${n}\``),Promise.resolve();const o=t.map(t=>function(t){const{type:e,timeout:n,propCount:o}=function(t,e){const n=window.getComputedStyle(t),o=U(n,`${b}Delay`),r=U(n,`${b}Duration`),i=C(o,r),s=U(n,`${E}Delay`),a=U(n,`${E}Duration`),c=C(s,a);let l=null,u=0,h=0;return e===b?i>0&&(l=b,u=i,h=r.length):e===E?c>0&&(l=E,u=c,h=a.length):(u=Math.max(i,c),l=u>0?i>c?b:E:null,h=l?l===b?r.length:a.length:0),{type:l,timeout:u,propCount:h}}(t);return!(!e||!n)&&new Promise(r=>{const i=`${e}end`,s=performance.now();let a=0;const c=()=>{t.removeEventListener(i,l),r()},l=e=>{if(e.target===t){if(!function(t){return[`${b}end`,`${E}end`].includes(t.type)}(e))throw new Error("Not a transition or animation event.");(performance.now()-s)/1e3<e.elapsedTime||++a>=o&&c()}};setTimeout(()=>{a<o&&c()},n+1),t.addEventListener(i,l)})}(t));return o.filter(Boolean).length>0?Promise.resolve(Promise.all(o)).then(function(){}):(n&&console.warn(`[swup] No CSS animation duration defined on elements matching \`${n}\``),Promise.resolve())}catch(t){return Promise.reject(t)}},b="transition",E="animation";function U(t,e){return(t[e]||"").split(", ")}function C(t,e){for(;t.length<e.length;)t=t.concat(t);return Math.max(...e.map((e,n)=>d(e)+d(t[n])))}const H=function(t,r){void 0===r&&(r={});try{const i=this;if("string"!=typeof t)throw new Error("swup.visit() requires a URL parameter");const{el:s}=i.context.trigger;i.context.to.url=a.fromUrl(t).url,r.referrer=r.referrer||i.currentPageUrl,!1===r.animate&&(i.context.animation.animate=!1),i.context.animation.animate||i.classes.clear();const c=r.history||(null==s?void 0:s.getAttribute("data-swup-history"))||void 0;c&&["push","replace"].includes(c)&&(i.context.history.action=c);const l=r.animation||(null==s?void 0:s.getAttribute("data-swup-animation"))||void 0;return l&&(i.context.animation.name=l),Promise.resolve(function(s,a){try{var c=Promise.resolve(i.hooks.call("visit:start")).then(function(){function s(){const t=i.animatePageOut();return Promise.resolve(Promise.all([a,t])).then(function(t){let[e]=t;return Promise.resolve(i.renderPage(i.context.to.url,e)).then(function(){return Promise.resolve(i.animatePageIn()).then(function(){return Promise.resolve(i.hooks.call("visit:end",void 0,()=>i.classes.clear())).then(function(){})})})})}const a=i.hooks.call("page:request",{url:i.context.to.url,options:r},function(t,e){let{options:n}=e;try{return Promise.resolve(i.fetchPage(t.to.url,n))}catch(t){return Promise.reject(t)}});if(!i.context.history.popstate){const e=t+(i.context.scroll.target||"");"replace"===i.context.history.action?o(e):n(e,{index:i.currentHistoryIndex+1})}i.currentPageUrl=e();const c=function(){if(i.context.animation.wait)return Promise.resolve(a).then(function(t){let{html:e}=t;i.context.to.html=e})}();return c&&c.then?c.then(s):s()})}catch(t){return a(t)}return c&&c.then?c.then(void 0,a):c}(0,function(t){t&&(console.error(t),i.options.skipPopStateHandling=()=>(window.location.href=i.context.to.url,!0),window.history.go(-1))}))}catch(t){return Promise.reject(t)}};function $(t,e,n){if(void 0===e&&(e={}),void 0===n&&(n={}),this.shouldIgnoreVisit(t))return void(window.location.href=t);const{url:o,hash:r}=a.fromUrl(t);this.context=this.createContext({...n,to:o,hash:r}),this.performVisit(o,e)}const A=function(t,e){void 0===e&&(e={});try{let o;const r=this;function n(n){if(o)return n;const i={...r.options.requestHeaders,...e.headers};return e={...e,headers:i},Promise.resolve(r.hooks.call("fetch:request",{url:t,options:e},(t,e)=>{let{url:n,options:o}=e;return fetch(n,o)})).then(function(n){const{status:o,url:i}=n;return Promise.resolve(n.text()).then(function(s){if(500===o)throw r.hooks.call("fetch:error",{status:o,response:n,url:i}),new j(`Server error: ${i}`,{status:o,url:i});if(!s)throw new j(`Empty response: ${i}`,{status:o,url:i});const{url:c}=a.fromUrl(i),l={url:c,html:s};t===c&&r.cache.set(l.url,l);const u=function(){if(!1!==e.triggerHooks)return Promise.resolve(r.hooks.call("page:load",{page:l,cache:!1})).then(function(){})}();return u&&u.then?u.then(function(){return l}):l})})}t=a.fromUrl(t).url;const i=function(){if(r.cache.has(t)){function n(){return o=1,i}const i=r.cache.get(t),s=function(){if(!1!==e.triggerHooks)return Promise.resolve(r.hooks.call("page:load",{page:i,cache:!0})).then(function(){})}();return s&&s.then?s.then(n):n()}}();return Promise.resolve(i&&i.then?i.then(n):n(i))}catch(s){return Promise.reject(s)}};class j extends Error{constructor(t,e){super(t),this.url=void 0,this.status=void 0,this.name="FetchError",this.url=e.url,this.status=e.status}}const q=function(){try{let n;const o=this;function e(e){return n?e:Promise.resolve(o.hooks.call("animation:out:start",void 0,()=>{o.classes.add("is-changing","is-leaving","is-animating"),o.context.history.popstate&&o.classes.add("is-popstate"),o.context.animation.name&&o.classes.add(`to-${t(o.context.animation.name)}`)})).then(function(){return Promise.resolve(o.hooks.call("animation:out:await",{skip:!1},function(t,e){let{skip:n}=e;try{return n?Promise.resolve():Promise.resolve(o.awaitAnimations({selector:t.animation.selector})).then(function(){})}catch(t){return Promise.reject(t)}})).then(function(){return Promise.resolve(o.hooks.call("animation:out:end")).then(function(){})})})}const r=function(){if(!o.context.animation.animate)return Promise.resolve(o.hooks.call("animation:skip")).then(function(){n=1})}();return Promise.resolve(r&&r.then?r.then(e):e(r))}catch(i){return Promise.reject(i)}},R=function(t,e){var n;let{html:o}=t,{containers:r}=void 0===e?this.options:e;const i=(new DOMParser).parseFromString(o,"text/html"),s=(null==(n=i.querySelector("title"))?void 0:n.innerText)||"";return document.title=s,r.map(t=>{const e=document.querySelector(t),n=i.querySelector(t);return e&&n?(e.replaceWith(n),!0):(e||console.warn(`[swup] Container missing in current document: ${t}`),n||console.warn(`[swup] Container missing in incoming document: ${t}`),!1)}).filter(Boolean).length===r.length},I=function(){try{const t=this;if(!t.context.animation.animate)return Promise.resolve();const e=t.hooks.call("animation:in:await",{skip:!1},function(e,n){let{skip:o}=n;try{return o?Promise.resolve():Promise.resolve(t.awaitAnimations({selector:e.animation.selector})).then(function(){})}catch(t){return Promise.reject(t)}});return Promise.resolve(h()).then(function(){return Promise.resolve(t.hooks.call("animation:in:start",void 0,()=>{t.classes.remove("is-animating")})).then(function(){return Promise.resolve(e).then(function(){return Promise.resolve(t.hooks.call("animation:in:end")).then(function(){})})})})}catch(t){return Promise.reject(t)}},L=function(n,r){try{const i=this,{url:s,html:a}=r;return i.classes.remove("is-leaving"),i.isSameResolvedUrl(e(),n)?(i.isSameResolvedUrl(e(),s)||(o(s),i.currentPageUrl=e(),i.context.to.url=i.currentPageUrl),i.context.animation.animate&&i.classes.add("is-rendering"),i.context.to.html=a,Promise.resolve(i.hooks.call("content:replace",{page:r},(e,n)=>{let{page:o}=n;if(!i.replaceContent(o,{containers:e.containers}))throw new Error("[swup] Container mismatch, aborting");i.context.animation.animate&&(i.classes.add("is-animating","is-changing","is-rendering"),i.context.animation.name&&i.classes.add(`to-${t(i.context.animation.name)}`))})).then(function(){return Promise.resolve(i.hooks.call("content:scroll",{options:{behavior:"auto"}},(t,e)=>{let{options:n}=e;if(i.context.scroll.target){const t=i.getAnchorElement(i.context.scroll.target);if(t)return void t.scrollIntoView(n)}i.context.scroll.reset&&window.scrollTo(0,0)})).then(function(){return Promise.resolve(i.hooks.call("page:view",{url:i.currentPageUrl,title:document.title})).then(function(){i.options.cache||i.cache.clear()})})})):Promise.resolve()}catch(t){return Promise.reject(t)}},D=function(t){var e;if(null==(e=t)?void 0:e.isSwupPlugin){if(t.swup=this,!t._checkRequirements||t._checkRequirements())return t._beforeMount&&t._beforeMount(),t.mount(),this.plugins.push(t),this.plugins}else console.error("Not a swup plugin instance",t)};function M(t){const e=this.findPlugin(t);if(e)return e.unmount(),e._afterUnmount&&e._afterUnmount(),this.plugins=this.plugins.filter(t=>t!==e),this.plugins;console.error("No such plugin",e)}function T(t){return this.plugins.find(e=>e===t||e.name===t)}return class{constructor(t){void 0===t&&(t={}),this.version="4.0.0-rc.24",this.options=void 0,this.plugins=[],this.context=void 0,this.cache=void 0,this.hooks=void 0,this.classes=void 0,this.currentPageUrl=e(),this.currentHistoryIndex=1,this.clickDelegate=void 0,this.visit=$,this.performVisit=H,this.animatePageOut=q,this.renderPage=L,this.replaceContent=R,this.animatePageIn=I,this.delegateEvent=s,this.fetchPage=A,this.awaitAnimations=S,this.getAnchorElement=x,this.use=D,this.unuse=M,this.findPlugin=T,this.getCurrentUrl=e,this.createContext=g,this.log=()=>{},this.defaults={animateHistoryBrowsing:!1,animationSelector:'[class*="transition-"]',animationScope:"html",cache:!0,containers:["#swup"],ignoreVisit:function(t,e){let{el:n}=void 0===e?{}:e;return!(null==n||!n.closest("[data-no-swup]"))},linkSelector:"a[href]",plugins:[],resolveUrl:t=>t,requestHeaders:{"X-Requested-With":"swup",Accept:"text/html, application/xhtml+xml"},skipPopStateHandling:t=>{var e;return"swup"!==(null==(e=t.state)?void 0:e.source)}},this.options={...this.defaults,...t},this.linkClickHandler=this.linkClickHandler.bind(this),this.popStateHandler=this.popStateHandler.bind(this),this.cache=new c(this),this.classes=new p(this),this.hooks=new k(this),this.context=this.createContext({to:void 0}),this.checkRequirements()&&this.enable()}checkRequirements(){return"undefined"!=typeof Promise||(console.warn("Promise is not supported"),!1)}enable(){try{const t=this,{linkSelector:e}=t.options;return t.clickDelegate=t.delegateEvent(e,"click",t.linkClickHandler),window.addEventListener("popstate",t.popStateHandler),t.options.plugins.forEach(e=>t.use(e)),o(null,{index:1}),Promise.resolve(h()).then(function(){return Promise.resolve(t.hooks.call("enable",void 0,()=>{document.documentElement.classList.add("swup-enabled")})).then(function(){return Promise.resolve(t.hooks.call("page:view",{url:t.currentPageUrl,title:document.title})).then(function(){})})})}catch(t){return Promise.reject(t)}}destroy(){try{const t=this;return t.clickDelegate.destroy(),window.removeEventListener("popstate",t.popStateHandler),t.cache.clear(),t.options.plugins.forEach(e=>t.unuse(e)),Promise.resolve(t.hooks.call("disable",void 0,()=>{document.documentElement.classList.remove("swup-enabled")})).then(function(){t.hooks.clear()})}catch(t){return Promise.reject(t)}}shouldIgnoreVisit(t,e){let{el:n,event:o}=void 0===e?{}:e;const{origin:r,url:i,hash:s}=a.fromUrl(t);return r!==window.location.origin||!(!n||!this.triggerWillOpenNewWindow(n))||!!this.options.ignoreVisit(i+s,{el:n,event:o})}linkClickHandler(t){const e=t.delegateTarget,{href:n,url:r,hash:i}=a.fromElement(e);this.shouldIgnoreVisit(n,{el:e,event:t})||(this.context=this.createContext({to:r,hash:i,el:e,event:t}),t.metaKey||t.ctrlKey||t.shiftKey||t.altKey?this.hooks.call("link:newtab",{href:n}):0===t.button&&this.hooks.callSync("link:click",{el:e,event:t},()=>{const e=this.context.from.url??"";t.preventDefault(),r&&r!==e?this.isSameResolvedUrl(r,e)||this.performVisit(r):i?(o(r+i),this.hooks.callSync("link:anchor",{hash:i,options:{behavior:"auto"}},(t,e)=>{let{hash:n,options:o}=e;const r=this.getAnchorElement(n);r&&r.scrollIntoView(o)})):this.hooks.callSync("link:self",void 0,t=>{t.scroll.reset&&window.scroll({top:0,left:0,behavior:"auto"})})}))}triggerWillOpenNewWindow(t){return!!t.matches('[download], [target="_blank"]')}popStateHandler(t){var n,o;const r=(null==(n=t.state)?void 0:n.url)??location.href;if(this.options.skipPopStateHandling(t))return;if(this.isSameResolvedUrl(e(),this.currentPageUrl))return;if(this.shouldIgnoreVisit(r,{event:t}))return;const{url:i,hash:s}=a.fromUrl(r);this.context=this.createContext({to:i,hash:s,event:t,animate:this.options.animateHistoryBrowsing,resetScroll:this.options.animateHistoryBrowsing}),this.context.history.popstate=!0;const c=Number(null==(o=t.state)?void 0:o.index);c&&(this.context.history.direction=c-this.currentHistoryIndex>0?"forwards":"backwards"),this.hooks.callSync("history:popstate",{event:t},()=>{this.performVisit(i)})}resolveUrl(t){if("function"!=typeof this.options.resolveUrl)return console.warn("[swup] options.resolveUrl expects a callback function."),t;const e=this.options.resolveUrl(t);return e&&"string"==typeof e?e.startsWith("//")||e.startsWith("http")?(console.warn("[swup] options.resolveUrl needs to return a relative url"),t):e:(console.warn("[swup] options.resolveUrl needs to return a url"),t)}isSameResolvedUrl(t,e){return this.resolveUrl(t)===this.resolveUrl(e)}}}); | ||
//# sourceMappingURL=Swup.umd.js.map |
@@ -7,5 +7,4 @@ import Swup, { Options } from '../Swup.js'; | ||
/** | ||
* Return a Promise that resolves when all animations are done on the page. | ||
* | ||
* @note We don't make use of the `direction` argument, but it's required by JS plugin | ||
* Return a Promise that resolves when all CSS animations and transitions | ||
* are done on the page. Filters by selector or takes elements directly. | ||
*/ | ||
@@ -15,3 +14,2 @@ export declare function awaitAnimations(this: Swup, { elements, selector }: { | ||
elements?: NodeListOf<HTMLElement> | HTMLElement[]; | ||
direction?: AnimationDirection; | ||
}): Promise<void>; | ||
@@ -18,0 +16,0 @@ export declare function getTransitionInfo(element: Element, expectedType?: AnimationTypes): { |
@@ -5,12 +5,14 @@ import { DelegateEvent } from 'delegate-it'; | ||
import { FetchOptions, PageData } from './fetchPage.js'; | ||
import { AnimationDirection } from './awaitAnimations.js'; | ||
export interface HookDefinitions { | ||
'animation:out:start': undefined; | ||
'animation:out:await': { | ||
skip: boolean; | ||
}; | ||
'animation:out:end': undefined; | ||
'animation:in:start': undefined; | ||
'animation:in:await': { | ||
skip: boolean; | ||
}; | ||
'animation:in:end': undefined; | ||
'animation:skip': undefined; | ||
'animation:await': { | ||
direction: AnimationDirection; | ||
}; | ||
'cache:clear': undefined; | ||
@@ -94,2 +96,3 @@ 'cache:set': { | ||
handler: Handler<T>; | ||
defaultHandler?: Handler<T>; | ||
} & HookOptions; | ||
@@ -119,3 +122,3 @@ type HookLedger<T extends HookName> = Map<Handler<T>, HookRegistration<T>>; | ||
*/ | ||
create(hook: HookName): void; | ||
create(hook: string): void; | ||
/** | ||
@@ -195,3 +198,3 @@ * Check if a hook is registered. | ||
*/ | ||
trigger<T extends HookName>(hook: T, args?: HookArguments<T>, defaultHandler?: Handler<T>): Promise<any>; | ||
call<T extends HookName>(hook: T, args?: HookArguments<T>, defaultHandler?: Handler<T>): Promise<any>; | ||
/** | ||
@@ -205,3 +208,3 @@ * Trigger a hook synchronously, executing its default handler and all registered handlers. | ||
*/ | ||
triggerSync<T extends HookName>(hook: T, args?: HookArguments<T>, defaultHandler?: Handler<T>): any; | ||
callSync<T extends HookName>(hook: T, args?: HookArguments<T>, defaultHandler?: Handler<T>): any; | ||
/** | ||
@@ -212,3 +215,3 @@ * Execute the handlers for a hook, in order, as `Promise`s that will be `await`ed. | ||
*/ | ||
execute<T extends HookName>(registrations: HookRegistration<T>[], args?: HookArguments<T>, defaultHandler?: Handler<T>): Promise<any>; | ||
private run; | ||
/** | ||
@@ -219,3 +222,3 @@ * Execute the handlers for a hook, in order, without `await`ing any returned `Promise`s. | ||
*/ | ||
executeSync<T extends HookName>(registrations: HookRegistration<T>[], args?: HookArguments<T>, defaultHandler?: Handler<T>): any[]; | ||
private runSync; | ||
/** | ||
@@ -228,9 +231,3 @@ * Get all registered handlers for a hook, sorted by priority and registration order. | ||
*/ | ||
getHandlers<T extends HookName>(hook: T, defaultHandler?: Handler<T>): { | ||
found: boolean; | ||
before: HookRegistration<T>[]; | ||
handler: HookRegistration<T>[]; | ||
after: HookRegistration<T>[]; | ||
replaced: boolean; | ||
}; | ||
private getHandlers; | ||
/** | ||
@@ -242,9 +239,9 @@ * Sort two hook registrations by priority and registration order. | ||
*/ | ||
sortRegistrations<T extends HookName>(a: HookRegistration<T>, b: HookRegistration<T>): number; | ||
private sortRegistrations; | ||
/** | ||
* Trigger a custom event on the `document`. Prefixed with `swup:` | ||
* @param hook Name of the hook to trigger. | ||
* Dispatch a custom event on the `document` for a hook. Prefixed with `swup:` | ||
* @param hook Name of the hook. | ||
*/ | ||
dispatchDomEvent<T extends HookName>(hook: T, args?: HookArguments<T>): void; | ||
private dispatchDomEvent; | ||
} | ||
export {}; |
{ | ||
"name": "swup", | ||
"amdName": "Swup", | ||
"version": "4.0.0-rc.23", | ||
"version": "4.0.0-rc.24", | ||
"description": "Versatile and extensible page transition library for server-rendered websites", | ||
@@ -6,0 +6,0 @@ "type": "module", |
@@ -93,9 +93,9 @@ <div align="center"> | ||
If you're having trouble implementing swup, check out the [Common Issues](https://swup.js.org/other/common-issues) section of the docs, look at [closed issues](https://github.com/swup/swup/issues?q=is%3Aissue+is%3Aclosed) or create a [new discussion](https://github.com/swup/swup/discussions/new). | ||
If you're having trouble implementing swup, check out the [Common Issues](https://swup.js.org/getting-started/common-issues/) section of the docs, look at [closed issues](https://github.com/swup/swup/issues?q=is%3Aissue+is%3Aclosed) or create a [new discussion](https://github.com/swup/swup/discussions/new). | ||
## Want to Contribute? | ||
<a href="https://github.com/swup/swup/discussions/424">We're looking for maintainers!</a> 👀 | ||
[We're looking for maintainers!](https://github.com/swup/swup/discussions/424) 👀 | ||
Become a sponsor on [Open Collective](https://opencollective.com/swup) or support development through | ||
[GitHub sponsors](https://github.com/sponsors/gmrchk). |
@@ -129,3 +129,3 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
// @ts-expect-no-error | ||
await swup.hooks.trigger('history:popstate', { event: new PopStateEvent('') }); | ||
await swup.hooks.call('history:popstate', { event: new PopStateEvent('') }); | ||
@@ -132,0 +132,0 @@ try { |
@@ -40,3 +40,3 @@ import { describe, expect, it, vi } from 'vitest'; | ||
await swup.hooks.trigger('enable'); | ||
await swup.hooks.call('enable'); | ||
@@ -48,3 +48,3 @@ expect(handler1).toBeCalledTimes(1); | ||
await swup.hooks.trigger('enable'); | ||
await swup.hooks.call('enable'); | ||
@@ -65,3 +65,3 @@ expect(handler1).toBeCalledTimes(2); | ||
await swup.hooks.trigger('enable'); | ||
await swup.hooks.call('enable'); | ||
@@ -73,3 +73,3 @@ expect(handler1).toBeCalledTimes(1); | ||
await swup.hooks.trigger('enable'); | ||
await swup.hooks.call('enable'); | ||
@@ -86,3 +86,3 @@ expect(handler1).toBeCalledTimes(2); | ||
await swup.hooks.trigger('enable'); | ||
await swup.hooks.call('enable'); | ||
@@ -98,4 +98,4 @@ expect(handler).toBeCalledTimes(1); | ||
await swup.hooks.trigger('enable', undefined, () => {}); | ||
await swup.hooks.trigger('enable', undefined, () => {}); | ||
await swup.hooks.call('enable', undefined, () => {}); | ||
await swup.hooks.call('enable', undefined, () => {}); | ||
@@ -111,4 +111,4 @@ expect(handler).toBeCalledTimes(1); | ||
await swup.hooks.trigger('enable', undefined, () => {}); | ||
await swup.hooks.trigger('enable', undefined, () => {}); | ||
await swup.hooks.call('enable', undefined, () => {}); | ||
await swup.hooks.call('enable', undefined, () => {}); | ||
@@ -122,3 +122,3 @@ expect(handler).toBeCalledTimes(1); | ||
await swup.hooks.trigger('enable', undefined, handler); | ||
await swup.hooks.call('enable', undefined, handler); | ||
@@ -151,3 +151,3 @@ expect(handler).toBeCalledTimes(1); | ||
await swup.hooks.trigger('disable', undefined, handlers.original); | ||
await swup.hooks.call('disable', undefined, handlers.original); | ||
@@ -185,2 +185,5 @@ expect(called).toEqual(['before', 'original', 'normal', 'after']); | ||
called.push(8); | ||
}, | ||
9: () => { | ||
called.push(9); | ||
} | ||
@@ -192,2 +195,3 @@ }; | ||
swup.hooks.on('disable', handlers['3'], { priority: 1 }); | ||
swup.hooks.on('disable', handlers['9']); | ||
swup.hooks.on('disable', handlers['4']); | ||
@@ -197,5 +201,5 @@ swup.hooks.on('disable', handlers['8'], { priority: 4 }); | ||
await swup.hooks.trigger('disable', undefined, handlers['5']); | ||
await swup.hooks.call('disable', undefined, handlers['5']); | ||
expect(called).toEqual([2, 1, 5, 4, 3, 8, 7]); | ||
expect(called).toEqual([2, 1, 5, 9, 4, 3, 8, 7]); | ||
}); | ||
@@ -210,6 +214,22 @@ | ||
await swup.hooks.trigger('enable', undefined, defaultHandler); | ||
await swup.hooks.call('enable', undefined, defaultHandler); | ||
expect(defaultHandler).toBeCalledTimes(0); | ||
expect(customHandler).toBeCalledTimes(1); | ||
}); | ||
it('should only execute the last replacing handler', async () => { | ||
const swup = new Swup(); | ||
const firstHandler = vi.fn(); | ||
const secondHandler = vi.fn(); | ||
const defaultHandler = vi.fn(); | ||
swup.hooks.on('enable', firstHandler, { replace: true }); | ||
swup.hooks.on('enable', secondHandler, { replace: true }); | ||
await swup.hooks.call('enable', undefined, defaultHandler); | ||
expect(defaultHandler).toBeCalledTimes(0); | ||
expect(firstHandler).toBeCalledTimes(0); | ||
expect(secondHandler).toBeCalledTimes(1); | ||
}); | ||
@@ -219,3 +239,4 @@ | ||
const swup = new Swup(); | ||
const customHandler = vi.fn(); | ||
const args = { foo: 'bar' }; | ||
const customHandler = vi.fn((c, a, handler) => handler(c, args)); | ||
const defaultHandler = vi.fn(); | ||
@@ -226,7 +247,43 @@ const ctx = swup.context; | ||
await swup.hooks.trigger('enable', undefined, defaultHandler); | ||
await swup.hooks.call('enable', undefined, defaultHandler); | ||
expect(customHandler).toBeCalledWith(ctx, undefined, defaultHandler); | ||
expect(defaultHandler).toBeCalledWith(ctx, args); | ||
}); | ||
it('should return result of replacing handler', async () => { | ||
const swup = new Swup(); | ||
const customHandler = vi.fn(async () => 'foo'); | ||
const defaultHandler = vi.fn(async () => 'bar'); | ||
swup.hooks.on('enable', customHandler, { replace: true }); | ||
const result = await swup.hooks.call('enable', undefined, defaultHandler); | ||
expect(result).toEqual('foo'); | ||
}); | ||
it('should pass previous handler into nested replacing handlers', async () => { | ||
const swup = new Swup(); | ||
const args1 = { foo: 'bar' }; | ||
const args2 = { foo: 'baz' }; | ||
const args3 = { foo: 'bag' }; | ||
const defaultHandler = vi.fn(); | ||
const firstHandler = vi.fn((c, a, handler) => handler(c, args1)); | ||
const secondHandler = vi.fn((c, a, handler) => handler(c, args2)); | ||
const thirdHandler = vi.fn((c, a, handler) => handler(c, args3, handler)); | ||
const ctx = swup.context; | ||
swup.hooks.on('enable', firstHandler, { replace: true }); | ||
swup.hooks.on('enable', secondHandler, { replace: true }); | ||
swup.hooks.on('enable', thirdHandler, { replace: true }); | ||
await swup.hooks.call('enable', undefined, defaultHandler); | ||
expect(defaultHandler).toBeCalledWith(ctx, args1); | ||
expect(thirdHandler).toBeCalledWith(ctx, undefined, expect.any(Function)); | ||
expect(secondHandler).toBeCalledWith(ctx, args3, expect.any(Function)); | ||
expect(firstHandler).toBeCalledWith(ctx, args2, expect.any(Function)); | ||
}); | ||
it('should not pass original handler into normal handlers', async () => { | ||
@@ -240,3 +297,3 @@ const swup = new Swup(); | ||
await swup.hooks.trigger('enable', undefined, handler); | ||
await swup.hooks.call('enable', undefined, handler); | ||
@@ -253,3 +310,3 @@ expect(listener).toBeCalledWith(ctx, undefined, undefined); | ||
swup.hooks.on('history:popstate', handler); | ||
await swup.hooks.trigger('history:popstate', args); | ||
await swup.hooks.call('history:popstate', args); | ||
@@ -271,3 +328,3 @@ expect(handler).toBeCalledTimes(1); | ||
// @ts-expect-no-error | ||
await swup.hooks.trigger('history:popstate', { event: new PopStateEvent('') }); | ||
await swup.hooks.call('history:popstate', { event: new PopStateEvent('') }); | ||
@@ -279,4 +336,4 @@ // @ts-expect-error | ||
// @ts-expect-error | ||
await swup.hooks.trigger('history:popstate', { event: new MouseEvent('') }); | ||
await swup.hooks.call('history:popstate', { event: new MouseEvent('') }); | ||
}); | ||
}); |
@@ -39,3 +39,2 @@ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; | ||
console.debug(document.documentElement.querySelector('#container-1')); | ||
const page = mockPage(/*html*/ ` | ||
@@ -42,0 +41,0 @@ <div id="container-1" data-from="incoming"></div> |
@@ -13,7 +13,8 @@ import Swup from '../Swup.js'; | ||
const animation = this.hooks.trigger( | ||
'animation:await', | ||
{ direction: 'in' }, | ||
async (context, { direction }) => { | ||
await this.awaitAnimations({ selector: context.animation.selector, direction }); | ||
const animation = this.hooks.call( | ||
'animation:in:await', | ||
{ skip: false }, | ||
async (context, { skip }) => { | ||
if (skip) return; | ||
await this.awaitAnimations({ selector: context.animation.selector }); | ||
} | ||
@@ -24,3 +25,3 @@ ); | ||
await this.hooks.trigger('animation:in:start', undefined, () => { | ||
await this.hooks.call('animation:in:start', undefined, () => { | ||
this.classes.remove('is-animating'); | ||
@@ -31,3 +32,3 @@ }); | ||
await this.hooks.trigger('animation:in:end'); | ||
await this.hooks.call('animation:in:end'); | ||
}; |
@@ -10,7 +10,7 @@ import Swup from '../Swup.js'; | ||
if (!this.context.animation.animate) { | ||
await this.hooks.trigger('animation:skip'); | ||
await this.hooks.call('animation:skip'); | ||
return; | ||
} | ||
await this.hooks.trigger('animation:out:start', undefined, () => { | ||
await this.hooks.call('animation:out:start', undefined, () => { | ||
this.classes.add('is-changing', 'is-leaving', 'is-animating'); | ||
@@ -25,11 +25,8 @@ if (this.context.history.popstate) { | ||
await this.hooks.trigger( | ||
'animation:await', | ||
{ direction: 'out' }, | ||
async (context, { direction }) => { | ||
await this.awaitAnimations({ selector: context.animation.selector, direction }); | ||
} | ||
); | ||
await this.hooks.call('animation:out:await', { skip: false }, async (context, { skip }) => { | ||
if (skip) return; | ||
await this.awaitAnimations({ selector: context.animation.selector }); | ||
}); | ||
await this.hooks.trigger('animation:out:end'); | ||
await this.hooks.call('animation:out:end'); | ||
}; |
@@ -15,5 +15,4 @@ import { queryAll, toMs } from '../utils.js'; | ||
/** | ||
* Return a Promise that resolves when all animations are done on the page. | ||
* | ||
* @note We don't make use of the `direction` argument, but it's required by JS plugin | ||
* Return a Promise that resolves when all CSS animations and transitions | ||
* are done on the page. Filters by selector or takes elements directly. | ||
*/ | ||
@@ -28,6 +27,5 @@ export async function awaitAnimations( | ||
elements?: NodeListOf<HTMLElement> | HTMLElement[]; | ||
direction?: AnimationDirection; | ||
} | ||
): Promise<void> { | ||
// Allow usage of swup without animations | ||
// Allow usage of swup without animations: { animationSelector: false } | ||
if (selector === false && !elements) { | ||
@@ -34,0 +32,0 @@ return; |
@@ -46,3 +46,3 @@ import Swup from '../Swup.js'; | ||
this.pages.set(url, page); | ||
this.swup.hooks.triggerSync('cache:set', { page }); | ||
this.swup.hooks.callSync('cache:set', { page }); | ||
} | ||
@@ -65,3 +65,3 @@ | ||
this.pages.clear(); | ||
this.swup.hooks.triggerSync('cache:clear'); | ||
this.swup.hooks.callSync('cache:clear'); | ||
} | ||
@@ -68,0 +68,0 @@ |
@@ -39,3 +39,3 @@ import Swup from '../Swup.js'; | ||
if (options.triggerHooks !== false) { | ||
await this.hooks.trigger('page:load', { page, cache: true }); | ||
await this.hooks.call('page:load', { page, cache: true }); | ||
} | ||
@@ -49,3 +49,3 @@ return page; | ||
// Allow hooking before this and returning a custom response-like object (e.g. custom fetch implementation) | ||
const response: Response = await this.hooks.trigger( | ||
const response: Response = await this.hooks.call( | ||
'fetch:request', | ||
@@ -60,3 +60,3 @@ { url, options }, | ||
if (status === 500) { | ||
this.hooks.trigger('fetch:error', { status, response, url: responseUrl }); | ||
this.hooks.call('fetch:error', { status, response, url: responseUrl }); | ||
throw new FetchError(`Server error: ${responseUrl}`, { status, url: responseUrl }); | ||
@@ -79,3 +79,3 @@ } | ||
if (options.triggerHooks !== false) { | ||
await this.hooks.trigger('page:load', { page, cache: false }); | ||
await this.hooks.call('page:load', { page, cache: false }); | ||
} | ||
@@ -82,0 +82,0 @@ |
@@ -7,11 +7,11 @@ import { DelegateEvent } from 'delegate-it'; | ||
import { FetchOptions, PageData } from './fetchPage.js'; | ||
import { AnimationDirection } from './awaitAnimations.js'; | ||
export interface HookDefinitions { | ||
'animation:out:start': undefined; | ||
'animation:out:await': { skip: boolean }; | ||
'animation:out:end': undefined; | ||
'animation:in:start': undefined; | ||
'animation:in:await': { skip: boolean }; | ||
'animation:in:end': undefined; | ||
'animation:skip': undefined; | ||
'animation:await': { direction: AnimationDirection }; | ||
'cache:clear': undefined; | ||
@@ -71,2 +71,3 @@ 'cache:set': { page: PageData }; | ||
handler: Handler<T>; | ||
defaultHandler?: Handler<T>; | ||
} & HookOptions; | ||
@@ -95,7 +96,8 @@ | ||
'animation:out:start', | ||
'animation:out:await', | ||
'animation:out:end', | ||
'animation:in:start', | ||
'animation:in:await', | ||
'animation:in:end', | ||
'animation:skip', | ||
'animation:await', | ||
'cache:clear', | ||
@@ -136,5 +138,5 @@ 'cache:set', | ||
*/ | ||
create(hook: HookName) { | ||
if (!this.registry.has(hook)) { | ||
this.registry.set(hook, new Map()); | ||
create(hook: string) { | ||
if (!this.registry.has(hook as HookName)) { | ||
this.registry.set(hook as HookName, new Map()); | ||
} | ||
@@ -284,3 +286,3 @@ } | ||
*/ | ||
async trigger<T extends HookName>( | ||
async call<T extends HookName>( | ||
hook: T, | ||
@@ -290,6 +292,6 @@ args?: HookArguments<T>, | ||
): Promise<any> { | ||
const { before, handler, after, replaced } = this.getHandlers(hook, defaultHandler); | ||
await this.execute(before, args); | ||
const [result] = await this.execute(handler, args, replaced ? defaultHandler : undefined); | ||
await this.execute(after, args); | ||
const { before, handler, after } = this.getHandlers(hook, defaultHandler); | ||
await this.run(before, args); | ||
const [result] = await this.run(handler, args); | ||
await this.run(after, args); | ||
this.dispatchDomEvent(hook, args); | ||
@@ -307,3 +309,3 @@ return result; | ||
*/ | ||
triggerSync<T extends HookName>( | ||
callSync<T extends HookName>( | ||
hook: T, | ||
@@ -313,6 +315,6 @@ args?: HookArguments<T>, | ||
): any { | ||
const { before, after, handler, replaced } = this.getHandlers(hook, defaultHandler); | ||
this.executeSync(before, args); | ||
const [result] = this.executeSync(handler, args, replaced ? defaultHandler : undefined); | ||
this.executeSync(after, args); | ||
const { before, handler, after } = this.getHandlers(hook, defaultHandler); | ||
this.runSync(before, args); | ||
const [result] = this.runSync(handler, args); | ||
this.runSync(after, args); | ||
this.dispatchDomEvent(hook, args); | ||
@@ -327,9 +329,8 @@ return result; | ||
*/ | ||
async execute<T extends HookName>( | ||
private async run<T extends HookName>( | ||
registrations: HookRegistration<T>[], | ||
args?: HookArguments<T>, | ||
defaultHandler?: Handler<T> | ||
args?: HookArguments<T> | ||
): Promise<any> { | ||
const results = []; | ||
for (const { hook, handler, once } of registrations) { | ||
for (const { hook, handler, defaultHandler, once } of registrations) { | ||
const result = await runAsPromise(handler, [this.swup.context, args, defaultHandler]); | ||
@@ -349,9 +350,8 @@ results.push(result); | ||
*/ | ||
executeSync<T extends HookName>( | ||
private runSync<T extends HookName>( | ||
registrations: HookRegistration<T>[], | ||
args?: HookArguments<T>, | ||
defaultHandler?: Handler<T> | ||
args?: HookArguments<T> | ||
): any[] { | ||
const results = []; | ||
for (const { hook, handler, once } of registrations) { | ||
for (const { hook, handler, defaultHandler, once } of registrations) { | ||
const result = handler(this.swup.context, args as HookArguments<T>, defaultHandler); | ||
@@ -379,3 +379,3 @@ results.push(result); | ||
*/ | ||
getHandlers<T extends HookName>(hook: T, defaultHandler?: Handler<T>) { | ||
private getHandlers<T extends HookName>(hook: T, defaultHandler?: Handler<T>) { | ||
const ledger = this.get(hook); | ||
@@ -389,2 +389,3 @@ if (!ledger) { | ||
// Filter into before, after, and replace handlers | ||
const before = registrations.filter(({ before, replace }) => before && !replace).sort(sort); | ||
@@ -395,7 +396,24 @@ const replace = registrations.filter(({ replace }) => replace).sort(sort); | ||
// Define main handler registration | ||
// This is an array to allow passing it into hooks.run() directly | ||
let handler: HookRegistration<T>[] = []; | ||
if (replaced) { | ||
handler = [{ id: 0, hook, handler: replace[0].handler }]; | ||
} else if (defaultHandler) { | ||
if (defaultHandler) { | ||
handler = [{ id: 0, hook, handler: defaultHandler }]; | ||
if (replaced) { | ||
const index = replace.length - 1; | ||
const replacingHandler = replace[index].handler; | ||
const createDefaultHandler = (index: number): Handler<T> | undefined => { | ||
const next = replace[index - 1]; | ||
if (next) { | ||
return (context, args) => | ||
next.handler(context, args, createDefaultHandler(index - 1)); | ||
} else { | ||
return defaultHandler; | ||
} | ||
}; | ||
const nestedDefaultHandler = createDefaultHandler(index); | ||
handler = [ | ||
{ id: 0, hook, handler: replacingHandler, defaultHandler: nestedDefaultHandler } | ||
]; | ||
} | ||
} | ||
@@ -412,3 +430,6 @@ | ||
*/ | ||
sortRegistrations<T extends HookName>(a: HookRegistration<T>, b: HookRegistration<T>): number { | ||
private sortRegistrations<T extends HookName>( | ||
a: HookRegistration<T>, | ||
b: HookRegistration<T> | ||
): number { | ||
const priority = (a.priority ?? 0) - (b.priority ?? 0); | ||
@@ -420,6 +441,6 @@ const id = a.id - b.id; | ||
/** | ||
* Trigger a custom event on the `document`. Prefixed with `swup:` | ||
* @param hook Name of the hook to trigger. | ||
* Dispatch a custom event on the `document` for a hook. Prefixed with `swup:` | ||
* @param hook Name of the hook. | ||
*/ | ||
dispatchDomEvent<T extends HookName>(hook: T, args?: HookArguments<T>): void { | ||
private dispatchDomEvent<T extends HookName>(hook: T, args?: HookArguments<T>): void { | ||
const detail = { hook, args, context: this.swup.context }; | ||
@@ -426,0 +447,0 @@ document.dispatchEvent(new CustomEvent(`swup:${hook}`, { detail })); |
@@ -35,3 +35,3 @@ import { updateHistoryRecord, getCurrentUrl, classify } from '../helpers.js'; | ||
// replace content: allow handlers and plugins to overwrite paga data and containers | ||
await this.hooks.trigger('content:replace', { page }, (context, { page }) => { | ||
await this.hooks.call('content:replace', { page }, (context, { page }) => { | ||
const success = this.replaceContent(page, { containers: context.containers }); | ||
@@ -50,3 +50,3 @@ if (!success) { | ||
await this.hooks.trigger( | ||
await this.hooks.call( | ||
'content:scroll', | ||
@@ -68,3 +68,3 @@ { options: { behavior: 'auto' } }, | ||
await this.hooks.trigger('page:view', { url: this.currentPageUrl, title: document.title }); | ||
await this.hooks.call('page:view', { url: this.currentPageUrl, title: document.title }); | ||
@@ -71,0 +71,0 @@ // empty cache if it's disabled (in case preload plugin filled it) |
@@ -87,6 +87,6 @@ import Swup from '../Swup.js'; | ||
try { | ||
await this.hooks.trigger('visit:start'); | ||
await this.hooks.call('visit:start'); | ||
// Begin fetching page | ||
const pagePromise = this.hooks.trigger( | ||
const pagePromise = this.hooks.call( | ||
'page:request', | ||
@@ -127,3 +127,3 @@ { url: this.context.to.url, options }, | ||
// Finalize visit | ||
await this.hooks.trigger('visit:end', undefined, () => this.classes.clear()); | ||
await this.hooks.call('visit:end', undefined, () => this.classes.clear()); | ||
@@ -130,0 +130,0 @@ // Reset context after visit? |
@@ -155,3 +155,3 @@ import { DelegateEvent } from 'delegate-it'; | ||
// Trigger enable hook | ||
await this.hooks.trigger('enable', undefined, () => { | ||
await this.hooks.call('enable', undefined, () => { | ||
// Add swup-enabled class to html tag | ||
@@ -162,3 +162,3 @@ document.documentElement.classList.add('swup-enabled'); | ||
// Trigger page view hook | ||
await this.hooks.trigger('page:view', { url: this.currentPageUrl, title: document.title }); | ||
await this.hooks.call('page:view', { url: this.currentPageUrl, title: document.title }); | ||
} | ||
@@ -180,3 +180,3 @@ | ||
// trigger disable hook | ||
await this.hooks.trigger('disable', undefined, () => { | ||
await this.hooks.call('disable', undefined, () => { | ||
// remove swup-enabled class from html tag | ||
@@ -225,3 +225,3 @@ document.documentElement.classList.remove('swup-enabled'); | ||
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) { | ||
this.hooks.trigger('link:newtab', { href }); | ||
this.hooks.call('link:newtab', { href }); | ||
return; | ||
@@ -235,3 +235,3 @@ } | ||
this.hooks.triggerSync('link:click', { el, event }, () => { | ||
this.hooks.callSync('link:click', { el, event }, () => { | ||
const from = this.context.from.url ?? ''; | ||
@@ -245,3 +245,3 @@ | ||
updateHistoryRecord(url + hash); | ||
this.hooks.triggerSync( | ||
this.hooks.callSync( | ||
'link:anchor', | ||
@@ -257,3 +257,3 @@ { hash, options: { behavior: 'auto' } }, | ||
} else { | ||
this.hooks.triggerSync('link:self', undefined, (context) => { | ||
this.hooks.callSync('link:self', undefined, (context) => { | ||
if (!context.scroll.reset) return; | ||
@@ -328,3 +328,3 @@ window.scroll({ top: 0, left: 0, behavior: 'auto' }); | ||
this.hooks.triggerSync('history:popstate', { event }, () => { | ||
this.hooks.callSync('history:popstate', { event }, () => { | ||
this.performVisit(url); | ||
@@ -331,0 +331,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
550274
3332