@insightphp/inertia-view
Version:
View abstraction layer for building architectured Inertia applications.
144 lines (143 loc) • 4.93 kB
JavaScript
var d = Object.defineProperty;
var y = (n, e, t) => e in n ? d(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
var c = (n, e, t) => (y(n, typeof e != "symbol" ? e + "" : e, t), t);
import { defineComponent as w, mergeProps as u, h as v } from "vue";
import { usePage as C } from "@inertiajs/inertia-vue3";
function f(n) {
if (n.length <= 0)
throw new Error("At least one file is required for resolving base path.");
if (n.length == 1) {
const a = n[0].split("/");
return `${a.slice(0, a.length - 1).join("/")}/`;
}
const e = [...n];
e.sort((a, i) => i.length - a.length);
const t = e[0], o = e[e.length - 1];
let s = -1, r;
for (r = 0; r < Math.min(t.length, o.length) && t[r] == o[r]; r++)
t[r] === "/" && (s = r + 1);
return t.substring(0, s >= 0 ? s : r);
}
class P {
constructor() {
c(this, "resolvedComponents", {});
}
addComponents(e, t = "app") {
const o = Object.keys(e);
if (o.length <= 0)
return console.warn(`No components have been found when registering namespace [${t}].`), this;
const s = f(o);
return Object.keys(e).forEach((r) => {
const a = r.replace(s, ""), i = this.resolveComponentName(a.replace(/\//g, "-"), t);
this.resolvedComponents[i] = e[r];
}), 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 o = e.split("/"), r = o[o.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}-${r}` : r;
}
}
const l = new P();
function b(n, e = "app") {
Array.isArray(n) ? n.forEach((t) => l.addComponents(t, e)) : l.addComponents(n, e);
}
function E(n) {
return l.getComponentWithName(n);
}
const p = w({
props: {
component: { type: Object, required: !1 },
for: { type: String, required: !1 }
},
setup(n, { slots: e }) {
return () => {
let t = {}, o;
if (n.component) {
const { component: r, for: a, ...i } = n, { _component: g, ...m } = r;
o = g, t = u(i, m);
} else if (n.for) {
const r = C(), { _component: a, ...i } = r.props.value[n.for];
o = a, t = u(n, i);
} else
throw new Error("The Portal must have either [for] or [component] props set.");
if (typeof o > "u")
throw new Error("The component is missing _component meta property.");
const s = E(o.name);
if (s === void 0)
throw new Error(`The component with name [${o.name}] could not be resolved.`);
if (typeof s == "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 v(s.default, t, e);
};
}
});
class $ {
constructor() {
c(this, "registeredPages", {});
}
getRegisteredPages() {
return this.registeredPages;
}
addPages(e, t = "app") {
const o = Object.keys(e);
if (o.length <= 0)
return console.warn(`No pages have been found when registering namespace [${t}].`), this;
const s = f(o);
return Object.keys(e).forEach((r) => {
const a = r.replace(s, "").replace(".vue", "");
this.registeredPages[`${t}:${a}`] = e[r];
}), 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 o = this.registeredPages[t];
return typeof o == "function" ? o().then((s) => this.configureLayoutOnPage(s)) : this.configureLayoutOnPage(o);
}
configureLayoutOnPage(e) {
return e.default.layout || (e.default.layout = (t, o) => {
if (Array.isArray(o.props._layouts)) {
const s = [...o.props._layouts].reverse(), r = [];
return s.forEach((a, i) => {
i == 0 ? r.push(() => t(p, { component: a }, () => o)) : r.push(() => t(p, { component: a }, r[i - 1]));
}), r[r.length - 1]();
}
return t(o);
}), e;
}
}
const h = new $();
function O(n, e = "app") {
Array.isArray(n) ? n.forEach((t) => h.addPages(t, e)) : h.addPages(n, e);
}
function T(n) {
return h.resolve(n);
}
const L = {
install(n, e) {
e != null && e.components && Object.keys(e.components).forEach((t) => {
b(e.components[t], t);
}), e != null && e.pages && Object.keys(e.pages).forEach((t) => {
O(e.pages[t], t);
});
}
};
export {
l as ComponentManager,
L as InertiaView,
h as PageManager,
p as Portal,
b as registerComponents,
O as registerPages,
E as resolveNamedComponent,
T as resolvePage
};