-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBrowserCell.react.js
437 lines (396 loc) · 15.5 KB
/
BrowserCell.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* 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 * as Filters from 'lib/Filters';
import { List, Map } from 'immutable';
import { dateStringUTC } from 'lib/DateUtils';
import getFileName from 'lib/getFileName';
import Parse from 'parse';
import Pill from 'components/Pill/Pill.react';
import React, { Component } from 'react';
import styles from 'components/BrowserCell/BrowserCell.scss';
import baseStyles from 'stylesheets/base.scss';
import * as ColumnPreferences from 'lib/ColumnPreferences';
export default class BrowserCell extends Component {
constructor() {
super();
this.cellRef = React.createRef();
this.copyableValue = undefined;
this.state = {
showTooltip: false,
content: null,
classes: []
};
}
renderCellContent() {
let content = this.props.value;
let isNewRow = this.props.row < 0;
this.copyableValue = content;
let classes = [styles.cell, baseStyles.unselectable];
if (this.props.hidden) {
content = this.props.value !== undefined || !isNewRow ? '(hidden)' : this.props.isRequired ? '(required)' : '(undefined)';
classes.push(styles.empty);
} else if (this.props.value === undefined) {
if (this.props.type === 'ACL') {
this.copyableValue = content = 'Public Read + Write';
} else {
this.copyableValue = content = '(undefined)';
classes.push(styles.empty);
}
content = isNewRow && this.props.isRequired && this.props.value === undefined ? '(required)' : content;
} else if (this.props.value === null) {
this.copyableValue = content = '(null)';
classes.push(styles.empty);
} else if (this.props.value === '') {
content = <span> </span>;
classes.push(styles.empty);
} else if (this.props.type === 'Pointer') {
const defaultPointerKey = ColumnPreferences.getPointerDefaultKey(this.props.appId, this.props.value.className);
let dataValue = this.props.value.id;
if( defaultPointerKey !== 'objectId' ) {
dataValue = this.props.value.get(defaultPointerKey);
if ( dataValue && typeof dataValue === 'object' ){
if ( dataValue instanceof Date ) {
dataValue = dataValue.toLocaleString();
}
else {
if ( !this.props.value.id ) {
dataValue = this.props.value.id;
} else {
dataValue = '(undefined)';
}
}
}
if ( !dataValue ) {
if ( this.props.value.id ) {
dataValue = this.props.value.id;
} else {
dataValue = '(undefined)';
}
}
}
if (this.props.value && this.props.value.__type) {
const object = new Parse.Object(this.props.value.className);
object.id = this.props.value.objectId;
this.props.value = object;
}
content = this.props.onPointerClick ? (
<Pill value={ dataValue } onClick={this.props.onPointerClick.bind(undefined, this.props.value)} followClick={true} shrinkablePill />
) : (
dataValue
);
this.copyableValue = this.props.value.id;
}
else if (this.props.type === 'Array') {
if ( this.props.value[0] && typeof this.props.value[0] === 'object' && this.props.value[0].__type === 'Pointer' && typeof this.props.onPointerClick === 'function' ) {
const array = [];
this.props.value.map( (v, i) => {
if ( typeof v !== 'object' || v.__type !== 'Pointer' ) {
throw new Error('Invalid type found in pointer array');
}
const object = new Parse.Object(v.className);
object.id = v.objectId;
array.push(
<Pill key={i} value={v.objectId} onClick={this.props.onPointerClick.bind(undefined, object)} followClick={true} shrinkablePill />
);
});
this.copyableValue = content = <ul>
{ array.map( a => <li>{a}</li>) }
</ul>
if ( array.length > 1 ) {
classes.push(styles.hasMore);
}
}
else {
this.copyableValue = content = JSON.stringify(this.props.value);
}
}
else if (this.props.type === 'Date') {
if (typeof value === 'object' && this.props.value.__type) {
this.props.value = new Date(this.props.value.iso);
} else if (typeof value === 'string') {
this.props.value = new Date(this.props.value);
}
this.copyableValue = content = dateStringUTC(this.props.value);
} else if (this.props.type === 'Boolean') {
this.copyableValue = content = this.props.value ? 'True' : 'False';
} else if (this.props.type === 'Object' || this.props.type === 'Bytes') {
this.copyableValue = content = JSON.stringify(this.props.value);
} else if (this.props.type === 'File') {
const fileName = this.props.value.url?.() ? getFileName(this.props.value) : 'Uploading\u2026';
content = <Pill value={fileName} fileDownloadLink={this.props.value.url?.()} shrinkablePill />;
this.copyableValue = fileName;
} else if (this.props.type === 'ACL') {
let pieces = [];
let json = this.props.value.toJSON();
if (Object.prototype.hasOwnProperty.call(json, '*')) {
if (json['*'].read && json['*'].write) {
pieces.push('Public Read + Write');
} else if (json['*'].read) {
pieces.push('Public Read');
} else if (json['*'].write) {
pieces.push('Public Write');
}
}
for (let role in json) {
if (role !== '*') {
pieces.push(role);
}
}
if (pieces.length === 0) {
pieces.push('Master Key Only');
}
this.copyableValue = content = pieces.join(', ');
} else if (this.props.type === 'GeoPoint') {
this.copyableValue = content = `(${this.props.value.latitude}, ${this.props.value.longitude})`;
} else if (this.props.type === 'Polygon') {
this.copyableValue = content = this.props.value.coordinates.map(coord => `(${coord})`)
} else if (this.props.type === 'Relation') {
content = this.props.setRelation ? (
<div style={{ textAlign: 'center' }}>
<Pill onClick={() => this.props.setRelation(this.props.value)} value='View relation' followClick={true} shrinkablePill />
</div>
) : (
'Relation'
);
this.copyableValue = undefined;
}
this.onContextMenu = this.onContextMenu.bind(this);
if (this.props.markRequiredField && this.props.isRequired && this.props.value == null) {
classes.push(styles.required);
}
this.setState({ ...this.state, content, classes })
}
componentDidUpdate(prevProps) {
if ( this.props.value !== prevProps.value ) {
this.renderCellContent();
this.props.value._previousSave
?.then(() => this.renderCellContent())
?.catch(err => console.log(err))
}
if (this.props.current) {
const node = this.cellRef.current;
const { setRelation } = this.props;
const { left, right, bottom, top } = node.getBoundingClientRect();
// Takes into consideration Sidebar width when over 980px wide.
// If setRelation is undefined, DataBrowser is used as ObjectPicker, so it does not have a sidebar.
const leftBoundary = window.innerWidth > 980 && setRelation ? 300 : 0;
// BrowserToolbar + DataBrowserHeader height
const topBoundary = 126;
if (left < leftBoundary || right > window.innerWidth) {
node.scrollIntoView({ block: 'nearest', inline: 'start' });
} else if (top < topBoundary || bottom > window.innerHeight) {
node.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
if (!this.props.hidden) {
this.props.setCopyableValue(this.copyableValue);
}
}
if (prevProps.current !== this.props.current) {
this.setState({ showTooltip: false });
}
}
shouldComponentUpdate(nextProps, nextState) {
if (nextState.showTooltip !== this.state.showTooltip || nextState.content !== this.state.content ) {
return true;
}
const shallowVerifyProps = [...new Set(Object.keys(this.props).concat(Object.keys(nextProps)))]
.filter(propName => propName !== 'value');
if (shallowVerifyProps.some(propName => this.props[propName] !== nextProps[propName])) {
return true;
}
const { value } = this.props;
const { value: nextValue } = nextProps;
if (typeof value !== typeof nextValue) {
return true;
}
const isRefDifferent = value !== nextValue;
if (isRefDifferent && typeof value === 'object') {
return JSON.stringify(value) !== JSON.stringify(nextValue);
}
return isRefDifferent;
}
//#region Cell Context Menu related methods
onContextMenu(event) {
if (event.type !== 'contextmenu') { return; }
event.preventDefault();
const { field, hidden, onSelect, setCopyableValue, setContextMenu, row, col } = this.props;
onSelect({ row, col });
setCopyableValue(hidden ? undefined : this.copyableValue);
const available = Filters.availableFilters(this.props.simplifiedSchema, this.props.filters, Filters.BLACKLISTED_FILTERS);
const constraints = available && available[field];
const { pageX, pageY } = event;
const menuItems = this.getContextMenuOptions(constraints);
menuItems.length && setContextMenu(pageX, pageY, menuItems);
}
getContextMenuOptions(constraints) {
let { onEditSelectedRow, readonly } = this.props;
const contextMenuOptions = [];
const setFilterContextMenuOption = this.getSetFilterContextMenuOption(constraints);
setFilterContextMenuOption && contextMenuOptions.push(setFilterContextMenuOption);
const addFilterContextMenuOption = this.getAddFilterContextMenuOption(constraints);
addFilterContextMenuOption && contextMenuOptions.push(addFilterContextMenuOption);
const relatedObjectsContextMenuOption = this.getRelatedObjectsContextMenuOption();
relatedObjectsContextMenuOption && contextMenuOptions.push(relatedObjectsContextMenuOption);
!readonly && onEditSelectedRow && contextMenuOptions.push({
text: 'Edit row',
callback: () => {
let { objectId, onEditSelectedRow } = this.props;
onEditSelectedRow(true, objectId);
}
});
if (this.props.type === 'Pointer') {
onEditSelectedRow && contextMenuOptions.push({
text: 'Open pointer in new tab',
callback: () => {
let { value, onPointerCmdClick } = this.props;
onPointerCmdClick(value);
}
});
}
return contextMenuOptions;
}
getSetFilterContextMenuOption(constraints) {
if (constraints) {
return {
text: 'Set filter...', items: constraints.map(constraint => {
const definition = Filters.Constraints[constraint];
const copyableValue = String(this.copyableValue);
// Smart ellipsis for value - if it's long trim it in the middle: Lorem ipsum dolor si... aliqua
const value =
copyableValue.length < 30
? copyableValue
: `${copyableValue.substr(0, 20)}...${copyableValue.substr(
copyableValue.length - 7
)}`;
const text = `${this.props.field} ${definition.name}${
definition.comparable ? ' ' + value : ''
}`;
return {
text,
callback: this.pickFilter.bind(this, constraint)
};
})
};
}
}
getAddFilterContextMenuOption(constraints) {
if (constraints && this.props.filters && this.props.filters.size > 0) {
return {
text: 'Add filter...', items: constraints.map(constraint => {
const definition = Filters.Constraints[constraint];
const text = `${this.props.field} ${definition.name}${definition.comparable ? (' ' + this.copyableValue) : ''}`;
return {
text,
callback: this.pickFilter.bind(this, constraint, true)
};
})
};
}
}
/**
* Returns "Get related records from..." context menu item if cell holds a Pointer
* or objectId and there's a class in relation.
*/
getRelatedObjectsContextMenuOption() {
const { value, schema, onPointerClick } = this.props;
const pointerClassName = (value && value.className)
|| (this.props.field === 'objectId' && this.props.className);
if (pointerClassName) {
const relatedRecordsMenuItem = { text: 'Get related records from...', items: [] };
schema.data.get('classes').sortBy((v, k) => k).forEach((cl, className) => {
cl.forEach((column, field) => {
if (column.targetClass !== pointerClassName) { return; }
relatedRecordsMenuItem.items.push({
text: `${className}`, subtext: `${field}`, callback: () => {
let relatedObject = value;
if (this.props.field === 'objectId') {
relatedObject = new Parse.Object(pointerClassName);
relatedObject.id = value;
}
onPointerClick({ className, id: relatedObject.toPointer(), field })
}
})
});
});
return relatedRecordsMenuItem.items.length ? relatedRecordsMenuItem : undefined;
}
}
pickFilter(constraint, addToExistingFilter) {
const definition = Filters.Constraints[constraint];
const { filters, type, value, field } = this.props;
const newFilters = addToExistingFilter ? filters : new List();
let compareTo;
if (definition.comparable) {
switch (type) {
case 'Pointer':
compareTo = value.toPointer()
break;
case 'Date':
compareTo = value.__type ? value : {
__type: 'Date',
iso: value
};
break;
default:
compareTo = value;
break;
}
}
this.props.onFilterChange(newFilters.push(new Map({
field,
constraint,
compareTo
})));
}
componentDidMount(){
this.renderCellContent();
}
//#endregion
render() {
let { type, value, hidden, width, current, onSelect, onEditChange, setCopyableValue, onPointerCmdClick, row, col, field, onEditSelectedRow, isRequired, markRequiredFieldRow } = this.props;
let classes = [...this.state.classes];
if ( current ) {
classes.push(styles.current);
}
if (markRequiredFieldRow === row && isRequired && value == null) {
classes.push(styles.required);
}
return <span
ref={this.cellRef}
className={classes.join(' ')}
style={{ width }}
onClick={(e) => {
if (e.metaKey === true && type === 'Pointer') {
onPointerCmdClick(value);
}
else {
onSelect({ row, col });
setCopyableValue(hidden ? undefined : this.copyableValue);
}
}}
onDoubleClick={() => {
// Since objectId can't be edited, double click event opens edit row dialog
if (field === 'objectId' && onEditSelectedRow) {
onEditSelectedRow(true, value);
} else if (type !== 'Relation') {
onEditChange(true)
}
}}
onTouchEnd={e => {
if (current && type !== 'Relation') {
// The touch event may trigger an unwanted change in the column value
if (['ACL', 'Boolean', 'File'].includes(type)) {
e.preventDefault();
}
}}}
onContextMenu={this.onContextMenu.bind(this)}
>
{this.state.content}
</span>
}
}