98 lines
3.4 KiB
JavaScript
98 lines
3.4 KiB
JavaScript
define([
|
|
'jquery',
|
|
'uiRegistry',
|
|
'mage/translate',
|
|
'mage/backend/notification'
|
|
], function ($, registry) {
|
|
'use strict';
|
|
|
|
function notify(message, isError) {
|
|
$('body').notification('add', {
|
|
error: isError,
|
|
message: message,
|
|
insertMethod: function (renderedMessage) {
|
|
$('.page-main-actions').after(renderedMessage);
|
|
}
|
|
});
|
|
}
|
|
|
|
return function (config, element) {
|
|
var $container = $(element),
|
|
$form = $container.find('[data-role=upload-form]'),
|
|
$button = $form.find('button[type=submit]'),
|
|
$progress = $form.find('[data-role=upload-progress]'),
|
|
$progressBar = $form.find('[data-role=upload-progress-bar]'),
|
|
$progressLabel = $form.find('[data-role=upload-progress-label]');
|
|
|
|
function setProgress(percent) {
|
|
$progressBar.val(percent);
|
|
$progressLabel.text(percent + '%');
|
|
}
|
|
|
|
$form.on('submit', function (event) {
|
|
var data;
|
|
|
|
event.preventDefault();
|
|
if (!$form.find('input[type=file]')[0].files.length) {
|
|
notify($.mage.__('Please select at least one file.'), true);
|
|
return;
|
|
}
|
|
|
|
data = new FormData($form[0]);
|
|
$button.prop('disabled', true);
|
|
setProgress(0);
|
|
$progress.prop('hidden', false);
|
|
$.ajax({
|
|
url: $form.attr('action'),
|
|
method: 'POST',
|
|
data: data,
|
|
processData: false,
|
|
contentType: false,
|
|
dataType: 'json',
|
|
xhr: function () {
|
|
var xhr = $.ajaxSettings.xhr();
|
|
|
|
if (xhr.upload) {
|
|
xhr.upload.addEventListener('progress', function (progressEvent) {
|
|
if (progressEvent.lengthComputable) {
|
|
setProgress(Math.round(
|
|
progressEvent.loaded / progressEvent.total * 100
|
|
));
|
|
}
|
|
});
|
|
}
|
|
|
|
return xhr;
|
|
}
|
|
}).done(function (response) {
|
|
setProgress(100);
|
|
if (response.ajaxExpired && response.ajaxRedirect) {
|
|
window.location.href = response.ajaxRedirect;
|
|
return;
|
|
}
|
|
$('body').notification('clear');
|
|
if (response.message) {
|
|
notify(response.message, response.error);
|
|
}
|
|
$.each(response.errors || [], function (index, message) {
|
|
notify(message, true);
|
|
});
|
|
if ((response.files || []).length) {
|
|
$form[0].reset();
|
|
registry.async(
|
|
'esun_filemanager_file_listing.esun_filemanager_file_listing_data_source'
|
|
)(function (provider) {
|
|
provider.reload({refresh: true});
|
|
});
|
|
}
|
|
}).fail(function () {
|
|
notify($.mage.__('The upload request failed.'), true);
|
|
}).always(function () {
|
|
$button.prop('disabled', false);
|
|
$progress.prop('hidden', true);
|
|
setProgress(0);
|
|
});
|
|
});
|
|
};
|
|
});
|