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:
208 lines (166 loc) • 6.32 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 _require = require('../../core/Utils'),
toArray = _require.toArray;
var dragDrop = require('drag-drop');
var _require2 = require('preact'),
h = _require2.h;
/**
* Drag & Drop plugin
*
*/
module.exports = function (_Plugin) {
_inherits(DragDrop, _Plugin);
function DragDrop(uppy, opts) {
_classCallCheck(this, DragDrop);
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts));
_this.type = 'acquirer';
_this.id = _this.opts.id || 'DragDrop';
_this.title = 'Drag & Drop';
var defaultLocale = {
strings: {
dropHereOr: 'Drop files here or',
browse: 'browse'
}
// Default options
};var defaultOpts = {
target: null,
width: '100%',
height: '100%',
note: '',
locale: defaultLocale
// Merge default options with the ones set by user
};_this.opts = _extends({}, defaultOpts, opts);
// Check for browser dragDrop support
_this.isDragDropSupported = _this.checkDragDropSupport();
_this.locale = _extends({}, defaultLocale, _this.opts.locale);
_this.locale.strings = _extends({}, defaultLocale.strings, _this.opts.locale.strings);
// i18n
_this.translator = new Translator({ locale: _this.locale });
_this.i18n = _this.translator.translate.bind(_this.translator);
// Bind `this` to class methods
_this.handleDrop = _this.handleDrop.bind(_this);
_this.handleBrowseClick = _this.handleBrowseClick.bind(_this);
_this.handleInputChange = _this.handleInputChange.bind(_this);
_this.checkDragDropSupport = _this.checkDragDropSupport.bind(_this);
_this.render = _this.render.bind(_this);
return _this;
}
/**
* Checks if the browser supports Drag & Drop (not supported on mobile devices, for example).
* @return {Boolean}
*/
DragDrop.prototype.checkDragDropSupport = function checkDragDropSupport() {
var div = document.createElement('div');
if (!('draggable' in div) || !('ondragstart' in div && 'ondrop' in div)) {
return false;
}
if (!('FormData' in window)) {
return false;
}
if (!('FileReader' in window)) {
return false;
}
return true;
};
DragDrop.prototype.handleDrop = function handleDrop(files) {
var _this2 = this;
this.uppy.log('[DragDrop] Files dropped');
files.forEach(function (file) {
_this2.uppy.addFile({
source: _this2.id,
name: file.name,
type: file.type,
data: file
}).catch(function () {
// Ignore
});
});
};
DragDrop.prototype.handleInputChange = function handleInputChange(ev) {
var _this3 = this;
this.uppy.log('[DragDrop] Files selected through input');
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
});
});
};
DragDrop.prototype.handleBrowseClick = function handleBrowseClick(ev) {
ev.stopPropagation();
this.input.click();
};
DragDrop.prototype.render = function render(state) {
var _this4 = this;
var DragDropClass = 'uppy uppy-DragDrop-container ' + (this.isDragDropSupported ? 'is-dragdrop-supported' : '');
var DragDropStyle = {
width: this.opts.width,
height: this.opts.height
};
return h(
'div',
{ 'class': DragDropClass, style: DragDropStyle },
h(
'div',
{ 'class': 'uppy-DragDrop-inner' },
h(
'svg',
{ 'aria-hidden': 'true', 'class': 'UppyIcon uppy-DragDrop-arrow', width: '16', height: '16', viewBox: '0 0 16 16', xmlns: 'http://www.w3.org/2000/svg' },
h('path', { d: 'M11 10V0H5v10H2l6 6 6-6h-3zm0 0', 'fill-rule': 'evenodd' })
),
h('input', { 'class': 'uppy-DragDrop-input',
type: 'file',
name: 'files[]',
multiple: 'true',
ref: function ref(input) {
_this4.input = input;
},
onchange: this.handleInputChange }),
h(
'label',
{ 'class': 'uppy-DragDrop-label', onclick: this.handleBrowseClick },
this.i18n('dropHereOr'),
' ',
h(
'span',
{ 'class': 'uppy-DragDrop-dragText' },
this.i18n('browse')
)
),
h(
'span',
{ 'class': 'uppy-DragDrop-note' },
this.opts.note
)
)
);
};
DragDrop.prototype.install = function install() {
var _this5 = this;
var target = this.opts.target;
if (target) {
this.mount(target, this);
}
this.removeDragDropListener = dragDrop(this.el, function (files) {
_this5.handleDrop(files);
_this5.uppy.log(files);
});
};
DragDrop.prototype.uninstall = function uninstall() {
this.unmount();
this.removeDragDropListener();
};
return DragDrop;
}(Plugin);
//# sourceMappingURL=index.js.map