UNPKG

12.7 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports", "../../globals/js/feature-flags", "../../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/feature-flags"), 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.featureFlags, 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, _featureFlags, _settings, _mixin2, _createComponent, _initComponentBySearch, _eventedState, _handles, _eventMatches, _on) {
14 "use strict";
15
16 Object.defineProperty(_exports, "__esModule", {
17 value: true
18 });
19 _exports.default = void 0;
20 _settings = _interopRequireDefault(_settings);
21 _mixin2 = _interopRequireDefault(_mixin2);
22 _createComponent = _interopRequireDefault(_createComponent);
23 _initComponentBySearch = _interopRequireDefault(_initComponentBySearch);
24 _eventedState = _interopRequireDefault(_eventedState);
25 _handles = _interopRequireDefault(_handles);
26 _eventMatches = _interopRequireDefault(_eventMatches);
27 _on = _interopRequireDefault(_on);
28
29 function _interopRequireDefault(obj) {
30 return obj && obj.__esModule ? obj : {
31 default: obj
32 };
33 }
34
35 function _typeof(obj) {
36 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
37 _typeof = function _typeof(obj) {
38 return typeof obj;
39 };
40 } else {
41 _typeof = function _typeof(obj) {
42 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
43 };
44 }
45
46 return _typeof(obj);
47 }
48
49 function _classCallCheck(instance, Constructor) {
50 if (!(instance instanceof Constructor)) {
51 throw new TypeError("Cannot call a class as a function");
52 }
53 }
54
55 function _defineProperties(target, props) {
56 for (var i = 0; i < props.length; i++) {
57 var descriptor = props[i];
58 descriptor.enumerable = descriptor.enumerable || false;
59 descriptor.configurable = true;
60 if ("value" in descriptor) descriptor.writable = true;
61 Object.defineProperty(target, descriptor.key, descriptor);
62 }
63 }
64
65 function _createClass(Constructor, protoProps, staticProps) {
66 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
67 if (staticProps) _defineProperties(Constructor, staticProps);
68 return Constructor;
69 }
70
71 function _possibleConstructorReturn(self, call) {
72 if (call && (_typeof(call) === "object" || typeof call === "function")) {
73 return call;
74 }
75
76 return _assertThisInitialized(self);
77 }
78
79 function _assertThisInitialized(self) {
80 if (self === void 0) {
81 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
82 }
83
84 return self;
85 }
86
87 function _getPrototypeOf(o) {
88 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
89 return o.__proto__ || Object.getPrototypeOf(o);
90 };
91 return _getPrototypeOf(o);
92 }
93
94 function _inherits(subClass, superClass) {
95 if (typeof superClass !== "function" && superClass !== null) {
96 throw new TypeError("Super expression must either be null or a function");
97 }
98
99 subClass.prototype = Object.create(superClass && superClass.prototype, {
100 constructor: {
101 value: subClass,
102 writable: true,
103 configurable: true
104 }
105 });
106 if (superClass) _setPrototypeOf(subClass, superClass);
107 }
108
109 function _setPrototypeOf(o, p) {
110 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
111 o.__proto__ = p;
112 return o;
113 };
114
115 return _setPrototypeOf(o, p);
116 }
117
118 var toArray = function toArray(arrayLike) {
119 return Array.prototype.slice.call(arrayLike);
120 };
121
122 var FileUploader =
123 /*#__PURE__*/
124 function (_mixin) {
125 _inherits(FileUploader, _mixin);
126 /**
127 * File uploader.
128 * @extends CreateComponent
129 * @extends InitComponentBySearch
130 * @extends eventedState
131 * @extends Handles
132 * @param {HTMLElement} element The element working as a file uploader.
133 * @param {Object} [options] The component options. See static options.
134 */
135
136
137 function FileUploader(element) {
138 var _this;
139
140 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
141
142 _classCallCheck(this, FileUploader);
143
144 _this = _possibleConstructorReturn(this, _getPrototypeOf(FileUploader).call(this, element, options));
145
146 _this._changeState = function (state, detail, callback) {
147 if (state === 'delete-filename-fileuploader') {
148 _this.container.removeChild(detail.filenameElement);
149 }
150
151 if (typeof callback === 'function') {
152 callback();
153 }
154 };
155
156 _this._handleDeleteButton = function (evt) {
157 var target = (0, _eventMatches.default)(evt, "[data-for=".concat(_this.inputId, "]"));
158
159 if (target) {
160 _this._changeState('delete-filename-fileuploader', {
161 initialEvt: evt,
162 filenameElement: target.parentNode
163 });
164 }
165 };
166
167 _this.input = _this.element.querySelector(_this.options.selectorInput);
168 _this.container = _this.element.querySelector(_this.options.selectorContainer);
169
170 if (!_this.input) {
171 throw new TypeError('Cannot find the file input box.');
172 }
173
174 if (!_this.container) {
175 throw new TypeError('Cannot find the file names container.');
176 }
177
178 _this.inputId = _this.input.getAttribute('id');
179
180 _this.manage((0, _on.default)(_this.input, 'change', function () {
181 return _this._displayFilenames();
182 }));
183
184 _this.manage((0, _on.default)(_this.container, 'click', _this._handleDeleteButton));
185
186 return _this;
187 }
188
189 _createClass(FileUploader, [{
190 key: "_filenamesHTML",
191 value: function _filenamesHTML(name, id) {
192 return "<span class=\"".concat(this.options.classSelectedFile, "\">\n <p class=\"").concat(this.options.classFileName, "\">").concat(name, "</p>\n <span data-for=\"").concat(id, "\" class=\"").concat(this.options.classStateContainer, "\"></span>\n </span>");
193 }
194 }, {
195 key: "_uploadHTML",
196 value: function _uploadHTML() {
197 return "\n <div data-loading class=\"".concat(this.options.classLoading, "\">\n <svg class=\"").concat(this.options.classLoadingSvg, "\" viewBox=\"-42 -42 84 84\">\n <circle cx=\"0\" cy=\"0\" r=\"37.5\" />\n </svg>\n </div>");
198 }
199 }, {
200 key: "_closeButtonHTML",
201 value: function _closeButtonHTML() {
202 if (_featureFlags.componentsX) {
203 return "\n <button class=\"".concat(this.options.classFileClose, "\" type=\"button\" aria-label=\"close\">\n <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" width=\"16\" height=\"16\">\n <path fill=\"#231F20\" d=\"M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z\"/>\n </svg>\n </button>");
204 }
205
206 return "\n <svg class=\"".concat(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>");
207 }
208 }, {
209 key: "_checkmarkHTML",
210 value: function _checkmarkHTML() {
211 return "\n <svg class=\"".concat(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>");
212 }
213 }, {
214 key: "_getStateContainers",
215 value: function _getStateContainers() {
216 var stateContainers = toArray(this.element.querySelectorAll("[data-for=".concat(this.inputId, "]")));
217
218 if (stateContainers.length === 0) {
219 throw new TypeError('State container elements not found; invoke _displayFilenames() first');
220 }
221
222 if (stateContainers[0].dataset.for !== this.inputId) {
223 throw new TypeError('File input id must equal [data-for] attribute');
224 }
225
226 return stateContainers;
227 }
228 /**
229 * Inject selected files into DOM. Invoked on change event.
230 */
231
232 }, {
233 key: "_displayFilenames",
234 value: function _displayFilenames() {
235 var _this2 = this;
236
237 var container = this.element.querySelector(this.options.selectorContainer);
238 var HTMLString = toArray(this.input.files).map(function (file) {
239 return _this2._filenamesHTML(file.name, _this2.inputId);
240 }).join('');
241 container.insertAdjacentHTML('afterbegin', HTMLString);
242 }
243 }, {
244 key: "_removeState",
245 value: function _removeState(element) {
246 if (!element || element.nodeType !== Node.ELEMENT_NODE) {
247 throw new TypeError('DOM element should be given to initialize this widget.');
248 }
249
250 while (element.firstChild) {
251 element.removeChild(element.firstChild);
252 }
253 }
254 }, {
255 key: "_handleStateChange",
256 value: function _handleStateChange(elements, selectIndex, html) {
257 var _this3 = this;
258
259 if (selectIndex === undefined) {
260 elements.forEach(function (el) {
261 _this3._removeState(el);
262
263 el.insertAdjacentHTML('beforeend', html);
264 });
265 } else {
266 elements.forEach(function (el, index) {
267 if (index === selectIndex) {
268 _this3._removeState(el);
269
270 el.insertAdjacentHTML('beforeend', html);
271 }
272 });
273 }
274 }
275 /**
276 * Handles delete button.
277 * @param {Event} evt The event triggering this action.
278 * @private
279 */
280
281 }, {
282 key: "setState",
283 value: function setState(state, selectIndex) {
284 var stateContainers = this._getStateContainers();
285
286 if (state === 'edit') {
287 this._handleStateChange(stateContainers, selectIndex, this._closeButtonHTML());
288 }
289
290 if (state === 'upload') {
291 this._handleStateChange(stateContainers, selectIndex, this._uploadHTML());
292 }
293
294 if (state === 'complete') {
295 this._handleStateChange(stateContainers, selectIndex, this._checkmarkHTML());
296 }
297 }
298 /**
299 * The map associating DOM element and file uploader instance.
300 * @member FileUploader.components
301 * @type {WeakMap}
302 */
303
304 }], [{
305 key: "options",
306 get: function get() {
307 var prefix = _settings.default.prefix;
308 return {
309 selectorInit: '[data-file]',
310 selectorInput: "input[type=\"file\"].".concat(prefix, "--file-input"),
311 selectorContainer: '[data-file-container]',
312 selectorCloseButton: ".".concat(prefix, "--file-close"),
313 classLoading: "".concat(prefix, "--loading"),
314 classLoadingSvg: "".concat(prefix, "--loading__svg"),
315 classFileName: "".concat(prefix, "--file-filename"),
316 classFileClose: "".concat(prefix, "--file-close"),
317 classFileComplete: "".concat(prefix, "--file-complete"),
318 classSelectedFile: "".concat(prefix, "--file__selected-file"),
319 classStateContainer: "".concat(prefix, "--file__state-container"),
320 eventBeforeDeleteFilenameFileuploader: 'fileuploader-before-delete-filename',
321 eventAfterDeleteFilenameFileuploader: 'fileuploader-after-delete-filename'
322 };
323 }
324 }]);
325
326 FileUploader.components = new WeakMap();
327 return FileUploader;
328 }((0, _mixin2.default)(_createComponent.default, _initComponentBySearch.default, _eventedState.default, _handles.default));
329
330 var _default = FileUploader;
331 _exports.default = _default;
332});
\No newline at end of file