refactor/cleanup uploader.js

v1.18.x
barisusakli 9 years ago
parent af51500951
commit 4b5ec512b9

@ -1,6 +1,6 @@
'use strict'; 'use strict';
/* globals define, templates, translator */ /* globals define, templates */
define('uploader', ['csrf', 'translator'], function(csrf, translator) { define('uploader', ['csrf', 'translator'], function(csrf, translator) {
@ -35,12 +35,18 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
uploadForm.attr('action', data.route); uploadForm.attr('action', data.route);
uploadForm.find('#params').val(JSON.stringify(data.params)); uploadForm.find('#params').val(JSON.stringify(data.params));
uploadModal.find('#fileUploadSubmitBtn').off('click').on('click', function() { uploadModal.find('#fileUploadSubmitBtn').on('click', function() {
uploadForm.submit(); uploadForm.submit();
}); });
uploadForm.off('submit').submit(function() { uploadForm.submit(function() {
onSubmit(uploadModal, callback);
return false;
});
});
};
function onSubmit(uploadModal, callback) {
function showAlert(type, message) { function showAlert(type, message) {
module.hideAlerts(uploadModal); module.hideAlerts(uploadModal);
uploadModal.find('#alert-' + type).translateText(message).removeClass('hide'); uploadModal.find('#alert-' + type).translateText(message).removeClass('hide');
@ -52,11 +58,10 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
uploadModal.find('#upload-progress-box').show().removeClass('hide'); uploadModal.find('#upload-progress-box').show().removeClass('hide');
if (!uploadModal.find('#fileInput').val()) { if (!uploadModal.find('#fileInput').val()) {
showAlert('error', '[[uploads:select-file-to-upload]]'); return showAlert('error', '[[uploads:select-file-to-upload]]');
return false;
} }
$(this).ajaxSubmit({ uploadModal.find('#uploadForm').ajaxSubmit({
headers: { headers: {
'x-csrf-token': csrf.get() 'x-csrf-token': csrf.get()
}, },
@ -64,17 +69,14 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
xhr = maybeParse(xhr); xhr = maybeParse(xhr);
showAlert('error', xhr.responseJSON ? (xhr.responseJSON.error || xhr.statusText) : 'error uploading, code : ' + xhr.status); showAlert('error', xhr.responseJSON ? (xhr.responseJSON.error || xhr.statusText) : 'error uploading, code : ' + xhr.status);
}, },
uploadProgress: function(event, position, total, percent) { uploadProgress: function(event, position, total, percent) {
uploadModal.find('#upload-progress-bar').css('width', percent + '%'); uploadModal.find('#upload-progress-bar').css('width', percent + '%');
}, },
success: function(response) { success: function(response) {
response = maybeParse(response); response = maybeParse(response);
if (response.error) { if (response.error) {
showAlert('error', response.error); return showAlert('error', response.error);
return;
} }
callback(response[0].url); callback(response[0].url);
@ -86,11 +88,7 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
}, 750); }, 750);
} }
}); });
}
return false;
});
});
};
function parseModal(tplVals, callback) { function parseModal(tplVals, callback) {
templates.parse('partials/modals/upload_file_modal', tplVals, function(html) { templates.parse('partials/modals/upload_file_modal', tplVals, function(html) {

Loading…
Cancel
Save