uppy
Version:
Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:
206 lines (168 loc) • 7.63 kB
JavaScript
;
var _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; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Plugin = require('../../core/Plugin');
var Translator = require('../../core/Translator');
var StatusBarUI = require('./StatusBar');
var _require = require('../../core/Utils'),
getSpeed = _require.getSpeed;
var _require2 = require('../../core/Utils'),
getBytesRemaining = _require2.getBytesRemaining;
var _require3 = require('../../core/Utils'),
prettyETA = _require3.prettyETA;
var prettyBytes = require('prettier-bytes');
/**
* A status bar.
*/
module.exports = function (_Plugin) {
_inherits(StatusBar, _Plugin);
function StatusBar(uppy, opts) {
_classCallCheck(this, StatusBar);
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts));
_this.id = _this.opts.id || 'StatusBar';
_this.title = 'StatusBar';
_this.type = 'progressindicator';
var defaultLocale = {
strings: {
uploading: 'Uploading...',
uploadComplete: 'Upload complete',
uploadFailed: 'Upload failed',
pleasePressRetry: 'Please press Retry to upload again',
paused: 'Paused',
error: 'Error',
retry: 'Retry',
pressToRetry: 'Press to retry',
retryUpload: 'Retry upload',
resumeUpload: 'Resume upload',
cancelUpload: 'Cancel upload',
pauseUpload: 'Pause upload',
uploadXFiles: {
0: 'Upload %{smart_count} file',
1: 'Upload %{smart_count} files'
},
uploadXNewFiles: {
0: 'Upload +%{smart_count} file',
1: 'Upload +%{smart_count} files'
}
}
// set default options
};var defaultOptions = {
target: 'body',
hideUploadButton: false,
showProgressDetails: false,
locale: defaultLocale,
hideAfterFinish: true
// merge default options with the ones set by user
};_this.opts = _extends({}, defaultOptions, opts);
_this.locale = _extends({}, defaultLocale, _this.opts.locale);
_this.locale.strings = _extends({}, defaultLocale.strings, _this.opts.locale.strings);
_this.translator = new Translator({ locale: _this.locale });
_this.i18n = _this.translator.translate.bind(_this.translator);
_this.startUpload = _this.startUpload.bind(_this);
_this.render = _this.render.bind(_this);
_this.install = _this.install.bind(_this);
return _this;
}
StatusBar.prototype.getTotalSpeed = function getTotalSpeed(files) {
var totalSpeed = 0;
files.forEach(function (file) {
totalSpeed = totalSpeed + getSpeed(file.progress);
});
return totalSpeed;
};
StatusBar.prototype.getTotalETA = function getTotalETA(files) {
var totalSpeed = this.getTotalSpeed(files);
if (totalSpeed === 0) {
return 0;
}
var totalBytesRemaining = files.reduce(function (total, file) {
return total + getBytesRemaining(file.progress);
}, 0);
return Math.round(totalBytesRemaining / totalSpeed * 10) / 10;
};
StatusBar.prototype.startUpload = function startUpload() {
return this.uppy.upload().catch(function () {
// Ignore
});
};
StatusBar.prototype.render = function render(state) {
var files = state.files;
var uploadStartedFiles = Object.keys(files).filter(function (file) {
return files[file].progress.uploadStarted;
});
var newFiles = Object.keys(files).filter(function (file) {
return !files[file].progress.uploadStarted && !files[file].progress.preprocess && !files[file].progress.postprocess;
});
var completeFiles = Object.keys(files).filter(function (file) {
return files[file].progress.uploadComplete;
});
var erroredFiles = Object.keys(files).filter(function (file) {
return files[file].error;
});
var inProgressFiles = Object.keys(files).filter(function (file) {
return !files[file].progress.uploadComplete && files[file].progress.uploadStarted && !files[file].isPaused;
});
var processingFiles = Object.keys(files).filter(function (file) {
return files[file].progress.preprocess || files[file].progress.postprocess;
});
var inProgressFilesArray = [];
inProgressFiles.forEach(function (file) {
inProgressFilesArray.push(files[file]);
});
var totalSpeed = prettyBytes(this.getTotalSpeed(inProgressFilesArray));
var totalETA = prettyETA(this.getTotalETA(inProgressFilesArray));
// total size and uploaded size
var totalSize = 0;
var totalUploadedSize = 0;
inProgressFilesArray.forEach(function (file) {
totalSize = totalSize + (file.progress.bytesTotal || 0);
totalUploadedSize = totalUploadedSize + (file.progress.bytesUploaded || 0);
});
totalSize = prettyBytes(totalSize);
totalUploadedSize = prettyBytes(totalUploadedSize);
var isUploadStarted = uploadStartedFiles.length > 0;
var isAllComplete = state.totalProgress === 100 && completeFiles.length === Object.keys(files).length && processingFiles.length === 0;
var isAllErrored = isUploadStarted && erroredFiles.length === uploadStartedFiles.length;
var isAllPaused = inProgressFiles.length === 0 && !isAllComplete && !isAllErrored && uploadStartedFiles.length > 0;
var resumableUploads = this.uppy.getState().capabilities.resumableUploads || false;
return StatusBarUI({
error: state.error,
totalProgress: state.totalProgress,
totalSize: totalSize,
totalUploadedSize: totalUploadedSize,
uploadStartedFiles: uploadStartedFiles,
isAllComplete: isAllComplete,
isAllPaused: isAllPaused,
isAllErrored: isAllErrored,
isUploadStarted: isUploadStarted,
i18n: this.i18n,
pauseAll: this.uppy.pauseAll,
resumeAll: this.uppy.resumeAll,
retryAll: this.uppy.retryAll,
cancelAll: this.uppy.cancelAll,
startUpload: this.startUpload,
complete: completeFiles.length,
newFiles: newFiles.length,
inProgress: uploadStartedFiles.length,
totalSpeed: totalSpeed,
totalETA: totalETA,
files: state.files,
resumableUploads: resumableUploads,
hideUploadButton: this.opts.hideUploadButton,
hideAfterFinish: this.opts.hideAfterFinish
});
};
StatusBar.prototype.install = function install() {
var target = this.opts.target;
if (target) {
this.mount(target, this);
}
};
StatusBar.prototype.uninstall = function uninstall() {
this.unmount();
};
return StatusBar;
}(Plugin);
//# sourceMappingURL=index.js.map