@insightphp/inertia-view
Version:
View abstraction layer for building architectured Inertia applications.
2 lines (1 loc) • 4.24 kB
JavaScript
(function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("vue"),require("@inertiajs/inertia-vue3")):typeof define=="function"&&define.amd?define(["exports","vue","@inertiajs/inertia-vue3"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s["inertia-view"]={},s.Vue,s.InertiaVue))})(this,function(s,a,u){"use strict";var $=Object.defineProperty;var O=(s,a,u)=>a in s?$(s,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):s[a]=u;var p=(s,a,u)=>(O(s,typeof a!="symbol"?a+"":a,u),u);function d(o){if(o.length<=0)throw new Error("At least one file is required for resolving base path.");if(o.length==1){const c=o[0].split("/");return`${c.slice(0,c.length-1).join("/")}/`}const e=[...o];e.sort((c,l)=>l.length-c.length);const t=e[0],r=e[e.length-1];let i=-1,n;for(n=0;n<Math.min(t.length,r.length)&&t[n]==r[n];n++)t[n]==="/"&&(i=n+1);return t.substring(0,i>=0?i:n)}class P{constructor(){p(this,"resolvedComponents",{})}addComponents(e,t="app"){const r=Object.keys(e);if(r.length<=0)return console.warn(`No components have been found when registering namespace [${t}].`),this;const i=d(r);return Object.keys(e).forEach(n=>{const c=n.replace(i,""),l=this.resolveComponentName(c.replace(/\//g,"-"),t);this.resolvedComponents[l]=e[n]}),this}getResolvedComponents(){return this.resolvedComponents}getComponentWithName(e){if(!Object.keys(this.resolvedComponents).includes(e))throw new Error(`The Component with name [${e}] is not registered.`);return this.resolvedComponents[e]}resolveComponentName(e,t=null){const r=e.split("/"),n=r[r.length-1].replace(".vue","").replace(/\B([A-Z])(?=[a-z])/g,"-$1").replace(/\B([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase();return t?`${t}-${n}`:n}}const f=new P;function m(o,e="app"){Array.isArray(o)?o.forEach(t=>f.addComponents(t,e)):f.addComponents(o,e)}function y(o){return f.getComponentWithName(o)}const g=a.defineComponent({props:{component:{type:Object,required:!1},for:{type:String,required:!1}},setup(o,{slots:e}){return()=>{let t={},r;if(o.component){const{component:n,for:c,...l}=o,{_component:j,...E}=n;r=j,t=a.mergeProps(l,E)}else if(o.for){const n=u.usePage(),{_component:c,...l}=n.props.value[o.for];r=c,t=a.mergeProps(o,l)}else throw new Error("The Portal must have either [for] or [component] props set.");if(typeof r>"u")throw new Error("The component is missing _component meta property.");const i=y(r.name);if(i===void 0)throw new Error(`The component with name [${r.name}] could not be resolved.`);if(typeof i=="function")throw new Error("The component is not loaded synchronously. Components renered within Portal must not be async components. Use import.meta.glob('path', { eager: true }) to import components synchronously.");return a.h(i.default,t,e)}}});class w{constructor(){p(this,"registeredPages",{})}getRegisteredPages(){return this.registeredPages}addPages(e,t="app"){const r=Object.keys(e);if(r.length<=0)return console.warn(`No pages have been found when registering namespace [${t}].`),this;const i=d(r);return Object.keys(e).forEach(n=>{const c=n.replace(i,"").replace(".vue","");this.registeredPages[`${t}:${c}`]=e[n]}),this}resolve(e){let t=e;if(t.includes(":")||(t=`app:${t}`),!Object.keys(this.registeredPages).includes(t))throw new Error(`The page [${e}] could not be found.`);const r=this.registeredPages[t];return typeof r=="function"?r().then(i=>this.configureLayoutOnPage(i)):this.configureLayoutOnPage(r)}configureLayoutOnPage(e){return e.default.layout||(e.default.layout=(t,r)=>{if(Array.isArray(r.props._layouts)){const i=[...r.props._layouts].reverse(),n=[];return i.forEach((c,l)=>{l==0?n.push(()=>t(g,{component:c},()=>r)):n.push(()=>t(g,{component:c},n[l-1]))}),n[n.length-1]()}return t(r)}),e}}const h=new w;function v(o,e="app"){Array.isArray(o)?o.forEach(t=>h.addPages(t,e)):h.addPages(o,e)}function C(o){return h.resolve(o)}const b={install(o,e){e!=null&&e.components&&Object.keys(e.components).forEach(t=>{m(e.components[t],t)}),e!=null&&e.pages&&Object.keys(e.pages).forEach(t=>{v(e.pages[t],t)})}};s.ComponentManager=f,s.InertiaView=b,s.PageManager=h,s.Portal=g,s.registerComponents=m,s.registerPages=v,s.resolveNamedComponent=y,s.resolvePage=C,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});