var invAjaxPaneUpload = (function() {
var animationMs = 250;
function appendAjaxUploadHTML($file, $form) {
var html = [
'
',
''
].join('');
$file.after(html);
html = [
''
].join('');
$form.append(html);
}
function init($pane) {
$pane.find('.pseudoform input:file').each(function() {
var dataobjs = [];
var uploaderrors = [];
//var clashes = [];
var addcount = 0;
var donecount = 0;
var lastprogress = 0;
var $file = $(this);
var $form = $(this).closest('.pseudoform');
appendAjaxUploadHTML($file, $form);
$file.fileupload({
url: $form.attr('data-uploadhandler'),
dataType: 'json',
add: function (e, data) {
var file = data.files[0];
// check file size
if($form.attr('data-uploadmaxfilesize') && (file.size > $form.attr('data-uploadmaxfilesize'))) {
alert(file.name + ' exceeds the maximum individual file upload size and will not be uploaded.');
return;
}
dataobjs.push(data);
addcount++;
$form.find('.file-feedback').html('' + addcount + ' files selected.').show();
},
done: function (e, data) {
// individual upload finished
donecount++;
var responseJSON = data.jqXHR.responseJSON;
if (responseJSON.status) {
// success
$form.append('')
} else {
// error
if(responseJSON.files) {
uploaderrors.push(responseJSON.files[0].error);
} else {
uploaderrors.push(null);
}
}
if (donecount == addcount) {
// all files done
if (!uploaderrors.length) {
// all ok - run callback!
$form.data('upload-finished-callback')();
return;
}
if (uploaderrors.length) {
$('Some or all files could not be uploaded.
').appendTo($form);
// reset gui
$form.find('.ajaxupload__progress').hide();
$form.find('.ajaxupload__progress .bar').css('width', '0%');
$form.find('.file-feedback').html('');
$form.find('.pseudosubmit').attr('disabled', false);
// reset data
dataobjs = [];
uploaderrors = [];
addcount = 0;
donecount = 0;
lastprogress = 0;
}
}
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
if (progress > lastprogress) {
$form.find('.ajaxupload__progress .bar').css('width', progress + '%');
lastprogress = progress;
}
},
error: function(){
alert('Oops, something went wrong.');
}
});
$form.data('do-upload', function(callback) {
$form.data('upload-finished-callback', callback);
if(!dataobjs.length) {
// no files to upload
callback();
return true;
}
// disable submit button
$form.find('.pseudosubmit').attr('disabled', true);
// start uploading files
$.each(dataobjs, function (i, dataobj) {
dataobj.submit();
});
// hide error
$form.find('.files-not-uploaded').remove();
// show progress bar
$form.find('.ajaxupload__progress').show();
});
});
}
return {
init: init
}
})();