UNPKG

11.1 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(['exports', '../../globals/js/settings', '../../globals/js/misc/mixin', '../../globals/js/mixins/create-component', '../../globals/js/mixins/init-component-by-search', '../../globals/js/mixins/evented-state', '../../globals/js/mixins/handles', '../../globals/js/misc/event-matches', '../../globals/js/misc/on'], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require('../../globals/js/settings'), require('../../globals/js/misc/mixin'), require('../../globals/js/mixins/create-component'), require('../../globals/js/mixins/init-component-by-search'), require('../../globals/js/mixins/evented-state'), require('../../globals/js/mixins/handles'), require('../../globals/js/misc/event-matches'), require('../../globals/js/misc/on'));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.settings, global.mixin, global.createComponent, global.initComponentBySearch, global.eventedState, global.handles, global.eventMatches, global.on);
11 global.fileUploader = mod.exports;
12 }
13})(this, function (exports, _settings, _mixin2, _createComponent, _initComponentBySearch, _eventedState, _handles, _eventMatches, _on) {
14 'use strict';
15
16 Object.defineProperty(exports, "__esModule", {
17 value: true
18 });
19
20 var _settings2 = _interopRequireDefault(_settings);
21
22 var _mixin3 = _interopRequireDefault(_mixin2);
23
24 var _createComponent2 = _interopRequireDefault(_createComponent);
25
26 var _initComponentBySearch2 = _interopRequireDefault(_initComponentBySearch);
27
28 var _eventedState2 = _interopRequireDefault(_eventedState);
29
30 var _handles2 = _interopRequireDefault(_handles);
31
32 var _eventMatches2 = _interopRequireDefault(_eventMatches);
33
34 var _on2 = _interopRequireDefault(_on);
35
36 function _interopRequireDefault(obj) {
37 return obj && obj.__esModule ? obj : {
38 default: obj
39 };
40 }
41
42 function _toConsumableArray(arr) {
43 if (Array.isArray(arr)) {
44 for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
45 arr2[i] = arr[i];
46 }
47
48 return arr2;
49 } else {
50 return Array.from(arr);
51 }
52 }
53
54 function _classCallCheck(instance, Constructor) {
55 if (!(instance instanceof Constructor)) {
56 throw new TypeError("Cannot call a class as a function");
57 }
58 }
59
60 var _createClass = function () {
61 function defineProperties(target, props) {
62 for (var i = 0; i < props.length; i++) {
63 var descriptor = props[i];
64 descriptor.enumerable = descriptor.enumerable || false;
65 descriptor.configurable = true;
66 if ("value" in descriptor) descriptor.writable = true;
67 Object.defineProperty(target, descriptor.key, descriptor);
68 }
69 }
70
71 return function (Constructor, protoProps, staticProps) {
72 if (protoProps) defineProperties(Constructor.prototype, protoProps);
73 if (staticProps) defineProperties(Constructor, staticProps);
74 return Constructor;
75 };
76 }();
77
78 function _possibleConstructorReturn(self, call) {
79 if (!self) {
80 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
81 }
82
83 return call && (typeof call === "object" || typeof call === "function") ? call : self;
84 }
85
86 function _inherits(subClass, superClass) {
87 if (typeof superClass !== "function" && superClass !== null) {
88 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
89 }
90
91 subClass.prototype = Object.create(superClass && superClass.prototype, {
92 constructor: {
93 value: subClass,
94 enumerable: false,
95 writable: true,
96 configurable: true
97 }
98 });
99 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
100 }
101
102 var FileUploader = function (_mixin) {
103 _inherits(FileUploader, _mixin);
104
105 /**
106 * File uploader.
107 * @extends CreateComponent
108 * @extends InitComponentBySearch
109 * @extends eventedState
110 * @extends Handles
111 * @param {HTMLElement} element The element working as a file uploader.
112 * @param {Object} [options] The component options. See static options.
113 */
114 function FileUploader(element) {
115 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
116
117 _classCallCheck(this, FileUploader);
118
119 var _this = _possibleConstructorReturn(this, (FileUploader.__proto__ || Object.getPrototypeOf(FileUploader)).call(this, element, options));
120
121 _this._changeState = function (state, detail, callback) {
122 if (state === 'delete-filename-fileuploader') {
123 _this.container.removeChild(detail.filenameElement);
124 }
125 if (typeof callback === 'function') {
126 callback();
127 }
128 };
129
130 _this._handleDeleteButton = function (evt) {
131 var target = (0, _eventMatches2.default)(evt, '[data-for=' + _this.inputId + ']');
132 if (target) {
133 _this._changeState('delete-filename-fileuploader', {
134 initialEvt: evt,
135 filenameElement: target.parentNode
136 });
137 }
138 };
139
140 _this.input = _this.element.querySelector(_this.options.selectorInput);
141 _this.container = _this.element.querySelector(_this.options.selectorContainer);
142
143 if (!_this.input) {
144 throw new TypeError('Cannot find the file input box.');
145 }
146
147 if (!_this.container) {
148 throw new TypeError('Cannot find the file names container.');
149 }
150
151 _this.inputId = _this.input.getAttribute('id');
152 _this.manage((0, _on2.default)(_this.input, 'change', function () {
153 return _this._displayFilenames();
154 }));
155 _this.manage((0, _on2.default)(_this.container, 'click', _this._handleDeleteButton));
156 return _this;
157 }
158
159 _createClass(FileUploader, [{
160 key: '_filenamesHTML',
161 value: function _filenamesHTML(name, id) {
162 return '<span class="' + this.options.classSelectedFile + '">\n <p class="' + this.options.classFileName + '">' + name + '</p>\n <span data-for="' + id + '" class="' + this.options.classStateContainer + '"></span>\n </span>';
163 }
164 }, {
165 key: '_uploadHTML',
166 value: function _uploadHTML() {
167 return '\n <div data-loading class="' + this.options.classLoading + '">\n <svg class="' + this.options.classLoadingSvg + '" viewBox="-42 -42 84 84">\n <circle cx="0" cy="0" r="37.5" />\n </svg>\n </div>';
168 }
169 }, {
170 key: '_closeButtonHTML',
171 value: function _closeButtonHTML() {
172 return '\n <svg class="' + this.options.classFileClose + '" tabindex="0" viewBox="0 0 16 16" fill-rule="evenodd" width="16" height="16">\n <path d="M8 0C3.6 0 0 3.6 0 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8zm3.5 10.1l-1.4 1.4L8\n 9.4l-2.1 2.1-1.4-1.4L6.6 8 4.5 5.9l1.4-1.4L8 6.6l2.1-2.1 1.4 1.4L9.4 8l2.1 2.1z" />\n </svg>';
173 }
174 }, {
175 key: '_checkmarkHTML',
176 value: function _checkmarkHTML() {
177 return '\n <svg class="' + this.options.classFileComplete + '" viewBox="0 0 16 16" fill-rule="evenodd" width="16" height="16">\n <path d="M8 0C3.6 0 0 3.6 0 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8zM6.7 11.5L3.4 8.1l1.4-1.4 1.9 1.9 4.1-4.1 1.4 1.4-5.5 5.6z"/>\n </svg>';
178 }
179 }, {
180 key: '_getStateContainers',
181 value: function _getStateContainers() {
182 var stateContainers = [].concat(_toConsumableArray(this.element.querySelectorAll('[data-for=' + this.inputId + ']')));
183
184 if (stateContainers.length === 0) {
185 throw new TypeError('State container elements not found; invoke _displayFilenames() first');
186 }
187
188 if (stateContainers[0].dataset.for !== this.inputId) {
189 throw new TypeError('File input id must equal [data-for] attribute');
190 }
191
192 return stateContainers;
193 }
194 }, {
195 key: '_displayFilenames',
196 value: function _displayFilenames() {
197 var _this2 = this;
198
199 var container = this.element.querySelector(this.options.selectorContainer);
200 var HTMLString = [].concat(_toConsumableArray(this.input.files)).map(function (file) {
201 return _this2._filenamesHTML(file.name, _this2.inputId);
202 }).join('');
203
204 container.insertAdjacentHTML('afterbegin', HTMLString);
205 }
206 }, {
207 key: '_removeState',
208 value: function _removeState(element) {
209 if (!element || element.nodeType !== Node.ELEMENT_NODE) {
210 throw new TypeError('DOM element should be given to initialize this widget.');
211 }
212 while (element.firstChild) {
213 element.removeChild(element.firstChild);
214 }
215 }
216 }, {
217 key: '_handleStateChange',
218 value: function _handleStateChange(elements, selectIndex, html) {
219 var _this3 = this;
220
221 if (selectIndex === undefined) {
222 elements.forEach(function (el) {
223 _this3._removeState(el);
224 el.insertAdjacentHTML('beforeend', html);
225 });
226 } else {
227 elements.forEach(function (el, index) {
228 if (index === selectIndex) {
229 _this3._removeState(el);
230 el.insertAdjacentHTML('beforeend', html);
231 }
232 });
233 }
234 }
235 }, {
236 key: 'setState',
237 value: function setState(state, selectIndex) {
238 var stateContainers = this._getStateContainers();
239
240 if (state === 'edit') {
241 this._handleStateChange(stateContainers, selectIndex, this._closeButtonHTML());
242 }
243
244 if (state === 'upload') {
245 this._handleStateChange(stateContainers, selectIndex, this._uploadHTML());
246 }
247
248 if (state === 'complete') {
249 this._handleStateChange(stateContainers, selectIndex, this._checkmarkHTML());
250 }
251 }
252 }], [{
253 key: 'options',
254 get: function get() {
255 var prefix = _settings2.default.prefix;
256
257 return {
258 selectorInit: '[data-file]',
259 selectorInput: 'input[type="file"].' + prefix + '--file-input',
260 selectorContainer: '[data-file-container]',
261 selectorCloseButton: '.' + prefix + '--file-close',
262 classLoading: prefix + '--loading',
263 classLoadingSvg: prefix + '--loading__svg',
264 classFileName: prefix + '--file-filename',
265 classFileClose: prefix + '--file-close',
266 classFileComplete: prefix + '--file-complete',
267 classSelectedFile: prefix + '--file__selected-file',
268 classStateContainer: prefix + '--file__state-container',
269 eventBeforeDeleteFilenameFileuploader: 'fileuploader-before-delete-filename',
270 eventAfterDeleteFilenameFileuploader: 'fileuploader-after-delete-filename'
271 };
272 }
273 }]);
274
275 return FileUploader;
276 }((0, _mixin3.default)(_createComponent2.default, _initComponentBySearch2.default, _eventedState2.default, _handles2.default));
277
278 FileUploader.components = new WeakMap();
279 exports.default = FileUploader;
280});
\No newline at end of file