UNPKG

5.01 kBJavaScriptView Raw
1var _class, _temp;
2
3function _extends() { _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; }; return _extends.apply(this, arguments); }
4
5function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
6
7function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
8
9var _require = require('@uppy/core'),
10 Plugin = _require.Plugin;
11
12var toArray = require('@uppy/utils/lib/toArray');
13
14var Translator = require('@uppy/utils/lib/Translator');
15
16var _require2 = require('preact'),
17 h = _require2.h;
18
19module.exports = (_temp = _class = /*#__PURE__*/function (_Plugin) {
20 _inheritsLoose(FileInput, _Plugin);
21
22 function FileInput(uppy, opts) {
23 var _this;
24
25 _this = _Plugin.call(this, uppy, opts) || this;
26 _this.id = _this.opts.id || 'FileInput';
27 _this.title = 'File Input';
28 _this.type = 'acquirer';
29 _this.defaultLocale = {
30 strings: {
31 // The same key is used for the same purpose by @uppy/robodog's `form()` API, but our
32 // locale pack scripts can't access it in Robodog. If it is updated here, it should
33 // also be updated there!
34 chooseFiles: 'Choose files'
35 }
36 }; // Default options
37
38 var defaultOptions = {
39 target: null,
40 pretty: true,
41 inputName: 'files[]'
42 }; // Merge default options with the ones set by user
43
44 _this.opts = _extends({}, defaultOptions, {}, opts);
45
46 _this.i18nInit();
47
48 _this.render = _this.render.bind(_assertThisInitialized(_this));
49 _this.handleInputChange = _this.handleInputChange.bind(_assertThisInitialized(_this));
50 _this.handleClick = _this.handleClick.bind(_assertThisInitialized(_this));
51 return _this;
52 }
53
54 var _proto = FileInput.prototype;
55
56 _proto.setOptions = function setOptions(newOpts) {
57 _Plugin.prototype.setOptions.call(this, newOpts);
58
59 this.i18nInit();
60 };
61
62 _proto.i18nInit = function i18nInit() {
63 this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale]);
64 this.i18n = this.translator.translate.bind(this.translator);
65 this.i18nArray = this.translator.translateArray.bind(this.translator);
66 this.setPluginState(); // so that UI re-renders and we see the updated locale
67 };
68
69 _proto.addFiles = function addFiles(files) {
70 var _this2 = this;
71
72 var descriptors = files.map(function (file) {
73 return {
74 source: _this2.id,
75 name: file.name,
76 type: file.type,
77 data: file
78 };
79 });
80
81 try {
82 this.uppy.addFiles(descriptors);
83 } catch (err) {
84 this.uppy.log(err);
85 }
86 };
87
88 _proto.handleInputChange = function handleInputChange(event) {
89 this.uppy.log('[FileInput] Something selected through input...');
90 var files = toArray(event.target.files);
91 this.addFiles(files); // We clear the input after a file is selected, because otherwise
92 // change event is not fired in Chrome and Safari when a file
93 // with the same name is selected.
94 // ___Why not use value="" on <input/> instead?
95 // Because if we use that method of clearing the input,
96 // Chrome will not trigger change if we drop the same file twice (Issue #768).
97
98 event.target.value = null;
99 };
100
101 _proto.handleClick = function handleClick(ev) {
102 this.input.click();
103 };
104
105 _proto.render = function render(state) {
106 var _this3 = this;
107
108 /* http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/ */
109 var hiddenInputStyle = {
110 width: '0.1px',
111 height: '0.1px',
112 opacity: 0,
113 overflow: 'hidden',
114 position: 'absolute',
115 zIndex: -1
116 };
117 var restrictions = this.uppy.opts.restrictions;
118 var accept = restrictions.allowedFileTypes ? restrictions.allowedFileTypes.join(',') : null;
119 return h("div", {
120 class: "uppy-Root uppy-FileInput-container"
121 }, h("input", {
122 class: "uppy-FileInput-input",
123 style: this.opts.pretty && hiddenInputStyle,
124 type: "file",
125 name: this.opts.inputName,
126 onchange: this.handleInputChange,
127 multiple: restrictions.maxNumberOfFiles !== 1,
128 accept: accept,
129 ref: function ref(input) {
130 _this3.input = input;
131 }
132 }), this.opts.pretty && h("button", {
133 class: "uppy-FileInput-btn",
134 type: "button",
135 onclick: this.handleClick
136 }, this.i18n('chooseFiles')));
137 };
138
139 _proto.install = function install() {
140 var target = this.opts.target;
141
142 if (target) {
143 this.mount(target, this);
144 }
145 };
146
147 _proto.uninstall = function uninstall() {
148 this.unmount();
149 };
150
151 return FileInput;
152}(Plugin), _class.VERSION = "1.4.14", _temp);
\No newline at end of file