linkmore-design
Version:
🌈 🚀lm组件库。🚀
399 lines (389 loc) • 15.1 kB
JavaScript
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _ from 'lodash';
/**
* 生成 start 到 end 数字组成的数组
* @param {number} start
* @param {number} end
* @returns {Array<number>}
*/
export var range = function range(start, end) {
var array = [];
var inc = end - start > 0;
for (var i = start; inc ? i <= end : i >= end; inc ? i += 1 : i -= 1) {
inc ? array.push(i) : array.unshift(i);
}
return array;
};
/**
* 对数据进行分组
* @param {Array} data
* @param {string} key
* @return {Object}
* 时间复杂度O(n), 空间复杂度O(n)
*/
function dataGroup(data, key) {
var result = {};
data === null || data === void 0 ? void 0 : data.forEach(function (item) {
if (item.children) {
var _Object$keys;
var temp = dataGroup(item.children, key);
(_Object$keys = Object.keys(temp)) === null || _Object$keys === void 0 ? void 0 : _Object$keys.forEach(function (k) {
if (!result[k]) result[k] = [];
result[k] = result[k].concat(temp[k]);
});
} else {
var value = item[key];
result[value] = [].concat(_toConsumableArray(result[value] || []), [item]);
}
});
return result;
}
/**
* 列分组数据转换
* @param columns 表格的columns属性值
* @param dataSource 表格数据源
* @param groupColKeys 列分组的keys
* @returns
*/
export function transformWithColGroup(columns, dataSource, groupColKeys, groupRowKeys) {
if (!Array.isArray(groupColKeys) || !groupColKeys.length) {
return {
columns: columns,
dataSource: dataSource
};
}
columns.forEach(function (c) {
if (groupRowKeys.includes(c.dataIndex)) {
c.show = false;
}
});
var columnsCopy = _.cloneDeep(columns.filter(function (item) {
return item.joinCol;
}));
var unColumns = _.cloneDeep(columns.filter(function (item) {
return !item.joinCol;
}));
var dataSourceCopy = _.cloneDeep(dataSource);
// 去重
var groupColKeysCopy = _toConsumableArray(new Set(groupColKeys));
/**
* @param {Array} source source数据源,树状结构,行分组会有children子级结构
* @param {Array} columns 列数据,用于表头,当列分组时会有children子级结构
* @param {Array<string>} oGroup 数组,列分组的keys值
* @param {string} markStr
* @returns
* 2万条数据 393ms
*/
var groupBy = function groupBy(source, columns, oGroup, markStr) {
if (!(oGroup !== null && oGroup !== void 0 && oGroup.length)) {
// 表头数据的生成,生成新的 dataIndex 值
// dataIndex最后的i:筛选的值相同需要加索引
var handledColumns = [];
source === null || source === void 0 ? void 0 : source.forEach(function (souItem, i) {
handledColumns = handledColumns.concat(columns.filter(function (c) {
// 过滤掉(表头操作) 使用列分组作为key的项
return groupColKeysCopy.indexOf(c.dataIndex) === -1;
}).map(function (item) {
return _objectSpread(_objectSpread({}, item), {}, {
dataIndex: "".concat(markStr, "_").concat(item.dataIndex, "_").concat(i)
});
}));
});
return handledColumns;
}
// 拷贝
var group = oGroup.slice();
// 获取列表的第一个值
var filterKey = group.shift();
var map = dataGroup(source, filterKey);
return Object.keys(map).map(function (item) {
return {
type: filterKey,
title: item,
key: "".concat(item, "_").concat(filterKey),
children: groupBy(map[item], columns, group, "".concat(markStr ? "".concat(markStr, "_") : '').concat(filterKey, "_").concat(item))
};
});
};
var tempColumns = groupBy(dataSourceCopy, columnsCopy, groupColKeysCopy, null);
tempColumns = unColumns.concat(tempColumns);
var keyMap = new Map();
var __index = 0;
var dataSourceBy = function dataSourceBy(source) {
return [source.reduce(function (prev, sItem) {
if (sItem.children && sItem.children.length) {
sItem.children = dataSourceBy(sItem.children).flat();
if (!prev) {
prev = [];
}
return [].concat(_toConsumableArray(prev), [sItem]);
} else {
var _Object$keys2;
var str = '';
// 根据筛选条件生成对应的前缀key值,比如[name, age] => 'name_xiaoming_age_18'
groupColKeysCopy === null || groupColKeysCopy === void 0 ? void 0 : groupColKeysCopy.forEach(function (dataIndex, i) {
if (i !== 0) {
str += '_';
}
str += "".concat(dataIndex, "_").concat(sItem[dataIndex]);
});
// 对dataSource的key值进行更改:例如:'name' => 'name_xiaoming_age_18_address'
(_Object$keys2 = Object.keys(sItem)) === null || _Object$keys2 === void 0 ? void 0 : _Object$keys2.forEach(function (key) {
if (key === '__index') {
return;
}
var newKey = "".concat(str, "_").concat(key);
if (keyMap.has(newKey)) {
var value = keyMap.get(newKey) + 1;
keyMap.set(newKey, value);
newKey = "".concat(newKey, "_").concat(value);
} else {
keyMap.set(newKey, 0);
newKey = "".concat(newKey, "_", 0);
}
sItem[newKey] = sItem[key];
sItem["".concat(newKey, "_originIndex")] = sItem.__index === undefined ? __index : sItem.__index;
});
__index += 1;
}
if (!prev) {
prev = {};
}
return _objectSpread(_objectSpread({}, prev), sItem);
}, null)];
};
var tempDataSource = dataSourceBy(dataSourceCopy).flat();
return {
columns: tempColumns,
dataSource: tempDataSource
};
}
/**
* 行分组
* @param {Array} dataSource 数据源
* @param {Array} groupRowKeys 行分组的keys
* @returns
*/
export function transformWithRowGroup(dataSource, groupRowKeys) {
var groupBy = function groupBy(lastFilter, oGroup, preKey) {
if (!(oGroup !== null && oGroup !== void 0 && oGroup.length)) return lastFilter;
var group = oGroup.slice();
var filterKey = group.shift();
// 记录数据原来的位置索引信息
var i = 0;
var map = lastFilter.reduce(function (acc, el) {
var value = el[filterKey];
if (value === undefined) {
value = '-';
}
acc[value] = [].concat(_toConsumableArray(acc[value] || []), [_objectSpread(_objectSpread({}, el), {}, {
__index: i
})]);
i += 1;
return acc;
}, {});
return Object.keys(map).map(function (item) {
var key;
if (preKey) {
key = "".concat(preKey, "_").concat(filterKey, "_").concat(item);
} else {
key = "".concat(filterKey, "_").concat(item);
}
return {
type: filterKey,
_group: item,
key: key,
children: groupBy(map[item], group, key)
};
});
};
return groupBy(dataSource, groupRowKeys);
}
export function checkIsSelectd(i, j) {
var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var posX = j >= start.j && j <= end.j;
var negX = j <= start.j && j >= end.j;
var posY = i >= start.i && i <= end.i;
var negY = i <= start.i && i >= end.i;
return posX && posY || negX && posY || negX && negY || posX && negY;
}
export function checkStatus(i, j, commiting) {
var start = commiting.start,
end = commiting.end;
var maxi = Math.max(start === null || start === void 0 ? void 0 : start.i, end.i);
var mini = Math.min(start === null || start === void 0 ? void 0 : start.i, end.i);
var maxj = Math.max(start === null || start === void 0 ? void 0 : start.j, end.j);
var minj = Math.min(start === null || start === void 0 ? void 0 : start.j, end.j);
return {
/** 是否有效拉伸 */
isVaildCommit: mini <= i && maxi >= i || minj <= j && maxj >= j
};
}
/**
* 获取选中行文本
* @param {Object}
* {
* selection: {start: {i, j}, end: {i, j}},
* columnsKeysList: Array,
* dataSource: Array
* }
* @returns {Array}
*/
export function getSelectionRowText(_ref) {
var selection = _ref.selection,
columnsKeysList = _ref.columnsKeysList,
dataSource = _ref.dataSource;
var rowtext = range(selection === null || selection === void 0 ? void 0 : selection.start.i, selection === null || selection === void 0 ? void 0 : selection.end.i).map(function (i) {
return columnsKeysList.map(function (j) {
var columnKey = j;
var dataItem = dataSource[i];
var cell = dataItem === null || dataItem === void 0 ? void 0 : dataItem[columnKey];
return cell;
});
});
return rowtext;
}
/**
* 获取选中列文本
* @param {Object}
* @returns {Array}
*/
export function getSelectionColText(_ref2) {
var selection = _ref2.selection,
columns = _ref2.columns,
dataSource = _ref2.dataSource;
var coltext = range(selection === null || selection === void 0 ? void 0 : selection.start.i, selection === null || selection === void 0 ? void 0 : selection.end.i).map(function (i) {
return range(selection === null || selection === void 0 ? void 0 : selection.start.j, selection === null || selection === void 0 ? void 0 : selection.end.j).map(function (j) {
var _columns$j, _columns$j2;
var columnKey = (_columns$j = columns[j]) === null || _columns$j === void 0 ? void 0 : _columns$j.dataIndex;
var dataItem = dataSource[i];
var cell = dataItem === null || dataItem === void 0 ? void 0 : dataItem[columnKey];
if ((_columns$j2 = columns[j]) !== null && _columns$j2 !== void 0 && _columns$j2.render) {
var _columns$j3, _columns$j3$render;
return (_columns$j3 = columns[j]) === null || _columns$j3 === void 0 ? void 0 : (_columns$j3$render = _columns$j3.render) === null || _columns$j3$render === void 0 ? void 0 : _columns$j3$render.call(_columns$j3, cell, dataItem, i);
}
return cell;
});
});
return coltext;
}
/**
* 获取key所对应的原始数据的索引
* @param {Object} {key, dataSource}
* @returns {number}
*/
export function getDataSourceIndex(_ref3) {
var key = _ref3.key,
_ref3$dataSource = _ref3.dataSource,
dataSource = _ref3$dataSource === void 0 ? [] : _ref3$dataSource;
var index = -1;
var dfs = function dfs(arr) {
if (!Array.isArray(arr)) {
return;
}
if (index !== -1) {
return;
}
for (var i = 0; i < arr.length; i += 1) {
var item = arr[i];
if (item["".concat(key, "_originIndex")] !== undefined) {
index = item["".concat(key, "_originIndex")];
return;
}
if (item.children) {
dfs(item.children);
}
}
};
dfs(dataSource);
return index;
}
/**
* 框选是否跨行分组
* @param {number} i
* @param {React.Ref} shellStatusRef
* @param {React.Ref} deepDataSourceRef
* @returns
*/
export function isSelectCrossRowGroup(i, shellStatusRef, deepDataSourceRef) {
var _shellStatusRef$curre;
var x;
var y;
if ((shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre = shellStatusRef.current) === null || _shellStatusRef$curre === void 0 ? void 0 : _shellStatusRef$curre.commiting.start.i) < i) {
var _shellStatusRef$curre2;
x = shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre2 = shellStatusRef.current) === null || _shellStatusRef$curre2 === void 0 ? void 0 : _shellStatusRef$curre2.commiting.start.i;
y = i;
} else {
var _shellStatusRef$curre3;
x = i;
y = shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre3 = shellStatusRef.current) === null || _shellStatusRef$curre3 === void 0 ? void 0 : _shellStatusRef$curre3.commiting.start.i;
}
for (; x <= y; x += 1) {
var _deepDataSourceRef$cu;
if (deepDataSourceRef !== null && deepDataSourceRef !== void 0 && (_deepDataSourceRef$cu = deepDataSourceRef.current) !== null && _deepDataSourceRef$cu !== void 0 && _deepDataSourceRef$cu.length && deepDataSourceRef !== null && deepDataSourceRef !== void 0 && deepDataSourceRef.current[x]._group) {
return true;
}
}
return false;
}
/**
* 不允许跨无效列
* @param {number} i
* @param {number} j
* @param {React.Ref} shellStatusRef
* @param {React.Ref} deepDataSourceRef
* @param {React.Ref} deepColumnsRef
* @returns
*/
export function isSelectCrossInvalidCol(i, j, shellStatusRef, deepDataSourceRef, deepColumnsRef) {
var _shellStatusRef$curre4;
var x;
var y;
if ((shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre4 = shellStatusRef.current) === null || _shellStatusRef$curre4 === void 0 ? void 0 : _shellStatusRef$curre4.commiting.start.j) < j) {
var _shellStatusRef$curre5;
x = shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre5 = shellStatusRef.current) === null || _shellStatusRef$curre5 === void 0 ? void 0 : _shellStatusRef$curre5.commiting.start.j;
y = j;
} else {
var _shellStatusRef$curre6;
x = j;
y = shellStatusRef === null || shellStatusRef === void 0 ? void 0 : (_shellStatusRef$curre6 = shellStatusRef.current) === null || _shellStatusRef$curre6 === void 0 ? void 0 : _shellStatusRef$curre6.commiting.start.j;
}
for (; x <= y; x += 1) {
var _deepColumnsRef$curre, _deepDataSourceRef$cu2;
if (deepColumnsRef !== null && deepColumnsRef !== void 0 && (_deepColumnsRef$curre = deepColumnsRef.current) !== null && _deepColumnsRef$curre !== void 0 && _deepColumnsRef$curre.length && deepDataSourceRef !== null && deepDataSourceRef !== void 0 && (_deepDataSourceRef$cu2 = deepDataSourceRef.current) !== null && _deepDataSourceRef$cu2 !== void 0 && _deepDataSourceRef$cu2.length) {
var key = deepColumnsRef === null || deepColumnsRef === void 0 ? void 0 : deepColumnsRef.current[x].dataIndex;
if (!(key in (deepDataSourceRef === null || deepDataSourceRef === void 0 ? void 0 : deepDataSourceRef.current[i]))) {
return true;
}
}
}
return false;
}
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;
}