WordPress/wp-includes/js/dist/script-modules/interactivity-router/index.min.js

1 line
3.4 KiB
JavaScript
Raw Normal View History

import*as e from"@wordpress/interactivity";var t={d:(e,o)=>{for(var i in o)t.o(o,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:o[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},o={};t.d(o,{o:()=>P,w:()=>x});const i=(e=>{var o={};return t.d(o,e),o})({getConfig:()=>e.getConfig,privateApis:()=>e.privateApis,store:()=>e.store});var n;const{directivePrefix:a,getRegionRootFragment:r,initialVdom:s,toVdom:d,render:l,parseServerData:c,populateServerData:g,batch:w}=(0,i.privateApis)("I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress."),u=null!==(n=(0,i.getConfig)("core/router").navigationMode)&&void 0!==n?n:"regionBased",h=new Map,m=(new Map,e=>{const t=new URL(e,window.location.href);return t.pathname+t.search}),p=async(e,{vdom:t}={})=>{const o={body:void 0};if("regionBased"===u){const i=`data-${a}-router-region`;e.querySelectorAll(`[${i}]`).forEach((e=>{const n=e.getAttribute(i);o[n]=t?.has(e)?t.get(e):d(e)}))}const i=e.querySelector("title")?.innerText,n=c(e);return{regions:o,head:undefined,title:i,initialData:n}},f=e=>{w((()=>{if("regionBased"===u){g(e.initialData);const t=`data-${a}-router-region`;document.querySelectorAll(`[${t}]`).forEach((o=>{const i=o.getAttribute(t),n=r(o);l(e.regions[i],n)}))}e.title&&(document.title=e.title)}))},v=e=>(window.location.assign(e),new Promise((()=>{})));window.addEventListener("popstate",(async()=>{const e=m(window.location.href),t=h.has(e)&&await h.get(e);t?(f(t),x.url=window.location.href):window.location.reload()})),h.set(m(window.location.href),Promise.resolve(p(document,{vdom:s})));let y="",S=!1;const b={loading:"Loading page, please wait.",loaded:"Page Loaded."},{state:x,actions:P}=(0,i.store)("core/router",{state:{url:window.location.href,navigation:{hasStarted:!1,hasFinished:!1,message:""}},actions:{*navigate(e,t={}){const{clientNavigationDisabled:o}=(0,i.getConfig)();o&&(yield v(e));const n=m(e),{navigation:a}=x,{loadingAnimation:r=!0,screenReaderAnnouncement:s=!0,timeout:d=1e4}=t;y=e,P.prefetch(n,t);const l=new Promise((e=>setTimeout(e,d))),c=setTimeout((()=>{y===e&&(r&&(a.hasStarted=!0,a.hasFinished=!1),s&&A("loading"))}),400),g=yield Promise.race([h.get(n),l]);if(clearTimeout(c),y===e)if(g&&!g.initialData?.config?.["core/router"]?.clientNavigationDisabled){yield f(g),window.history[t.replace?"replaceState":"pushState"]({},"",e),x.url=e,r&&(a.hasStarted=!1,a.hasFinished=!0),s&&A("loaded");const{hash:o}=new URL(e,window.location.href);o&&document.querySelector(o)?.scrollIntoView()}else yield v(e)},prefetch(e,t={}){const{clientNavigationDisabled:o}=(0,i.getConfig)();if(o)return;const n=m(e);!t.force&&h.has(n)||h.set(n,(async(e,{html:t})=>{try{if(!t){const o=await window.fetch(e);if(200!==o.status)return!1;t=await o.text()}const o=(new window.DOMParser).parseFromString(t,"text/html");return p(o)}catch(e){return!1}})(n,{html:t.html}))}}});function A(e){if(!S){S=!0;const e=document.getElementById("wp-script-module-data-@wordpress/interactivity-router")?.textContent;if(e)try{const t=JSON.parse(e);"string"==typeof t?.i18n?.loading&&(b.loading=t.i18n.loading),"string"==typeof t?.i18n?.loaded&&(b.loaded=t.i18n.loaded)}catch{}else x.navigation.texts?.loading&&(b.loading=x.navigation.texts.loading),x.navigation.texts?.loaded&&(b.loaded=x.navigation.texts.loaded)}const t=b[e];x.navigation.message=t+(x.navigation.message===t?" ":"")}var D=o.o,C=o.w;export{D as actions,C as state};