forked from schrodinger/fixed-data-table-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedDataTableColumnReorderHandle.js
145 lines (126 loc) · 3.94 KB
/
FixedDataTableColumnReorderHandle.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
/**
* 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.
*
* This is to be used with the FixedDataTable. It is a header icon
* that allows you to reorder the corresponding column.
*
* @providesModule FixedDataTableColumnReorderHandle
* @typechecks
*/
import DOMMouseMoveTracker from 'DOMMouseMoveTracker';
import Locale from 'Locale';
import React from 'React';
import PropTypes from 'prop-types';
import FixedDataTableEventHelper from 'FixedDataTableEventHelper';
import clamp from 'clamp';
import cx from 'cx';
class FixedDataTableColumnReorderHandle extends React.PureComponent {
static propTypes = {
/**
* When resizing is complete this is called.
*/
onColumnReorderEnd: PropTypes.func,
/**
* Column key for the column being reordered.
*/
columnKey: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
/**
* Whether the reorder handle should respond to touch events or not.
*/
touchEnabled: PropTypes.bool,
}
state = /*object*/ {
dragDistance: 0
}
componentWillReceiveProps(/*object*/ newProps) {
}
componentWillUnmount() {
if (this._mouseMoveTracker) {
cancelAnimationFrame(this.frameId);
this.frameId = null;
this._mouseMoveTracker.releaseMouseMoves();
this._mouseMoveTracker = null;
}
}
render() /*object*/ {
var style = {
height: this.props.height,
};
return (
<div
className={cx({
'fixedDataTableCellLayout/columnReorderContainer': true,
'fixedDataTableCellLayout/columnReorderContainer/active': false,
})}
onMouseDown={this.onMouseDown}
onTouchStart={this.props.touchEnabled ? this.onMouseDown : null}
onTouchEnd={this.props.touchEnabled ? e => e.stopPropagation() : null}
onTouchMove={this.props.touchEnabled ? e => e.stopPropagation() : null}
style={style}>
</div>
);
}
onMouseDown = (event) => {
var targetRect = event.target.getBoundingClientRect();
var coordinates = FixedDataTableEventHelper.getCoordinatesFromEvent(event);
var mouseLocationInElement = coordinates.x - targetRect.left;
var mouseLocationInRelationToColumnGroup = mouseLocationInElement + event.target.parentElement.offsetLeft;
this._mouseMoveTracker = new DOMMouseMoveTracker(
this._onMove,
this._onColumnReorderEnd,
document.body,
this.props.touchEnabled
);
this._mouseMoveTracker.captureMouseMoves(event);
this.setState({
dragDistance: 0
});
this.props.onMouseDown({
columnKey: this.props.columnKey,
mouseLocation: {
dragDistance: 0,
inElement: mouseLocationInElement,
inColumnGroup: mouseLocationInRelationToColumnGroup
}
});
this._distance = 0;
this._animating = true;
this.frameId = requestAnimationFrame(this._updateState);
/**
* This prevents the rows from moving around when we drag the
* headers on touch devices.
*/
if(this.props.touchEnabled) {
event.stopPropagation();
}
}
_onMove = (/*number*/ deltaX) => {
this._distance = this.state.dragDistance + deltaX;
}
_onColumnReorderEnd = (/*boolean*/ cancelReorder) => {
this._animating = false;
cancelAnimationFrame(this.frameId);
this.frameId = null;
this._mouseMoveTracker.releaseMouseMoves();
this.props.columnReorderingData.cancelReorder = cancelReorder;
this.props.onColumnReorderEnd();
}
_updateState = () => {
if (this._animating) {
this.frameId = requestAnimationFrame(this._updateState)
}
this.setState({
dragDistance: this._distance
});
this.props.onColumnReorderMove(this._distance);
}
};
module.exports = FixedDataTableColumnReorderHandle;