UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

226 lines (222 loc) 16.1 kB
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import { isBoolean, isEqual, isFunction, isObject, keyBy, omit, pick } from 'lodash'; export function isObjEmpty(obj) { var _Object$keys; return ((_Object$keys = Object.keys(obj || {})) === null || _Object$keys === void 0 ? void 0 : _Object$keys.length) === 0; } export function deepDataSourcePreKeys(dataSource, rowKey) { var deepDataSource = []; var eachChildren = function eachChildren(children, preKeys, deepIds) { children === null || children === void 0 ? void 0 : children.forEach(function (item, index) { deepDataSource.push(_objectSpread(_objectSpread({}, item), {}, { preKeys: [].concat(_toConsumableArray(preKeys), [item === null || item === void 0 ? void 0 : item[rowKey]]), _deepIds: [].concat(_toConsumableArray(deepIds), [index + 1]) })); if (item !== null && item !== void 0 && item.children) { eachChildren(item === null || item === void 0 ? void 0 : item.children, [].concat(_toConsumableArray(preKeys), [item === null || item === void 0 ? void 0 : item[rowKey]]), [].concat(_toConsumableArray(deepIds), [index + 1])); // deepDataSource.push({ ...item, preKeys: [...preKeys, item[rowKey]] }) } }); }; eachChildren(dataSource, [], []); return keyBy(deepDataSource, rowKey); } export function isExpandRow(children) { var _children$, _children$$props, _children$$props$chil, _children$$props$chil2, _children$$props$chil3, _children$2, _children$2$props, _children$2$props$chi, _children$2$props$chi2, _children$2$props$chi3, _children$2$props$chi4; return ['Collapse row', 'Expand row'].includes(children === null || children === void 0 ? void 0 : (_children$ = children[0]) === null || _children$ === void 0 ? void 0 : (_children$$props = _children$.props) === null || _children$$props === void 0 ? void 0 : (_children$$props$chil = _children$$props.children) === null || _children$$props$chil === void 0 ? void 0 : (_children$$props$chil2 = _children$$props$chil[1]) === null || _children$$props$chil2 === void 0 ? void 0 : (_children$$props$chil3 = _children$$props$chil2.props) === null || _children$$props$chil3 === void 0 ? void 0 : _children$$props$chil3['aria-label']) || (children === null || children === void 0 ? void 0 : (_children$2 = children[0]) === null || _children$2 === void 0 ? void 0 : (_children$2$props = _children$2.props) === null || _children$2$props === void 0 ? void 0 : (_children$2$props$chi = _children$2$props.children) === null || _children$2$props$chi === void 0 ? void 0 : (_children$2$props$chi2 = _children$2$props$chi[1]) === null || _children$2$props$chi2 === void 0 ? void 0 : (_children$2$props$chi3 = _children$2$props$chi2.props) === null || _children$2$props$chi3 === void 0 ? void 0 : (_children$2$props$chi4 = _children$2$props$chi3.className) === null || _children$2$props$chi4 === void 0 ? void 0 : _children$2$props$chi4.indexOf('ant-table-row-expand-icon')) > -1; } export function checkRowKeyByDataSource(dataSource, rowKey) { var addRowKey = function addRowKey(children, pkey) { return children === null || children === void 0 ? void 0 : children.map(function (item, index) { if (item.children) { var _objectSpread2; var rchildren = addRowKey(item.children, "".concat(pkey, "_").concat(index + 1)); return _objectSpread(_objectSpread({}, item), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, rowKey, item[rowKey] || "".concat(pkey, "_").concat(index + 1)), _defineProperty(_objectSpread2, "children", rchildren), _objectSpread2)); } return _objectSpread(_objectSpread({}, item), {}, _defineProperty({}, rowKey, item[rowKey] || "".concat(pkey, "_").concat(index + 1))); }); }; var res = dataSource === null || dataSource === void 0 ? void 0 : dataSource.map(function (v, idx) { if (v !== null && v !== void 0 && v.children) { var _objectSpread4; var rchildren = addRowKey(v.children, v[rowKey] || "v".concat(idx + 1)); return _objectSpread(_objectSpread({}, v), {}, (_objectSpread4 = {}, _defineProperty(_objectSpread4, rowKey, v[rowKey] || "v".concat(idx + 1)), _defineProperty(_objectSpread4, "children", rchildren), _objectSpread4)); } return _objectSpread(_objectSpread({}, v), {}, _defineProperty({}, rowKey, v[rowKey] || "v".concat(idx + 1))); }); return res || []; } export function getExpandStatus(children) { var _children$3, _children$3$props, _children$3$props$chi, _children$3$props$chi2, _children$3$props$chi3; return isExpandRow(children) ? children === null || children === void 0 ? void 0 : (_children$3 = children[0]) === null || _children$3 === void 0 ? void 0 : (_children$3$props = _children$3.props) === null || _children$3$props === void 0 ? void 0 : (_children$3$props$chi = _children$3$props.children) === null || _children$3$props$chi === void 0 ? void 0 : (_children$3$props$chi2 = _children$3$props$chi[1]) === null || _children$3$props$chi2 === void 0 ? void 0 : (_children$3$props$chi3 = _children$3$props$chi2.props) === null || _children$3$props$chi3 === void 0 ? void 0 : _children$3$props$chi3['aria-label'] : ''; } export function checkMemoShouldUploadSpecialFun(prev, next) { var _prevCol$componentPro, _nextCol$componentPro, _prevCol$componentPro2, _nextCol$componentPro2, _prevCol$componentPro3, _nextCol$componentPro3, _prevCol$componentPro4, _nextCol$componentPro4, _prevCol$componentPro5, _nextCol$componentPro5, _next$className, _next$className2, _prevCol$componentPro6, _nextCol$componentPro6; var checkExpandStatus = getExpandStatus(prev.children) === getExpandStatus(next.children); if (!checkExpandStatus) { return false; } var prevCol = prev === null || prev === void 0 ? void 0 : prev.col; var nextCol = next === null || next === void 0 ? void 0 : next.col; /** TODO: 使用了自定义render, 是否要判断rowIndex? 这样排序之后,渲染的性能就会变差 */ if ((prevCol !== null && prevCol !== void 0 && prevCol.render || nextCol !== null && nextCol !== void 0 && nextCol.render) && !isEqual(pick(prev, ['colIndex', 'record', 'children']), pick(next, ['colIndex', 'record', 'children']))) { return false; } if (!(next !== null && next !== void 0 && next.quickOpetateClearAll) && ((next === null || next === void 0 ? void 0 : next.getLength) < 2 || (prev === null || prev === void 0 ? void 0 : prev.getLength) === 1) && (next === null || next === void 0 ? void 0 : 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 === null || prevCol === void 0 ? void 0 : prevCol.editable) === 'render' || (nextCol === null || nextCol === void 0 ? void 0 : nextCol.editable) === 'render') && (prevCol !== null && prevCol !== void 0 && (_prevCol$componentPro = prevCol.componentProps) !== null && _prevCol$componentPro !== void 0 && _prevCol$componentPro.render || nextCol !== null && nextCol !== void 0 && (_nextCol$componentPro = nextCol.componentProps) !== null && _nextCol$componentPro !== void 0 && _nextCol$componentPro.render) && (!isEqual(omit(prevCol === null || prevCol === void 0 ? void 0 : prevCol.componentProps, ['render']), omit(nextCol === null || nextCol === void 0 ? void 0 : nextCol.componentProps, ['render'])) || !isEqual(pick(prev, ['colIndex', 'record', 'rowIndex']), pick(next, ['colIndex', 'record', 'rowIndex'])))) { return false; } if ((prevCol !== null && prevCol !== void 0 && prevCol.fixed || nextCol !== null && nextCol !== void 0 && nextCol.fixed) && !isEqual(prev === null || prev === void 0 ? void 0 : prev.style, next === null || next === void 0 ? void 0 : next.style)) { return false; } if ((prevCol !== null && prevCol !== void 0 && (_prevCol$componentPro2 = prevCol.componentProps) !== null && _prevCol$componentPro2 !== void 0 && _prevCol$componentPro2.optionOnly || nextCol !== null && nextCol !== void 0 && (_nextCol$componentPro2 = nextCol.componentProps) !== null && _nextCol$componentPro2 !== void 0 && _nextCol$componentPro2.optionOnly) && !isEqual(prevCol === null || prevCol === void 0 ? void 0 : (_prevCol$componentPro3 = prevCol.componentProps) === null || _prevCol$componentPro3 === void 0 ? void 0 : _prevCol$componentPro3.options, nextCol === null || nextCol === void 0 ? void 0 : (_nextCol$componentPro3 = nextCol.componentProps) === null || _nextCol$componentPro3 === void 0 ? void 0 : _nextCol$componentPro3.options)) { return false; } if ((prevCol !== null && prevCol !== void 0 && (_prevCol$componentPro4 = prevCol.componentProps) !== null && _prevCol$componentPro4 !== void 0 && _prevCol$componentPro4.options || nextCol !== null && nextCol !== void 0 && (_nextCol$componentPro4 = nextCol.componentProps) !== null && _nextCol$componentPro4 !== void 0 && _nextCol$componentPro4.options) && !isEqual(prevCol === null || prevCol === void 0 ? void 0 : (_prevCol$componentPro5 = prevCol.componentProps) === null || _prevCol$componentPro5 === void 0 ? void 0 : _prevCol$componentPro5.options, nextCol === null || nextCol === void 0 ? void 0 : (_nextCol$componentPro5 = nextCol.componentProps) === null || _nextCol$componentPro5 === void 0 ? void 0 : _nextCol$componentPro5.options)) { return false; } if ((isFunction(prevCol === null || prevCol === void 0 ? void 0 : prevCol.componentProps) || isFunction(nextCol === null || nextCol === void 0 ? void 0 : nextCol.componentProps)) && !isEqual(prevCol.newOptions, nextCol.newOptions) && !isEqual(prevCol === null || prevCol === void 0 ? void 0 : prevCol.componentProps, nextCol === null || nextCol === void 0 ? void 0 : nextCol.componentProps)) { return false; } if ((prevCol !== null && prevCol !== void 0 && prevCol.order || nextCol !== null && nextCol !== void 0 && nextCol.order) && !isEqual(prevCol === null || prevCol === void 0 ? void 0 : prevCol.order, nextCol === null || nextCol === void 0 ? void 0 : nextCol.order)) { // return isEqual(prevCol?.order, nextCol?.fixed) return false; } if (next.className.indexOf('drag-visible') > -1) { return isEqual(prev.style, next.style); } if (((_next$className = next.className) === null || _next$className === void 0 ? void 0 : _next$className.indexOf('ant-table-row-expand-icon-cell')) > -1) { return false; } if (((_next$className2 = next.className) === null || _next$className2 === void 0 ? void 0 : _next$className2.indexOf('ant-table-cell-with-append')) > -1) { return false; } if (next.className.indexOf('ant-table-selection-column') > -1) { var _prev$children, _prev$children$, _next$children, _next$children$; return isEqual((_prev$children = prev.children) === null || _prev$children === void 0 ? void 0 : (_prev$children$ = _prev$children[1]) === null || _prev$children$ === void 0 ? void 0 : _prev$children$.props, (_next$children = next.children) === null || _next$children === void 0 ? void 0 : (_next$children$ = _next$children[1]) === null || _next$children$ === void 0 ? void 0 : _next$children$.props); } /** 如果启用了快速复制功能,需要实时判断rowIndex与getLength */ if (prevCol !== null && prevCol !== void 0 && (_prevCol$componentPro6 = prevCol.componentProps) !== null && _prevCol$componentPro6 !== void 0 && _prevCol$componentPro6.quickcopy || nextCol !== null && nextCol !== void 0 && (_nextCol$componentPro6 = nextCol.componentProps) !== null && _nextCol$componentPro6 !== void 0 && _nextCol$componentPro6.quickcopy) { var pickProps = ['record', 'colIndex', 'rowIndex', 'getLength']; var p = pick(prev, pickProps); var n = pick(next, pickProps); return isEqual(p, n); } return true; } export function checkExpandIconColumnIndex(_ref) { var rowSelection = _ref.rowSelection, sortOpen = _ref.sortOpen, indexCol = _ref.indexCol; var arr = [rowSelection, sortOpen, indexCol].filter(function (item) { return !!item; }); return arr.length; } export function checkDataSourceIsEmpty(config, dataSource, scrollInfo) { var resultConfig = _objectSpread({}, config); if (scrollInfo) { var _resultConfig$scroll, _resultConfig$scroll2; resultConfig = _objectSpread(_objectSpread({}, resultConfig), {}, { scroll: { x: ((_resultConfig$scroll = resultConfig.scroll) === null || _resultConfig$scroll === void 0 ? void 0 : _resultConfig$scroll.x) || '100%', y: scrollInfo.height || ((_resultConfig$scroll2 = resultConfig.scroll) === null || _resultConfig$scroll2 === void 0 ? void 0 : _resultConfig$scroll2.y) } }); } if (dataSource.length) { return resultConfig; } return _objectSpread(_objectSpread({}, resultConfig), {}, { components: _objectSpread(_objectSpread({}, resultConfig.components), {}, { body: {} }) }); } /** 根据传入的disabedRows, 得出最终需要disabed的rowKeys */ export function checkTableRowIsDisable(deepDataSource, disabledRows, rowKey) { var disabledRkeys = []; if (!disabledRows.length) { return disabledRkeys; } var disableIds = disabledRows.map(function (item) { return isObject(item) ? item === null || item === void 0 ? void 0 : item[rowKey] : item; }); function deep(children, arrs) { children === null || children === void 0 ? void 0 : children.forEach(function (item) { arrs.push(item[rowKey]); if (item.children) { deep(item.children, arrs); } }); } if (disableIds.length) { disableIds === null || disableIds === void 0 ? void 0 : disableIds.forEach(function (item) { disabledRkeys.push(item); var record = deepDataSource[item]; if (record !== null && record !== void 0 && record.children) { deep(record.children, disabledRkeys); } }); } return disabledRkeys; } export function getLeafNodes(data) { var leafNodes = []; function checkChildren(nodes) { // eslint-disable-next-line no-restricted-syntax var _iterator = _createForOfIteratorHelper(nodes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _node$children; var node = _step.value; if (!node.children || ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) === 0) { leafNodes.push(node); } else { checkChildren(node === null || node === void 0 ? void 0 : node.children); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } checkChildren(data); return leafNodes; } /** 查找父级元素 */ export function findFirstMatchingParent(element, condition) { var parent = element.parentNode; while (parent !== null) { if (condition(parent)) { return parent; } parent = parent.parentNode; } return null; // 如果没有找到符合条件的父元素,则返回 null } export var SCROLL_POSITION_CLASS_NAME = '__scroll_position_node__'; export var SCROLL_POSITION_DOT_CLASS_NAME = ".".concat(SCROLL_POSITION_CLASS_NAME); export function computationSummaryValue(params) { var _summary$dataIndex, _ref2; var summary = params.summary, dataIndex = params.dataIndex, pageData = params.pageData; return isFunction(summary[dataIndex]) ? (_summary$dataIndex = summary[dataIndex]) === null || _summary$dataIndex === void 0 ? void 0 : _summary$dataIndex.call(summary, pageData, dataIndex) : isBoolean(summary[dataIndex]) ? (_ref2 = pageData || []) === null || _ref2 === void 0 ? void 0 : _ref2.reduce(function (a, b) { return a + Number(b[dataIndex] || 0); }, 0) : summary[dataIndex] ? summary[dataIndex] : ' '; } export default { isObjEmpty: isObjEmpty };