/
index.js
220 lines (191 loc) · 5.82 KB
/
index.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
const { UIPlugin } = require('@uppy/core')
const getSpeed = require('@uppy/utils/lib/getSpeed')
const getBytesRemaining = require('@uppy/utils/lib/getBytesRemaining')
const getTextDirection = require('@uppy/utils/lib/getTextDirection')
const statusBarStates = require('./StatusBarStates')
const StatusBarUI = require('./StatusBar')
const locale = require('./locale.js')
/**
* StatusBar: renders a status bar with upload/pause/resume/cancel/retry buttons,
* progress percentage and time remaining.
*/
module.exports = class StatusBar extends UIPlugin {
// eslint-disable-next-line global-require
static VERSION = require('../package.json').version;
constructor (uppy, opts) {
super(uppy, opts)
this.id = this.opts.id || 'StatusBar'
this.title = 'StatusBar'
this.type = 'progressindicator'
this.defaultLocale = locale
// set default options
const defaultOptions = {
target: 'body',
hideUploadButton: false,
hideRetryButton: false,
hidePauseResumeButton: false,
hideCancelButton: false,
showProgressDetails: false,
hideAfterFinish: true,
doneButtonHandler: null,
}
this.opts = { ...defaultOptions, ...opts }
this.i18nInit()
this.render = this.render.bind(this)
this.install = this.install.bind(this)
}
startUpload = () => {
const { recoveredState } = this.uppy.getState()
if (recoveredState) {
this.uppy.emit('restore-confirmed')
return undefined
}
return this.uppy.upload().catch(() => {
// Error logged in Core
})
};
render (state) {
const {
capabilities,
files,
allowNewUpload,
totalProgress,
error,
recoveredState,
} = state
const {
newFiles,
startedFiles,
completeFiles,
inProgressNotPausedFiles,
isUploadStarted,
isAllComplete,
isAllErrored,
isAllPaused,
isUploadInProgress,
isSomeGhost,
} = this.uppy.getObjectOfFilesPerState()
// If some state was recovered, we want to show Upload button/counter
// for all the files, because in this case it’s not an Upload button,
// but “Confirm Restore Button”
const newFilesOrRecovered = recoveredState
? Object.values(files)
: newFiles
const totalETA = getTotalETA(inProgressNotPausedFiles)
const resumableUploads = !!capabilities.resumableUploads
const supportsUploadProgress = capabilities.uploadProgress !== false
let totalSize = 0
let totalUploadedSize = 0
startedFiles.forEach((file) => {
totalSize += file.progress.bytesTotal || 0
totalUploadedSize += file.progress.bytesUploaded || 0
})
return StatusBarUI({
error,
uploadState: getUploadingState(
error,
isAllComplete,
recoveredState,
state.files || {},
),
allowNewUpload,
totalProgress,
totalSize,
totalUploadedSize,
isAllComplete: false,
isAllPaused,
isAllErrored,
isUploadStarted,
isUploadInProgress,
isSomeGhost,
recoveredState,
complete: completeFiles.length,
newFiles: newFilesOrRecovered.length,
numUploads: startedFiles.length,
totalETA,
files,
i18n: this.i18n,
uppy: this.uppy,
startUpload: this.startUpload,
doneButtonHandler: this.opts.doneButtonHandler,
resumableUploads,
supportsUploadProgress,
showProgressDetails: this.opts.showProgressDetails,
hideUploadButton: this.opts.hideUploadButton,
hideRetryButton: this.opts.hideRetryButton,
hidePauseResumeButton: this.opts.hidePauseResumeButton,
hideCancelButton: this.opts.hideCancelButton,
hideAfterFinish: this.opts.hideAfterFinish,
isTargetDOMEl: this.isTargetDOMEl,
})
}
onMount () {
// Set the text direction if the page has not defined one.
const element = this.el
const direction = getTextDirection(element)
if (!direction) {
element.dir = 'ltr'
}
}
install () {
const { target } = this.opts
if (target) {
this.mount(target, this)
}
}
uninstall () {
this.unmount()
}
}
function getTotalSpeed (files) {
let totalSpeed = 0
files.forEach((file) => {
totalSpeed += getSpeed(file.progress)
})
return totalSpeed
}
function getTotalETA (files) {
const totalSpeed = getTotalSpeed(files)
if (totalSpeed === 0) {
return 0
}
const totalBytesRemaining = files.reduce((total, file) => {
return total + getBytesRemaining(file.progress)
}, 0)
return Math.round((totalBytesRemaining / totalSpeed) * 10) / 10
}
function getUploadingState (error, isAllComplete, recoveredState, files) {
if (error && !isAllComplete) {
return statusBarStates.STATE_ERROR
}
if (isAllComplete) {
return statusBarStates.STATE_COMPLETE
}
if (recoveredState) {
return statusBarStates.STATE_WAITING
}
let state = statusBarStates.STATE_WAITING
const fileIDs = Object.keys(files)
for (let i = 0; i < fileIDs.length; i++) {
const { progress } = files[fileIDs[i]]
// If ANY files are being uploaded right now, show the uploading state.
if (progress.uploadStarted && !progress.uploadComplete) {
return statusBarStates.STATE_UPLOADING
}
// If files are being preprocessed AND postprocessed at this time, we show the
// preprocess state. If any files are being uploaded we show uploading.
if (progress.preprocess && state !== statusBarStates.STATE_UPLOADING) {
state = statusBarStates.STATE_PREPROCESSING
}
// If NO files are being preprocessed or uploaded right now, but some files are
// being postprocessed, show the postprocess state.
if (
progress.postprocess
&& state !== statusBarStates.STATE_UPLOADING
&& state !== statusBarStates.STATE_PREPROCESSING
) {
state = statusBarStates.STATE_POSTPROCESSING
}
}
return state
}