This commit is contained in:
Marcin Kurczewski 2013-10-19 16:00:47 +02:00
parent d85728064a
commit ab26cb8ef2

View file

@ -53,89 +53,83 @@ $(function()
}); });
$('#the-submit').click(function(e) function sendNextPost()
{ {
e.preventDefault();
var theSubmit = $(this);
theSubmit.addClass('inactive');
var posts = $('#upload-step2 .post'); var posts = $('#upload-step2 .post');
if (posts.length == 0) if (posts.length == 0)
{ {
//shouldn't happen uploadFinished();
alert('No posts to upload!');
return; return;
} }
var ajaxCalls = []; var postDom = posts.first();
posts.each(function() var url = postDom.find('form').attr('action') + '?json';
var file = postDom.data('file');
var tags = postDom.find('[name=tags]').val();
var safety = postDom.find('[name=safety]:checked').val();
var fd = new FormData();
fd.append('file', file);
fd.append('tags', tags);
fd.append('safety', safety);
var ajaxData =
{ {
var postDom = $(this); url: url,
var url = postDom.find('form').attr('action') + '?json'; data: fd,
var file = postDom.data('file'); processData: false,
var tags = postDom.find('[name=tags]').val(); contentType: false,
var safety = postDom.find('[name=safety]:checked').val(); dataType: 'json',
var fd = new FormData(); type: 'POST',
fd.append('file', file); success: function(data)
fd.append('tags', tags);
fd.append('safety', safety);
postDom.find(':input').attr('readonly', true);
postDom.addClass('inactive');
var ajaxData =
{ {
url: url,
data: fd,
processData: false,
contentType: false,
type: 'POST',
context: postDom
};
var defer = $.ajax(ajaxData)
.then(function(data)
{
data.postDom = $(this);
return data;
}).promise();
ajaxCalls.push(defer);
});
$.when.all(ajaxCalls).then(function(allData)
{
var errors = false;
for (var i in allData)
{
var data = allData[i];
var postDom = data.postDom;
if (data['success']) if (data['success'])
{ {
postDom.slideUp(function() postDom.slideUp(function()
{ {
$(this).remove(); postDom.remove();
sendNextPost();
}); });
} }
else else
{ {
postDom.removeClass('inactive');
postDom.find(':input').attr('readonly', false);
postDom.find('.alert').html(data['errorHtml']).slideDown(); postDom.find('.alert').html(data['errorHtml']).slideDown();
errors = true; enableUpload();
} }
} }
};
if (errors) $.ajax(ajaxData);
{ }
theSubmit.removeClass('inactive');
} function uploadFinished()
else {
{ window.location.href = $('#upload-step2').attr('data-redirect-url');
window.location.href = $('#upload-step2').attr('data-redirect-url'); }
}
}); function disableUpload()
{
var theSubmit = $('#the-submit');
theSubmit.addClass('inactive');
var posts = $('#upload-step2 .post');
posts.find(':input').attr('readonly', true);
posts.addClass('inactive');
}
function enableUpload()
{
var theSubmit = $('#the-submit');
theSubmit.removeClass('inactive');
var posts = $('#upload-step2 .post');
posts.removeClass('inactive');
posts.find(':input').attr('readonly', false);
}
$('#the-submit').click(function(e)
{
e.preventDefault();
var theSubmit = $(this);
disableUpload();
sendNextPost();
}); });
function handleFiles(files) function handleFiles(files)