forked from frontend-collective/react-sortable-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-node.js
204 lines (185 loc) · 5.7 KB
/
tree-node.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, { Component, Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classnames from './utils/classnames';
import './tree-node.css';
class TreeNode extends Component {
render() {
const {
children,
listIndex,
swapFrom,
swapLength,
swapDepth,
scaffoldBlockPxWidth,
lowerSiblingCounts,
connectDropTarget,
isOver,
draggedNode,
canDrop,
treeIndex,
treeId, // Delete from otherProps
getPrevRow, // Delete from otherProps
node, // Delete from otherProps
path, // Delete from otherProps
rowDirection,
...otherProps
} = this.props;
const rowDirectionClass = rowDirection === 'rtl' ? 'rst__rtl' : null;
// Construct the scaffold representing the structure of the tree
const scaffoldBlockCount = lowerSiblingCounts.length;
const scaffold = [];
lowerSiblingCounts.forEach((lowerSiblingCount, i) => {
let lineClass = '';
if (lowerSiblingCount > 0) {
// At this level in the tree, the nodes had sibling nodes further down
if (listIndex === 0) {
// Top-left corner of the tree
// +-----+
// | |
// | +--+
// | | |
// +--+--+
lineClass =
'rst__lineHalfHorizontalRight rst__lineHalfVerticalBottom';
} else if (i === scaffoldBlockCount - 1) {
// Last scaffold block in the row, right before the row content
// +--+--+
// | | |
// | +--+
// | | |
// +--+--+
lineClass = 'rst__lineHalfHorizontalRight rst__lineFullVertical';
} else {
// Simply connecting the line extending down to the next sibling on this level
// +--+--+
// | | |
// | | |
// | | |
// +--+--+
lineClass = 'rst__lineFullVertical';
}
} else if (listIndex === 0) {
// Top-left corner of the tree, but has no siblings
// +-----+
// | |
// | +--+
// | |
// +-----+
lineClass = 'rst__lineHalfHorizontalRight';
} else if (i === scaffoldBlockCount - 1) {
// The last or only node in this level of the tree
// +--+--+
// | | |
// | +--+
// | |
// +-----+
lineClass = 'rst__lineHalfVerticalTop rst__lineHalfHorizontalRight';
}
scaffold.push(
<div
key={`pre_${1 + i}`}
style={{ width: scaffoldBlockPxWidth }}
className={classnames('rst__lineBlock', lineClass, rowDirectionClass)}
/>
);
if (treeIndex !== listIndex && i === swapDepth) {
// This row has been shifted, and is at the depth of
// the line pointing to the new destination
let highlightLineClass = '';
if (listIndex === swapFrom + swapLength - 1) {
// This block is on the bottom (target) line
// This block points at the target block (where the row will go when released)
highlightLineClass = 'rst__highlightBottomLeftCorner';
} else if (treeIndex === swapFrom) {
// This block is on the top (source) line
highlightLineClass = 'rst__highlightTopLeftCorner';
} else {
// This block is between the bottom and top
highlightLineClass = 'rst__highlightLineVertical';
}
let style;
if (rowDirection === 'rtl') {
style = {
width: scaffoldBlockPxWidth,
right: scaffoldBlockPxWidth * i,
};
} else {
// Default ltr
style = {
width: scaffoldBlockPxWidth,
left: scaffoldBlockPxWidth * i,
};
}
scaffold.push(
<div
// eslint-disable-next-line react/no-array-index-key
key={i}
style={style}
className={classnames(
'rst__absoluteLineBlock',
highlightLineClass,
rowDirectionClass
)}
/>
);
}
});
let style;
if (rowDirection === 'rtl') {
style = { right: scaffoldBlockPxWidth * scaffoldBlockCount };
} else {
// Default ltr
style = { left: scaffoldBlockPxWidth * scaffoldBlockCount };
}
return connectDropTarget(
<div
{...otherProps}
className={classnames('rst__node', rowDirectionClass)}
>
{scaffold}
<div className="rst__nodeContent" style={style}>
{Children.map(children, child =>
cloneElement(child, {
isOver,
canDrop,
draggedNode,
})
)}
</div>
</div>
);
}
}
TreeNode.defaultProps = {
swapFrom: null,
swapDepth: null,
swapLength: null,
canDrop: false,
draggedNode: null,
rowDirection: 'ltr',
};
TreeNode.propTypes = {
treeIndex: PropTypes.number.isRequired,
treeId: PropTypes.string.isRequired,
swapFrom: PropTypes.number,
swapDepth: PropTypes.number,
swapLength: PropTypes.number,
scaffoldBlockPxWidth: PropTypes.number.isRequired,
lowerSiblingCounts: PropTypes.arrayOf(PropTypes.number).isRequired,
listIndex: PropTypes.number.isRequired,
children: PropTypes.node.isRequired,
// Drop target
connectDropTarget: PropTypes.func.isRequired,
isOver: PropTypes.bool.isRequired,
canDrop: PropTypes.bool,
draggedNode: PropTypes.shape({}),
// used in dndManager
getPrevRow: PropTypes.func.isRequired,
node: PropTypes.shape({}).isRequired,
path: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
).isRequired,
// rtl support
rowDirection: PropTypes.string,
};
export default TreeNode;