UNPKG

9.9 kBJavaScriptView Raw
1var _class, _temp;
2
3function _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); }
4
5function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
6
7function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
8
9var _require = require('@uppy/core'),
10 Plugin = _require.Plugin;
11
12var Translator = require('@uppy/utils/lib/Translator');
13
14var StatusBarUI = require('./StatusBar');
15
16var statusBarStates = require('./StatusBarStates');
17
18var getSpeed = require('@uppy/utils/lib/getSpeed');
19
20var getBytesRemaining = require('@uppy/utils/lib/getBytesRemaining');
21
22var getTextDirection = require('@uppy/utils/lib/getTextDirection');
23/**
24 * StatusBar: renders a status bar with upload/pause/resume/cancel/retry buttons,
25 * progress percentage and time remaining.
26 */
27
28
29module.exports = (_temp = _class = /*#__PURE__*/function (_Plugin) {
30 _inheritsLoose(StatusBar, _Plugin);
31
32 function StatusBar(uppy, opts) {
33 var _this;
34
35 _this = _Plugin.call(this, uppy, opts) || this;
36
37 _this.startUpload = function () {
38 return _this.uppy.upload().catch(function () {// Error logged in Core
39 });
40 };
41
42 _this.id = _this.opts.id || 'StatusBar';
43 _this.title = 'StatusBar';
44 _this.type = 'progressindicator';
45 _this.defaultLocale = {
46 strings: {
47 uploading: 'Uploading',
48 upload: 'Upload',
49 complete: 'Complete',
50 uploadFailed: 'Upload failed',
51 paused: 'Paused',
52 retry: 'Retry',
53 retryUpload: 'Retry upload',
54 cancel: 'Cancel',
55 pause: 'Pause',
56 resume: 'Resume',
57 done: 'Done',
58 filesUploadedOfTotal: {
59 0: '%{complete} of %{smart_count} file uploaded',
60 1: '%{complete} of %{smart_count} files uploaded'
61 },
62 dataUploadedOfTotal: '%{complete} of %{total}',
63 xTimeLeft: '%{time} left',
64 uploadXFiles: {
65 0: 'Upload %{smart_count} file',
66 1: 'Upload %{smart_count} files'
67 },
68 uploadXNewFiles: {
69 0: 'Upload +%{smart_count} file',
70 1: 'Upload +%{smart_count} files'
71 },
72 xMoreFilesAdded: {
73 0: '%{smart_count} more file added',
74 1: '%{smart_count} more files added'
75 }
76 }
77 }; // set default options
78
79 var defaultOptions = {
80 target: 'body',
81 hideUploadButton: false,
82 hideRetryButton: false,
83 hidePauseResumeButton: false,
84 hideCancelButton: false,
85 showProgressDetails: false,
86 hideAfterFinish: true,
87 doneButtonHandler: null
88 };
89 _this.opts = _extends({}, defaultOptions, opts);
90
91 _this.i18nInit();
92
93 _this.render = _this.render.bind(_assertThisInitialized(_this));
94 _this.install = _this.install.bind(_assertThisInitialized(_this));
95 return _this;
96 }
97
98 var _proto = StatusBar.prototype;
99
100 _proto.setOptions = function setOptions(newOpts) {
101 _Plugin.prototype.setOptions.call(this, newOpts);
102
103 this.i18nInit();
104 };
105
106 _proto.i18nInit = function i18nInit() {
107 this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale]);
108 this.i18n = this.translator.translate.bind(this.translator);
109 this.setPluginState(); // so that UI re-renders and we see the updated locale
110 };
111
112 _proto.getTotalSpeed = function getTotalSpeed(files) {
113 var totalSpeed = 0;
114 files.forEach(function (file) {
115 totalSpeed = totalSpeed + getSpeed(file.progress);
116 });
117 return totalSpeed;
118 };
119
120 _proto.getTotalETA = function getTotalETA(files) {
121 var totalSpeed = this.getTotalSpeed(files);
122
123 if (totalSpeed === 0) {
124 return 0;
125 }
126
127 var totalBytesRemaining = files.reduce(function (total, file) {
128 return total + getBytesRemaining(file.progress);
129 }, 0);
130 return Math.round(totalBytesRemaining / totalSpeed * 10) / 10;
131 };
132
133 _proto.getUploadingState = function getUploadingState(isAllErrored, isAllComplete, files) {
134 if (isAllErrored) {
135 return statusBarStates.STATE_ERROR;
136 }
137
138 if (isAllComplete) {
139 return statusBarStates.STATE_COMPLETE;
140 }
141
142 var state = statusBarStates.STATE_WAITING;
143 var fileIDs = Object.keys(files);
144
145 for (var i = 0; i < fileIDs.length; i++) {
146 var progress = files[fileIDs[i]].progress; // If ANY files are being uploaded right now, show the uploading state.
147
148 if (progress.uploadStarted && !progress.uploadComplete) {
149 return statusBarStates.STATE_UPLOADING;
150 } // If files are being preprocessed AND postprocessed at this time, we show the
151 // preprocess state. If any files are being uploaded we show uploading.
152
153
154 if (progress.preprocess && state !== statusBarStates.STATE_UPLOADING) {
155 state = statusBarStates.STATE_PREPROCESSING;
156 } // If NO files are being preprocessed or uploaded right now, but some files are
157 // being postprocessed, show the postprocess state.
158
159
160 if (progress.postprocess && state !== statusBarStates.STATE_UPLOADING && state !== statusBarStates.STATE_PREPROCESSING) {
161 state = statusBarStates.STATE_POSTPROCESSING;
162 }
163 }
164
165 return state;
166 };
167
168 _proto.render = function render(state) {
169 var capabilities = state.capabilities,
170 files = state.files,
171 allowNewUpload = state.allowNewUpload,
172 totalProgress = state.totalProgress,
173 error = state.error; // TODO: move this to Core, to share between Status Bar and Dashboard
174 // (and any other plugin that might need it, too)
175
176 var filesArray = Object.keys(files).map(function (file) {
177 return files[file];
178 });
179 var newFiles = filesArray.filter(function (file) {
180 return !file.progress.uploadStarted && !file.progress.preprocess && !file.progress.postprocess;
181 });
182 var uploadStartedFiles = filesArray.filter(function (file) {
183 return file.progress.uploadStarted;
184 });
185 var pausedFiles = uploadStartedFiles.filter(function (file) {
186 return file.isPaused;
187 });
188 var completeFiles = filesArray.filter(function (file) {
189 return file.progress.uploadComplete;
190 });
191 var erroredFiles = filesArray.filter(function (file) {
192 return file.error;
193 });
194 var inProgressFiles = filesArray.filter(function (file) {
195 return !file.progress.uploadComplete && file.progress.uploadStarted;
196 });
197 var inProgressNotPausedFiles = inProgressFiles.filter(function (file) {
198 return !file.isPaused;
199 });
200 var startedFiles = filesArray.filter(function (file) {
201 return file.progress.uploadStarted || file.progress.preprocess || file.progress.postprocess;
202 });
203 var processingFiles = filesArray.filter(function (file) {
204 return file.progress.preprocess || file.progress.postprocess;
205 });
206 var totalETA = this.getTotalETA(inProgressNotPausedFiles);
207 var totalSize = 0;
208 var totalUploadedSize = 0;
209 startedFiles.forEach(function (file) {
210 totalSize = totalSize + (file.progress.bytesTotal || 0);
211 totalUploadedSize = totalUploadedSize + (file.progress.bytesUploaded || 0);
212 });
213 var isUploadStarted = startedFiles.length > 0;
214 var isAllComplete = totalProgress === 100 && completeFiles.length === Object.keys(files).length && processingFiles.length === 0;
215 var isAllErrored = error && erroredFiles.length === filesArray.length;
216 var isAllPaused = inProgressFiles.length !== 0 && pausedFiles.length === inProgressFiles.length;
217 var isUploadInProgress = inProgressFiles.length > 0;
218 var resumableUploads = capabilities.resumableUploads || false;
219 var supportsUploadProgress = capabilities.uploadProgress !== false;
220 return StatusBarUI({
221 error: error,
222 uploadState: this.getUploadingState(isAllErrored, isAllComplete, state.files || {}),
223 allowNewUpload: allowNewUpload,
224 totalProgress: totalProgress,
225 totalSize: totalSize,
226 totalUploadedSize: totalUploadedSize,
227 isAllComplete: isAllComplete,
228 isAllPaused: isAllPaused,
229 isAllErrored: isAllErrored,
230 isUploadStarted: isUploadStarted,
231 isUploadInProgress: isUploadInProgress,
232 complete: completeFiles.length,
233 newFiles: newFiles.length,
234 numUploads: startedFiles.length,
235 totalETA: totalETA,
236 files: files,
237 i18n: this.i18n,
238 pauseAll: this.uppy.pauseAll,
239 resumeAll: this.uppy.resumeAll,
240 retryAll: this.uppy.retryAll,
241 cancelAll: this.uppy.cancelAll,
242 startUpload: this.startUpload,
243 doneButtonHandler: this.opts.doneButtonHandler,
244 resumableUploads: resumableUploads,
245 supportsUploadProgress: supportsUploadProgress,
246 showProgressDetails: this.opts.showProgressDetails,
247 hideUploadButton: this.opts.hideUploadButton,
248 hideRetryButton: this.opts.hideRetryButton,
249 hidePauseResumeButton: this.opts.hidePauseResumeButton,
250 hideCancelButton: this.opts.hideCancelButton,
251 hideAfterFinish: this.opts.hideAfterFinish,
252 isTargetDOMEl: this.isTargetDOMEl
253 });
254 };
255
256 _proto.onMount = function onMount() {
257 // Set the text direction if the page has not defined one.
258 var element = this.el;
259 var direction = getTextDirection(element);
260
261 if (!direction) {
262 element.dir = 'ltr';
263 }
264 };
265
266 _proto.install = function install() {
267 var target = this.opts.target;
268
269 if (target) {
270 this.mount(target, this);
271 }
272 };
273
274 _proto.uninstall = function uninstall() {
275 this.unmount();
276 };
277
278 return StatusBar;
279}(Plugin), _class.VERSION = "1.9.2", _temp);
\No newline at end of file