tav-ui
Version:
267 lines (264 loc) • 8.18 kB
JavaScript
import { unref, ref, computed, watch, toRaw } from 'vue';
import { cloneDeep, isEqual } from 'lodash-es';
import '../../../../locales/index2.mjs';
import { formatToDate } from '../../../../utils/dateUtil2.mjs';
import '../../../../utils/index2.mjs';
import { isBoolean, isFunction, isArray, isString, isMap } from '../../../../utils/is2.mjs';
import { renderEditCell } from '../components/editable/index2.mjs';
import { DEFAULT_ALIGN, INDEX_COLUMN_FLAG, PAGE_SIZE, ACTION_COLUMN_FLAG } from '../const2.mjs';
import { tavI18n } from '../../../../locales/transfer2.mjs';
import { formatNumber } from '../../../../utils/formatNumber2.mjs';
function handleItem(item, ellipsis) {
const { key, dataIndex, children } = item;
item.align = item.align || DEFAULT_ALIGN;
if (ellipsis) {
if (!key) {
item.key = dataIndex;
}
if (!isBoolean(item.ellipsis)) {
Object.assign(item, {
ellipsis
});
}
}
if (children && children.length) {
handleChildren(children, !!ellipsis);
}
}
function handleChildren(children, ellipsis) {
if (!children)
return;
children.forEach((item) => {
const { children: children2 } = item;
handleItem(item, ellipsis);
handleChildren(children2, ellipsis);
});
}
function handleIndexColumn(propsRef, getPaginationRef, columns) {
const { showIndexColumn, indexColumnProps, isTreeTable } = unref(propsRef);
let pushIndexColumns = false;
if (unref(isTreeTable)) {
return;
}
columns.forEach(() => {
const indIndex = columns.findIndex((column) => column.flag === INDEX_COLUMN_FLAG);
if (showIndexColumn) {
pushIndexColumns = indIndex === -1;
} else if (!showIndexColumn && indIndex !== -1) {
columns.splice(indIndex, 1);
}
});
if (!pushIndexColumns)
return;
const isFixedLeft = columns.some((item) => item.fixed === "left");
columns.unshift({
flag: INDEX_COLUMN_FLAG,
width: 50,
title: tavI18n("Tav.tablePro.columns.1"),
align: "center",
customRender: ({ index }) => {
const getPagination = unref(getPaginationRef);
if (isBoolean(getPagination)) {
return `${index + 1}`;
}
const { current = 1, pageSize = PAGE_SIZE } = getPagination;
return ((current < 1 ? 1 : current) - 1) * pageSize + index + 1;
},
...isFixedLeft ? {
fixed: "left"
} : {},
...indexColumnProps
});
}
function handleActionColumn(propsRef, columns) {
const { actionColumn } = unref(propsRef);
if (!actionColumn)
return;
const hasIndex = columns.findIndex((column) => column.flag === ACTION_COLUMN_FLAG);
if (hasIndex === -1) {
columns.push({
...columns[hasIndex],
fixed: "right",
...actionColumn,
flag: ACTION_COLUMN_FLAG
});
}
}
function useColumns(propsRef, getPaginationRef) {
const columnsRef = ref(unref(propsRef).columns);
let cacheColumns = unref(propsRef).columns;
const getColumnsRef = computed(() => {
const columns = cloneDeep(unref(columnsRef));
handleIndexColumn(propsRef, getPaginationRef, columns);
handleActionColumn(propsRef, columns);
if (!columns) {
return [];
}
const { ellipsis } = unref(propsRef);
columns.forEach((item) => {
const { customRender, slots } = item;
handleItem(item, Reflect.has(item, "ellipsis") ? !!item.ellipsis : !!ellipsis && !customRender && !slots);
});
return columns;
});
function isIfShow(column) {
const ifShow = column.ifShow;
let isIfShow2 = true;
if (isBoolean(ifShow)) {
isIfShow2 = ifShow;
}
if (isFunction(ifShow)) {
isIfShow2 = ifShow(column);
}
return isIfShow2;
}
const getViewColumns = computed(() => {
const viewColumns = sortFixedColumn(unref(getColumnsRef));
const columns = cloneDeep(viewColumns);
return columns.filter((column) => {
return isIfShow(column);
}).map((column) => {
const { slots, dataIndex, customRender, format, edit, editRow, flag } = column;
if (!slots || !slots?.title) {
column.slots = { title: `header-${dataIndex}`, ...slots || {} };
column.customTitle = column.title;
Reflect.deleteProperty(column, "title");
}
const isDefaultAction = [INDEX_COLUMN_FLAG, ACTION_COLUMN_FLAG].includes(flag);
if (!customRender && format && !edit && !isDefaultAction) {
column.customRender = ({ text, record, index }) => {
return formatCell(text, format, record, index);
};
}
if ((edit || editRow) && !isDefaultAction) {
column.customRender = renderEditCell(column);
}
return column;
});
});
watch(() => unref(propsRef).columns, (columns) => {
columnsRef.value = columns;
cacheColumns = columns?.filter((item) => !item.flag) ?? [];
});
function setCacheColumnsByField(dataIndex, value) {
if (!dataIndex || !value) {
return;
}
cacheColumns.forEach((item) => {
if (item.dataIndex === dataIndex) {
Object.assign(item, value);
return;
}
});
}
function setColumns(columnList) {
const columns = cloneDeep(columnList);
if (!isArray(columns))
return;
if (columns.length <= 0) {
columnsRef.value = [];
return;
}
const firstColumn = columns[0];
const cacheKeys = cacheColumns.map((item) => item.dataIndex);
if (!isString(firstColumn)) {
columnsRef.value = columns;
} else {
const columnKeys = columns;
const newColumns = [];
cacheColumns.forEach((item) => {
newColumns.push({
...item,
defaultHidden: !columnKeys.includes(item.dataIndex?.toString() || item.key)
});
});
if (!isEqual(cacheKeys, columns)) {
newColumns.sort((prev, next) => {
return columnKeys.indexOf(prev.dataIndex) - columnKeys.indexOf(next.dataIndex);
});
}
columnsRef.value = newColumns;
}
}
function getColumns(opt) {
const { ignoreIndex, ignoreAction, sort } = opt || {};
let columns = toRaw(unref(getColumnsRef));
if (ignoreIndex) {
columns = columns.filter((item) => item.flag !== INDEX_COLUMN_FLAG);
}
if (ignoreAction) {
columns = columns.filter((item) => item.flag !== ACTION_COLUMN_FLAG);
}
if (sort) {
columns = sortFixedColumn(columns);
}
return columns;
}
function getCacheColumns() {
return cacheColumns;
}
return {
getColumnsRef,
getCacheColumns,
getColumns,
setColumns,
getViewColumns,
setCacheColumnsByField
};
}
function sortFixedColumn(columns) {
const fixedLeftColumns = [];
const fixedRightColumns = [];
const defColumns = [];
for (const column of columns) {
if (column.fixed === "left") {
fixedLeftColumns.push(column);
continue;
}
if (column.fixed === "right") {
fixedRightColumns.push(column);
continue;
}
defColumns.push(column);
}
return [...fixedLeftColumns, ...defColumns, ...fixedRightColumns].filter((item) => !item.defaultHidden);
}
function formatCell(text, format, record, index) {
if (!format) {
return text;
}
if (isFunction(format)) {
return format(text, record, index);
}
try {
const FORMAT_PREFIX = ["date|", "number|"];
const FORMAT_FN = [formatToDate, formatNumber];
const FORMAT_MAP = /* @__PURE__ */ new Map();
FORMAT_PREFIX.forEach((el, index2) => {
FORMAT_MAP.set(el, FORMAT_FN[index2]);
});
if (isString(format)) {
const PREFIX = FORMAT_PREFIX.find((prefix) => format.startsWith(prefix));
if (PREFIX) {
let dateFormat = format.replace(PREFIX, "");
if (PREFIX === "number|" && dateFormat === "") {
dateFormat = "auto";
}
if (PREFIX !== "number|" && !dateFormat || text == null) {
return text;
}
const formatFn = FORMAT_MAP.get(PREFIX);
if (!formatFn)
return text;
return formatFn(text, dateFormat);
}
}
if (isMap(format)) {
return format.get(text);
}
} catch (error) {
return text;
}
}
export { formatCell, useColumns };
//# sourceMappingURL=useColumns2.mjs.map