UNPKG

@aplus-frontend/ui

Version:

135 lines (134 loc) 3.46 kB
import { ref as D, shallowRef as G, unref as i, computed as W, watchEffect as X } from "vue"; import "../../utils/index.mjs"; import q, { customCloneColumnStates as v, getColumnStateString as H } from "../components/setting/utils.mjs"; import { useInjectApTable as L } from "../context.mjs"; import { isObject as T, mergeWith as A, isBoolean as P, omit as Q, isString as U, isNumber as b } from "lodash-unified"; import "../../config-provider/index.mjs"; import { useToken as V } from "@aplus-frontend/antdv/es/theme/internal"; import { getTableTitle as Y } from "../utils.mjs"; import { useNamespace as Z } from "../../config-provider/hooks/use-namespace.mjs"; import { isDef as $ } from "../../utils/is.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 : P(r.fixed) ? r.fixed ? "left" : void 0 : r.fixed; } const de = (r, o, n) => { const { be: a } = Z("ap-table"), [te, B] = V(), l = D(n), d = G( v(n) ), { columnsBackup: m, updateColumns: E } = L(), 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: U(f.title) ? Y( 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, ...Q(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 p = W(() => i(l).every((e) => e.show)); function S() { 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(p) ? x() : S(); } function J(e, t) { c(i(l), (s) => { s.key === e && (s.show = $(t) ? t : !s.show); }); } function C(e) { l.value = e; } function M(e) { d.value = e; } function w(e) { if (i(o)) { const t = H(e); h.setItem(i(o), JSON.stringify(t)); } } return X(() => { const e = I(); C(e), r.onChange?.(e), E?.( q( i(m), i(e) ) ), d.value = v(e), w(e); }), { columnState: l, selectAll: S, unSelectAll: x, toggleSelect: J, setColumnState: C, isAllSelected: p, toggleSelectAll: _, columnStateBackup: d, setColumnStateBackup: M, cacheColumnStateIfNeeded: w }; }; export { de as useTableColumnState };