UNPKG

14 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3var throttle = require('lodash.throttle');
4
5var classNames = require('classnames');
6
7var statusBarStates = require('./StatusBarStates');
8
9var prettierBytes = require('@transloadit/prettier-bytes');
10
11var prettyETA = require('@uppy/utils/lib/prettyETA');
12
13var _require = require('preact'),
14 h = _require.h;
15
16function calculateProcessingProgress(files) {
17 // Collect pre or postprocessing progress states.
18 var progresses = [];
19 Object.keys(files).forEach(function (fileID) {
20 var progress = files[fileID].progress;
21
22 if (progress.preprocess) {
23 progresses.push(progress.preprocess);
24 }
25
26 if (progress.postprocess) {
27 progresses.push(progress.postprocess);
28 }
29 }); // In the future we should probably do this differently. For now we'll take the
30 // mode and message from the first file…
31
32 var _progresses$ = progresses[0],
33 mode = _progresses$.mode,
34 message = _progresses$.message;
35 var value = progresses.filter(isDeterminate).reduce(function (total, progress, index, all) {
36 return total + progress.value / all.length;
37 }, 0);
38
39 function isDeterminate(progress) {
40 return progress.mode === 'determinate';
41 }
42
43 return {
44 mode: mode,
45 message: message,
46 value: value
47 };
48}
49
50function togglePauseResume(props) {
51 if (props.isAllComplete) return;
52
53 if (!props.resumableUploads) {
54 return props.cancelAll();
55 }
56
57 if (props.isAllPaused) {
58 return props.resumeAll();
59 }
60
61 return props.pauseAll();
62}
63
64module.exports = function (props) {
65 props = props || {};
66 var _props = props,
67 newFiles = _props.newFiles,
68 allowNewUpload = _props.allowNewUpload,
69 isUploadInProgress = _props.isUploadInProgress,
70 isAllPaused = _props.isAllPaused,
71 resumableUploads = _props.resumableUploads,
72 error = _props.error,
73 hideUploadButton = _props.hideUploadButton,
74 hidePauseResumeButton = _props.hidePauseResumeButton,
75 hideCancelButton = _props.hideCancelButton,
76 hideRetryButton = _props.hideRetryButton;
77 var uploadState = props.uploadState;
78 var progressValue = props.totalProgress;
79 var progressMode;
80 var progressBarContent;
81
82 if (uploadState === statusBarStates.STATE_PREPROCESSING || uploadState === statusBarStates.STATE_POSTPROCESSING) {
83 var progress = calculateProcessingProgress(props.files);
84 progressMode = progress.mode;
85
86 if (progressMode === 'determinate') {
87 progressValue = progress.value * 100;
88 }
89
90 progressBarContent = ProgressBarProcessing(progress);
91 } else if (uploadState === statusBarStates.STATE_COMPLETE) {
92 progressBarContent = ProgressBarComplete(props);
93 } else if (uploadState === statusBarStates.STATE_UPLOADING) {
94 if (!props.supportsUploadProgress) {
95 progressMode = 'indeterminate';
96 progressValue = null;
97 }
98
99 progressBarContent = ProgressBarUploading(props);
100 } else if (uploadState === statusBarStates.STATE_ERROR) {
101 progressValue = undefined;
102 progressBarContent = ProgressBarError(props);
103 }
104
105 var width = typeof progressValue === 'number' ? progressValue : 100;
106 var isHidden = uploadState === statusBarStates.STATE_WAITING && props.hideUploadButton || uploadState === statusBarStates.STATE_WAITING && !props.newFiles > 0 || uploadState === statusBarStates.STATE_COMPLETE && props.hideAfterFinish;
107 var showUploadBtn = !error && newFiles && !isUploadInProgress && !isAllPaused && allowNewUpload && !hideUploadButton;
108 var showCancelBtn = !hideCancelButton && uploadState !== statusBarStates.STATE_WAITING && uploadState !== statusBarStates.STATE_COMPLETE;
109 var showPauseResumeBtn = resumableUploads && !hidePauseResumeButton && uploadState === statusBarStates.STATE_UPLOADING;
110 var showRetryBtn = error && !hideRetryButton;
111 var showDoneBtn = props.doneButtonHandler && uploadState === statusBarStates.STATE_COMPLETE;
112 var progressClassNames = "uppy-StatusBar-progress\n " + (progressMode ? 'is-' + progressMode : '');
113 var statusBarClassNames = classNames({
114 'uppy-Root': props.isTargetDOMEl
115 }, 'uppy-StatusBar', "is-" + uploadState);
116 return h("div", {
117 class: statusBarClassNames,
118 "aria-hidden": isHidden
119 }, h("div", {
120 class: progressClassNames,
121 style: {
122 width: width + '%'
123 },
124 role: "progressbar",
125 "aria-valuemin": "0",
126 "aria-valuemax": "100",
127 "aria-valuenow": progressValue
128 }), progressBarContent, h("div", {
129 class: "uppy-StatusBar-actions"
130 }, showUploadBtn ? h(UploadBtn, _extends({}, props, {
131 uploadState: uploadState
132 })) : null, showRetryBtn ? h(RetryBtn, props) : null, showPauseResumeBtn ? h(PauseResumeButton, props) : null, showCancelBtn ? h(CancelBtn, props) : null, showDoneBtn ? h(DoneBtn, props) : null));
133};
134
135var UploadBtn = function UploadBtn(props) {
136 var uploadBtnClassNames = classNames('uppy-u-reset', 'uppy-c-btn', 'uppy-StatusBar-actionBtn', 'uppy-StatusBar-actionBtn--upload', {
137 'uppy-c-btn-primary': props.uploadState === statusBarStates.STATE_WAITING
138 });
139 return h("button", {
140 type: "button",
141 class: uploadBtnClassNames,
142 "aria-label": props.i18n('uploadXFiles', {
143 smart_count: props.newFiles
144 }),
145 onclick: props.startUpload,
146 "data-uppy-super-focusable": true
147 }, props.newFiles && props.isUploadStarted ? props.i18n('uploadXNewFiles', {
148 smart_count: props.newFiles
149 }) : props.i18n('uploadXFiles', {
150 smart_count: props.newFiles
151 }));
152};
153
154var RetryBtn = function RetryBtn(props) {
155 return h("button", {
156 type: "button",
157 class: "uppy-u-reset uppy-c-btn uppy-StatusBar-actionBtn uppy-StatusBar-actionBtn--retry",
158 "aria-label": props.i18n('retryUpload'),
159 onclick: props.retryAll,
160 "data-uppy-super-focusable": true
161 }, h("svg", {
162 "aria-hidden": "true",
163 focusable: "false",
164 class: "uppy-c-icon",
165 width: "8",
166 height: "10",
167 viewBox: "0 0 8 10"
168 }, h("path", {
169 d: "M4 2.408a2.75 2.75 0 1 0 2.75 2.75.626.626 0 0 1 1.25.018v.023a4 4 0 1 1-4-4.041V.25a.25.25 0 0 1 .389-.208l2.299 1.533a.25.25 0 0 1 0 .416l-2.3 1.533A.25.25 0 0 1 4 3.316v-.908z"
170 })), props.i18n('retry'));
171};
172
173var CancelBtn = function CancelBtn(props) {
174 return h("button", {
175 type: "button",
176 class: "uppy-u-reset uppy-StatusBar-actionCircleBtn",
177 title: props.i18n('cancel'),
178 "aria-label": props.i18n('cancel'),
179 onclick: props.cancelAll,
180 "data-uppy-super-focusable": true
181 }, h("svg", {
182 "aria-hidden": "true",
183 focusable: "false",
184 class: "uppy-c-icon",
185 width: "16",
186 height: "16",
187 viewBox: "0 0 16 16"
188 }, h("g", {
189 fill: "none",
190 "fill-rule": "evenodd"
191 }, h("circle", {
192 fill: "#888",
193 cx: "8",
194 cy: "8",
195 r: "8"
196 }), h("path", {
197 fill: "#FFF",
198 d: "M9.283 8l2.567 2.567-1.283 1.283L8 9.283 5.433 11.85 4.15 10.567 6.717 8 4.15 5.433 5.433 4.15 8 6.717l2.567-2.567 1.283 1.283z"
199 }))));
200};
201
202var PauseResumeButton = function PauseResumeButton(props) {
203 var isAllPaused = props.isAllPaused,
204 i18n = props.i18n;
205 var title = isAllPaused ? i18n('resume') : i18n('pause');
206 return h("button", {
207 title: title,
208 "aria-label": title,
209 class: "uppy-u-reset uppy-StatusBar-actionCircleBtn",
210 type: "button",
211 onclick: function onclick() {
212 return togglePauseResume(props);
213 },
214 "data-uppy-super-focusable": true
215 }, isAllPaused ? h("svg", {
216 "aria-hidden": "true",
217 focusable: "false",
218 class: "uppy-c-icon",
219 width: "16",
220 height: "16",
221 viewBox: "0 0 16 16"
222 }, h("g", {
223 fill: "none",
224 "fill-rule": "evenodd"
225 }, h("circle", {
226 fill: "#888",
227 cx: "8",
228 cy: "8",
229 r: "8"
230 }), h("path", {
231 fill: "#FFF",
232 d: "M6 4.25L11.5 8 6 11.75z"
233 }))) : h("svg", {
234 "aria-hidden": "true",
235 focusable: "false",
236 class: "uppy-c-icon",
237 width: "16",
238 height: "16",
239 viewBox: "0 0 16 16"
240 }, h("g", {
241 fill: "none",
242 "fill-rule": "evenodd"
243 }, h("circle", {
244 fill: "#888",
245 cx: "8",
246 cy: "8",
247 r: "8"
248 }), h("path", {
249 d: "M5 4.5h2v7H5v-7zm4 0h2v7H9v-7z",
250 fill: "#FFF"
251 }))));
252};
253
254var DoneBtn = function DoneBtn(props) {
255 var i18n = props.i18n;
256 return h("button", {
257 type: "button",
258 class: "uppy-u-reset uppy-c-btn uppy-StatusBar-actionBtn uppy-StatusBar-actionBtn--done",
259 onClick: props.doneButtonHandler,
260 "data-uppy-super-focusable": true
261 }, i18n('done'));
262};
263
264var LoadingSpinner = function LoadingSpinner() {
265 return h("svg", {
266 class: "uppy-StatusBar-spinner",
267 "aria-hidden": "true",
268 focusable: "false",
269 width: "14",
270 height: "14"
271 }, h("path", {
272 d: "M13.983 6.547c-.12-2.509-1.64-4.893-3.939-5.936-2.48-1.127-5.488-.656-7.556 1.094C.524 3.367-.398 6.048.162 8.562c.556 2.495 2.46 4.52 4.94 5.183 2.932.784 5.61-.602 7.256-3.015-1.493 1.993-3.745 3.309-6.298 2.868-2.514-.434-4.578-2.349-5.153-4.84a6.226 6.226 0 0 1 2.98-6.778C6.34.586 9.74 1.1 11.373 3.493c.407.596.693 1.282.842 1.988.127.598.073 1.197.161 1.794.078.525.543 1.257 1.15.864.525-.341.49-1.05.456-1.592-.007-.15.02.3 0 0",
273 "fill-rule": "evenodd"
274 }));
275};
276
277var ProgressBarProcessing = function ProgressBarProcessing(props) {
278 var value = Math.round(props.value * 100);
279 return h("div", {
280 class: "uppy-StatusBar-content"
281 }, h(LoadingSpinner, null), props.mode === 'determinate' ? value + "% \xB7 " : '', props.message);
282};
283
284var renderDot = function renderDot() {
285 return " \xB7 ";
286};
287
288var ProgressDetails = function ProgressDetails(props) {
289 var ifShowFilesUploadedOfTotal = props.numUploads > 1;
290 return h("div", {
291 class: "uppy-StatusBar-statusSecondary"
292 }, ifShowFilesUploadedOfTotal && props.i18n('filesUploadedOfTotal', {
293 complete: props.complete,
294 smart_count: props.numUploads
295 }), h("span", {
296 class: "uppy-StatusBar-additionalInfo"
297 }, ifShowFilesUploadedOfTotal && renderDot(), props.i18n('dataUploadedOfTotal', {
298 complete: prettierBytes(props.totalUploadedSize),
299 total: prettierBytes(props.totalSize)
300 }), renderDot(), props.i18n('xTimeLeft', {
301 time: prettyETA(props.totalETA)
302 })));
303};
304
305var UnknownProgressDetails = function UnknownProgressDetails(props) {
306 return h("div", {
307 class: "uppy-StatusBar-statusSecondary"
308 }, props.i18n('filesUploadedOfTotal', {
309 complete: props.complete,
310 smart_count: props.numUploads
311 }));
312};
313
314var UploadNewlyAddedFiles = function UploadNewlyAddedFiles(props) {
315 var uploadBtnClassNames = classNames('uppy-u-reset', 'uppy-c-btn', 'uppy-StatusBar-actionBtn', 'uppy-StatusBar-actionBtn--uploadNewlyAdded');
316 return h("div", {
317 class: "uppy-StatusBar-statusSecondary"
318 }, h("div", {
319 class: "uppy-StatusBar-statusSecondaryHint"
320 }, props.i18n('xMoreFilesAdded', {
321 smart_count: props.newFiles
322 })), h("button", {
323 type: "button",
324 class: uploadBtnClassNames,
325 "aria-label": props.i18n('uploadXFiles', {
326 smart_count: props.newFiles
327 }),
328 onclick: props.startUpload
329 }, props.i18n('upload')));
330};
331
332var ThrottledProgressDetails = throttle(ProgressDetails, 500, {
333 leading: true,
334 trailing: true
335});
336
337var ProgressBarUploading = function ProgressBarUploading(props) {
338 if (!props.isUploadStarted || props.isAllComplete) {
339 return null;
340 }
341
342 var title = props.isAllPaused ? props.i18n('paused') : props.i18n('uploading');
343 var showUploadNewlyAddedFiles = props.newFiles && props.isUploadStarted;
344 return h("div", {
345 class: "uppy-StatusBar-content",
346 "aria-label": title,
347 title: title
348 }, !props.isAllPaused ? h(LoadingSpinner, null) : null, h("div", {
349 class: "uppy-StatusBar-status"
350 }, h("div", {
351 class: "uppy-StatusBar-statusPrimary"
352 }, props.supportsUploadProgress ? title + ": " + props.totalProgress + "%" : title), !props.isAllPaused && !showUploadNewlyAddedFiles && props.showProgressDetails ? props.supportsUploadProgress ? h(ThrottledProgressDetails, props) : h(UnknownProgressDetails, props) : null, showUploadNewlyAddedFiles ? h(UploadNewlyAddedFiles, props) : null));
353};
354
355var ProgressBarComplete = function ProgressBarComplete(_ref) {
356 var totalProgress = _ref.totalProgress,
357 i18n = _ref.i18n;
358 return h("div", {
359 class: "uppy-StatusBar-content",
360 role: "status",
361 title: i18n('complete')
362 }, h("div", {
363 class: "uppy-StatusBar-status"
364 }, h("div", {
365 class: "uppy-StatusBar-statusPrimary"
366 }, h("svg", {
367 "aria-hidden": "true",
368 focusable: "false",
369 class: "uppy-StatusBar-statusIndicator uppy-c-icon",
370 width: "15",
371 height: "11",
372 viewBox: "0 0 15 11"
373 }, h("path", {
374 d: "M.414 5.843L1.627 4.63l3.472 3.472L13.202 0l1.212 1.213L5.1 10.528z"
375 })), i18n('complete'))));
376};
377
378var ProgressBarError = function ProgressBarError(_ref2) {
379 var error = _ref2.error,
380 retryAll = _ref2.retryAll,
381 hideRetryButton = _ref2.hideRetryButton,
382 i18n = _ref2.i18n;
383
384 function displayErrorAlert() {
385 var errorMessage = i18n('uploadFailed') + " \n\n " + error;
386 alert(errorMessage);
387 }
388
389 return h("div", {
390 class: "uppy-StatusBar-content",
391 role: "alert",
392 title: i18n('uploadFailed')
393 }, h("div", {
394 class: "uppy-StatusBar-status"
395 }, h("div", {
396 class: "uppy-StatusBar-statusPrimary"
397 }, h("svg", {
398 "aria-hidden": "true",
399 focusable: "false",
400 class: "uppy-StatusBar-statusIndicator uppy-c-icon",
401 width: "11",
402 height: "11",
403 viewBox: "0 0 11 11"
404 }, h("path", {
405 d: "M4.278 5.5L0 1.222 1.222 0 5.5 4.278 9.778 0 11 1.222 6.722 5.5 11 9.778 9.778 11 5.5 6.722 1.222 11 0 9.778z"
406 })), i18n('uploadFailed'))), h("span", {
407 class: "uppy-StatusBar-details",
408 "aria-label": error,
409 "data-microtip-position": "top-right",
410 "data-microtip-size": "medium",
411 role: "tooltip",
412 onclick: displayErrorAlert
413 }, "?"));
414};
\No newline at end of file