uppy
Version:
Almost as cute as a Puppy :dog:
162 lines (128 loc) • 6.02 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('../Plugin');
var StatusBar = 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(StatusBarUI, _Plugin);
function StatusBarUI(core, opts) {
_classCallCheck(this, StatusBarUI);
var _this = _possibleConstructorReturn(this, _Plugin.call(this, core, opts));
_this.id = 'StatusBar';
_this.title = 'StatusBar';
_this.type = 'progressindicator';
// set default options
var defaultOptions = {
target: 'body',
showProgressDetails: false
// merge default options with the ones set by user
};_this.opts = _extends({}, defaultOptions, opts);
_this.pauseAll = _this.pauseAll.bind(_this);
_this.resumeAll = _this.resumeAll.bind(_this);
_this.cancelAll = _this.cancelAll.bind(_this);
_this.render = _this.render.bind(_this);
_this.install = _this.install.bind(_this);
return _this;
}
StatusBarUI.prototype.cancelAll = function cancelAll() {
this.core.emit('core:cancel-all');
};
StatusBarUI.prototype.pauseAll = function pauseAll() {
this.core.emit('core:pause-all');
};
StatusBarUI.prototype.resumeAll = function resumeAll() {
this.core.emit('core:resume-all');
};
StatusBarUI.prototype.getTotalSpeed = function getTotalSpeed(files) {
var totalSpeed = 0;
files.forEach(function (file) {
totalSpeed = totalSpeed + getSpeed(file.progress);
});
return totalSpeed;
};
StatusBarUI.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;
};
StatusBarUI.prototype.render = function render(state) {
var files = state.files;
var uploadStartedFiles = Object.keys(files).filter(function (file) {
return files[file].progress.uploadStarted;
});
var completeFiles = Object.keys(files).filter(function (file) {
return files[file].progress.uploadComplete;
});
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 isAllComplete = state.totalProgress === 100 && completeFiles.length === Object.keys(files).length && processingFiles.length === 0;
var isAllPaused = inProgressFiles.length === 0 && !isAllComplete && uploadStartedFiles.length > 0;
var isUploadStarted = uploadStartedFiles.length > 0;
var resumableUploads = this.core.getState().capabilities.resumableUploads || false;
return StatusBar({
error: state.error,
totalProgress: state.totalProgress,
totalSize: totalSize,
totalUploadedSize: totalUploadedSize,
uploadStartedFiles: uploadStartedFiles,
isAllComplete: isAllComplete,
isAllPaused: isAllPaused,
isUploadStarted: isUploadStarted,
pauseAll: this.pauseAll,
resumeAll: this.resumeAll,
cancelAll: this.cancelAll,
complete: completeFiles.length,
inProgress: uploadStartedFiles.length,
totalSpeed: totalSpeed,
totalETA: totalETA,
files: state.files,
resumableUploads: resumableUploads
});
};
StatusBarUI.prototype.install = function install() {
var target = this.opts.target;
var plugin = this;
this.target = this.mount(target, plugin);
};
StatusBarUI.prototype.uninstall = function uninstall() {
this.unmount();
};
return StatusBarUI;
}(Plugin);
//# sourceMappingURL=index.js.map