|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +define([ |
| 7 | + 'jquery' |
| 8 | +], function ($) { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + return function (Element) { |
| 12 | + return Element.extend({ |
| 13 | + |
| 14 | + /** |
| 15 | + * {@inheritDoc} |
| 16 | + */ |
| 17 | + initUploader: function (fileInput) { |
| 18 | + _.extend(this.uploaderConfig, { |
| 19 | + dropZone: $(fileInput).closest(this.dropZone), |
| 20 | + change: this.onFilesChoosed.bind(this), |
| 21 | + drop: this.onFilesChoosed.bind(this), |
| 22 | + add: this.onBeforeFileUpload.bind(this), |
| 23 | + fail: this.onFail.bind(this), |
| 24 | + done: this.onFileUploaded.bind(this), |
| 25 | + start: this.onLoadingStart.bind(this), |
| 26 | + stop: this.onLoadingStop.bind(this) |
| 27 | + }); |
| 28 | + |
| 29 | + // uppy implementation |
| 30 | + if (fileInput !== undefined) { |
| 31 | + let targetElement = $(fileInput).closest('.file-uploader-area')[0], |
| 32 | + dropTargetElement = $(fileInput).closest(this.dropZone)[0], |
| 33 | + formKey = window.FORM_KEY !== undefined ? window.FORM_KEY : $.cookie('form_key'), |
| 34 | + fileInputName = this.fileInputName, |
| 35 | + arrayFromObj = Array.from, |
| 36 | + options = { |
| 37 | + proudlyDisplayPoweredByUppy: false, |
| 38 | + target: targetElement, |
| 39 | + hideUploadButton: true, |
| 40 | + hideRetryButton: true, |
| 41 | + hideCancelButton: true, |
| 42 | + inline: true, |
| 43 | + showRemoveButtonAfterComplete: true, |
| 44 | + showProgressDetails: false, |
| 45 | + showSelectedFiles: false, |
| 46 | + allowMultipleUploads: false, |
| 47 | + hideProgressAfterFinish: true |
| 48 | + }; |
| 49 | + |
| 50 | + if (fileInputName === undefined) { |
| 51 | + fileInputName = $(fileInput).attr('name'); |
| 52 | + } |
| 53 | + // handle input type file |
| 54 | + this.replaceInputTypeFile(fileInput); |
| 55 | + |
| 56 | + const uppy = new Uppy.Uppy({ |
| 57 | + autoProceed: true, |
| 58 | + |
| 59 | + onBeforeFileAdded: (currentFile) => { |
| 60 | + let file = currentFile, |
| 61 | + allowed = this.isFileAllowed(file); |
| 62 | + |
| 63 | + if (this.disabled()) { |
| 64 | + this.notifyError($t('The file upload field is disabled.')); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (!allowed.passed) { |
| 69 | + this.aggregateError(file.name, allowed.message); |
| 70 | + this.uploaderConfig.stop(); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // code to allow duplicate files from same folder |
| 75 | + const modifiedFile = { |
| 76 | + ...currentFile, |
| 77 | + id: currentFile.id + '-' + Date.now() |
| 78 | + }; |
| 79 | + |
| 80 | + this.onLoadingStart(); |
| 81 | + return modifiedFile; |
| 82 | + }, |
| 83 | + |
| 84 | + meta: { |
| 85 | + 'form_key': formKey, |
| 86 | + 'param_name': fileInputName, |
| 87 | + isAjax : true |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + // initialize Uppy upload |
| 92 | + uppy.use(Uppy.Dashboard, options); |
| 93 | + |
| 94 | + // drop area for file upload |
| 95 | + uppy.use(Uppy.DropTarget, { |
| 96 | + target: dropTargetElement, |
| 97 | + onDragOver: () => { |
| 98 | + // override Array.from method of legacy-build.min.js file |
| 99 | + Array.from = null; |
| 100 | + }, |
| 101 | + onDragLeave: () => { |
| 102 | + Array.from = arrayFromObj; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // upload files on server |
| 107 | + uppy.use(Uppy.XHRUpload, { |
| 108 | + endpoint: this.uploaderConfig.url, |
| 109 | + fieldName: fileInputName |
| 110 | + }); |
| 111 | + |
| 112 | + uppy.on('upload-success', (file, response) => { |
| 113 | + let data = { |
| 114 | + files : [response.body], |
| 115 | + result : response.body |
| 116 | + }; |
| 117 | + |
| 118 | + this.onFileUploaded('', data); |
| 119 | + }); |
| 120 | + |
| 121 | + uppy.on('upload-error', (file, error) => { |
| 122 | + console.error(error.message); |
| 123 | + console.error(error.status); |
| 124 | + }); |
| 125 | + |
| 126 | + uppy.on('complete', () => { |
| 127 | + this.onLoadingStop(); |
| 128 | + Array.from = arrayFromObj; |
| 129 | + }); |
| 130 | + } |
| 131 | + return this; |
| 132 | + }, |
| 133 | + |
| 134 | + /** |
| 135 | + * {@inheritDoc} |
| 136 | + */ |
| 137 | + replaceInputTypeFile: function (fileInput) { |
| 138 | + let fileId = fileInput.id, fileName = fileInput.name, |
| 139 | + spanElement = '<span id=\'' + fileId + '\'></span>'; |
| 140 | + |
| 141 | + $('#' + fileId).closest('.file-uploader-area').attr('upload-area-id', fileName); |
| 142 | + $(fileInput).replaceWith(spanElement); |
| 143 | + $('#' + fileId).closest('.file-uploader-area').find('.file-uploader-button:first').on('click', function () { |
| 144 | + $('#' + fileId).closest('.file-uploader-area').find('.uppy-Dashboard-browse').trigger('click'); |
| 145 | + }); |
| 146 | + $('#' + fileId).closest('.pagebuilder-image-uploader-container').find('.action-upload-image').on('click', function () { |
| 147 | + $('#' + fileId).closest('.file-uploader-area').find('.uppy-Dashboard-browse').trigger('click'); |
| 148 | + }); |
| 149 | + }, |
| 150 | + }); |
| 151 | + }; |
| 152 | +}); |
0 commit comments