-
Notifications
You must be signed in to change notification settings - Fork 6
/
mfu-fileitem-view.js
700 lines (627 loc) · 23.6 KB
/
mfu-fileitem-view.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
YUI.add('mfu-fileitem-view', function (Y) {
'use strict';
/**
* Provides the file item view
*
* @module mfu-fileitem-view
*/
Y.namespace('mfu');
const CLASS_HIDDEN = 'mfu--hidden';
const CLASS_DISCOVERYBAR_HIDDEN = 'is-menu-hidden';
const SELECTOR_BTN_EDIT = '.mfu-file-item__option[data-option="edit"]';
const SELECTOR_BTN_REMOVE = '.mfu-file-item__option[data-option="remove"]';
const SELECTOR_BTN_ABORT = '.mfu-file-item__option[data-option="abort"]';
const SELECTOR_PROGRESS_VALUE = '.mfu-file-item__progress-value';
const SELECTOR_PROGRESS_STATUS = '.mfu-file-item__progress-status';
const SELECTOR_UPLOAD_STATUS = '.mfu-file-item__upload-status';
const SELECTOR_STATUS_DONE = '.mfu-file-item__upload-state--done';
const SELECTOR_STATUS_INPROGRESS = '.mfu-file-item__upload-state--in-progress';
const SELECTOR_STATUS_ERROR = '.mfu-file-item__upload-state--error';
const SELECTOR_FILENAME_LABEL = '.mfu-file-item__filename';
const EVENTS = {};
EVENTS[SELECTOR_BTN_ABORT] = {tap: '_abortUpload'};
EVENTS[SELECTOR_BTN_EDIT] = {tap: '_fireEditContentRequestEvent'};
EVENTS[SELECTOR_BTN_REMOVE] = {tap: '_fireDeleteFileEvent'};
/**
* The File Item view
*
* @namespace mfu
* @class FileItemView
* @constructor
* @extends eZ.TemplateBasedView
*/
Y.mfu.FileItemView = Y.Base.create('mfuFileItemView', Y.eZ.TemplateBasedView, [Y.mfu.Helper.TextFormat], {
containerTemplate: '<li/>',
initializer: function () {
this._addDOMEventHandlers(EVENTS);
},
render: function () {
const container = this.get('container');
const filesize = this._formatFileSize(this.get('file').blob.size);
container.setHTML(this.template({
type: this._detectFileType(),
fileUploadDoneText: this.get('fileUploadDoneText').replace('{total}', filesize),
fileUploadStatusText: this._getUploadStatusText(0, filesize),
progressStatus: '0%',
filename: this._getFileName(),
}));
this.set('formattedFileSize', filesize);
this._fireUploadFileEvent();
return this;
},
/**
* Detects file type identifier. Only for rendering purposes.
*
* @method _detectFileType
* @protected
* @return {String} detected file type identifier
*/
_detectFileType: function () {
const filetype = this.get('file').blob.type;
let type = 'file';
if (filetype.indexOf('/pdf') > -1) {
type = 'pdf';
} else if (filetype.indexOf('video/') > -1) {
type = 'video';
} else if (filetype.indexOf('image/') > -1) {
type = 'image';
}
return type;
},
/**
* Sets `xhr` attribute value
*
* @method _setXhr
* @protected
* @param xhr {XMLHttpRequest} request object
*/
_setXhr: function (xhr) {
this._set('xhr', xhr);
},
/**
* Fires `mfuUploadFile` event
*
* @method _fireUploadFileEvent
*/
_fireUploadFileEvent: function () {
/**
* Event informs about a file to be uploaded.
* Listened by {{#crossLink "mfu.Plugin.FileUploadService"}}mfu.Plugin.FileUploadService{{/crossLink}}
*
* @event mfuUploadFile
* @param config {Object} config object
* @param config.file {File} File object
* @param config.onabort {Function} on abort callback
* @param config.onerror {Function} on error callback
* @param config.onload {Function} on load callback
* @param config.onprogress {Function} on progress callback
* @param config.ontimeout {Function} on timeout callback
* @param config.setXhrCallback {Function} a callback to return a created XHR object
* @param config.publishedCallback {Function} a callback to invoke when content is published
* @param config.fileTypeNotAllowedCallback {Function} a callback to invoke when an uploaded file content type is not allowed
* @param config.maxFileSizeExceededCallback {Function} a callback to invoke when an uploaded file size has exceeded max size
*/
this.fire('mfuUploadFile', {
file: this.get('file').blob,
onprogress: this._updateProgressStatus.bind(this),
onerror: this._showFileUploadError.bind(this),
ontimeout: this._showFileUploadError.bind(this),
onabort: this.destroy.bind(this, {remove: true}),
onload: this._showUploadedFileInfo.bind(this),
setXhrCallback: this._setXhr.bind(this),
publishedCallback: this._setContentInfo.bind(this),
fileTypeNotAllowedCallback: this._showFileTypeNotAllowedError.bind(this),
maxFileSizeExceededCallback: this._showMaxFileSizeExceededError.bind(this),
});
},
/**
* Displays the max file size exceeded error message
*
* @method _showMaxFileSizeExceededError
* @protected
*/
_showMaxFileSizeExceededError: function () {
const message = this.get('fileSizeExceededText')
.replace('{filesize}', this._formatFileSize(this.get('file').blob.size))
.replace('{filename}', this._getFileName());
this._uiShowErrorMessage(message);
},
/**
* Displays the File Type not allowed error message
*
* @method _showFileTypeNotAllowedError
* @protected
*/
_showFileTypeNotAllowedError: function () {
this._uiShowErrorMessage(this.get('fileTypeNotAllowedText').replace('{filename}', this._getFileName()));
},
/**
* Displays an error message in UI
*
* @method _uiShowErrorMessage
* @protected
* @param message {String} error message
*/
_uiShowErrorMessage: function (message) {
const container = this.get('container');
const errorStatus = container.one(SELECTOR_STATUS_ERROR);
errorStatus.removeClass(CLASS_HIDDEN);
errorStatus.setHTML(message);
container.one(SELECTOR_FILENAME_LABEL).addClass(CLASS_HIDDEN);
container.one(SELECTOR_STATUS_DONE).addClass(CLASS_HIDDEN);
container.one(SELECTOR_STATUS_INPROGRESS).addClass(CLASS_HIDDEN);
},
/**
* Update progress status information
*
* @method _updateProgressStatus
* @protected
* @param event {Object} event facade
* @param event.loaded {Number} uploaded file size
* @param event.total {Number} total file size to upload (it differs from actual file size)
*/
_updateProgressStatus: function (event) {
const container = this.get('container');
const fraction = event.loaded / event.total;
const totalFileSize = this.get('file').blob.size;
const uploadedSize = fraction * parseInt(totalFileSize, 10);
const value = parseInt(fraction * 100, 10) + '%';
container.one(SELECTOR_PROGRESS_VALUE).setStyle('width', value);
container.one(SELECTOR_PROGRESS_STATUS).setHTML(value);
container.one(SELECTOR_UPLOAD_STATUS).setHTML(
this._getUploadStatusText(
this._formatFileSize(uploadedSize),
this._formatFileSize(totalFileSize)
)
);
},
/**
* Gets uploaded status text
*
* @method _getUploadStatusText
* @protected
* @param loaded {Number} uploaded file size
* @param total {Number} total uploaded file size
* @return {String}
*/
_getUploadStatusText: function (loaded, total) {
return this.get('fileUploadStatusText').replace('{loaded}', loaded).replace('{total}', total);
},
/**
* Displays an error notification when a file upload request fails.
* Destroys a view.
*
* @method _showFileUploadError
* @protected
* @param event {Object} event facade
*/
_showFileUploadError: function (event) {
let errorText = this.get('fileUploadUnexpectedErrorText');
if (event.message) {
errorText = this.get('fileUploadErrorText')
.replace('{message}', `${this._getFileName()} - ${event.message}`);
} else if (event.target && event.target.hasOwnProperty('status') && event.target.hasOwnProperty('statusText')) {
let status = event.target.status ?
event.target.status :
'N/A';
let message = event.target.statusText ?
event.target.statusText :
this.get('fileUploadUknownContentTypeText');
errorText = this.get('fileUploadFailedText')
.replace('{statusCode}', status)
.replace('{statusText}', `${this._getFileName()} - ${message}`);
} else if (event.document) {
errorText = this.get('fileUploadFailedText')
.replace('{statusCode}', event.document.ErrorMessage.errorCode)
.replace('{statusText}', `${this._getFileName()} - ${event.document.ErrorMessage.errorDescription}`);
}
this._fireNotifyEvent({
text: errorText,
identifier: 'mfu-upload-failed-' + this._yuid,
state: 'error',
timeout: 0,
});
this._fireRefreshFilesListEvent();
this.destroy({remove: true});
},
/**
* Displays a final upload status information
*
* @method _showUploadedFileInfo
* @protected
* @param event {Object} event facade
*/
_showUploadedFileInfo: function (event) {
const container = this.get('container');
const filename = this._getFileName();
this._fireNotifyEvent({
text: this.get('fileStartPublishText').replace('{filename}', filename),
identifier: 'mfu-file-publishing-' + filename,
state: 'started',
timeout: 5,
});
container.one(SELECTOR_STATUS_DONE).removeClass(CLASS_HIDDEN);
container.one(SELECTOR_BTN_ABORT).addClass(CLASS_HIDDEN);
container.one(SELECTOR_STATUS_INPROGRESS).addClass(CLASS_HIDDEN);
},
/**
* Aborts a file upload
*
* @method _abortUpload
* @protected
*/
_abortUpload: function () {
const filename = this._getFileName();
const xhr = this.get('xhr');
if (!xhr || typeof xhr.abort !== 'function') {
this.destroy({remove: true});
return;
}
xhr.abort();
this._set('xhr');
this._fireNotifyEvent({
text: this.get('fileUploadAbortedText').replace('{filename}', filename),
identifier: 'mfu-file-upload-aborted-' + filename,
timeout: 10,
});
},
/**
* Gets file name
*
* @method _getFileName
* @protected
* @return {String} file name
*/
_getFileName: function () {
return this.get('file').blob.name;
},
/**
* Fires a `editContentRequest` event to redirect a user file edit
*
* @method _fireEditContentRequestEvent
* @protected
*/
_fireEditContentRequestEvent: function () {
const contentInfo = this.get('contentInfo');
document.body.classList.remove(CLASS_DISCOVERYBAR_HIDDEN);
/**
* Redirects to content editing in PlatformUI.
* Listened by {{#crossLink "eZ.Plugin.ContentEdit"}}eZ.Plugin.ContentEdit{{/crossLink}}
*
* @event editContentRequest
* @param config.contentInfo {eZ.ContentInfo} content info model
* @param config.languageCode {String} content language code
* @param config.contentType {eZ.ContentType} content type model
*/
this.fire('editContentRequest',{
contentInfo: contentInfo,
languageCode: contentInfo.get('mainLanguageCode'),
contentType: this.get('contentType'),
});
},
/**
* Fires a `mfuDeleteFile` event to delete a file
*
* @method _fireDeleteFileEvent
* @protected
*/
_fireDeleteFileEvent: function () {
const filename = this._getFileName();
const contentId = this.get('contentInfo').get('id');
this._fireNotifyEvent({
state: 'started',
text: this.get('fileStartDeleteText').replace('{filename}', filename),
identifier: 'mfu-delete-file-' + contentId,
timeout: 0,
});
/**
* Event informs about a file to be deleted
* Listened by {{#crossLink "mfu.Plugin.FileUploadService"}}mfu.Plugin.FileUploadService{{/crossLink}}
*
* @event mfuDeleteFile
* @param config {Object} config object
* @param config.contentId {String} content REST id
* @param config.deletedCallback {Function} a callback to invoke when content is deleted
*/
this.fire('mfuDeleteFile', {
contentId: contentId,
deletedCallback: this._handleFileDeletion.bind(this)
});
},
/**
* Handles file deletion
*
* @method _handleFileDeletion
* @protected
*/
_handleFileDeletion: function () {
this._fireNotifyEvent({
state: 'done',
text: this.get('fileDeletedText').replace('{filename}', this._getFileName()),
identifier: 'mfu-delete-file-' + this.get('contentInfo').get('id'),
timeout: 5,
});
this._fireRefreshFilesListEvent();
this.destroy({remove: true});
},
/**
* Sets published file content info
*
* @method _setContentInfo
* @protected
* @param contentInfo {eZ.ContentInfoModel} content info model
* @param contentType {eZ.ContentTypeModel} content type model
*/
_setContentInfo: function (contentInfo, contentType) {
const filename = this._getFileName();
this._set('contentInfo', contentInfo);
this._set('contentType', contentType);
this._showContentActionButtons();
this._fireFilePublishedEvent();
this._fireRefreshFilesListEvent();
this._fireNotifyEvent({
text: this.get('filePublishedText').replace('{filename}', filename),
identifier: 'mfu-file-publishing-' + filename,
state: 'done',
timeout: 5,
});
},
/**
* Fires file published event
*
* @method _fireFilePublishedEvent
* @protected
*/
_fireFilePublishedEvent: function () {
/**
* Informs the file has been succesfuly uploaded
* Listened by {{#crossLink "mfu.UploadPopupView"}}mfu.UploadPopupView{{/crossLink}}
*
* @event mfuFilePublished
*/
this.fire('mfuFilePublished');
},
/**
* Displays content action buttons, like an edit button, a remove button
*
* @method _showContentActionButtons
* @protected
* @return {mfu.FileItemView} the view itself
*/
_showContentActionButtons: function () {
const container = this.get('container');
container.one(SELECTOR_BTN_EDIT).removeClass(CLASS_HIDDEN);
container.one(SELECTOR_BTN_REMOVE).removeClass(CLASS_HIDDEN);
return this;
},
/**
* Fires `mfuRefreshList` event
*
* @method _fireRefreshFilesListEvent
* @protected
*/
_fireRefreshFilesListEvent: function () {
/**
* Event informs to refresh a list of files in the subitem box view
* Listened by {{#crossLink "mfu.Plugin.FileUploadView"}}mfu.Plugin.FileUploadView{{/crossLink}}
*
* @event mfuRefreshList
*/
this.fire('mfuRefreshList');
},
/**
* Fires `notify` event
*
* @method _fireNotifyEvent
* @protected
* @param notification {Object} notification config
*/
_fireNotifyEvent: function (notification) {
/**
* Displays a notification in the notification bar
* Listened by {{#crossLink "eZ.Plugin.NotificationHub"}}eZ.Plugin.NotificationHub{{/crossLink}}
*
* @event notify
* @param config.notification {Object} notification config
*/
this.fire('notify', {notification: notification});
},
destructor: function () {
const contentInfo = this.get('contentInfo');
const contentType = this.get('contentType');
if (contentInfo) {
contentInfo.destroy();
}
if (contentType) {
contentType.destroy();
}
}
}, {
ATTRS: {
/**
* File data hash
*
* @attribute file
* @type {Object}
*/
file: {},
/**
* Content info model
*
* @attribute contentInfo
* @type {eZ.ContentInfo}
* @readOnly
*/
contentInfo: {
readOnly: true,
},
/**
* Content type model
*
* @attribute contentType
* @type {eZ.ContentType}
* @readOnly
*/
contentType: {
readOnly: true,
},
/**
* XHR request object
*
* @attribute xhr
* @type {XMLHttpRequest}
* @readOnly
*/
xhr: {
readOnly: true,
},
/**
* File upload status text
*
* @attribute fileUploadStatusText
* @type {String}
* @readOnly
*/
fileUploadStatusText: {
valueFn: () => Y.eZ.trans('file.upload.status', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload done text
*
* @attribute fileUploadDoneText
* @type {String}
* @readOnly
*/
fileUploadDoneText: {
valueFn: () => Y.eZ.trans('file.upload.done', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* Start fiel publishing text
*
* @attribute fileStartPublishText
* @type {String}
* @readOnly
*/
fileStartPublishText: {
valueFn: () => Y.eZ.trans('publishing.file', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File published text
*
* @attribute filePublishedText
* @type {String}
* @readOnly
*/
filePublishedText: {
valueFn: () => Y.eZ.trans('file.published', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File deleted text
*
* @attribute fileDeletedText
* @type {String}
* @readOnly
*/
fileDeletedText: {
valueFn: () => Y.eZ.trans('file.deleted', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload aborted text
*
* @attribute fileUploadAbortedText
* @type {String}
* @readOnly
*/
fileUploadAbortedText: {
valueFn: () => Y.eZ.trans('file.upload.aborted', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* Start deleting file text
*
* @attribute fileStartDeleteText
* @type {String}
* @readOnly
*/
fileStartDeleteText: {
valueFn: () => Y.eZ.trans('deleting.file', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload failed text
*
* @attribute fileUploadFailedText
* @type {String}
* @readOnly
*/
fileUploadFailedText: {
valueFn: () => Y.eZ.trans('file.upload.failed', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload error text
*
* @attribute fileUploadErrorText
* @type {String}
* @readOnly
*/
fileUploadErrorText: {
valueFn: () => Y.eZ.trans('file.upload.error', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload unknown content type text
*
* @attribute fileUploadUknownContentTypeText
* @type {String}
* @readOnly
*/
fileUploadUknownContentTypeText: {
valueFn: () => Y.eZ.trans('file.upload.content.type.uknown', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File upload unexpected error text
*
* @attribute fileUploadUnexpectedErrorText
* @type {String}
* @readOnly
*/
fileUploadUnexpectedErrorText: {
valueFn: () => Y.eZ.trans('file.upload.unexpected.error', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File type not allowed error text
*
* @attribute fileTypeNotAllowedText
* @type {String}
* @readOnly
*/
fileTypeNotAllowedText: {
valueFn: () => Y.eZ.trans('file.type.not.allowed', {}, 'fileuploaditem'),
readOnly: true,
},
/**
* File size exceeded max allowed file size text
*
* @attribute fileSizeExceededText
* @type {String}
* @readOnly
*/
fileSizeExceededText: {
valueFn: () => Y.eZ.trans('file.size.not.allowed', {}, 'fileuploaditem'),
readOnly: true,
},
}
});
});