UNPKG

@aplus-frontend/ui

Version:

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