UNPKG

vjsrouter

Version:

A modern, file-system based router for vanilla JavaScript with SSR support.

3 lines (2 loc) 5.22 kB
import{_ as t,a as e,l as r,b as a,c as n,d as i,e as o,f as s}from"./Logger-79661028.js";import{DOMUtils as l}from"./DOMUtils.js";import{HistoryManager as c}from"./HistoryManager.js";var u=new WeakMap,h=new WeakMap,f=new WeakMap,d=new WeakMap,p=new WeakMap,v=new WeakMap,m=new WeakMap,g=new WeakMap,y=new WeakMap,w=new WeakSet;class L{constructor(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,w),e(this,u,void 0),e(this,h,void 0),e(this,f,void 0),e(this,d,[]),e(this,p,null),e(this,v,"active"),e(this,m,!1),e(this,g,!1),e(this,y,!1);var o="VJSRouter.constructor";r.info(o,"Initializing vjsrouter...");var s=i.appSelector||"#app";if(a(u,this,document.querySelector(s)),a(f,this,i.routesManifestPath||"/routes.json"),a(v,this,i.activeLinkClass||"active"),a(m,this,!0===i.transitions),!n(u,this)){var l='Initialization failed: The app container with selector "'.concat(s,'" was not found in the DOM.');throw r.error(o,l),new Error(l)}a(h,this,new c)}start(){var t=this;return i(function*(){var e="VJSRouter.start";r.info(e,"Starting router...");try{yield o(w,t,_).call(t),n(h,t).onURLChange(e=>t._navigateTo(e)),n(h,t).start(),o(w,t,k).call(t);var i=n(h,t).getCurrentPath();r.info(e,'Hydrating on initial path: "'.concat(i,'"')),o(w,t,I).call(t,i),a(y,t,!0)}catch(a){r.error(e,"A critical error occurred during router startup.",a),o(w,t,b).call(t,a)}})()}navigate(t){n(g,this)||"string"==typeof t&&t.startsWith("/")&&(n(h,this).push(t),this._navigateTo(t))}_navigateTo(t){var e=this;return i(function*(){if(n(y,e))a(y,e,!1);else if(!n(g,e)){document.body.classList.add("router-loading");var r,i=o(w,e,E).call(e,t);try{if(i){var{route:s,params:c}=i;r=yield o(w,e,T).call(e,s,c)}else r=yield o(w,e,x).call(e)}catch(t){r=o(w,e,b).call(e,t)}var h=n(u,e).firstElementChild;n(m,e)&&h?(a(g,e,!0),r.classList.add("page-enter"),l.appendChild(n(u,e),r),setTimeout(()=>{h.classList.add("page-exit","page-exit-active"),r.classList.add("page-enter-active"),h.addEventListener("transitionend",()=>{h.remove(),a(g,e,!1)},{once:!0}),r.addEventListener("transitionend",()=>{r.classList.remove("page-enter","page-enter-active")},{once:!0})},10)):(l.clearElement(n(u,e)),l.appendChild(n(u,e),r)),o(w,e,I).call(e,t),document.body.classList.remove("router-loading")}})()}}function _(){return A.apply(this,arguments)}function A(){return(A=i(function*(){var t=yield fetch(n(f,this));if(!t.ok)throw new Error("Failed to fetch routes manifest: ".concat(t.statusText));var e=yield t.json();if(!e||!Array.isArray(e.routes))throw new Error("Invalid routes manifest format.");var r=e.routes.filter(t=>"/404"!==t.path||(a(p,this,t),!1));a(d,this,r.map(t=>{var e=[],r=t.path.replace(/:([^/]+)/g,(t,r)=>(e.push(r),"([^/]+)"));return s(s({},t),{},{paramNames:e,regex:new RegExp("^".concat(r,"$"))})}))})).apply(this,arguments)}function E(t){for(var e of n(d,this)){var r=t.match(e.regex);if(r){for(var a={},i=0;i<e.paramNames.length;i++)a[e.paramNames[i]]=r[i+1];return{route:e,params:a}}}return null}function T(t,e){return M.apply(this,arguments)}function M(){return(M=i(function*(t,e){var r=yield import(t.file),a=r.default;if("function"!=typeof a)throw new Error("Default export for ".concat(t.file," is not a function."));var n={},i=null;if(window.__INITIAL_DATA__)n=window.__INITIAL_DATA__.data,i=window.__INITIAL_DATA__.error,delete window.__INITIAL_DATA__;else if("function"==typeof r.load)try{n=yield r.load(e)}catch(t){i=t}var o=a({data:n,params:e,error:i});if(!(o instanceof HTMLElement))throw new Error("Component for ".concat(t.path," did not return an HTMLElement."));if(t.layouts&&t.layouts.length>0)for(var s=t.layouts.length-1;s>=0;s--){var l=t.layouts[s],c=(yield import(l)).default;if("function"!=typeof c)throw new Error("Default export for layout ".concat(l," is not a function."));if(!((o=c(o))instanceof HTMLElement))throw new Error("Layout ".concat(l," did not return an HTMLElement."))}return o})).apply(this,arguments)}function k(){document.body.addEventListener("click",t=>{var e=l.findClosestAncestor(t.target,"a[href]");e&&e.getAttribute("href").startsWith("/")&&(t.preventDefault(),this.navigate(e.getAttribute("href")))})}function I(t){document.querySelectorAll("a[href]").forEach(e=>{e.getAttribute("href")===t?e.classList.add(n(v,this)):e.classList.remove(n(v,this))})}function x(){return W.apply(this,arguments)}function W(){return(W=i(function*(){if(n(p,this))try{return yield o(w,this,T).call(this,n(p,this),{})}catch(t){return r.error("VJSRouter.#renderNotFound","The custom 404 page failed to load. Using fallback.",t),o(w,this,b).call(this,t)}return o(w,this,C).call(this,"404 - Page Not Found","The page you are looking for does not exist.")})).apply(this,arguments)}function b(t){var e=t instanceof Error?t.message:"A problem occurred while loading this page.",r=o(w,this,C).call(this,"Application Error",e);return l.clearElement(n(u,this)),l.appendChild(n(u,this),r),r}function C(t,e){var r=document.createElement("div");return r.style.cssText="padding: 40px; text-align: center; font-family: sans-serif; color: #333;",r.innerHTML='<h1 style="color: #c0392b; margin-bottom: 10px;">'.concat(t,"</h1><p>").concat(e,"</p>"),r}export{L as VJSRouter}; //# sourceMappingURL=VJSRouter.js.map