@aplus-frontend/ui
Version:
509 lines (508 loc) • 16 kB
JavaScript
import { defineComponent as oo, useSlots as to, ref as I, toRef as T, unref as t, computed as l, nextTick as ro, watch as z, createElementBlock as A, openBlock as b, normalizeStyle as V, normalizeClass as m, createCommentVNode as ae, createElementVNode as B, createVNode as M, mergeProps as O, createSlots as ao, withCtx as ne, Fragment as no, renderList as lo, createBlock as le, resolveDynamicComponent as so, renderSlot as se, withDirectives as io, vShow as uo } from "vue";
import { AgGridVue as co } from "ag-grid-vue3";
import { ModuleRegistry as mo, AllCommunityModule as po } from "ag-grid-community";
import { mergeProps as fo } from "./utils.mjs";
import { Spin as go, Pagination as wo } from "@aplus-frontend/antdv";
import { isUndefined as F, isFunction as G } from "lodash-unified";
import "../config-provider/index.mjs";
import { ApForm as So } from "../ap-form/index.mjs";
import { recursionApColumns as yo } from "../ap-table/utils.mjs";
import { useTablePaging as Co } from "../ap-table/hooks/use-table-paging-ng.mjs";
import ho from "./hooks/use-row-selection.mjs";
import { useProvideSorter as vo } from "./context.mjs";
import { useProvideApTable as Ro } from "../ap-table/context.mjs";
import { DEFAULT_ROW_SELECTION_CONFIG as ie, sizeReverseMap as bo, sizeMap as Bo } from "../ap-grid/constants.mjs";
import { useToken as Fo } from "@aplus-frontend/antdv/es/theme/internal";
import { useTheme as ko } from "./theme.mjs";
import { useColumns as xo } from "./hooks/use-columns.mjs";
import { useColumnsDef as Io } from "./hooks/use-columns-def.mjs";
import { isDef as To } from "../utils/index.mjs";
import { getValidVNodeList as Do } from "../utils/slot.mjs";
import { useSearchForm as No } from "./hooks/use-search-form.mjs";
import "./components/empty/index.vue.mjs";
import Eo from "./hooks/use-virtual-config.mjs";
import { usePinnedRow as Ko } from "./hooks/use-pinned-row.mjs";
import Po from "./hooks/use-selection-col-def.mjs";
import { useNamespace as zo } from "../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as Ao } from "../config-provider/hooks/use-global-config.mjs";
import Vo from "./components/empty/index.vue2.mjs";
const mt = /* @__PURE__ */ oo({
name: "AgGrid",
__name: "index",
props: {
dropdownPrefixCls: {},
bordered: { type: Boolean },
locale: {},
onChange: {},
onResizeColumn: {},
getPopupContainer: {},
scroll: {},
sortDirections: {},
showSorterTooltip: { type: [Boolean, Object] },
prefixCls: {},
rowKey: { type: [String, Function], default: "key" },
tableLayout: {},
title: {},
id: {},
showHeader: { type: Boolean },
components: {},
customRow: {},
customHeaderRow: {},
direction: {},
expandFixed: { type: [String, Boolean] },
expandColumnWidth: {},
expandedRowKeys: {},
defaultExpandedRowKeys: {},
expandedRowRender: {},
expandRowByClick: { type: Boolean },
expandIcon: {},
onExpand: {},
onExpandedRowsChange: {},
defaultExpandAllRows: { type: Boolean },
indentSize: {},
expandIconColumnIndex: {},
showExpandColumn: { type: Boolean },
expandedRowClassName: {},
childrenColumnName: {},
rowExpandable: {},
transformCellText: {},
columns: {},
rowSelection: { type: Boolean },
card: { type: Boolean },
params: {},
request: {},
defaultData: {},
dataSource: {},
onLoadingChange: {},
onShownColumnsChange: {},
searchForm: { type: [Boolean, Object], default: void 0 },
beforeSearchSubmit: {},
pagination: { type: [Boolean, Object], default: void 0 },
searchFormWrapperStyle: {},
tableWrapperStyle: {},
wrapperStyle: {},
tableStyle: {},
manual: { type: Boolean, default: void 0 },
size: { default: "mini" },
adaptive: { type: Boolean },
columnResizable: { type: Boolean, default: !1 },
loading: { type: Boolean, default: void 0 },
summary: {},
rowHeight: {},
advanceRenderer: { type: Boolean },
browserTooltips: { type: Boolean },
virtual: { type: [Boolean, Object], default: void 0 },
onRowClicked: {},
onScrollEnd: {},
rowClassName: {},
rowClassRules: {},
autoHeight: { type: Boolean },
onUpdate: {}
},
setup(de, { expose: ue }) {
mo.registerModules([po]);
const o = de, W = to(), { e: k, b: u, be: ce } = zo("ag-grid"), C = I(o.size), me = ko(C, T(o, "rowHeight")), pe = Eo(T(o, "virtual")), { rowPinnedGridConfig: H, getRowPinnedCellClass: fe } = Ko(
T(o, "summary")
), ge = Ao("apGrid"), s = I(), [, we] = Fo();
let p = !!o.dataSource;
const L = No(o), { shownColumns: f, columns: U, columnKeys: Se, getTargetColumnByKey: ye } = xo(o), { defaultColDef: Ce, columnDefs: he, gridWrapperRef: ve } = Io(U, o, fe), x = I({});
vo({
sorter: x,
setSorter(e) {
x.value = e;
}
});
const Re = {
asc: "ascend",
desc: "descend"
};
function be() {
const e = s.value.getColumns()?.filter((c) => c.getSort() !== void 0), r = t(x) || {};
let a = e?.[0];
const d = Object.keys(r);
if (d.length && d.every((c) => r[c] === null)) {
const c = Object.keys(r)[0];
c && (a = s.value?.getColumn(c));
}
if (!a)
return;
const i = a.getColDef(), y = a.getSort(), v = ye(i.colId);
$(void 0, void 0, {
column: i,
field: v?.field ?? i.field ?? i.colId,
order: y ? Re[y] : null
});
}
const Be = l(() => yo(t(f), (e) => {
if (e.sorter === !0)
return e.field ?? e.dataIndex ?? e.key;
}).filter(Boolean)), Fe = () => {
x.value = {};
const e = s.value;
if (!e)
return;
const r = e.getColumnState()?.map((a) => ({ ...a, sort: null }));
e.applyColumnState({ state: r });
}, ke = l(() => To(o.manual) ? o.manual : Do(W.searchFormExtra?.() || []).some((r) => r.type?.name === "ApView")), {
formRef: g,
submit: D,
reset: N,
data: w,
tableProps: j,
handleTableChangeOptional: $,
submitWith: q,
refresh: J,
getPaging: xe,
setPaging: Ie,
setDataSource: Te
} = Co({
async request(e) {
if (p = !1, S.value && !S.value.preserveSelectedRowKeys && F(o.dataSource) && P(), !F(o.dataSource)) {
const a = e.pageSize * (e.current - 1);
return {
data: (o.pagination === !1 ? o.dataSource : o.dataSource.slice(a, a + e.pageSize)) || [],
total: o.dataSource.length || 0
};
}
const r = await o.request?.(e);
return {
data: r?.data || [],
total: r?.total || 0
};
},
namespace: "ap-grid",
filterFields: I([]),
params: T(o, "params"),
defaultData: o.defaultData,
manual: t(ke),
formatParams: o.beforeSearchSubmit,
pagination: o.pagination,
sortFields: Be,
onClickReset: Fe
}), S = l(() => {
const e = o.rowSelection || ge.value?.rowSelection;
if (e)
return e === !0 ? ie : {
...ie,
...e
};
}), De = Po(o, f), Ne = l(
() => F(o.dataSource) ? w.records : o.dataSource
), {
select: Ee,
unSelect: Ke,
selectedRows: E,
selectedRowKeys: Pe,
clearAll: ze,
selectMultiByKeys: Ae
} = ho({
...S.value,
dataSource: Ne,
rowKey: o.rowKey
}), Ve = {
checkbox: "multiRow",
radio: "singleRow"
}, Me = l(() => {
const e = S.value;
if (!e)
return;
const { type: r = "checkbox", disabled: a } = e;
return {
mode: Ve[r],
isRowSelectable: (d) => a ? !a(d.data) : !0,
hideDisabledCheckboxes: e.hideDisabled ?? !1
};
});
function K(e) {
return G(o.rowKey) ? o.rowKey(e) : e[o.rowKey];
}
const Q = ({
api: e = s.value
}) => {
if (o.onUpdate?.(), !S.value)
return;
const r = E.value;
if (!r.length) {
setTimeout(() => {
p = !0;
}, 0);
return;
}
p = !1;
const a = [];
e.forEachNode((d) => {
const h = d.data, i = K(h);
r.find((y) => K(y) === i) && a.push(d);
}), e.setNodesSelected({ nodes: a, newValue: !0 }), setTimeout(() => {
p = !0;
}, 0);
}, Oe = (e) => {
p && (e.node.isSelected() ? Ee(e.data) : Ke(e.data), G(S.value?.tooltip) && e.api.refreshCells({
rowNodes: [e.node],
// todo: 取消硬编码
columns: ["ag-Grid-SelectionColumn"],
force: !0
}));
}, Ge = () => {
p = !0;
}, P = () => {
ze(), s.value?.deselectAll();
};
function We(e) {
Ae(e), ro(() => {
Q({});
});
}
function X(e = !1) {
const r = e ? "getFieldsValueTransformed" : "getFieldsValue";
return g.value?.apForm?.[r]?.(!0);
}
function Y(e) {
g.value?.apForm?.setFieldsValue?.(e);
}
function Z() {
return g.value?.getSorterItems() || [];
}
function _(e) {
g.value?.setSorterItems(e);
}
function ee() {
g.value?.resetSorterItems();
}
function oe(e) {
return e && String(e);
}
function He(e) {
const r = s.value;
if (!r)
return;
const a = r.getColumnState() || [], d = e.map((n) => n.key ? String(n.key) : n.key).filter(Boolean), h = a.map((n) => {
if (!Se.value.includes(n.colId))
return n;
const R = !d.includes(n.colId), te = {
...n,
hide: R
}, re = e.find(
(eo) => oe(eo.key) === n.colId
);
return re && (te.pinned = re.fixed || null), te;
}), i = new Map(
e.map((n, R) => [oe(n.key), R])
), y = h.filter((n) => i.has(n.colId)).sort(
(n, R) => i.get(n.colId) - i.get(R.colId)
), v = [];
let c = 0;
for (const n of h)
i.has(n.colId) ? v.push(y[c++]) : v.push(n);
r.applyColumnState({
state: v,
applyOrder: !0
});
}
Ro({
columns: l(() => f.value),
columnsBackup: l(() => U.value),
size: l(() => Bo[C.value]),
updateColumns(e) {
f.value = e, o.onShownColumnsChange?.(e), He(e);
},
updateSize(e) {
C.value = bo[e];
},
renderConfig: l(() => ({
className: ce("table-header", "title"),
color: we.value.colorTextTertiary
})),
dataSource: l(() => t(w).records),
getSearchFormValues: X,
setSearchFormValues: Y,
submit: D,
submitWith: q,
reset: N,
refresh: J,
getSearchFormSorterItems: Z,
setSearchFormSorterItems: _,
resetSearchFormSorterItems: ee
});
const Le = l(
() => F(o.loading) ? w.loading : o.loading
), Ue = l(() => [
u(),
o.card ? null : u("wrapper"),
u("adaptive"),
u(`size-${C.value}`)
].filter(Boolean)), je = l(() => ({
height: "100%",
...o.tableStyle || {}
})), $e = (e, r) => {
$({ current: e, pageSize: r });
};
function qe(e) {
const r = e.data;
return K(r);
}
function Je(e, r) {
s.value?.ensureIndexVisible(e, r);
}
function Qe(e) {
o.onRowClicked?.(e.data, e.event);
}
function Xe(e) {
o.onScrollEnd?.(e.direction);
}
function Ye(e) {
return s.value?.refreshCells(e);
}
function Ze(e) {
return s.value?.redrawRows(e);
}
const _e = (e) => {
const r = [
H.value.getRowClass?.(e)
];
if (!e.node.rowPinned) {
const a = G(o.rowClassName) ? o.rowClassName(e.data, e.rowIndex) : o.rowClassName;
r.push(a);
}
return r.flat().filter(Boolean);
};
return z(
() => o.dataSource,
(e) => {
Te(e);
},
{ immediate: !0, deep: !0 }
), z(
() => o.size,
(e) => {
C.value = e;
}
), z(
() => t(w).loading,
(e) => {
o.onLoadingChange?.(e);
}
), ue({
submit: D,
reset: N,
refresh: J,
submitWith: q,
setSearchFormValues: Y,
getSearchFormValues: X,
getShowColumns: () => t(f),
rowSelection: {
selectedRows: l(() => E.value),
setSelectedRowKeys: We,
clearAll: P
},
scrollToRow: Je,
getDataSource: () => t(w.records),
getPaging: xe,
setPaging: Ie,
getSearchFormSorterItems: Z,
setSearchFormSorterItems: _,
resetSearchFormSorterItems: ee,
refreshCells: Ye,
redrawRows: Ze,
_internalGridApi: l(() => s.value)
}), (e, r) => (b(), A("div", {
class: m(Ue.value),
style: V(e.wrapperStyle)
}, [
t(F)(e.dataSource) && e.searchForm !== !1 && t(L).length > 0 ? (b(), A("div", {
key: 0,
class: m(e.card ? t(k)("search-wrapper") : null),
style: V(e.searchFormWrapperStyle)
}, [
M(t(So).SearchForm, O(e.searchForm || {}, {
ref_key: "formRef",
ref: g,
"custom-reset": "",
"submit-loading": t(j).loading,
onSubmit: t(D),
onReset: t(N)
}), ao({
default: ne(() => [
(b(!0), A(no, null, lo(t(L), (a) => (b(), le(so(a.renderNode), {
key: a.dataIndex
}))), 128))
]),
_: 2
}, [
W.searchFormExtra ? {
name: "extra",
fn: ne(() => [
se(e.$slots, "searchFormExtra")
]),
key: "0"
} : void 0
]), 1040, ["submit-loading", "onSubmit", "onReset"])
], 6)) : ae("", !0),
B("div", {
class: m([t(k)("table-wrapper"), e.card ? t(k)("table-wrapper-card") : null]),
style: V(e.tableWrapperStyle)
}, [
B("div", {
class: m(t(u)("header-wrapper"))
}, [
se(e.$slots, "title", {
selectedRows: t(E),
selectedRowKeys: t(Pe),
shownColumns: t(f),
clearAll: P
})
], 2),
B("div", {
ref_key: "gridWrapperRef",
ref: ve,
class: m(t(u)("grid-wrapper"))
}, [
M(t(co), O(t(fo)(t(pe), t(H)), {
"get-row-class": _e,
style: je.value,
"row-data": t(w).records,
"column-defs": t(he),
"default-col-def": t(Ce),
"suppress-loading-overlay": "",
"row-selection": Me.value,
"selection-column-def": t(De),
"get-row-id": qe,
theme: t(me),
"tooltip-show-delay": 500,
"enable-browser-tooltips": e.browserTooltips,
"tooltip-interaction": "",
"row-class-rules": e.rowClassRules,
"no-rows-overlay-component": Vo,
"suppress-cell-focus": "",
"dom-layout": e.autoHeight ? "autoHeight" : "normal",
onSortChanged: be,
onGridReady: r[0] || (r[0] = (a) => s.value = a.api),
onRowDataUpdated: Q,
onRowSelected: Oe,
onFirstDataRendered: Ge,
onRowClicked: Qe,
onBodyScrollEnd: Xe
}), null, 16, ["style", "row-data", "column-defs", "default-col-def", "row-selection", "selection-column-def", "theme", "enable-browser-tooltips", "row-class-rules", "dom-layout"]),
io(B("div", {
class: m(t(k)("table-loading-wrapper"))
}, [
M(t(go), { delay: 300 })
], 2), [
[uo, Le.value]
])
], 2),
B("div", {
class: m(t(u)("pagination-wrapper"))
}, [
e.pagination !== !1 ? (b(), le(t(wo), O({
key: 0,
class: t(u)("pagination")
}, t(j).pagination, { onChange: $e }), null, 16, ["class"])) : ae("", !0)
], 2)
], 6)
], 6));
}
});
export {
mt as default
};