-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathUploadFieldItem.js
More file actions
420 lines (379 loc) · 9.02 KB
/
UploadFieldItem.js
File metadata and controls
420 lines (379 loc) · 9.02 KB
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
import i18n from 'i18n';
import React, { Component } from 'react';
import CONSTANTS from 'constants';
import fileShape from 'lib/fileShape';
import { fileSize } from 'lib/DataFormat';
import PropTypes from 'prop-types';
import FileStatusIcon from 'components/FileStatusIcon/FileStatusIcon';
class UploadFieldItem extends Component {
constructor(props) {
super(props);
this.handleRemove = this.handleRemove.bind(this);
this.handleItemClick = this.handleItemClick.bind(this);
this.handleView = this.handleView.bind(this);
}
/**
* Gets props for thumbnail
*
* @returns {Object}
*/
getThumbnailStyles() {
if (this.isImage() && (this.exists() || this.uploading())) {
const thumbnail = this.props.item.smallThumbnail || this.props.item.url || '';
return {
backgroundImage: `url(${thumbnail})`,
};
}
return {};
}
/**
* Retrieve list of thumbnail classes
*
* @returns {string}
*/
getThumbnailClassNames() {
const thumbnailClassNames = ['uploadfield-item__thumbnail'];
if (this.isImageSmallerThanThumbnail()) {
thumbnailClassNames.push('uploadfield-item__thumbnail--small');
}
return thumbnailClassNames.join(' ');
}
/**
* Retrieves class names for the item
*
* @returns {string}
*/
getItemClassNames() {
const category = this.props.item.category || 'none';
const itemClassNames = [
'fill-width',
'uploadfield-item',
`uploadfield-item--${category}`,
];
if (this.missing()) {
itemClassNames.push('uploadfield-item--missing');
}
if (this.hasError()) {
itemClassNames.push('uploadfield-item--error');
}
return itemClassNames.join(' ');
}
/**
* Checks if the component has an error set.
*
* @return {boolean}
*/
hasError() {
if (this.props.item.message) {
return this.props.item.message.type === 'error';
}
return false;
}
/**
* Determine if this is an image type
*
* @returns {Boolean}
*/
isImage() {
return this.props.item.category === 'image';
}
/**
* Validate that the file backing this record is not missing
*
* @returns {Boolean}
*/
exists() {
return this.props.item.exists;
}
/**
* Check if this item is in the process uploaded.
* If false this file was attached to this editor instead.
*
* @returns {boolean}
*/
uploading() {
return this.props.item.queuedId && !this.saved();
}
/**
* Check if this item has been successfully uploaded.
* Excludes items not uploaded in this request.
*
* @returns {Boolean}
*/
complete() {
// Uploading is complete if saved with a DB id
return this.props.item.queuedId && this.saved();
}
/**
* Check if this item has been saved, either in this request or in a prior one
*
* @return {Boolean}
*/
saved() {
return this.props.item.id > 0;
}
/**
* Check if this item should have a file, but is missing.
*
* @return {Boolean}
*/
missing() {
return !this.exists() && this.saved();
}
/**
* Determine that this record is an image, and the thumbnail is smaller than the given
* thumbnail area
*
* @returns {boolean}
*/
isImageSmallerThanThumbnail() {
if (!this.isImage() || this.missing()) {
return false;
}
const width = this.props.item.width;
const height = this.props.item.height;
// Note: dimensions will be null if the back-end image is lost
return (
height
&& width
&& height < CONSTANTS.SMALL_THUMBNAIL_HEIGHT
&& width < CONSTANTS.SMALL_THUMBNAIL_WIDTH
);
}
/**
* Handles remove (x) button click
*
* @param {Object} event
*/
handleRemove(event) {
event.preventDefault();
if (this.props.onRemove) {
this.props.onRemove(event, this.props.item);
}
}
/**
* Handles edit button click
*
* @param {Object} event
*/
handleView(event) {
event.preventDefault();
if (this.props.onView) {
this.props.onView(event, this.props.item);
}
}
/**
* Handles click of an item
*
* @param {Object} event
*/
handleItemClick(event) {
event.preventDefault();
if (this.props.onItemClick) {
this.props.onItemClick(event, this.props.item);
}
}
renderStatus() {
if (this.props.item.draft) {
return (
<span className="uploadfield-item__status">{i18n._t('File.DRAFT', 'Draft')}</span>
);
} else if (this.props.item.modified) {
return (
<span className="uploadfield-item__status">{i18n._t('File.MODIFIED', 'Modified')}</span>
);
}
return null;
}
/**
* Returns markup for an error message if one is set.
*
* @returns {Object}
*/
renderErrorMessage() {
let message = null;
if (this.hasError()) {
message = this.props.item.message.value;
} else if (this.missing()) {
message = i18n._t('AssetAdmin.FILE_MISSING', 'File cannot be found');
}
if (message !== null) {
return (
<span className="uploadfield-item__error-message" title={message}>
{message}
</span>
);
}
return null;
}
/**
* Gets upload progress bar
*
* @returns {object}
*/
renderProgressBar() {
const progressBarProps = {
className: 'uploadfield-item__progress-bar',
style: {
width: `${this.props.item.progress}%`,
},
};
if (!this.hasError() && this.props.item.queuedId) {
if (this.complete()) {
return (
<div className="uploadfield-item__complete-icon" />
);
}
return (
<div className="uploadfield-item__upload-progress">
<div {...progressBarProps} />
</div>
);
}
return null;
}
/**
* Gets the remove item button
*
* @returns {object}
*/
renderRemoveButton() {
if (!this.props.canEdit) {
return null;
}
const classes = [
'btn',
'uploadfield-item__remove-btn',
'btn-secondary',
'btn--no-text',
'font-icon-cancel',
'btn--icon-md',
].join(' ');
return (
<button
className={classes}
onClick={this.handleRemove}
/>
);
}
/**
* Gets the edit item button
*
* @returns {object}
*/
renderViewButton() {
if (!this.props.canEdit || !this.props.item.id) {
return null;
}
const classes = [
'btn',
'uploadfield-item__view-btn',
'btn-secondary',
'btn--no-text',
'font-icon-eye',
'btn--icon-md',
].join(' ');
return (
<button
className={classes}
onClick={this.handleView}
/>
);
}
/**
* @param {Object} item
* @returns {*}
*/
renderRestrictedAccess(item) {
const { id, hasRestrictedAccess } = item;
const attrs = {
fileID: id,
placement: 'top',
hasRestrictedAccess
};
return <FileStatusIcon {...attrs} />;
}
/**
* @param {Object} item
* @returns {*}
*/
renderTrackedFormUpload(item) {
const { id, isTrackedFormUpload, hasRestrictedAccess } = item;
const attrs = {
fileID: id,
placement: 'top',
isTrackedFormUpload,
hasRestrictedAccess
};
return <FileStatusIcon {...attrs} />;
}
/**
* Get file title / metadata block
*
* @returns {object}
*/
renderFileDetails() {
const item = this.props.item;
let size = '';
if (item.size) {
size = `, ${fileSize(item.size)}`;
}
return (
<div className="uploadfield-item__details fill-height flexbox-area-grow">
<div className="fill-width">
<span className="uploadfield-item__title flexbox-area-grow">
{item.title}
</span>
</div>
<div className="fill-width uploadfield-item__meta">
<span className="uploadfield-item__specs">
{item.extension}{size}
</span>
{this.renderStatus()}
{item.hasRestrictedAccess && this.renderRestrictedAccess(item)}
{item.isTrackedFormUpload && this.renderTrackedFormUpload(item)}
</div>
<div className="fill-width">
{this.renderErrorMessage()}
</div>
</div>
);
}
renderThumbnail() {
return (
<div
className={this.getThumbnailClassNames()}
style={this.getThumbnailStyles()}
onClick={this.handleItemClick}
role="button"
tabIndex={this.props.onItemClick ? 0 : -1}
/>
);
}
/**
*
* @returns {object}
*/
render() {
const fieldName = `${this.props.name}[Files][]`;
return (
<div className={this.getItemClassNames()}>
<input type="hidden" value={this.props.item.id} name={fieldName} />
{this.renderThumbnail()}
{this.renderFileDetails()}
{this.renderProgressBar()}
{this.renderViewButton()}
{this.renderRemoveButton()}
</div>
);
}
}
UploadFieldItem.propTypes = {
canEdit: PropTypes.bool,
name: PropTypes.string.isRequired,
item: fileShape,
onRemove: PropTypes.func,
onItemClick: PropTypes.func,
onView: PropTypes.func,
};
export default UploadFieldItem;