linkmore-design
Version:
🌈 🚀lm组件库。🚀
252 lines (243 loc) • 9.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SCROLL_POSITION_DOT_CLASS_NAME = exports.SCROLL_POSITION_CLASS_NAME = void 0;
exports.checkDataSourceIsEmpty = checkDataSourceIsEmpty;
exports.checkExpandIconColumnIndex = checkExpandIconColumnIndex;
exports.checkMemoShouldUploadSpecialFun = checkMemoShouldUploadSpecialFun;
exports.checkRowKeyByDataSource = checkRowKeyByDataSource;
exports.checkTableRowIsDisable = checkTableRowIsDisable;
exports.computationSummaryValue = computationSummaryValue;
exports.deepDataSourcePreKeys = deepDataSourcePreKeys;
exports.default = void 0;
exports.findFirstMatchingParent = findFirstMatchingParent;
exports.getExpandStatus = getExpandStatus;
exports.getLeafNodes = getLeafNodes;
exports.isExpandRow = isExpandRow;
exports.isObjEmpty = isObjEmpty;
var _lodash = require("lodash");
function isObjEmpty(obj) {
return Object.keys(obj || {})?.length === 0;
}
function deepDataSourcePreKeys(dataSource, rowKey) {
const deepDataSource = [];
const eachChildren = (children, preKeys, deepIds) => {
children?.forEach((item, index) => {
deepDataSource.push({
...item,
preKeys: [...preKeys, item?.[rowKey]],
_deepIds: [...deepIds, index + 1]
});
if (item?.children) {
eachChildren(item?.children, [...preKeys, item?.[rowKey]], [...deepIds, index + 1]);
// deepDataSource.push({ ...item, preKeys: [...preKeys, item[rowKey]] })
}
});
};
eachChildren(dataSource, [], []);
return (0, _lodash.keyBy)(deepDataSource, rowKey);
}
function isExpandRow(children) {
return ['Collapse row', 'Expand row'].includes(children?.[0]?.props?.children?.[1]?.props?.['aria-label']) || children?.[0]?.props?.children?.[1]?.props?.className?.indexOf('ant-table-row-expand-icon') > -1;
}
function checkRowKeyByDataSource(dataSource, rowKey) {
const addRowKey = (children, pkey) => {
return children?.map((item, index) => {
if (item.children) {
const rchildren = addRowKey(item.children, `${pkey}_${index + 1}`);
return {
...item,
[rowKey]: item[rowKey] || `${pkey}_${index + 1}`,
children: rchildren
// _deepIds: [...deepIds, index],
};
}
return {
...item,
[rowKey]: item[rowKey] || `${pkey}_${index + 1}`
// _deepIds: [...deepIds, index],
};
});
};
const res = dataSource?.map((v, idx) => {
if (v?.children) {
const rchildren = addRowKey(v.children, v[rowKey] || `v${idx + 1}`);
return {
...v,
[rowKey]: v[rowKey] || `v${idx + 1}`,
children: rchildren
// _deepIds: [idx],
};
}
return {
...v,
[rowKey]: v[rowKey] || `v${idx + 1}`
// _deepIds: [idx],
};
});
return res || [];
}
function getExpandStatus(children) {
return isExpandRow(children) ? children?.[0]?.props?.children?.[1]?.props?.['aria-label'] : '';
}
function checkMemoShouldUploadSpecialFun(prev, next) {
const checkExpandStatus = getExpandStatus(prev.children) === getExpandStatus(next.children);
if (!checkExpandStatus) {
return false;
}
const prevCol = prev?.col;
const nextCol = next?.col;
/** TODO: 使用了自定义render, 是否要判断rowIndex? 这样排序之后,渲染的性能就会变差 */
if ((prevCol?.render || nextCol?.render) && !(0, _lodash.isEqual)((0, _lodash.pick)(prev, ['colIndex', 'record', 'children']), (0, _lodash.pick)(next, ['colIndex', 'record', 'children']))) {
return false;
}
if (!next?.quickOpetateClearAll && (next?.getLength < 2 || prev?.getLength === 1) && next?.dataIndex === 'lm_edit_opetate') {
return false;
}
if (prev.isEdit !== next.isEdit) {
return false;
}
// console.log(22, prevCol?.editable === 'render', nextCol?.editable === 'render', prevCol?.componentProps, prevCol?.componentProps)
if ((prevCol?.editable === 'render' || nextCol?.editable === 'render') && (prevCol?.componentProps?.render || nextCol?.componentProps?.render) && (!(0, _lodash.isEqual)((0, _lodash.omit)(prevCol?.componentProps, ['render']), (0, _lodash.omit)(nextCol?.componentProps, ['render'])) || !(0, _lodash.isEqual)((0, _lodash.pick)(prev, ['colIndex', 'record', 'rowIndex']), (0, _lodash.pick)(next, ['colIndex', 'record', 'rowIndex'])))) {
return false;
}
if ((prevCol?.fixed || nextCol?.fixed) && !(0, _lodash.isEqual)(prev?.style, next?.style)) {
return false;
}
if ((prevCol?.componentProps?.optionOnly || nextCol?.componentProps?.optionOnly) && !(0, _lodash.isEqual)(prevCol?.componentProps?.options, nextCol?.componentProps?.options)) {
return false;
}
if ((prevCol?.componentProps?.options || nextCol?.componentProps?.options) && !(0, _lodash.isEqual)(prevCol?.componentProps?.options, nextCol?.componentProps?.options)) {
return false;
}
if (((0, _lodash.isFunction)(prevCol?.componentProps) || (0, _lodash.isFunction)(nextCol?.componentProps)) && !(0, _lodash.isEqual)(prevCol.newOptions, nextCol.newOptions) && !(0, _lodash.isEqual)(prevCol?.componentProps, nextCol?.componentProps)) {
return false;
}
if ((prevCol?.order || nextCol?.order) && !(0, _lodash.isEqual)(prevCol?.order, nextCol?.order)) {
// return isEqual(prevCol?.order, nextCol?.fixed)
return false;
}
if (next.className.indexOf('drag-visible') > -1) {
return (0, _lodash.isEqual)(prev.style, next.style);
}
if (next.className?.indexOf('ant-table-row-expand-icon-cell') > -1) {
return false;
}
if (next.className?.indexOf('ant-table-cell-with-append') > -1) {
return false;
}
if (next.className.indexOf('ant-table-selection-column') > -1) {
return (0, _lodash.isEqual)(prev.children?.[1]?.props, next.children?.[1]?.props);
}
/** 如果启用了快速复制功能,需要实时判断rowIndex与getLength */
if (prevCol?.componentProps?.quickcopy || nextCol?.componentProps?.quickcopy) {
const pickProps = ['record', 'colIndex', 'rowIndex', 'getLength'];
const p = (0, _lodash.pick)(prev, pickProps);
const n = (0, _lodash.pick)(next, pickProps);
return (0, _lodash.isEqual)(p, n);
}
return true;
}
function checkExpandIconColumnIndex({
rowSelection,
sortOpen,
indexCol
}) {
const arr = [rowSelection, sortOpen, indexCol].filter(item => !!item);
return arr.length;
}
function checkDataSourceIsEmpty(config, dataSource, scrollInfo) {
let resultConfig = {
...config
};
if (scrollInfo) {
resultConfig = {
...resultConfig,
scroll: {
x: resultConfig.scroll?.x || '100%',
y: scrollInfo.height || resultConfig.scroll?.y
}
};
}
if (dataSource.length) {
return resultConfig;
}
return {
...resultConfig,
components: {
...resultConfig.components,
body: {}
}
};
}
/** 根据传入的disabedRows, 得出最终需要disabed的rowKeys */
function checkTableRowIsDisable(deepDataSource, disabledRows, rowKey) {
const disabledRkeys = [];
if (!disabledRows.length) {
return disabledRkeys;
}
const disableIds = disabledRows.map(item => (0, _lodash.isObject)(item) ? item?.[rowKey] : item);
function deep(children, arrs) {
children?.forEach(item => {
arrs.push(item[rowKey]);
if (item.children) {
deep(item.children, arrs);
}
});
}
if (disableIds.length) {
disableIds?.forEach(item => {
disabledRkeys.push(item);
const record = deepDataSource[item];
if (record?.children) {
deep(record.children, disabledRkeys);
}
});
}
return disabledRkeys;
}
function getLeafNodes(data) {
const leafNodes = [];
function checkChildren(nodes) {
// eslint-disable-next-line no-restricted-syntax
for (const node of nodes) {
if (!node.children || node.children?.length === 0) {
leafNodes.push(node);
} else {
checkChildren(node?.children);
}
}
}
checkChildren(data);
return leafNodes;
}
/** 查找父级元素 */
function findFirstMatchingParent(element, condition) {
let parent = element.parentNode;
while (parent !== null) {
if (condition(parent)) {
return parent;
}
parent = parent.parentNode;
}
return null; // 如果没有找到符合条件的父元素,则返回 null
}
const SCROLL_POSITION_CLASS_NAME = '__scroll_position_node__';
exports.SCROLL_POSITION_CLASS_NAME = SCROLL_POSITION_CLASS_NAME;
const SCROLL_POSITION_DOT_CLASS_NAME = `.${SCROLL_POSITION_CLASS_NAME}`;
exports.SCROLL_POSITION_DOT_CLASS_NAME = SCROLL_POSITION_DOT_CLASS_NAME;
function computationSummaryValue(params) {
const {
summary,
dataIndex,
pageData
} = params;
return (0, _lodash.isFunction)(summary[dataIndex]) ? summary[dataIndex]?.(pageData, dataIndex) : (0, _lodash.isBoolean)(summary[dataIndex]) ? (pageData || [])?.reduce((a, b) => {
return a + Number(b[dataIndex] || 0);
}, 0) : summary[dataIndex] ? summary[dataIndex] : ' ';
}
var _default = {
isObjEmpty
};
exports.default = _default;