@aplus-frontend/ui
Version:
589 lines (588 loc) • 18.5 kB
JavaScript
import { defineComponent as po, useSlots as fo, ref as M, toRef as F, unref as t, computed as n, nextTick as x, watch as G, createElementBlock as L, openBlock as C, normalizeStyle as O, normalizeClass as y, createCommentVNode as se, createElementVNode as A, createVNode as go, mergeProps as W, createSlots as wo, withCtx as ie, Fragment as So, renderList as Co, createBlock as H, resolveDynamicComponent as yo, renderSlot as ue } from "vue";
import { AG_GRID_LOCALE_EN as ho, AG_GRID_LOCALE_CN as Ro } from "@ag-grid-community/locale";
import { Pagination as vo } from "@aplus-frontend/antdv";
import { useToken as bo } from "@aplus-frontend/antdv/es/theme/internal";
import { ModuleRegistry as Mo, ValidationModule as Fo, ColumnAutoSizeModule as Bo, PinnedRowModule as ko, RowAutoHeightModule as Io, RowStyleModule as xo, CellSpanModule as Ao, CellStyleModule as No, TooltipModule as To, RowSelectionModule as Do, LocaleModule as Eo, ValueCacheModule as Po, GridStateModule as Ko, ColumnApiModule as Vo, RowApiModule as zo, CellApiModule as Go, RenderApiModule as Lo, EventApiModule as Oo, ScrollApiModule as Wo, ClientSideRowModelApiModule as Ho, ClientSideRowModelModule as Uo } from "ag-grid-community";
import { AgGridVue as _o } from "ag-grid-vue3";
import { isUndefined as B, isFunction as N } from "lodash-unified";
import { DEFAULT_ROW_SELECTION_CONFIG as de, sizeReverseMap as jo, sizeMap as $o } from "../ap-grid/constants.mjs";
import { useProvideApTable as qo } from "../ap-table/context.mjs";
import { useTablePaging as Jo } from "../ap-table/hooks/use-table-paging-ng.mjs";
import { recursionApColumns as Qo } from "../ap-table/utils.mjs";
import "../config-provider/index.mjs";
import "../utils/index.mjs";
import { retryUntil as Xo } from "../utils/retry.mjs";
import { getValidVNodeList as Yo } from "../utils/slot.mjs";
import { ApForm as Zo } from "../ap-form/index.mjs";
import "./components/empty/index.vue.mjs";
import "./components/loading/index.vue.mjs";
import "./components/row/index.vue.mjs";
import { useProvideSorter as et } from "./context.mjs";
import { useColumns as ot } from "./hooks/use-columns.mjs";
import { useColumnsDef as tt } from "./hooks/use-columns-def.mjs";
import { usePinnedRow as rt } from "./hooks/use-pinned-row.mjs";
import lt from "./hooks/use-row-selection.mjs";
import { useSearchForm as at } from "./hooks/use-search-form.mjs";
import nt from "./hooks/use-selection-col-def.mjs";
import st from "./hooks/use-virtual-config.mjs";
import it from "./style/index.mjs";
import { useTheme as ut } from "./theme.mjs";
import { mergeProps as dt } from "./utils.mjs";
import { useNamespace as ct } from "../config-provider/hooks/use-namespace.mjs";
import { useLocale as mt } from "../config-provider/hooks/use-locale.mjs";
import { useGlobalConfig as pt } from "../config-provider/hooks/use-global-config.mjs";
import { isDef as ft } from "../utils/is.mjs";
import gt from "./components/row/index.vue2.mjs";
import wt from "./components/loading/index.vue2.mjs";
import St from "./components/empty/index.vue2.mjs";
const or = /* @__PURE__ */ po({
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: {},
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: {},
rowHoverable: { type: Boolean },
columns: {},
rowSelection: { type: [Boolean, Object] },
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: {},
getRowHeight: {},
advanceRenderer: { type: Boolean },
browserTooltips: { type: Boolean },
virtual: { type: [Boolean, Object], default: void 0 },
onRowClicked: {},
onScrollEnd: {},
rowClassName: {},
rowClassRules: {},
autoHeight: { type: Boolean },
onUpdate: {},
customRow: {},
theme: {},
emptyComponent: {}
},
setup(ce, { expose: me }) {
const pe = {
"zh-cn": Ro,
en: ho
};
Mo.registerModules([
Fo,
Bo,
ko,
Io,
xo,
Ao,
No,
To,
Do,
Eo,
Po,
Ko,
Vo,
zo,
Go,
Lo,
Oo,
Wo,
Ho,
Uo
]);
const o = ce, U = fo(), { e: T, b: d, be: fe } = ct("ag-grid"), { lang: _ } = mt(), ge = it("ag-grid"), h = M(o.size), we = ut(
h,
F(o, "rowHeight"),
F(o, "theme")
), Se = st(F(o, "virtual")), { rowPinnedGridConfig: j, getRowPinnedCellClass: Ce } = rt(
F(o, "summary")
), ye = pt("apGrid"), s = M(), [, he] = bo();
let R = !!o.dataSource;
const $ = at(o), { shownColumns: g, columns: q, columnKeys: Re, getTargetColumnByKey: ve } = ot(o), { defaultColDef: be, columnDefs: Me, gridWrapperRef: Fe } = tt(q, o, Ce), k = M({});
et({
sorter: k,
setSorter(e) {
k.value = e;
}
});
const Be = {
asc: "ascend",
desc: "descend"
};
function ke() {
const e = s.value.getColumns()?.filter((c) => c.getSort() !== void 0), r = t(k) || {};
let l = e?.[0];
const i = Object.keys(r);
if (i.length && i.every((c) => r[c] === null)) {
const c = Object.keys(r)[0];
c && (l = s.value?.getColumn(c));
}
if (!l)
return;
const u = l.getColDef(), p = l.getSort(), f = ve(u.colId);
Q(void 0, void 0, {
column: u,
field: f?.field ?? u.field ?? u.colId,
order: p ? Be[p] : null
});
}
const Ie = n(() => Qo(t(g), (e) => {
if (e.sorter === !0)
return e.field ?? e.dataIndex ?? e.key;
}).filter(Boolean)), xe = () => {
k.value = {};
const e = s.value;
if (!e)
return;
const r = e.getColumnState()?.map((l) => ({ ...l, sort: null }));
e.applyColumnState({ state: r });
}, D = M(!0), Ae = n(() => ft(o.manual) ? o.manual : Yo(U.searchFormExtra?.() || []).some((r) => r.type?.name === "ApView")), {
formRef: w,
submit: E,
reset: P,
data: m,
tableProps: J,
handleTableChangeOptional: Q,
submitWith: X,
refresh: Y,
getPaging: Ne,
setPaging: Te,
setDataSource: De,
getLastParams: Ee
} = Jo({
async request(e) {
if (R = !1, S.value && !S.value.preserveSelectedRowKeys && B(o.dataSource) && !D.value && z(), D.value && (D.value = !1), !B(o.dataSource)) {
const l = e.pageSize * (e.current - 1);
return {
data: (o.pagination === !1 ? o.dataSource : o.dataSource.slice(l, l + 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: M([]),
params: F(o, "params"),
defaultData: o.defaultData,
manual: t(Ae),
formatParams: o.beforeSearchSubmit,
pagination: o.pagination,
sortFields: Ie,
onClickReset: xe
}), S = n(() => {
const e = o.rowSelection || ye.value?.rowSelection;
if (e)
return e === !0 ? de : {
...de,
...e
};
}), Pe = nt(o, g), Ke = n(
() => B(o.dataSource) ? m.records : o.dataSource
), {
select: Ve,
unSelect: ze,
selectedRows: K,
selectedRowKeys: Ge,
clearAll: Le,
selectMultiByKeys: Oe,
selectMulti: We,
unSelectMulti: He
} = lt({
...S.value,
dataSource: Ke,
rowKey: o.rowKey
}), Ue = {
checkbox: "multiRow",
radio: "singleRow"
}, _e = n(() => {
const e = S.value;
if (!e)
return;
const { type: r = "checkbox", disabled: l } = e;
return {
mode: Ue[r],
isRowSelectable: (i) => l ? !l(i.data) : !0,
hideDisabledCheckboxes: e.hideDisabled ?? !1
};
});
function V(e) {
return N(o.rowKey) ? o.rowKey(e) : e[o.rowKey];
}
const I = ({
api: e = s.value
}) => {
if (o.onUpdate?.(), !S.value)
return;
const r = K.value || [];
R = !1;
const l = [];
e.forEachNode((i) => {
const v = i.data, u = V(v), p = i.isSelected();
r.find((f) => V(f) === u) ? !p && l.push(i) : p && i.setSelected(!1);
}), e.setNodesSelected({ nodes: l, newValue: !0 }), setTimeout(() => {
R = !0;
}, 0);
}, je = (e) => {
R && (e.node.isSelected() ? Ve(e.data) : ze(e.data), N(S.value?.tooltip) && e.api.refreshCells({
rowNodes: [e.node],
// todo: 取消硬编码
columns: ["ag-Grid-SelectionColumn"],
force: !0
}));
}, $e = () => {
R = !0;
}, z = () => {
Le(), s.value?.deselectAll();
};
function qe(e) {
Oe(e), x(() => {
I({});
});
}
const Je = n(() => {
const e = o.customRow;
return e ? {
isFullWidthRow: e.isCustomRow,
fullWidthCellRenderer: gt,
fullWidthCellRendererParams: {
render: e.render
}
} : {};
});
function Qe(e) {
We(e), x(() => {
I({});
});
}
function Xe(e) {
He(e), x(() => {
I({});
});
}
function Z(e = !1) {
const r = e ? "getFieldsValueTransformed" : "getFieldsValue";
return w.value?.apForm?.[r]?.(!0);
}
function ee(e) {
w.value?.apForm?.setFieldsValue?.(e);
}
function oe() {
return w.value?.getSorterItems() || [];
}
function te(e) {
w.value?.setSorterItems(e);
}
function re() {
w.value?.resetSorterItems();
}
function le(e) {
return e && String(e);
}
function Ye(e) {
const r = s.value;
if (!r)
return;
const l = r.getColumnState() || [], i = e.map((a) => a.key ? String(a.key) : a.key).filter(Boolean), v = l.map((a) => {
if (!Re.value.includes(a.colId))
return a;
const b = !i.includes(a.colId), ae = {
...a,
hide: b
}, ne = e.find(
(mo) => le(mo.key) === a.colId
);
return ne && (ae.pinned = ne.fixed || null), ae;
}), u = new Map(
e.map((a, b) => [le(a.key), b])
), p = v.filter((a) => u.has(a.colId)).sort(
(a, b) => u.get(a.colId) - u.get(b.colId)
), f = [];
let c = 0;
for (const a of v)
u.has(a.colId) ? f.push(p[c++]) : f.push(a);
r.applyColumnState({
state: f,
applyOrder: !0
});
}
qo({
columns: n(() => g.value),
columnsBackup: n(() => q.value),
size: n(() => $o[h.value]),
updateColumns(e) {
g.value = e, o.onShownColumnsChange?.(e), Xo(
() => s.value,
() => {
Ye(e);
},
{
interval: 17
}
);
},
updateSize(e) {
h.value = jo[e];
},
renderConfig: n(() => ({
className: fe("table-header", "title"),
color: he.value.colorTextTertiary
})),
dataSource: n(() => t(m).records),
getSearchFormValues: Z,
setSearchFormValues: ee,
submit: E,
submitWith: X,
reset: P,
refresh: Y,
getSearchFormSorterItems: oe,
setSearchFormSorterItems: te,
resetSearchFormSorterItems: re,
getLastParams: Ee
});
const Ze = n(
() => B(o.loading) ? m.loading : o.loading
), eo = n(() => [
d(),
o.card ? null : d("wrapper"),
d("adaptive"),
d(`size-${h.value}`),
o.autoHeight && m.records.length ? d("auto-height") : null,
ge.value
].filter(Boolean)), oo = n(() => ({
height: "100%",
...o.tableStyle || {}
})), to = (e, r) => {
Q({ current: e, pageSize: r });
};
function ro(e) {
const r = e.data;
return V(r);
}
function lo(e, r) {
(N(e) ? s.value?.ensureNodeVisible : s.value?.ensureIndexVisible)?.(e, r);
}
function ao(e, r) {
s.value?.ensureColumnVisible(e, r);
}
function no(e) {
o.onRowClicked?.(e.data, e.event);
}
function so(e) {
o.onScrollEnd?.(e.direction);
}
function io(e) {
return s.value?.refreshCells(e);
}
function uo(e) {
return s.value?.redrawRows(e);
}
const co = (e) => {
const r = [
j.value.getRowClass?.(e)
];
if (!e.node.rowPinned) {
const l = N(o.rowClassName) ? o.rowClassName(e.data, e.rowIndex) : o.rowClassName;
r.push(l);
}
return r.flat().filter(Boolean);
};
return G(
() => o.dataSource,
(e) => {
x(() => {
De(e);
});
},
{ immediate: !0, deep: !0 }
), G(
() => o.size,
(e) => {
h.value = e;
}
), G(
() => t(m).loading,
(e) => {
o.onLoadingChange?.(e);
}
), me({
submit: E,
reset: P,
refresh: Y,
submitWith: X,
setSearchFormValues: ee,
getSearchFormValues: Z,
getShowColumns: () => t(g),
rowSelection: {
selectedRows: n(() => K.value),
setSelectedRowKeys: qe,
setSelectedRows: Qe,
clearAll: z,
unSelectRows: Xe
},
scrollToRow: lo,
scrollToColumn: ao,
getDataSource: () => t(m.records),
getPaging: Ne,
setPaging: Te,
getSearchFormSorterItems: oe,
setSearchFormSorterItems: te,
resetSearchFormSorterItems: re,
refreshCells: io,
redrawRows: uo,
_internalGridApi: n(() => s.value)
}), (e, r) => (C(), L("div", {
class: y(eo.value),
style: O(e.wrapperStyle)
}, [
t(B)(e.dataSource) && e.searchForm !== !1 && t($).length > 0 ? (C(), L("div", {
key: 0,
class: y(e.card ? t(T)("search-wrapper") : null),
style: O(e.searchFormWrapperStyle)
}, [
go(t(Zo).SearchForm, W(e.searchForm || {}, {
ref_key: "formRef",
ref: w,
"custom-reset": "",
"submit-loading": t(J).loading,
onSubmit: t(E),
onReset: t(P)
}), wo({
default: ie(() => [
(C(!0), L(So, null, Co(t($), (l) => (C(), H(yo(l.renderNode), {
key: l.dataIndex
}))), 128))
]),
_: 2
}, [
U.searchFormExtra ? {
name: "extra",
fn: ie(() => [
ue(e.$slots, "searchFormExtra")
]),
key: "0"
} : void 0
]), 1040, ["submit-loading", "onSubmit", "onReset"])
], 6)) : se("", !0),
A("div", {
class: y([t(T)("table-wrapper"), e.card ? t(T)("table-wrapper-card") : null]),
style: O(e.tableWrapperStyle)
}, [
A("div", {
class: y(t(d)("header-wrapper"))
}, [
ue(e.$slots, "title", {
selectedRows: t(K),
selectedRowKeys: t(Ge),
shownColumns: t(g),
clearAll: z
})
], 2),
A("div", {
ref_key: "gridWrapperRef",
ref: Fe,
class: y(t(d)("grid-wrapper"))
}, [
(C(), H(t(_o), W(
t(dt)(t(Se), t(j), Je.value),
{
key: t(_),
"get-row-class": co,
style: oo.value,
"row-data": t(m).records,
"column-defs": t(Me),
"default-col-def": t(be),
"suppress-loading-overlay": "",
"row-selection": _e.value,
"selection-column-def": t(Pe),
"get-row-id": ro,
"get-row-height": o.getRowHeight,
theme: t(we),
"tooltip-show-delay": 500,
"enable-browser-tooltips": e.browserTooltips,
"tooltip-interaction": "",
"row-class-rules": e.rowClassRules,
"no-rows-overlay-component": e.emptyComponent ?? St,
"loading-overlay-component": wt,
loading: Ze.value,
"suppress-cell-focus": "",
"enable-cell-span": "",
"dom-layout": e.autoHeight ? "autoHeight" : "normal",
"locale-text": pe[t(_)],
onSortChanged: ke,
onGridReady: r[0] || (r[0] = (l) => s.value = l.api),
onRowDataUpdated: I,
onRowSelected: je,
onFirstDataRendered: $e,
onRowClicked: no,
onBodyScrollEnd: so
}
), null, 16, ["style", "row-data", "column-defs", "default-col-def", "row-selection", "selection-column-def", "get-row-height", "theme", "enable-browser-tooltips", "row-class-rules", "no-rows-overlay-component", "loading", "dom-layout", "locale-text"]))
], 2),
A("div", {
class: y(t(d)("pagination-wrapper"))
}, [
e.pagination !== !1 ? (C(), H(t(vo), W({
key: 0,
class: t(d)("pagination")
}, t(J).pagination, { onChange: to }), null, 16, ["class"])) : se("", !0)
], 2)
], 6)
], 6));
}
});
export {
or as default
};