UNPKG

uppy

Version:

Almost as cute as a Puppy :dog:

103 lines (73 loc) 4.62 kB
'use strict'; var _appendChild = require('yo-yoify/lib/appendChild'); 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 _require = require('../core/Utils'), toArray = _require.toArray; var Translator = require('../core/Translator'); module.exports = function (_Plugin) { _inherits(FileInput, _Plugin); function FileInput(core, opts) { _classCallCheck(this, FileInput); var _this = _possibleConstructorReturn(this, _Plugin.call(this, core, opts)); _this.id = 'FileInput'; _this.title = 'File Input'; _this.type = 'acquirer'; var defaultLocale = { strings: { selectToUpload: 'Select to upload' } // Default options };var defaultOptions = { target: '.UppyForm', getMetaFromForm: true, replaceTargetContent: true, multipleFiles: true, pretty: true, locale: defaultLocale, inputName: 'files[]' // 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); return _this; } FileInput.prototype.handleInputChange = function handleInputChange(ev) { var _this2 = this; this.core.log('All right, something selected through input...'); var files = toArray(ev.target.files); files.forEach(function (file) { _this2.core.addFile({ source: _this2.id, name: file.name, type: file.type, data: file }); }); }; FileInput.prototype.render = function render(state) { var _uppyFileInputInput, _uppy, _uppyFileInputBtn; var hiddenInputStyle = 'width: 0.1px; height: 0.1px; opacity: 0; overflow: hidden; position: absolute; z-index: -1;'; var input = (_uppyFileInputInput = document.createElement('input'), _uppyFileInputInput.setAttribute('style', '' + String(this.opts.pretty ? hiddenInputStyle : '') + ''), _uppyFileInputInput.setAttribute('type', 'file'), _uppyFileInputInput.setAttribute('name', '' + String(this.opts.inputName) + ''), _uppyFileInputInput.onchange = this.handleInputChange.bind(this), (this.opts.multipleFiles ? 'true' : 'false') && _uppyFileInputInput.setAttribute('multiple', 'multiple'), _uppyFileInputInput.setAttribute('value', ''), _uppyFileInputInput.setAttribute('class', 'uppy-FileInput-input'), _uppyFileInputInput); return _uppy = document.createElement('form'), _uppy.setAttribute('class', 'Uppy uppy-FileInput-form'), _appendChild(_uppy, [' ', input, ' ', this.opts.pretty ? (_uppyFileInputBtn = document.createElement('button'), _uppyFileInputBtn.setAttribute('type', 'button'), _uppyFileInputBtn.onclick = function () { return input.click(); }, _uppyFileInputBtn.setAttribute('class', 'uppy-FileInput-btn'), _appendChild(_uppyFileInputBtn, [' ', this.i18n('selectToUpload'), ' ']), _uppyFileInputBtn) : null, ' ']), _uppy; }; FileInput.prototype.install = function install() { var target = this.opts.target; var plugin = this; this.target = this.mount(target, plugin); }; FileInput.prototype.uninstall = function uninstall() { this.unmount(); }; return FileInput; }(Plugin); //# sourceMappingURL=FileInput.js.map