UNPKG

4.59 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _typeof from 'babel-runtime/helpers/typeof';
3import classnames from 'classnames';
4
5var blackList = ['defaultProps', 'propTypes', 'contextTypes', 'childContextTypes', 'displayName', 'getDerivedStateFromProps'];
6
7export var statics = function statics(Target, Component) {
8 Object.keys(Component).forEach(function (property) {
9 if (blackList.indexOf(property) === -1) {
10 Target[property] = Component[property];
11 }
12 });
13};
14
15export var fetchDataByPath = function fetchDataByPath(object, path) {
16 if (!object || !path) {
17 return false;
18 }
19 path = path.toString();
20 var field = path.split('.');
21 var val = void 0,
22 key = void 0;
23 if (field.length) {
24 key = field[0];
25 // lists[1].name
26 if (key.indexOf('[') >= 0) {
27 key = key.match(/(.*)\[(.*)\]/);
28 if (key && _typeof(key[1]) === 'object' && _typeof(object[key[1]]) === 'object') {
29 val = object[key[1]][key[2]];
30 }
31 } else {
32 val = object[field[0]];
33 }
34 if (val) {
35 for (var colIndex = 1; colIndex < field.length; colIndex++) {
36 val = val[field[colIndex]];
37 if (typeof val === 'undefined') {
38 break;
39 }
40 }
41 }
42 }
43 return val;
44};
45
46/**
47 * @param {Array} lockChildren
48 * @param {String} dir 'left', 'right'
49 */
50export var setStickyStyle = function setStickyStyle(lockChildren, flatenChildren, dir) {
51 var offsetArr = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
52 var prefix = arguments[4];
53
54 var len = flatenChildren.length;
55
56 flatenChildren.forEach(function (col, index) {
57 var _classnames;
58
59 var isLeftLast = dir === 'left' && index === len - 1;
60 var isRightFirst = dir === 'right' && index === 0;
61 var style = {
62 position: 'sticky'
63 };
64 var offset = offsetArr[index];
65
66 if (offset > -1) {
67 style[dir] = offset;
68 }
69
70 col.className = classnames(col.className, (_classnames = {}, _classnames[prefix + 'table-fix-' + dir] = true, _classnames[prefix + 'table-fix-left-last'] = isLeftLast, _classnames[prefix + 'table-fix-right-first'] = isRightFirst, _classnames));
71 col.style = _extends({}, col.style, style);
72 col.cellStyle = style;
73 });
74
75 var setOffset = function setOffset(col, index, dir, isBorder) {
76 var _classnames2;
77
78 var style = {
79 position: 'sticky'
80 };
81 var offset = offsetArr[index];
82
83 if (offset > -1) {
84 style[dir] = offset;
85 }
86
87 col.className = classnames(col.className, (_classnames2 = {}, _classnames2[prefix + 'table-fix-' + dir] = true, _classnames2[prefix + 'table-fix-left-last'] = dir === 'left' && isBorder, _classnames2[prefix + 'table-fix-right-first'] = dir === 'right' && isBorder, _classnames2));
88 col.style = _extends({}, col.style, style);
89 col.cellStyle = style;
90 };
91
92 // 查看当前元素的叶子结点数量
93 var getLeafNodes = function getLeafNodes(node) {
94 var nodesLen = 0;
95 var arrLen = Array.isArray(node && node.children) && node.children.length || 0;
96 if (arrLen > 0) {
97 nodesLen = node.children.reduce(function (ret, item, idx) {
98 return ret + getLeafNodes(item.children);
99 }, 0);
100 } else {
101 nodesLen = 1;
102 }
103 return nodesLen;
104 };
105
106 var getPreNodes = function getPreNodes(arr, idx) {
107 return arr.reduce(function (ret, item, i) {
108 if (i < idx) {
109 return ret + getLeafNodes(item);
110 }
111 return ret;
112 }, 0);
113 };
114
115 // for multiple header
116 // nodesLen 前序叶子结点数
117 var loop = function loop(arr, i) {
118 dir === 'right' && arr.reverse();
119 arr.forEach(function (child, j) {
120 var p = dir === 'right' ? i - getPreNodes(arr, j) : i + getPreNodes(arr, j);
121 if (child.children) {
122 // 合并单元格的节点
123 loop(child.children, p);
124 // 查询当前元素下的 前序叶子结点数 比如为n
125 // const isBorder = (dir === 'right' && j === 0) || (dir === 'left' && j === (arr.length - 1));
126 setOffset(child, p, dir, j === arr.length - 1);
127 }
128 });
129 dir === 'right' && arr.reverse();
130 };
131
132 loop(lockChildren, dir === 'left' ? 0 : len - 1);
133};
\No newline at end of file