This repository was archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathSplitBox.js
More file actions
271 lines (240 loc) · 7.72 KB
/
Copy pathSplitBox.js
File metadata and controls
271 lines (240 loc) · 7.72 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const React = require("react");
const ReactDOM = require("react-dom");
const Draggable = React.createFactory(require("./Draggable"));
const { Component } = React;
const PropTypes = require("prop-types");
const dom = require("react-dom-factories");
require("./SplitBox.css");
/**
* This component represents a Splitter. The splitter supports vertical
* as well as horizontal mode.
*/
class SplitBox extends Component {
static get propTypes() {
return {
// Custom class name. You can use more names separated by a space.
className: PropTypes.string,
// Initial size of controlled panel.
initialSize: PropTypes.any,
// Optional initial width of controlled panel.
initialWidth: PropTypes.number,
// Optional initial height of controlled panel.
initialHeight: PropTypes.number,
// Left/top panel
startPanel: PropTypes.any,
// Left/top panel collapse state.
startPanelCollapsed: PropTypes.bool,
// Min panel size.
minSize: PropTypes.any,
// Max panel size.
maxSize: PropTypes.any,
// Right/bottom panel
endPanel: PropTypes.any,
// Right/bottom panel collapse state.
endPanelCollapsed: PropTypes.bool,
// True if the right/bottom panel should be controlled.
endPanelControl: PropTypes.bool,
// Size of the splitter handle bar.
splitterSize: PropTypes.number,
// True if the splitter bar is vertical (default is vertical).
vert: PropTypes.bool,
// Optional style properties passed into the splitbox
style: PropTypes.object,
// Optional callback when splitbox resize stops
onResizeEnd: PropTypes.func
};
}
static get defaultProps() {
return {
splitterSize: 5,
vert: true,
endPanelControl: false,
endPanelCollapsed: false,
startPanelCollapsed: false,
};
}
constructor(props) {
super(props);
this.state = {
vert: props.vert,
// We use integers for these properties
width: parseInt(props.initialWidth || props.initialSize, 10),
height: parseInt(props.initialHeight || props.initialSize, 10),
};
this.onStartMove = this.onStartMove.bind(this);
this.onStopMove = this.onStopMove.bind(this);
this.onMove = this.onMove.bind(this);
this.preparePanelStyles = this.preparePanelStyles.bind(this);
}
componentWillReceiveProps(nextProps) {
if (this.props.vert !== nextProps.vert) {
this.setState({ vert: nextProps.vert });
}
if (this.props.initialSize !== nextProps.initialSize
|| this.props.initialWidth !== nextProps.initialWidth
|| this.props.initialHeight !== nextProps.initialHeight) {
this.setState({
width: parseInt(nextProps.initialWidth || nextProps.initialSize, 10),
height: parseInt(nextProps.initialHeight || nextProps.initialSize, 10),
});
}
}
// Dragging Events
/**
* Set 'resizing' cursor on entire document during splitter dragging.
* This avoids cursor-flickering that happens when the mouse leaves
* the splitter bar area (happens frequently).
*/
onStartMove() {
const splitBox = ReactDOM.findDOMNode(this);
const doc = splitBox.ownerDocument;
let defaultCursor = doc.documentElement.style.cursor;
doc.documentElement.style.cursor = this.state.vert
? "ew-resize"
: "ns-resize";
splitBox.classList.add("dragging");
this.setState({
defaultCursor: defaultCursor,
});
}
onStopMove() {
const splitBox = ReactDOM.findDOMNode(this);
const doc = splitBox.ownerDocument;
doc.documentElement.style.cursor = this.state.defaultCursor;
splitBox.classList.remove("dragging");
if (this.props.onResizeEnd) {
this.props.onResizeEnd(
(this.state.vert) ? this.state.width : this.state.height
);
}
}
/**
* Adjust size of the controlled panel. Depending on the current
* orientation we either remember the width or height of
* the splitter box.
*/
onMove({ movementX, movementY }) {
const node = ReactDOM.findDOMNode(this);
const doc = node.ownerDocument;
if (this.props.endPanelControl) {
// For the end panel we need to increase the width/height when the
// movement is towards the left/top.
movementX = -movementX;
movementY = -movementY;
}
if (this.state.vert) {
const isRtl = doc.dir === "rtl";
if (isRtl) {
// In RTL we need to reverse the movement again -- but only for vertical
// splitters
movementX = -movementX;
}
this.setState((state, props) => ({
width: state.width + movementX,
}));
} else {
this.setState((state, props) => ({
height: state.height + movementY,
}));
}
}
// Rendering
preparePanelStyles() {
const vert = this.state.vert;
const {
minSize,
maxSize,
startPanelCollapsed,
endPanelControl,
endPanelCollapsed,
} = this.props;
let leftPanelStyle, rightPanelStyle;
// Set proper size for panels depending on the current state.
if (vert) {
let startWidth = endPanelControl ? null : this.state.width,
endWidth = endPanelControl ? this.state.width : null;
leftPanelStyle = {
maxWidth: endPanelControl ? null : maxSize,
minWidth: endPanelControl ? null : minSize,
width: startPanelCollapsed ? 0 : startWidth,
};
rightPanelStyle = {
maxWidth: endPanelControl ? maxSize : null,
minWidth: endPanelControl ? minSize : null,
width: endPanelCollapsed ? 0 : endWidth,
};
} else {
let startHeight = endPanelControl ? null : this.state.height,
endHeight = endPanelControl ? this.state.height : null;
leftPanelStyle = {
maxHeight: endPanelControl ? null : maxSize,
minHeight: endPanelControl ? null : minSize,
height: endPanelCollapsed ? maxSize : startHeight,
};
rightPanelStyle = {
maxHeight: endPanelControl ? maxSize : null,
minHeight: endPanelControl ? minSize : null,
height: startPanelCollapsed ? maxSize : endHeight,
};
}
return { leftPanelStyle, rightPanelStyle };
}
render() {
const vert = this.state.vert;
const {
startPanelCollapsed,
startPanel,
endPanel,
endPanelControl,
splitterSize,
endPanelCollapsed,
} = this.props;
let style = Object.assign({}, this.props.style);
// Calculate class names list.
let classNames = ["split-box"];
classNames.push(vert ? "vert" : "horz");
if (this.props.className) {
classNames = classNames.concat(this.props.className.split(" "));
}
const { leftPanelStyle, rightPanelStyle } = this.preparePanelStyles();
// Calculate splitter size
let splitterStyle = {
flex: `0 0 ${splitterSize}px`,
};
return dom.div(
{
className: classNames.join(" "),
style: style,
},
!startPanelCollapsed
? dom.div(
{
className: endPanelControl ? "uncontrolled" : "controlled",
style: leftPanelStyle,
},
startPanel,
)
: null,
Draggable({
className: "splitter",
style: splitterStyle,
onStart: this.onStartMove,
onStop: this.onStopMove,
onMove: this.onMove,
}),
!endPanelCollapsed
? dom.div(
{
className: endPanelControl ? "controlled" : "uncontrolled",
style: rightPanelStyle,
},
endPanel,
)
: null,
);
}
}
module.exports = SplitBox;