-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathColumnsConfiguration.react.js
176 lines (161 loc) · 5.56 KB
/
ColumnsConfiguration.react.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
import React from 'react';
import { DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import Button from 'components/Button/Button.react';
import ColumnConfigurationItem from 'components/ColumnsConfiguration/ColumnConfigurationItem.react';
import styles from 'components/ColumnsConfiguration/ColumnsConfiguration.scss';
import Icon from 'components/Icon/Icon.react';
import Popover from 'components/Popover/Popover.react';
import Position from 'lib/Position';
const POPOVER_CONTENT_ID = 'columnsConfigurationPopover';
export default class ColumnsConfiguration extends React.Component {
constructor() {
super();
this.state = {
open: false,
};
this.entryRef = React.createRef();
}
componentDidMount() {
const rect = this.entryRef.current?.getBoundingClientRect();
this.maxWidth = rect.x + rect.width - 20 + 'px';
}
componentWillReceiveProps(props) {
if (props.schema !== this.props.schema) {
this.setState({
open: false,
});
}
}
toggle() {
this.setState({
open: !this.state.open,
});
}
showAll() {
let shouldReload = false;
const updatedOrder = this.props.order.map(field => {
if (!shouldReload && !field.cached) {
shouldReload = true;
}
return { ...field, visible: true };
});
this.props.handleColumnsOrder(updatedOrder, shouldReload);
}
hideAll() {
this.props.handleColumnsOrder(this.props.order.map(order => ({ ...order, visible: false })));
}
autoSort() {
const defaultOrder = ['objectId', 'createdAt', 'updatedAt', 'ACL'];
const order = {
default: [],
other: [],
};
for (const column of this.props.order) {
const index = defaultOrder.indexOf(column.name);
if (index !== -1) {
order.default[index] = column;
} else {
order.other.push(column);
}
}
this.props.handleColumnsOrder([
...order.default.filter(column => column),
...order.other.sort((a, b) => a.name.localeCompare(b.name)),
]);
}
render() {
const { handleColumnDragDrop, handleColumnsOrder, order, disabled } = this.props;
const title = (
<div className={styles.title} onClick={this.toggle.bind(this)}>
<Icon name="manage-columns" width={14} height={14} />
<span>Manage Columns</span>
</div>
);
let entry = (
<div className={styles.entry} onClick={this.toggle.bind(this)} ref={this.entryRef}>
<Icon name="manage-columns" width={14} height={14} />
<span>Manage Columns</span>
</div>
);
if (disabled) {
entry = (
<div className={styles.entry + ' ' + styles.disabled} onClick={null}>
<Icon name="manage-columns" width={14} height={14} />
<span>Manage Columns</span>
</div>
);
}
let popover = null;
if (this.state.open) {
popover = (
<Popover
fixed={true}
position={Position.inDocument(this.entryRef.current)}
onExternalClick={this.toggle.bind(this)}
contentId={POPOVER_CONTENT_ID}
>
<div className={styles.popover} id={POPOVER_CONTENT_ID}>
{title}
<div className={styles.body} style={{
maxWidth: this.maxWidth,
}}>
<div className={styles.columnConfigContainer}>
<DndProvider backend={HTML5Backend}>
{order.map(({ name, visible, ...rest }, index) => {
return (
<ColumnConfigurationItem
key={index}
index={index}
name={name}
visible={visible}
onChangeVisible={visible => {
const updatedOrder = [...order];
updatedOrder[index] = {
...rest,
name,
visible,
};
let shouldReload = visible;
// these fields are always cached as they are never excluded from server
// therefore no need to make another request.
if (
name === 'objectId' ||
name === 'createdAt' ||
name === 'updatedAt' ||
name === 'ACL'
) {
shouldReload = false;
}
if (this.props.className === '_User' && name === 'password') {
shouldReload = false;
}
if (updatedOrder[index].cached) {
shouldReload = false;
}
handleColumnsOrder(updatedOrder, shouldReload);
}}
handleColumnDragDrop={handleColumnDragDrop}
/>
);
})}
</DndProvider>
</div>
<div className={styles.footer}>
<Button color="white" value="Hide all" onClick={this.hideAll.bind(this)} />
<Button color="white" value="Show all" onClick={this.showAll.bind(this)} />
<Button color="white" value="Auto-sort" onClick={this.autoSort.bind(this)} />
</div>
</div>
</div>
</Popover>
);
}
return (
<>
{entry}
{popover}
</>
);
}
}