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