@aplus-frontend/ui
Version:
151 lines (150 loc) • 3.63 kB
JavaScript
import { useToken as F } from "@aplus-frontend/antdv/es/theme/internal";
import { isBoolean as J, isNumber as T, isObject as x, mergeWith as y, omit as L, isString as j } from "lodash-unified";
import { ref as S, unref as c, watch as h } from "vue";
import "../../config-provider/index.mjs";
import { randomStr as k } from "../../utils/index.mjs";
import { getColumnStateString as G } from "../components/setting/utils.mjs";
import { useInjectApTable as M } from "../context.mjs";
import { getTableTitle as R } from "../utils.mjs";
import { useNamespace as W } from "../../config-provider/hooks/use-namespace.mjs";
function X(t, e, i, o) {
return o ? o.fixed : e ? e.fixed : i && "fixed" in i ? i.fixed : J(t.fixed) ? t.fixed ? "left" : void 0 : t.fixed;
}
const b = Number.MAX_SAFE_INTEGER, q = k();
function H(t, e) {
const i = T(t.order) ? t.order : b;
let o = T(e.order) ? e.order : b;
return t.fixed === "right" && e.fixed !== "right" && (o = i - 1), e.fixed === "left" && t.fixed !== "left" && (o = i - 1), i - o;
}
function P(t, e) {
e && t.removeItem(e);
}
function Q(t, e) {
if (!e)
return [];
const i = t.getItem(e);
if (!i)
return [];
const o = JSON.parse(i);
return Array.isArray(o) ? o : (P(t, e), []);
}
function I(t, e) {
if (x(t) && x(e))
return y({}, t, e, I);
}
const ie = (t, e) => {
const { columnsBackup: i, size: o } = M(), { be: A } = W("ap-table"), [U, N] = F(), m = S(), u = S(
z(
Q(localStorage, c(e))
)
);
h(
[i, o],
([r, n]) => {
r && (m.value = {
label: "basic_template",
columnState: l(r),
key: q,
size: n
});
},
{ immediate: !0, deep: !0 }
);
function z(r) {
return r.length ? r.map((n) => ({
...n,
columnState: l(
i.value,
n.columnState,
!1
)
})) : [
{
label: "默认模版",
key: k(),
size: o?.value,
columnState: l(i?.value || [])
}
];
}
function l(r, n = {}, a = !0) {
const d = y(
{},
a ? t.defaultValue || {} : {},
n,
I
);
function g(E, w, C) {
return E.map((f) => {
const v = w?.[f.key] || {}, s = {
key: f.key,
label: j(f.title) ? R(
f,
A("table-header", "title"),
void 0,
N.value.colorTextTertiary
) : f.title,
show: !0,
fixed: X(
f,
void 0,
a ? t.defaultValue?.[f.key] : void 0,
C
),
disabled: !1,
...L(v, ["children", "fixed"])
};
return s.children = f.children?.length ? g(
f.children,
v.children,
s
) : void 0, s;
}).sort(H);
}
return g(r || [], d);
}
function p(r) {
if (c(e)) {
const n = r.map((a) => ({
...a,
columnState: G(a.columnState)
}));
localStorage.setItem(c(e), JSON.stringify(n));
}
}
function O(r) {
u.value.push(r);
}
function _(r) {
const n = u.value.findIndex(
(a) => a.key === r
);
u.value.splice(n, 1);
}
function B(r, n) {
const a = u.value.findIndex(
(d) => d.key === r
);
return u.value[a] = {
...u.value[a],
...n
}, u.value[a];
}
return h(
() => u.value,
(r) => {
p(r);
},
{ deep: !0, immediate: !0 }
), {
basicTemplate: m,
customizeTemplateList: u,
syncTemplateListToStorage: p,
addTemplate: O,
deleteTemplate: _,
updateTemplate: B
};
};
export {
ie as useTableTemplateStorage
};