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:
130 lines (101 loc) • 4.29 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 _require = require('../core/Utils'),
toArray = _require.toArray;
var Translator = require('../core/Translator');
var _require2 = require('preact'),
h = _require2.h;
module.exports = function (_Plugin) {
_inherits(FileInput, _Plugin);
function FileInput(uppy, opts) {
_classCallCheck(this, FileInput);
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts));
_this.id = _this.opts.id || 'FileInput';
_this.title = 'File Input';
_this.type = 'acquirer';
var defaultLocale = {
strings: {
chooseFiles: 'Choose files'
}
// Default options
};var defaultOptions = {
target: null,
allowMultipleFiles: true,
pretty: true,
inputName: 'files[]',
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);
// i18n
_this.translator = new Translator({ locale: _this.locale });
_this.i18n = _this.translator.translate.bind(_this.translator);
_this.render = _this.render.bind(_this);
_this.handleInputChange = _this.handleInputChange.bind(_this);
_this.handleClick = _this.handleClick.bind(_this);
return _this;
}
FileInput.prototype.handleInputChange = function handleInputChange(ev) {
var _this2 = this;
this.uppy.log('[FileInput] Something selected through input...');
var files = toArray(ev.target.files);
files.forEach(function (file) {
_this2.uppy.addFile({
source: _this2.id,
name: file.name,
type: file.type,
data: file
}).catch(function () {
// Ignore
});
});
};
FileInput.prototype.handleClick = function handleClick(ev) {
this.input.click();
};
FileInput.prototype.render = function render(state) {
var _this3 = this;
var hiddenInputStyle = {
width: '0.1px',
height: '0.1px',
opacity: 0,
overflow: 'hidden',
position: 'absolute',
zIndex: -1
};
return h(
'div',
{ 'class': 'uppy uppy-FileInput-container' },
h('input', { 'class': 'uppy-FileInput-input',
style: this.opts.pretty && hiddenInputStyle,
type: 'file',
name: this.opts.inputName,
onchange: this.handleInputChange,
multiple: this.opts.allowMultipleFiles,
ref: function ref(input) {
_this3.input = input;
} }),
this.opts.pretty && h(
'button',
{ 'class': 'uppy-FileInput-btn', type: 'button', onclick: this.handleClick },
this.i18n('chooseFiles')
)
);
};
FileInput.prototype.install = function install() {
var target = this.opts.target;
if (target) {
this.mount(target, this);
}
};
FileInput.prototype.uninstall = function uninstall() {
this.unmount();
};
return FileInput;
}(Plugin);
//# sourceMappingURL=FileInput.js.map