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:
609 lines (505 loc) • 20.4 kB
JavaScript
'use strict';
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 dragDrop = require('drag-drop');
var DashboardUI = require('./Dashboard');
var StatusBar = require('../StatusBar');
var Informer = require('../Informer');
var ThumbnailGenerator = require('../ThumbnailGenerator');
var _require = require('../../core/Utils'),
findAllDOMElements = _require.findAllDOMElements,
toArray = _require.toArray;
var prettyBytes = require('prettier-bytes');
var _require2 = require('./icons'),
defaultTabIcon = _require2.defaultTabIcon;
// Some code for managing focus was adopted from https://github.com/ghosh/micromodal
// MIT licence, https://github.com/ghosh/micromodal/blob/master/LICENSE.md
// Copyright (c) 2017 Indrashish Ghosh
var FOCUSABLE_ELEMENTS = ['a[href]', 'area[href]', 'input:not([disabled]):not([type="hidden"]):not([aria-hidden])', 'select:not([disabled]):not([aria-hidden])', 'textarea:not([disabled]):not([aria-hidden])', 'button:not([disabled]):not([aria-hidden])', 'iframe', 'object', 'embed', '[contenteditable]', '[tabindex]:not([tabindex^="-"])'];
/**
* Dashboard UI with previews, metadata editing, tabs for various services and more
*/
module.exports = function (_Plugin) {
_inherits(Dashboard, _Plugin);
function Dashboard(uppy, opts) {
_classCallCheck(this, Dashboard);
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts));
_this.id = _this.opts.id || 'Dashboard';
_this.title = 'Dashboard';
_this.type = 'orchestrator';
var defaultLocale = {
strings: {
selectToUpload: 'Select files to upload',
closeModal: 'Close Modal',
upload: 'Upload',
importFrom: 'Import files from',
dashboardWindowTitle: 'Uppy Dashboard Window (Press escape to close)',
dashboardTitle: 'Uppy Dashboard',
copyLinkToClipboardSuccess: 'Link copied to clipboard.',
copyLinkToClipboardFallback: 'Copy the URL below',
copyLink: 'Copy link',
fileSource: 'File source',
done: 'Done',
name: 'Name',
removeFile: 'Remove file',
editFile: 'Edit file',
editing: 'Editing',
finishEditingFile: 'Finish editing file',
localDisk: 'Local Disk',
myDevice: 'My Device',
dropPasteImport: 'Drop files here, paste, import from one of the locations above or',
dropPaste: 'Drop files here, paste or',
browse: 'browse',
fileProgress: 'File progress: upload speed and ETA',
numberOfSelectedFiles: 'Number of selected files',
uploadAllNewFiles: 'Upload all new files',
emptyFolderAdded: 'No files were added from empty folder',
uploadXFiles: {
0: 'Upload %{smart_count} file',
1: 'Upload %{smart_count} files'
},
uploadXNewFiles: {
0: 'Upload +%{smart_count} file',
1: 'Upload +%{smart_count} files'
},
folderAdded: {
0: 'Added %{smart_count} file from %{folder}',
1: 'Added %{smart_count} files from %{folder}'
}
}
// set default options
};var defaultOptions = {
target: 'body',
metaFields: [],
trigger: '#uppy-select-files',
inline: false,
width: 750,
height: 550,
thumbnailWidth: 280,
defaultTabIcon: defaultTabIcon,
showProgressDetails: false,
hideUploadButton: false,
hideProgressAfterFinish: false,
note: null,
closeModalOnClickOutside: false,
disableStatusBar: false,
disableInformer: false,
disableThumbnailGenerator: false,
disablePageScrollWhenModalOpen: true,
onRequestCloseModal: function onRequestCloseModal() {
return _this.closeModal();
},
locale: defaultLocale
// 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.openModal = _this.openModal.bind(_this);
_this.closeModal = _this.closeModal.bind(_this);
_this.requestCloseModal = _this.requestCloseModal.bind(_this);
_this.isModalOpen = _this.isModalOpen.bind(_this);
_this.addTarget = _this.addTarget.bind(_this);
_this.hideAllPanels = _this.hideAllPanels.bind(_this);
_this.showPanel = _this.showPanel.bind(_this);
_this.getFocusableNodes = _this.getFocusableNodes.bind(_this);
_this.setFocusToFirstNode = _this.setFocusToFirstNode.bind(_this);
_this.maintainFocus = _this.maintainFocus.bind(_this);
_this.initEvents = _this.initEvents.bind(_this);
_this.onKeydown = _this.onKeydown.bind(_this);
_this.handleClickOutside = _this.handleClickOutside.bind(_this);
_this.handleFileCard = _this.handleFileCard.bind(_this);
_this.handleDrop = _this.handleDrop.bind(_this);
_this.handlePaste = _this.handlePaste.bind(_this);
_this.handleInputChange = _this.handleInputChange.bind(_this);
_this.updateDashboardElWidth = _this.updateDashboardElWidth.bind(_this);
_this.render = _this.render.bind(_this);
_this.install = _this.install.bind(_this);
return _this;
}
Dashboard.prototype.addTarget = function addTarget(plugin) {
var callerPluginId = plugin.id || plugin.constructor.name;
var callerPluginName = plugin.title || callerPluginId;
var callerPluginType = plugin.type;
if (callerPluginType !== 'acquirer' && callerPluginType !== 'progressindicator' && callerPluginType !== 'presenter') {
var msg = 'Dashboard: Modal can only be used by plugins of types: acquirer, progressindicator, presenter';
this.uppy.log(msg);
return;
}
var target = {
id: callerPluginId,
name: callerPluginName,
type: callerPluginType
};
var state = this.getPluginState();
var newTargets = state.targets.slice();
newTargets.push(target);
this.setPluginState({
targets: newTargets
});
return this.el;
};
Dashboard.prototype.hideAllPanels = function hideAllPanels() {
this.setPluginState({
activePanel: false
});
};
Dashboard.prototype.showPanel = function showPanel(id) {
var _getPluginState = this.getPluginState(),
targets = _getPluginState.targets;
var activePanel = targets.filter(function (target) {
return target.type === 'acquirer' && target.id === id;
})[0];
this.setPluginState({
activePanel: activePanel
});
};
Dashboard.prototype.requestCloseModal = function requestCloseModal() {
if (this.opts.onRequestCloseModal) {
return this.opts.onRequestCloseModal();
} else {
this.closeModal();
}
};
Dashboard.prototype.getFocusableNodes = function getFocusableNodes() {
var nodes = this.el.querySelectorAll(FOCUSABLE_ELEMENTS);
return Object.keys(nodes).map(function (key) {
return nodes[key];
});
};
Dashboard.prototype.setFocusToFirstNode = function setFocusToFirstNode() {
var focusableNodes = this.getFocusableNodes();
if (focusableNodes.length) focusableNodes[0].focus();
};
Dashboard.prototype.maintainFocus = function maintainFocus(event) {
var focusableNodes = this.getFocusableNodes();
var focusedItemIndex = focusableNodes.indexOf(document.activeElement);
if (event.shiftKey && focusedItemIndex === 0) {
focusableNodes[focusableNodes.length - 1].focus();
event.preventDefault();
}
if (!event.shiftKey && focusedItemIndex === focusableNodes.length - 1) {
focusableNodes[0].focus();
event.preventDefault();
}
};
Dashboard.prototype.openModal = function openModal() {
this.setPluginState({
isHidden: false
});
// save scroll position
this.savedScrollPosition = window.scrollY;
// save active element, so we can restore focus when modal is closed
this.savedActiveElement = document.activeElement;
if (this.opts.disablePageScrollWhenModalOpen) {
document.body.classList.add('uppy-Dashboard-isOpen');
}
this.updateDashboardElWidth();
this.setFocusToFirstNode();
};
Dashboard.prototype.closeModal = function closeModal() {
this.setPluginState({
isHidden: true
});
if (this.opts.disablePageScrollWhenModalOpen) {
document.body.classList.remove('uppy-Dashboard-isOpen');
}
this.savedActiveElement.focus();
};
Dashboard.prototype.isModalOpen = function isModalOpen() {
return !this.getPluginState().isHidden || false;
};
Dashboard.prototype.onKeydown = function onKeydown(event) {
// close modal on esc key press
if (event.keyCode === 27) this.requestCloseModal(event);
// maintainFocus on tab key press
if (event.keyCode === 9) this.maintainFocus(event);
};
Dashboard.prototype.handleClickOutside = function handleClickOutside() {
if (this.opts.closeModalOnClickOutside) this.requestCloseModal();
};
Dashboard.prototype.handlePaste = function handlePaste(ev) {
var _this2 = this;
var files = toArray(ev.clipboardData.items);
files.forEach(function (file) {
if (file.kind !== 'file') return;
var blob = file.getAsFile();
if (!blob) {
_this2.uppy.log('[Dashboard] File pasted, but the file blob is empty');
_this2.uppy.info('Error pasting file', 'error');
return;
}
_this2.uppy.log('[Dashboard] File pasted');
_this2.uppy.addFile({
source: _this2.id,
name: file.name,
type: file.type,
data: blob
}).catch(function () {
// Ignore
});
});
};
Dashboard.prototype.handleInputChange = function handleInputChange(ev) {
var _this3 = this;
ev.preventDefault();
var files = toArray(ev.target.files);
files.forEach(function (file) {
_this3.uppy.addFile({
source: _this3.id,
name: file.name,
type: file.type,
data: file
}).catch(function () {
// Ignore
});
});
};
Dashboard.prototype.initEvents = function initEvents() {
var _this4 = this;
// Modal open button
var showModalTrigger = findAllDOMElements(this.opts.trigger);
if (!this.opts.inline && showModalTrigger) {
showModalTrigger.forEach(function (trigger) {
return trigger.addEventListener('click', _this4.openModal);
});
}
if (!this.opts.inline && !showModalTrigger) {
this.uppy.log('Dashboard modal trigger not found. Make sure `trigger` is set in Dashboard options unless you are planning to call openModal() method yourself');
}
if (!this.opts.inline) {
document.addEventListener('keydown', this.onKeydown);
}
// Drag Drop
this.removeDragDropListener = dragDrop(this.el, function (files) {
_this4.handleDrop(files);
});
this.uppy.on('dashboard:file-card', this.handleFileCard);
this.updateDashboardElWidth();
window.addEventListener('resize', this.updateDashboardElWidth);
};
Dashboard.prototype.removeEvents = function removeEvents() {
var _this5 = this;
var showModalTrigger = findAllDOMElements(this.opts.trigger);
if (!this.opts.inline && showModalTrigger) {
showModalTrigger.forEach(function (trigger) {
return trigger.removeEventListener('click', _this5.openModal);
});
}
if (!this.opts.inline) {
document.removeEventListener('keydown', this.onKeydown);
}
this.removeDragDropListener();
this.uppy.off('dashboard:file-card', this.handleFileCard);
window.removeEventListener('resize', this.updateDashboardElWidth);
};
Dashboard.prototype.updateDashboardElWidth = function updateDashboardElWidth() {
var dashboardEl = this.el.querySelector('.uppy-Dashboard-inner');
this.uppy.log('Dashboard width: ' + dashboardEl.offsetWidth);
this.setPluginState({
containerWidth: dashboardEl.offsetWidth
});
};
Dashboard.prototype.handleFileCard = function handleFileCard(fileId) {
this.setPluginState({
fileCardFor: fileId || false
});
};
Dashboard.prototype.handleDrop = function handleDrop(files) {
var _this6 = this;
this.uppy.log('[Dashboard] Files were dropped');
files.forEach(function (file) {
_this6.uppy.addFile({
source: _this6.id,
name: file.name,
type: file.type,
data: file
}).catch(function () {
// Ignore
});
});
};
Dashboard.prototype.render = function render(state) {
var _this7 = this;
var pluginState = this.getPluginState();
var files = state.files;
var newFiles = Object.keys(files).filter(function (file) {
return !files[file].progress.uploadStarted;
});
var inProgressFiles = Object.keys(files).filter(function (file) {
return !files[file].progress.uploadComplete && files[file].progress.uploadStarted && !files[file].isPaused;
});
var inProgressFilesArray = [];
inProgressFiles.forEach(function (file) {
inProgressFilesArray.push(files[file]);
});
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 attachRenderFunctionToTarget = function attachRenderFunctionToTarget(target) {
var plugin = _this7.uppy.getPlugin(target.id);
return _extends({}, target, {
icon: plugin.icon || _this7.opts.defaultTabIcon,
render: plugin.render
});
};
var isSupported = function isSupported(target) {
var plugin = _this7.uppy.getPlugin(target.id);
// If the plugin does not provide a `supported` check, assume the plugin works everywhere.
if (typeof plugin.isSupported !== 'function') {
return true;
}
return plugin.isSupported();
};
var acquirers = pluginState.targets.filter(function (target) {
return target.type === 'acquirer' && isSupported(target);
}).map(attachRenderFunctionToTarget);
var progressindicators = pluginState.targets.filter(function (target) {
return target.type === 'progressindicator';
}).map(attachRenderFunctionToTarget);
var startUpload = function startUpload(ev) {
_this7.uppy.upload().catch(function (err) {
// Log error.
_this7.uppy.log(err.stack || err.message || err);
});
};
var cancelUpload = function cancelUpload(fileID) {
_this7.uppy.emit('upload-cancel', fileID);
_this7.uppy.removeFile(fileID);
};
var showFileCard = function showFileCard(fileID) {
_this7.uppy.emit('dashboard:file-card', fileID);
};
var fileCardDone = function fileCardDone(meta, fileID) {
_this7.uppy.setFileMeta(fileID, meta);
_this7.uppy.emit('dashboard:file-card');
};
return DashboardUI({
state: state,
modal: pluginState,
newFiles: newFiles,
files: files,
totalFileCount: Object.keys(files).length,
totalProgress: state.totalProgress,
acquirers: acquirers,
activePanel: pluginState.activePanel,
getPlugin: this.uppy.getPlugin,
progressindicators: progressindicators,
autoProceed: this.uppy.opts.autoProceed,
hideUploadButton: this.opts.hideUploadButton,
id: this.id,
closeModal: this.requestCloseModal,
handleClickOutside: this.handleClickOutside,
handleInputChange: this.handleInputChange,
handlePaste: this.handlePaste,
showProgressDetails: this.opts.showProgressDetails,
inline: this.opts.inline,
showPanel: this.showPanel,
hideAllPanels: this.hideAllPanels,
log: this.uppy.log,
i18n: this.i18n,
addFile: this.uppy.addFile,
removeFile: this.uppy.removeFile,
info: this.uppy.info,
note: this.opts.note,
metaFields: this.getPluginState().metaFields,
resumableUploads: this.uppy.state.capabilities.resumableUploads || false,
startUpload: startUpload,
pauseUpload: this.uppy.pauseResume,
retryUpload: this.uppy.retryUpload,
cancelUpload: cancelUpload,
fileCardFor: pluginState.fileCardFor,
showFileCard: showFileCard,
fileCardDone: fileCardDone,
updateDashboardElWidth: this.updateDashboardElWidth,
maxWidth: this.opts.maxWidth,
maxHeight: this.opts.maxHeight,
currentWidth: pluginState.containerWidth,
isWide: pluginState.containerWidth > 400
});
};
Dashboard.prototype.discoverProviderPlugins = function discoverProviderPlugins() {
var _this8 = this;
this.uppy.iteratePlugins(function (plugin) {
if (plugin && !plugin.target && plugin.opts && plugin.opts.target === _this8.constructor) {
_this8.addTarget(plugin);
}
});
};
Dashboard.prototype.install = function install() {
var _this9 = this;
// Set default state for Modal
this.setPluginState({
isHidden: true,
showFileCard: false,
activePanel: false,
metaFields: this.opts.metaFields,
targets: []
});
var target = this.opts.target;
if (target) {
this.mount(target, this);
}
var plugins = this.opts.plugins || [];
plugins.forEach(function (pluginID) {
var plugin = _this9.uppy.getPlugin(pluginID);
if (plugin) plugin.mount(_this9, plugin);
});
if (!this.opts.disableStatusBar) {
this.uppy.use(StatusBar, {
target: this,
hideUploadButton: this.opts.hideUploadButton,
hideAfterFinish: this.opts.hideProgressAfterFinish,
locale: this.opts.locale
});
}
if (!this.opts.disableInformer) {
this.uppy.use(Informer, {
target: this
});
}
if (!this.opts.disableThumbnailGenerator) {
this.uppy.use(ThumbnailGenerator, {
thumbnailWidth: this.opts.thumbnailWidth
});
}
this.discoverProviderPlugins();
this.initEvents();
};
Dashboard.prototype.uninstall = function uninstall() {
var _this10 = this;
if (!this.opts.disableInformer) {
var informer = this.uppy.getPlugin('Informer');
// Checking if this plugin exists, in case it was removed by uppy-core
// before the Dashboard was.
if (informer) this.uppy.removePlugin(informer);
}
if (!this.opts.disableStatusBar) {
var statusBar = this.uppy.getPlugin('StatusBar');
if (statusBar) this.uppy.removePlugin(statusBar);
}
if (!this.opts.disableThumbnailGenerator) {
var thumbnail = this.uppy.getPlugin('ThumbnailGenerator');
if (thumbnail) this.uppy.removePlugin(thumbnail);
}
var plugins = this.opts.plugins || [];
plugins.forEach(function (pluginID) {
var plugin = _this10.uppy.getPlugin(pluginID);
if (plugin) plugin.unmount();
});
this.unmount();
this.removeEvents();
};
return Dashboard;
}(Plugin);
//# sourceMappingURL=index.js.map