@userfrosting/sprinkle-core
Version:
Core Sprinkle for UserFrosting
180 lines (179 loc) • 6.18 kB
JavaScript
import { ref as s, computed as O, watch as N } from "vue";
import { useRoute as L } from "vue-router";
import { defineStore as T } from "pinia";
import j from "axios";
import { DateTime as P } from "luxon";
import { getProperty as m } from "dot-prop";
const D = T("config", {
persist: !0,
state: () => ({
config: {}
}),
getters: {
get: (t) => (u, o) => m(t.config, u, o)
},
actions: {
async load() {
j.get("/api/config").then((t) => {
this.config = t.data;
});
}
}
}), E = (t) => 1, K = (t) => t === 1 ? 1 : 2, M = (t) => t === 0 || t === 1 ? 1 : 2, w = (t) => t === 0 ? 1 : t % 10 == 1 && t % 100 != 11 ? 2 : 3, F = (t) => t === 1 || t == 11 ? 1 : t === 2 || t === 12 ? 2 : t >= 3 && t <= 19 ? 3 : 4, I = (t) => t === 1 ? 1 : t === 0 || t % 100 > 0 && t % 100 < 20 ? 2 : 3, _ = (t) => t % 10 === 1 && t % 100 !== 11 ? 1 : t % 10 < 2 || t % 100 >= 10 && t % 100 < 20 ? 2 : 3, x = (t) => t % 10 === 1 && t % 100 !== 11 ? 1 : t % 10 >= 2 && t % 10 <= 4 && (t % 100 < 10 || t % 100 >= 20) ? 2 : 3, h = (t) => t === 1 ? 1 : t >= 2 && t <= 4 ? 2 : 3, W = (t) => t === 1 ? 1 : t % 10 >= 2 && t % 10 <= 4 && (t % 100 < 12 || t % 100 > 14) ? 2 : 3, $ = (t) => t % 100 === 1 ? 1 : t % 100 === 2 ? 2 : t % 100 === 3 || t % 100 === 4 ? 3 : 4, q = (t) => t === 1 ? 1 : t === 2 ? 2 : t >= 3 && t <= 6 ? 3 : t >= 7 && t <= 10 ? 4 : 5, z = (t) => t === 1 ? 1 : t === 2 ? 2 : t % 100 >= 3 && t % 100 <= 10 ? 3 : t % 100 >= 11 ? 4 : t != 0 ? 5 : 6, B = (t) => t === 1 ? 1 : t === 0 || t % 100 >= 1 && t % 100 < 11 ? 2 : t % 100 > 10 && t % 100 < 20 ? 3 : 4, C = (t) => t % 10 === 1 ? 1 : t % 10 === 2 ? 2 : 3, H = (t) => t % 10 === 1 && t % 100 != 11 ? 1 : 2, R = {
0: E,
1: K,
2: M,
3: w,
4: F,
5: I,
6: _,
7: x,
8: h,
9: W,
10: $,
11: q,
12: z,
13: B,
14: C,
15: H
}, U = T(
"translator",
() => {
const t = "plural", u = s(""), o = s({}), n = s({
name: "",
regional: "",
authors: [],
plural_rule: 0,
dates: ""
});
async function g() {
j.get("/api/dictionary").then((r) => {
u.value = r.data.identifier, n.value = r.data.config, o.value = r.data.dictionary;
});
}
function c(r, e = {}) {
const { message: i, placeholders: a } = A(
r,
e
);
return e = a, S(i, e);
}
function d(r, e = P.DATETIME_MED_WITH_WEEKDAY) {
const i = p(r);
return typeof e == "object" ? i.toLocaleString(e) : i.toFormat(e);
}
function p(r) {
return P.fromISO(r).setLocale(n.value.dates);
}
function A(r, e) {
if (o.value[r] !== void 0)
return { message: o.value[r], placeholders: e };
const i = o.value[r + ".@PLURAL"] || t;
let a = 1;
if (typeof e == "object" && e[i] !== void 0)
a = Number(e[i]);
else if (typeof e == "number" || typeof e == "string")
a = Number(e);
else if (o.value[r + ".@TRANSLATION"] !== void 0)
return { message: o.value[r + ".@TRANSLATION"], placeholders: e };
typeof e == "number" || typeof e == "string" ? e = { [i]: a } : typeof e == "object" && e[i] === void 0 && (e = { ...e, [i]: a });
const l = f(r, a);
return o.value[r + "." + l] !== void 0 ? { message: o.value[r + "." + l], placeholders: e } : o.value[r + "." + a] !== void 0 ? { message: o.value[r + "." + a], placeholders: e } : o.value[r + ".@TRANSLATION"] !== void 0 ? { message: o.value[r + ".@TRANSLATION"], placeholders: e } : { message: r, placeholders: e };
}
function S(r, e) {
typeof e != "object" && (e = { [t]: e });
for (const [i, a] of Object.entries(e))
if (typeof a == "string" && a.startsWith("&")) {
const l = Object.fromEntries(
Object.entries(e).filter(([y]) => y !== i)
);
e[i] = c(a.substring(1), l);
}
r = r.replace(/{{&(([^}]+[^a-z]))}}/g, (i, a) => c(a, e));
for (const [i, a] of Object.entries(e)) {
const l = new RegExp(`{{${i}}}`, "g");
r = r.replace(l, String(a));
}
return r;
}
function f(r, e) {
if (e === 0 && o.value[r + ".0"] !== void 0)
return 0;
const i = v(e);
if (o.value[r + "." + i] !== void 0)
return i;
for (let a = i; a >= 0; a--)
if (o.value[r + "." + a] !== void 0)
return a;
return null;
}
function v(r, e) {
const i = e ?? n.value.plural_rule;
if (i < 0 || i >= Object.keys(R).length)
throw new Error(`The rule number ${i} must be between 0 and 15`);
return R[i](r);
}
return {
dictionary: o,
load: g,
config: n,
identifier: u,
translate: c,
translateDate: d,
getPluralForm: v,
getDateTime: p
};
},
{ persist: !0 }
), Z = T("pageMeta", () => {
const t = L(), { translate: u } = U(), o = s(""), n = s(""), g = s([]), c = s(!1), d = s(!1);
function p() {
c.value = !1, d.value = !1;
const e = t.matched.filter(
(i) => i.meta.title !== void 0 && i.meta.title !== ""
).map((i) => {
var a;
return {
label: ((a = i.meta) == null ? void 0 : a.title) || "",
to: i.path
};
});
e.unshift({
label: f.value,
to: "/"
}), g.value = e, o.value = t.meta.title || "", n.value = u(t.meta.description || "");
}
function A() {
document.title = v.value;
}
function S() {
const r = document.querySelector('head meta[name="description"]');
r == null || r.setAttribute("content", n.value);
}
const f = O(() => D().get("site.title") || ""), v = O(() => o.value ? u(o.value) + " | " + f.value : f.value);
return N(t, p, { immediate: !0 }), N(v, A, { immediate: !0 }), N(n, S, { immediate: !0 }), { breadcrumbs: g, title: o, description: n, hideBreadcrumbs: c, hideTitle: d };
}), k = T("alerts", {
state: () => ({
alerts: []
}),
actions: {
push(t) {
this.alerts.push(t);
},
pop() {
return this.alerts.pop();
},
shift() {
return this.alerts.shift();
},
clear() {
this.alerts = [];
}
}
});
export {
D as a,
Z as b,
U as c,
k as u
};