-
Notifications
You must be signed in to change notification settings - Fork 289
/
ResizerKnob.js
243 lines (214 loc) · 6.29 KB
/
ResizerKnob.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
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
/**
* Copyright Schrodinger, LLC
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ResizerKnob
* @typechecks
* @file Contains resizing logic
*/
import React from 'react';
import cx from '../../vendor_upstream/stubs/cx';
import FixedDataTableEventHelper from '../../FixedDataTableEventHelper';
import ResizerLine from './ResizerLine';
import clamp from '../../vendor_upstream/core/clamp';
import DOMMouseMoveTracker from '../../vendor_upstream/dom/DOMMouseMoveTracker';
import PropTypes from 'prop-types';
class ResizerKnob extends React.PureComponent {
initialState = {
/**
* @type {boolean} Set true when column resizing starts. It is used to make ResizerLine visible.
*/
isColumnResizing: false,
/**
* @type {number} X coordinate of ResizerLine during resizing. It is passed do to ResizerLine to render at appropriate position.
*/
currentMouseXCoordinate: 0,
/**
* @type {number} Total displacement of mouse calculated from initial position when resizing started
*/
totalDisplacement: 0,
/**
* @type {number} Top position of ResizerKnow. It is passed to ResizerLine to render at appropriate position.
*/
top: 0,
};
state = { ...this.initialState };
/**
* Ref to ResizerKnob
* @type {HTMLDivElement}
*/
curRef = null;
/**
*
* @type {DOMMouseMoveTracker}
*/
mouseMoveTracker = null;
constructor(props) {
super(props);
}
componentDidMount() {
this.setState({
top: this.curRef.getBoundingClientRect().top,
});
}
render() {
const resizerKnobStyle = {
height: this.props.height,
};
const resizerLine = (
<ResizerLine
height={this.props.resizerLineHeight}
visible={!!this.state.isColumnResizing}
left={this.state.currentMouseXCoordinate}
top={this.state.top}
/>
);
return (
<div
className={cx('fixedDataTableCellLayout/columnResizerContainer')}
ref={(element) => (this.curRef = element)}
style={resizerKnobStyle}
onMouseDown={this.onMouseDown}
onTouchStart={this.props.touchEnabled ? this.onMouseDown : null}
onTouchEnd={this.props.touchEnabled ? this.suppressEvent : null}
onTouchMove={this.props.touchEnabled ? this.suppressEvent : null}
>
{resizerLine}
</div>
);
}
/**
* Registers event listeners for mouse tracking
* @param {MouseEvent} event
*/
initializeDOMMouseMoveTracker = (event) => {
this.mouseMoveTracker = new DOMMouseMoveTracker(
this.onMouseMove,
this.onMouseUp,
document.body,
this.props.touchEnabled
);
this.mouseMoveTracker.captureMouseMoves(event);
};
/**
* @param {MouseEvent} ev Mouse down event
*/
onMouseDown = (ev) => {
this.initializeDOMMouseMoveTracker(ev);
const initialMouseXCoordinate =
FixedDataTableEventHelper.getCoordinatesFromEvent(ev).x;
this.setState({
initialMouseXCoordinate,
isColumnResizing: true,
totalDisplacement: 0,
currentMouseXCoordinate: initialMouseXCoordinate,
});
};
onMouseUp = () => {
const { minWidth, maxWidth } = this.getMinMaxWidth();
const newWidth = clamp(
this.props.width +
this.state.totalDisplacement * (this.props.isRTL ? -1 : 1),
minWidth,
maxWidth
);
this.mouseMoveTracker.releaseMouseMoves();
this.setState(
{
isColumnResizing: false,
totalDisplacement: 0,
},
() => {
this.props.onColumnResizeEnd(newWidth, this.props.columnKey);
}
);
};
/**
*
* @param {number} displacementX Displacement of mouse along x-direction
*/
onMouseMove = (displacementX) => {
const { isRTL, width } = this.props;
const {
initialMouseXCoordinate,
totalDisplacement: previousTotalDisplacement,
} = this.state;
// displacementX is negative if movement is in left direction
const newTotalDisplacement = previousTotalDisplacement + displacementX;
let newResizerLineXCoordinate =
initialMouseXCoordinate + newTotalDisplacement;
const { minWidth, maxWidth } = this.getMinMaxWidth();
const currentWidth = width + newTotalDisplacement * (isRTL ? -1 : 1);
// Limit the resizer line to not move ahead or back of maxWidth and minWidth respectively
if (currentWidth < minWidth || currentWidth > maxWidth) {
// If new position is going out of bounds, instead of updating, use the previous value
newResizerLineXCoordinate = this.state.currentMouseXCoordinate;
}
this.setState({
totalDisplacement: newTotalDisplacement,
currentMouseXCoordinate: newResizerLineXCoordinate,
});
};
/**
* If minWidth and Width not given, mapping them to a valid range i.e. 0 to Number.MAX_SAFE_INTEGER
* @returns {{minWidth: number, maxWidth: number}}
*/
getMinMaxWidth = () => {
return {
minWidth: this.props.minWidth || 0,
maxWidth: this.props.maxWidth || Number.MAX_SAFE_INTEGER,
};
};
/**
* @param {Object} event
*/
suppressEvent = (event) => {
event.preventDefault();
event.stopPropagation();
};
}
ResizerKnob.propTypes = {
/**
* Optional prop that if specified on the `Column` will be passed to the
* cell. It can be used to uniquely identify which column is the cell is in.
*/
columnKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
/**
* The minimum width of the column.
*/
minWidth: PropTypes.number,
/**
* The maximum width of the column.
*/
maxWidth: PropTypes.number,
/**
* Outer width of the cell.
*/
width: PropTypes.number.isRequired,
/**
* Line of resizing line
*/
resizerLineHeight: PropTypes.number.isRequired,
/**
* Whether touch is enabled or not.
*/
touchEnabled: PropTypes.bool,
/**
* True if FDT has right to left orientation
*/
isRTL: PropTypes.bool,
/**
* Callback function which is called when reordering ends
*/
onColumnResizeEnd: PropTypes.func.isRequired,
/**
* Outer height of the cell.
*/
height: PropTypes.number.isRequired,
};
export default ResizerKnob;