UNPKG

@layui/layui-vue

Version:

a component library for Vue 3 base on layui-vue

97 lines (96 loc) 3.92 kB
import { ref as g, reactive as G, watch as R, isReactive as z, watchEffect as B } from "vue"; import { useAutoColsWidth as H } from "./useAutoColsWidth.js"; import { useTableColumns as J } from "./useTableColumns.js"; import { useTableExpand as K } from "./useTableExpand.js"; import { useTableSelected as O } from "./useTableSelected.js"; import { loopForEach as P, isValueArray as h } from "../../utils/arrayUtil.js"; import { isEqual as y } from "../../utils/type.js"; function te(i, A) { const m = /* @__PURE__ */ new WeakMap(), p = g([]), w = g([]), C = g([]), k = g(!1), j = J(i.columns, m), q = O(i, A), D = K(i, A), S = G([...i.dataSource]); R(() => i.dataSource, (e) => { S.splice(0, S.length, ...e); }, { deep: !0 }); const x = g([]); function E(e, t) { for (let n = 0; n < e.length; n++) { const r = e[n], s = h(t) ? t : [r]; m.set(r, s), h(r.children) && E(r.children, [...s, r]); } } function F(e) { h(e.children) || (w.value.push(e), !e.hide && C.value.push(e)); } function L(e, t = 0) { p.value[t] || (p.value[t] = []); for (const n of e) p.value[t].push(n), h(n.children) && L(n.children, t + 1); } function I(e) { e.totalRow && (k.value = !0); } function M(e) { const t = m.get(e), [n] = t; return n.fixed && n.fixed === "left" ? { left: `${r(t) + s(t)}px` } : n.fixed && n.fixed === "right" ? { right: `${r(t, !1) + s(t, !1)}px` } : {}; function r(d, o = !0) { if (d.length > 1) { let l = function(v) { const a = o ? v : [...v].reverse(); for (let b = 0; b < a.length; b++) { const c = a[b]; if (y(c, e)) return !0; if (h(c == null ? void 0 : c.children)) { if (l(c.children)) return !0; } else u += W(c); } return !1; }, u = 0; const [f] = d; return l((f == null ? void 0 : f.children) || []), u; } return 0; } function s(d, o = !0) { const [l] = d, u = o ? i.columns : [...i.columns].reverse(); let f = u.findIndex((a) => y(a, l)) - 1; const v = []; for (; f > -1; ) { const a = u[f]; a.fixed && a.fixed === (o ? "left" : "right") && v.push(a), f--; } return function a(b, c) { return b.reduce((T, $) => (h($.children) ? a($.children, T) : T += W($), T), c); }(v, 0); } } function N(e) { const [t] = m.get(e); let n = "", r = ""; const s = i.columns.findLast((l) => l.fixed === "left"), d = i.columns.find((l) => l.fixed === "right"); function o(l, u) { return !!y(l, e) || !!h(l.children) && o(u === "left" ? l.children.at(-1) : l.children[0], u); } return y(t, s) && o(t, "left") && (n = "layui-table-fixed-left-last"), y(t, d) && o(t, "right") && (r = "layui-table-fixed-right-first"), `${n} ${r}`; } function V(e) { const [t] = m.get(e); return (t.fixed && `layui-table-fixed-${t.fixed}`) ?? ""; } function W(e) { return e.hide || !e.width ? 0 : Number(e.width.replace("px", "")); } return R(() => i.columns, (e) => { x.value = z(e) ? e : G(e); }, { immediate: !0, deep: !0 }), B(() => { i.autoColsWidth && H(x.value, S), E(x.value, []), p.value = [], w.value = [], C.value = [], P(x.value, [F, I]), L(x.value); }), { hierarchicalColumns: p, lastLevelAllColumns: w, lastLevelShowColumns: C, tableDataSource: S, columnsState: j, selectedState: q, expandState: D, hasTotalRow: k, commonGetClasses: function(e, ...t) { return ["layui-table-cell", N(e), V(e), e.type === "checkbox" ? "layui-table-cell-checkbox" : "", e.type === "radio" ? "layui-table-cell-radio" : "", e.type === "number" ? "layui-table-cell-number" : "", ...t]; }, commonGetStylees: function(e, ...t) { return [{ textAlign: e.align, ...M(e) }, ...t]; } }; } export { te as useTable };