allow uploading/deleting multiple documents, fixes #1960. replace select option with item aware add button, fixes #1961

This commit is contained in:
j 2014-01-02 10:11:27 +00:00
commit 19b8bcfc83
5 changed files with 135 additions and 109 deletions

View file

@ -1,16 +1,17 @@
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
pandora.ui.uploadDocumentDialog = function(file, callback) {
pandora.ui.uploadDocumentDialog = function(files, callback) {
var extension = file.name.split('.').pop().toLowerCase(),
var extensions = files.map(function(file) {
return file.name.split('.').pop().toLowerCase()
}),
extensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'],
supportedExtensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'],
filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension),
filename,
id = pandora.user.username + ':' + filename,
ids = [],
upload,
@ -19,7 +20,7 @@ pandora.ui.uploadDocumentDialog = function(file, callback) {
$content = Ox.Element().css({margin: '16px'}),
$text = $('<div>')
.html(Ox._('Uploading {0}', [file.name]))
.html(Ox._('Uploading {0}', [files[0].name]))
.appendTo($content),
$progress = Ox.Progressbar({
@ -43,10 +44,10 @@ pandora.ui.uploadDocumentDialog = function(file, callback) {
var title = this.options('title');
$uploadDialog.close();
if (title == Ox._('Cancel Upload')) {
upload.abort();
upload && upload.abort();
} else if (title == Ox._('Done')) {
callback({
id: id
ids: ids
});
}
}
@ -56,57 +57,49 @@ pandora.ui.uploadDocumentDialog = function(file, callback) {
height: 112,
keys: {escape: 'close'},
width: 288,
title: Ox._('Upload File')
title: files.length == 1
? Ox._('Upload Document')
: Ox._('Upload {0} Documents', [files.length])
})
.bindEvent({
open: function() {
upload = pandora.chunkupload({
data: {
filename: filename
},
file: file,
url: '/api/upload/document/',
})
.bindEvent({
done: function(data) {
if (data.progress == 1) {
$uploadDialog.options('buttons')[0].options({title: Ox._('Done')});
Ox.print('SUCCEEDED');
} else {
$message.html(Ox._('Upload failed.'))
$uploadDialog.options('buttons')[0].options({title: Ox._('Close')});
Ox.print('FAILED');
}
},
progress: function(data) {
$progress.options({progress: data.progress || 0});
}
});
uploadFile(0);
}
});
if (!Ox.contains(extensions, extension)) {
if (!Ox.every(extensions, function(extension) {
return Ox.contains(supportedExtensions, extension)
})) {
return errorDialog(Ox._('Supported file types are GIF, JPG, PNG and PDF.'));
} else {
Ox.oshash(file, function(oshash) {
pandora.api.findDocuments({
keys: ['id'],
query: {
conditions: [{key: 'oshash', value: oshash, operator: '=='}],
operator: '&'
},
range: [0, 1],
sort: [{key: 'name', operator: '+'}]
}, function(result) {
if (result.data.items.length) {
errorDialog(filename == result.data.items[0].id
? Ox._('The file {0} already exists', [filename])
: Ox._('The file {0} already exists as {1}', [filename, result.data.items[0].id])
).open();
} else {
$uploadDialog.open();
}
})
var valid = true;
Ox.parallelForEach(files, function(file, index, array, callback) {
var extension = file.name.split('.').pop().toLowerCase(),
filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension);
valid && Ox.oshash(file, function(oshash) {
pandora.api.findDocuments({
keys: ['id', 'user', 'name', 'extension'],
query: {
conditions: [{key: 'oshash', value: oshash, operator: '=='}],
operator: '&'
},
range: [0, 1],
sort: [{key: 'name', operator: '+'}]
}, function(result) {
if (result.data.items.length) {
var id = result.data.items[0].name + '.' + result.data.items[0].extension;
valid && errorDialog(filename == id
? Ox._('The file {0} already exists', [filename])
: Ox._('The file {0} already exists as {1}', [filename, id])
).open();
valid = false;
}
callback();
})
});
} ,function() {
valid && $uploadDialog.open();
});
return {open: Ox.noop};
}
@ -125,9 +118,46 @@ pandora.ui.uploadDocumentDialog = function(file, callback) {
})
],
content: text,
title: Ox._('Upload File')
title: Ox._('Upload Document')
});
}
function uploadFile(part) {
var file = files[part],
extension = file.name.split('.').pop().toLowerCase(),
filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension);
$text.html(Ox._('Uploading {0}', [file.name]));
upload = pandora.chunkupload({
data: {
filename: filename
},
file: file,
url: '/api/upload/document/',
})
.bindEvent({
done: function(data) {
if (data.progress == 1) {
part++;
if (part == files.length) {
$progress.options({progress: data.progress});
$uploadDialog.options('buttons')[0].options({title: Ox._('Done')});
ids.push(data.id);
} else {
uploadFile(part);
}
} else {
$message.html(Ox._('Upload failed.'))
$uploadDialog.options('buttons')[0].options({title: Ox._('Close')});
}
},
progress: function(data) {
var progress = data.progress || 0;
progress = part/files.length + 1/files.length * progress;
$progress.options({progress: progress});
}
});
}
};