@aplus-frontend/ui
Version:
278 lines (277 loc) • 7.04 kB
JavaScript
import { ref as pe, reactive as k, unref as g, nextTick as he, onMounted as me, watch as Se, computed as L, createVNode as C } from "vue";
import { cloneDeep as ze, isArray as B, isEqualWith as Pe, isUndefined as Te } from "lodash-unified";
import "../../config-provider/index.mjs";
import "../../utils/index.mjs";
import { parseFieldValue as be, dataIndexToStr as Ce, isEqualCustom as xe } from "../utils.mjs";
import { useTableSortedAndFiltered as Fe } from "./use-sorted-filtered.mjs";
import { useLocale as we } from "../../config-provider/hooks/use-locale.mjs";
import { useNamespace as De } from "../../config-provider/hooks/use-namespace.mjs";
import { isDef as J } from "../../utils/is.mjs";
const d = 1, O = 20, K = {
defaultCurrent: d,
defaultPageSize: O,
showLessItems: !1,
showTotal: !0
};
function Ee(i) {
return i === !1 ? {} : i == null || i === !0 ? K : {
...K,
...i
};
}
function ve(i) {
const f = {};
return i === !1 || (i === !0 || i == null ? (f.current = d, f.pageSize = O) : (f.current = i.defaultCurrent || d, f.pageSize = i.defaultPageSize || O)), f;
}
const je = ({
manual: i = !1,
request: f,
params: y = {},
defaultData: R = [],
formatParams: I,
paramsValueTypeMap: M,
resetFieldsIgnores: N,
filterFields: Q,
sortFields: W,
namespace: $ = "ap-table",
pagination: p,
onClickReset: G,
onAfterRequest: Z
}) => {
let x = 0, s = {};
const H = Ee(p), X = (e) => I ? I(e) : e, h = pe(), c = k(ve(p)), {
getColumnSFConfig: Y,
setSF: ee,
clearAll: te,
sortedInfo: V,
filteredInfo: j
} = Fe(), {
t: m
} = we(), {
b: S
} = De($), a = k({
total: R.length,
records: R,
loading: !1
}), z = () => h.value?.apForm?.getFieldsValueTransformed(!0) || {}, F = async (e, t) => {
const n = X(e), r = Date.now(), o = t?.cache;
x = r, a.loading = !0;
try {
const u = await f({
...g(y),
...n
});
if (x === r) {
a.total = u.total || u.data?.length || 0, o ? a.records.push(...u.data || []) : a.records = [...u.data || []];
const b = a.records.length < a.total;
t?.onSuccess?.(e, b), a.loading = !1;
}
} catch {
x === r && !o && (a.records = [], a.total = 0), a.loading = !1;
} finally {
Z?.(a.records);
}
}, w = (e) => {
const t = {};
return Object.entries(e).forEach(([n, r]) => {
if (t[n] = r, M && r) {
const o = M[n];
o && (t[n] = be(o, r));
}
}), t;
}, ne = async (e) => {
const t = z(), n = {};
Object.entries(t).forEach(([r, o]) => {
N && N.indexOf(r) > -1 && (n[r] = o);
}), await h.value?.apForm?.resetFields(), h.value?.apForm?.setFieldsValue?.(n), he(() => {
e?.();
});
}, P = (e) => {
s = e;
const t = w(e);
F(t);
}, re = (e) => {
P({
...s,
...e
});
};
let T, D = !0;
function U() {
T = void 0, D = !0;
}
const ae = () => {
if (!D)
return;
const e = T ? T + 1 : 2, t = {
...s,
current: e
};
s = t;
const n = w(t);
F(n, {
cache: !0,
onSuccess(r, o) {
T = r.current, D = o;
}
});
}, oe = () => ze(s), E = () => {
q(!0), U();
}, q = (e = !1) => {
const t = z(), n = p !== !1;
n && e && (c.current = d), P({
...t,
filter: s.filter,
sort: s.sort,
current: n ? e ? d : s.current || c.current : void 0,
pageSize: n ? s.pageSize || c.pageSize : void 0
});
}, se = () => {
te(), s.sort = {}, s.filter = {}, G?.(), ne(E), U();
}, v = () => {
P(s);
}, ce = (e) => {
if (p === !1)
v();
else {
const {
current: t,
pageSize: n
} = c, r = a.total - e, o = Math.ceil(r / n), u = t > o ? o : t;
re({
current: u > 0 ? u : 1
});
}
}, A = (e, t, n) => {
const r = z();
c.current = e.current, c.pageSize = e.pageSize, ee(t, n);
const o = B(n) ? [...n] : [n], u = {};
for (const l of o)
g(W)?.includes(l.columnKey || l.field) && l.order && (u[Ce(l.columnKey || l.field)] = l.order);
const b = {};
for (const l of Object.keys(t))
g(Q)?.includes(l) && t[l] && (b[l] = t[l]);
const _ = {
...r,
filter: b,
sort: u,
current: e.current,
pageSize: e.pageSize
};
Pe(s, _, xe) || P(_);
}, ie = (e, t = j.value || {}, n = V.value || {}) => {
const r = e?.current ?? c.current, o = e?.pageSize ?? c.pageSize;
A(
{
current: r,
pageSize: o
},
t,
n,
// 此参数随意传递,并无使用
{
action: "sort",
currentDataSource: []
}
);
};
me(() => {
const e = z(), {
current: t,
pageSize: n
} = c;
if (s = {
current: t,
pageSize: n,
filter: {},
sort: {},
...e
}, !i) {
const r = w(s);
F(r);
}
}), Se(() => g(y), () => {
E();
}, {
deep: !0
});
const ue = L(() => {
if (a.total === 0)
return "-";
const {
current: e,
pageSize: t
} = c;
return `${e}/${Math.ceil(a.total / t)}`;
});
function le(e) {
B(e) && (a.records = e, a.total = e.length);
}
const fe = L(() => {
const {
showLessItems: e,
showTotal: t,
showQuickJumper: n,
showSizeChanger: r,
pageSizeOptions: o
} = H;
return {
loading: a.loading,
pagination: {
current: c.current,
pageSize: c.pageSize,
total: a.total,
showSizeChanger: J(r) ? r : !e,
showQuickJumper: J(n) ? n : !e,
showLessItems: e,
size: "default",
pageSizeOptions: Te(o) ? ["10", "20", "50", "100"] : o,
showTotal: t ? (u) => C("span", {
class: S("pagination-total-wrapper")
}, [m("ap.apTable.pagination.totalLeftPrefix"), " ", C("span", {
class: S("pagination-count-text")
}, [u]), " ", m("ap.apTable.pagination.totalLeftSuffix"), C("span", {
class: S("pagination--total-right")
}, [m("ap.apTable.pagination.totalRightPrefix"), " ", C("span", {
class: S("pagination-count-text")
}, [g(ue)]), " ", m("ap.apTable.pagination.totalRightSuffix")])]) : void 0
},
onChange: A,
dataSource: a.records
};
});
function ge(e, t = !0) {
Object.assign(c, e), s.current = e.current, s.pageSize = e.pageSize, t && v();
}
function de() {
return {
...c
};
}
return {
formRef: h,
submit: E,
reset: se,
refresh: v,
refreshByDelete: ce,
data: a,
handleTableChange: A,
handleTableChangeOptional: ie,
tableProps: fe,
dataSource: L(() => a.records),
setDataSource: le,
getColumnSFConfig: Y,
sortedInfo: V,
filteredInfo: j,
getPaging: de,
setPaging: ge,
submitWith: q,
fetchNextPageAndCache: ae,
getLastParams: oe
};
};
export {
d as DEFAULT_CURRENT,
O as DEFAULT_PAGE_SIZE,
je as useTablePaging
};