-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBrowserToolbar.react.js
350 lines (336 loc) · 11.8 KB
/
BrowserToolbar.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import BrowserFilter from 'components/BrowserFilter/BrowserFilter.react';
import BrowserMenu from 'components/BrowserMenu/BrowserMenu.react';
import Icon from 'components/Icon/Icon.react';
import MenuItem from 'components/BrowserMenu/MenuItem.react';
import prettyNumber from 'lib/prettyNumber';
import React, { useRef } from 'react';
import Separator from 'components/BrowserMenu/Separator.react';
import styles from 'dashboard/Data/Browser/Browser.scss';
import Toolbar from 'components/Toolbar/Toolbar.react';
import SecurityDialog from 'dashboard/Data/Browser/SecurityDialog.react';
import ColumnsConfiguration from 'components/ColumnsConfiguration/ColumnsConfiguration.react'
import SecureFieldsDialog from 'dashboard/Data/Browser/SecureFieldsDialog.react';
import LoginDialog from 'dashboard/Data/Browser/LoginDialog.react';
import Toggle from 'components/Toggle/Toggle.react';
let BrowserToolbar = ({
className,
classNameForEditors,
count,
perms,
schema,
filters,
selection,
relation,
setCurrent,
onFilterChange,
onAddColumn,
onAddRow,
onAddRowWithModal,
onAddClass,
onEditSelectedRow,
onAttachRows,
onAttachSelectedRows,
onCloneSelectedRows,
onExportSelectedRows,
onExportSchema,
onExport,
onRemoveColumn,
onDeleteRows,
onDropClass,
onChangeCLP,
onRefresh,
onImport,
onEditPermissions,
hidePerms,
isUnique,
uniqueField,
handleColumnDragDrop,
handleColumnsOrder,
editCloneRows,
onCancelPendingEditRows,
order,
enableDeleteAllRows,
enableExportClass,
enableSecurityDialog,
enableColumnManipulation,
enableClassManipulation,
onShowPointerKey,
currentUser,
useMasterKey,
login,
logout,
toggleMasterKeyUsage,
}) => {
let selectionLength = Object.keys(selection).length;
let isPendingEditCloneRows = editCloneRows && editCloneRows.length > 0;
let details = [];
if (count !== undefined) {
if (count === 1) {
details.push('1 object');
} else {
details.push(prettyNumber(count) + ' objects');
}
}
if (!relation && !isUnique) {
if (perms && !hidePerms) {
let read = perms.get && perms.find && perms.get['*'] && perms.find['*'];
let write = perms.create && perms.update && perms.delete && perms.create['*'] && perms.update['*'] && perms.delete['*'];
if (read && write) {
details.push('Public Read and Write enabled');
} else if (read) {
details.push('Public Read enabled');
} else if (write) {
details.push('Public Write enabled');
}
}
}
let menu = null;
if (relation) {
menu = (
<BrowserMenu title='Edit' icon='edit-solid' setCurrent={setCurrent}>
<MenuItem
text={`Create ${relation.targetClassName} and attach`}
onClick={onAddRow}
/>
<MenuItem
text="Attach existing row"
onClick={onAttachRows}
/>
<Separator />
<MenuItem
disabled={selectionLength === 0}
text={selectionLength === 1 && !selection['*'] ? 'Detach this row' : 'Detach these rows'}
onClick={() => onDeleteRows(selection)}
/>
</BrowserMenu>
);
} else if (onAddRow) {
menu = (
<BrowserMenu title='Edit' icon='edit-solid' disabled={isUnique || isPendingEditCloneRows} setCurrent={setCurrent}>
<MenuItem text='Add a row' onClick={onAddRow} />
<MenuItem text='Add a row with modal' onClick={onAddRowWithModal} />
{enableColumnManipulation ? <MenuItem text='Add a column' onClick={onAddColumn} /> : <noscript />}
{enableClassManipulation ? <MenuItem text='Add a class' onClick={onAddClass} /> : <noscript />}
<Separator />
<MenuItem text='Change pointer key' onClick={onShowPointerKey} />
<MenuItem
disabled={selectionLength !== 1}
text={'Edit this row with modal'}
onClick={onEditSelectedRow}
/>
<Separator />
<MenuItem
disabled={!selectionLength}
text={`Attach ${selectionLength <= 1 ? 'this row' : 'these rows'} to relation`}
onClick={onAttachSelectedRows}
/>
<Separator />
<MenuItem
disabled={!selectionLength}
text={`Clone ${selectionLength <= 1 ? 'this row' : 'these rows'}`}
onClick={onCloneSelectedRows}
/>
<Separator />
<MenuItem
disabled={selectionLength === 0}
text={selectionLength === 1 && !selection['*'] ? 'Delete this row' : 'Delete these rows'}
onClick={() => onDeleteRows(selection)} />
{enableColumnManipulation ? <MenuItem text='Delete a column' onClick={onRemoveColumn} /> : <noscript />}
{enableDeleteAllRows ? <MenuItem text='Delete all rows' onClick={() => onDeleteRows({ '*': true })} /> : <noscript />}
{enableClassManipulation ? <MenuItem text='Delete this class' onClick={onDropClass} /> : <noscript />}
{enableExportClass ? <Separator /> : <noscript />}
{enableExportClass ? <MenuItem text='Export this data' onClick={onExport} /> : <noscript />}
</BrowserMenu>
);
}
let subsection = className;
if (relation) {
subsection = `'${relation.key}' on ${relation.parent.id}`;
} else if (subsection.length > 30) {
subsection = subsection.substr(0, 30) + '\u2026';
}
const classes = [styles.toolbarButton];
let onClick = onAddRow;
if (isUnique) {
classes.push(styles.toolbarButtonDisabled);
onClick = null;
}
if (isPendingEditCloneRows) {
classes.push(styles.toolbarButtonDisabled);
onClick = null;
}
const columns = {};
const userPointers = [];
const schemaSimplifiedData = {};
const classSchema = schema.data.get('classes').get(classNameForEditors);
if (classSchema) {
classSchema.forEach(({ type, targetClass }, col) => {
schemaSimplifiedData[col] = {
type,
targetClass,
};
columns[col] = { type, targetClass };
if (col === 'objectId' || isUnique && col !== uniqueField) {
return;
}
if ((type ==='Pointer' && targetClass === '_User') || type === 'Array' ) {
userPointers.push(col);
}
});
}
let clpDialogRef = useRef(null);
let protectedDialogRef = useRef(null);
let loginDialogRef = useRef(null);
const showCLP = ()=> clpDialogRef.current.handleOpen();
const showProtected = () => protectedDialogRef.current.handleOpen();
const showLogin = () => loginDialogRef.current.handleOpen();
return (
<Toolbar
relation={relation}
filters={filters}
section={relation ? `Relation <${relation.targetClassName}>` : 'Class'}
subsection={subsection}
details={details.join(' \u2022 ')}
>
{onAddRow && (
<a className={classes.join(' ')} onClick={onClick}>
<Icon name="plus-solid" width={14} height={14} />
<span>Add Row</span>
</a>
)}
{onAddRow && <div className={styles.toolbarSeparator} />}
<ColumnsConfiguration
handleColumnsOrder={handleColumnsOrder}
handleColumnDragDrop={handleColumnDragDrop}
order={order}
disabled={isPendingEditCloneRows}
className={classNameForEditors}
/>
<div className={styles.toolbarSeparator} />
{onAddRow && (
<LoginDialog
ref={loginDialogRef}
currentUser={currentUser}
login={login}
logout={logout}
/>
)}
{onAddRow && (
<BrowserMenu
setCurrent={setCurrent}
title={currentUser ? 'Browsing' : 'Browse'}
icon="users-solid"
active={!!currentUser}
disabled={isPendingEditCloneRows}
>
<MenuItem text={currentUser ? 'Switch User' : 'As User'} onClick={showLogin} />
{currentUser ? <MenuItem text={<span>Use Master Key <Toggle type={Toggle.Types.HIDE_LABELS} value={useMasterKey} onChange={toggleMasterKeyUsage} switchNoMargin={true} additionalStyles={{ display: 'inline', lineHeight: 0, margin: 0, paddingLeft: 5 }} /></span>} onClick={toggleMasterKeyUsage} /> : <noscript />}
{currentUser ? <MenuItem text={<span>Stop browsing (<b>{currentUser.get('username')}</b>)</span>} onClick={logout} /> : <noscript />}
</BrowserMenu>
)}
{onAddRow && <div className={styles.toolbarSeparator} />}
{onAddRow && (
<BrowserMenu title='Export' icon='down-solid' disabled={isUnique || isPendingEditCloneRows} setCurrent={setCurrent}>
<MenuItem
disabled={!selectionLength}
text={`Export ${selectionLength} selected ${selectionLength <= 1 ? 'row' : 'rows'}`}
onClick={() => onExportSelectedRows(selection)}
/>
<MenuItem
text={'Export all rows'}
onClick={() => onExportSelectedRows({ '*': true })}
/>
<MenuItem
text={'Export schema'}
onClick={() => onExportSchema()}
/>
</BrowserMenu>
)}
{onAddRow && <div className={styles.toolbarSeparator} />}
<a className={classes.join(' ')} onClick={isPendingEditCloneRows ? null : onImport}>
<Icon name="up-solid" width={14} height={14} />
<span>Import</span>
</a>
<div className={styles.toolbarSeparator} />
<a className={classes.join(' ')} onClick={isPendingEditCloneRows ? null : onRefresh}>
<Icon name="refresh-solid" width={14} height={14} />
<span>Refresh</span>
</a>
<div className={styles.toolbarSeparator} />
<BrowserFilter
setCurrent={setCurrent}
schema={schemaSimplifiedData}
filters={filters}
onChange={onFilterChange}
className={classNameForEditors}
blacklistedFilters={onAddRow ? [] : ['unique']}
disabled={isPendingEditCloneRows}
/>
{onAddRow && <div className={styles.toolbarSeparator} />}
{perms && enableSecurityDialog ? (
<SecurityDialog
ref={clpDialogRef}
disabled={!!relation || !!isUnique}
perms={perms}
columns={columns}
className={classNameForEditors}
onChangeCLP={onChangeCLP}
userPointers={userPointers}
title="ClassLevelPermissions"
icon="locked-solid"
onEditPermissions={onEditPermissions}
/>
) : (
<noscript />
)}
<SecureFieldsDialog
ref={protectedDialogRef}
columns={columns}
disabled={!!relation || !!isUnique}
perms={perms}
className={classNameForEditors}
onChangeCLP={onChangeCLP}
userPointers={userPointers}
title='ProtectedFields'
icon='locked-solid'
onEditPermissions={onEditPermissions}
/>
{enableSecurityDialog ? (
<BrowserMenu
setCurrent={setCurrent}
title="Security"
icon="locked-solid"
disabled={!!relation || !!isUnique || isPendingEditCloneRows}
>
<MenuItem text={'Class Level Permissions'} onClick={showCLP} />
<MenuItem text={'Protected Fields'} onClick={showProtected} />
</BrowserMenu>
) : (
<noscript />
)}
{enableSecurityDialog ? (
<div className={styles.toolbarSeparator} />
) : (
<noscript />
)}
{menu}
{editCloneRows && editCloneRows.length > 0 && <div className={styles.toolbarSeparator} />}
{editCloneRows && editCloneRows.length > 0 && (
<BrowserMenu title="Clone" icon="clone-icon">
<MenuItem
text={'Cancel all pending rows'}
onClick={onCancelPendingEditRows}
/>
</BrowserMenu>
)}
</Toolbar>
);
};
export default BrowserToolbar;