UNPKG

@aplus-frontend/ui

Version:

134 lines (133 loc) 3.43 kB
import { ref as D, shallowRef as G, unref as i, computed as W, watchEffect as X } from "vue"; import { isDef as q } from "../../utils/index.mjs"; import H, { customCloneColumnStates as v, getColumnStateString as L } from "../components/setting/utils.mjs"; import { useInjectApTable as P } from "../context.mjs"; import { isObject as T, mergeWith as A, isBoolean as Q, omit as U, isString as V, isNumber as b } from "lodash-unified"; import "../../config-provider/index.mjs"; import { useToken as Y } from "@aplus-frontend/antdv/es/theme/internal"; import { getTableTitle as Z } from "../utils.mjs"; import { useNamespace as $ } from "../../config-provider/hooks/use-namespace.mjs"; function c(r, o) { r.forEach((n) => { o(n), n.children?.length && c(n.children, o); }); } function K(r, o) { if (!o) return {}; const n = r.getItem(o); return n ? JSON.parse(n) : {}; } function N(r, o) { if (T(r) && T(o)) return A({}, r, o, N); } const y = Number.MAX_SAFE_INTEGER; function ee(r, o, n, a) { return a ? a.fixed : o ? o.fixed : n && "fixed" in n ? n.fixed : Q(r.fixed) ? r.fixed ? "left" : void 0 : r.fixed; } const ce = (r, o, n) => { const { be: a } = $("ap-table"), [te, B] = Y(), l = D(n), d = G( v(n) ), { columnsBackup: m, updateColumns: E } = P(), F = r.persistenceType || "localStorage", h = window[F]; function I() { const e = K( h, i(o) ), t = A( {}, r.defaultValue || {}, e, N ), s = i(m); function u(R, j, z) { return R.map((f) => { const k = j?.[f.key] || {}, g = { key: f.key, label: V(f.title) ? Z( f, a("table-header", "title"), void 0, B.value.colorTextTertiary ) : f.title, show: !0, fixed: ee( f, e[f.key], r.defaultValue?.[f.key], z ), disabled: !1, ...U(k, ["children", "fixed"]) }; return g.children = f.children?.length ? u( f.children, k.children, g ) : void 0, g; }).sort(O); } return u(s || [], t); } function O(e, t) { const s = b(e.order) ? e.order : y; let u = b(t.order) ? t.order : y; return e.fixed === "right" && t.fixed !== "right" && (u = s - 1), t.fixed === "left" && e.fixed !== "left" && (u = s - 1), s - u; } const S = W(() => i(l).every((e) => e.show)); function p() { const e = i(l); c(e, (t) => { !t.disabled && (t.show = !0); }); } function x() { const e = i(l); c(e, (t) => { !t.disabled && (t.show = !1); }); } function _() { i(S) ? x() : p(); } function J(e, t) { c(i(l), (s) => { s.key === e && (s.show = q(t) ? t : !s.show); }); } function C(e) { l.value = e; } function M(e) { d.value = e; } function w(e) { if (i(o)) { const t = L(e); h.setItem(i(o), JSON.stringify(t)); } } return X(() => { const e = I(); C(e), r.onChange?.(e), E?.( H( i(m), i(e) ) ), d.value = v(e), w(e); }), { columnState: l, selectAll: p, unSelectAll: x, toggleSelect: J, setColumnState: C, isAllSelected: S, toggleSelectAll: _, columnStateBackup: d, setColumnStateBackup: M, cacheColumnStateIfNeeded: w }; }; export { ce as useTableColumnState };