UNPKG

@userfrosting/sprinkle-core

Version:
152 lines (151 loc) 5.38 kB
import { ref as _, computed as c, watchEffect as T, toValue as B } from "vue"; import D from "axios"; import { u as y, a as h } from "./useAxiosInterceptor-DcOpTLHG.js"; import { required as q, withMessage as i, email as G, minLength as S, maxLength as V, integer as $, oneOf as p, regex as m, not as M, numeric as O, between as z, url as L } from "@regle/rules"; import { c as H } from "./useAlertsStore-Ca6nXz8C.js"; const X = (s, a = {}, w = {}, o = 10, t = 0) => { const e = _(o), n = _(t), g = _(a), u = _(w), r = _({ count: 0, count_filtered: 0, rows: [], listable: {}, sortable: [], filterable: [] }), b = _(!1), x = _(null); async function l() { b.value = !0, D.get(B(s), { params: { size: e.value, page: n.value, sorts: g.value, filters: u.value } }).then((f) => { r.value.count = f.data.count, r.value.count_filtered = f.data.count_filtered, r.value.rows = f.data.rows, r.value.listable = f.data.listable ?? {}, r.value.sortable = f.data.sortable ?? [], r.value.filterable = f.data.filterable ?? []; }).catch((f) => { x.value = f.response.data; }).finally(() => { b.value = !1; }); } const j = c(() => Math.max(Math.ceil((r.value.count_filtered ?? 0) / e.value) - 1, 0)), C = c(() => r.value.count ?? 0), E = c(() => Math.min(n.value * e.value + 1, r.value.count ?? 0)), d = c(() => Math.min((n.value + 1) * e.value, r.value.count_filtered ?? 0)), A = c(() => r.value.count_filtered ?? 0), P = c(() => r.value.rows ?? []); function I() { console.log("Not yet implemented"); } function N(f) { let v; g.value[f] === "asc" ? v = "desc" : g.value[f] === "desc" ? v = null : v = "asc", g.value[f] = v; } return T(() => { l(); }), { dataUrl: s, size: e, page: n, sorts: g, filters: u, data: r, fetch: l, loading: b, error: x, downloadCsv: I, totalPages: j, countFiltered: A, count: C, rows: P, first: E, last: d, toggleSort: N }; }; function Y() { function s(o) { var e; const t = {}; for (const n in o) if (Object.prototype.hasOwnProperty.call(o, n)) { const g = ((e = o[n]) == null ? void 0 : e.validators) || {}, u = {}; for (const r of Object.keys(g)) w(r, g, u); t[n] = u; } return t; } function a(o) { const { translate: t } = H(); if (!o.message) return ""; const e = { ...o }; return delete e.message, t(o.message, e); } function w(o, t, e) { if (o === "required" && t.required) { const n = a(t.required); e.required = n === "" ? q : i(q, n); } if (o === "email" && t.email) { const n = a(t.email); e.email = i(G, n); } if (o === "length" && t.length) { if (t.length.min !== void 0) { const n = a(t.length); e.minLength = n === "" ? S(t.length.min) : i(S(t.length.min), n); } if (t.length.max !== void 0) { const n = a(t.length); e.maxLength = n === "" ? V(t.length.max) : i(V(t.length.max), n); } } if (o === "matches" && t.matches && console.warn('Validation rule "matches" not implemented yet'), o === "equals" && t.equals && console.warn('Validation rule "equals" not implemented yet'), o === "integer" && t.integer) { const n = a(t.integer); e.integer = n === "" ? $ : i($, n); } if (o === "member_of" && t.member_of) { const n = a(t.member_of); e.member_of = n === "" ? p(t.member_of.values) : i(p(t.member_of.values), n); } if (o === "no_leading_whitespace" && t.no_leading_whitespace) { const n = a(t.no_leading_whitespace); e.no_leading_whitespace = n === "" ? m(/^\S.*$/) : i(m(/^\S.*$/), n); } if (o === "no_trailing_whitespace" && t.no_trailing_whitespace) { const n = a(t.no_trailing_whitespace); e.no_trailing_whitespace = n === "" ? m(/^.*\S$/) : i(m(/^.*\S$/), n); } if (o === "not_equals" && t.not_equals && console.warn('Validation rule "not_equals" not implemented yet'), o === "not_matches" && t.not_matches && console.warn('Validation rule "not_matches" not implemented yet'), o === "not_member_of" && t.not_member_of) { const n = a(t.not_member_of); e.not_member_of = n === "" ? M(p(t.not_member_of.values)) : i(M(p(t.not_member_of.values)), n); } if (o === "numeric" && t.numeric) { const n = a(t.numeric); e.numeric = n === "" ? O : i(O, n); } if (o === "range" && t.range) { const n = a(t.range); e.range = n === "" ? z(t.range.min, t.range.max) : i( z(t.range.min, t.range.max), n ); } if (o === "regex" && t.regex) { const n = a(t.regex); e.regex = n === "" ? m(new RegExp(t.regex.regex)) : i(m(new RegExp(t.regex.regex)), n); } if (o === "telephone" && t.telephone && console.warn('Validation rule "telephone" not implemented yet'), o === "uri" && t.uri) { const n = a(t.uri); e.uri = n === "" ? L : i(L, n); } if (o === "username" && t.username) { const n = a(t.username); e.username = n === "" ? m(/^([a-z0-9.\-_])+$/i) : i(m(/^([a-z0-9.\-_])+$/i), n); } } return { adapt: s, translateMessage: a }; } export { y as useAxiosInterceptor, h as useCsrf, Y as useRuleSchemaAdapter, X as useSprunjer };