2013-03-24 09:43:24 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-25 21:08:13 +00:00
|
|
|
pandora.ui.uploadDocumentDialog = function(options, callback) {
|
|
|
|
var files = options.files,
|
|
|
|
extensions = files.map(function(file) {
|
2014-01-02 10:11:27 +00:00
|
|
|
return file.name.split('.').pop().toLowerCase()
|
|
|
|
}),
|
2013-03-24 09:43:24 +00:00
|
|
|
|
2014-01-02 10:11:27 +00:00
|
|
|
supportedExtensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'],
|
2013-03-24 09:43:24 +00:00
|
|
|
|
2014-01-02 10:11:27 +00:00
|
|
|
filename,
|
2013-04-28 17:57:24 +00:00
|
|
|
|
2014-01-02 10:11:27 +00:00
|
|
|
ids = [],
|
2013-04-28 17:57:24 +00:00
|
|
|
|
2013-03-24 09:43:24 +00:00
|
|
|
upload,
|
|
|
|
|
2013-03-24 11:30:18 +00:00
|
|
|
$errorDialog,
|
2013-03-24 09:43:24 +00:00
|
|
|
|
2013-03-24 11:30:18 +00:00
|
|
|
$content = Ox.Element().css({margin: '16px'}),
|
2013-03-24 09:45:09 +00:00
|
|
|
|
|
|
|
$text = $('<div>')
|
2014-01-02 10:11:27 +00:00
|
|
|
.html(Ox._('Uploading {0}', [files[0].name]))
|
2013-03-24 09:45:09 +00:00
|
|
|
.appendTo($content),
|
|
|
|
|
|
|
|
$progress = Ox.Progressbar({
|
|
|
|
width: 256,
|
|
|
|
showPercent: true,
|
|
|
|
showTime: true
|
|
|
|
})
|
2013-03-24 11:30:18 +00:00
|
|
|
.css({margin: '16px 0 16px 0'})
|
|
|
|
.appendTo($content),
|
|
|
|
|
|
|
|
$message = $('<div>')
|
2013-03-24 09:45:09 +00:00
|
|
|
.appendTo($content),
|
2013-03-24 09:43:24 +00:00
|
|
|
|
|
|
|
$uploadDialog = Ox.Dialog({
|
|
|
|
buttons: [
|
|
|
|
Ox.Button({
|
|
|
|
id: 'close',
|
2013-05-09 10:13:58 +00:00
|
|
|
title: Ox._('Cancel Upload')
|
2013-03-24 09:43:24 +00:00
|
|
|
}).bindEvent({
|
|
|
|
click: function() {
|
2013-04-26 07:40:46 +00:00
|
|
|
var title = this.options('title');
|
|
|
|
$uploadDialog.close();
|
2013-05-09 10:13:58 +00:00
|
|
|
if (title == Ox._('Cancel Upload')) {
|
2014-01-02 10:11:27 +00:00
|
|
|
upload && upload.abort();
|
2013-05-09 10:13:58 +00:00
|
|
|
} else if (title == Ox._('Done')) {
|
2016-10-04 22:00:03 +00:00
|
|
|
callback && callback({
|
2014-01-02 10:11:27 +00:00
|
|
|
ids: ids
|
2013-04-28 17:57:24 +00:00
|
|
|
});
|
2013-03-24 09:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
],
|
|
|
|
content: $content,
|
2013-03-24 11:30:18 +00:00
|
|
|
height: 112,
|
2013-03-24 09:43:24 +00:00
|
|
|
keys: {escape: 'close'},
|
|
|
|
width: 288,
|
2014-01-02 10:11:27 +00:00
|
|
|
title: files.length == 1
|
|
|
|
? Ox._('Upload Document')
|
|
|
|
: Ox._('Upload {0} Documents', [files.length])
|
2013-03-24 09:43:24 +00:00
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
open: function() {
|
2014-01-02 10:11:27 +00:00
|
|
|
uploadFile(0);
|
2013-03-24 09:43:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-02 10:11:27 +00:00
|
|
|
if (!Ox.every(extensions, function(extension) {
|
|
|
|
return Ox.contains(supportedExtensions, extension)
|
|
|
|
})) {
|
2015-04-20 08:13:56 +00:00
|
|
|
return errorDialog(
|
|
|
|
Ox._('Supported file types are GIF, JPG, PNG and PDF.')
|
|
|
|
);
|
2013-03-24 11:30:18 +00:00
|
|
|
} else {
|
2014-01-02 10:11:27 +00:00
|
|
|
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({
|
2016-10-04 22:00:03 +00:00
|
|
|
keys: ['id', 'user', 'title', 'extension'],
|
2014-01-02 10:11:27 +00:00
|
|
|
query: {
|
2015-04-20 08:13:56 +00:00
|
|
|
conditions: [{
|
|
|
|
key: 'oshash',
|
|
|
|
operator: '==',
|
|
|
|
value: oshash
|
|
|
|
}],
|
2014-01-02 10:11:27 +00:00
|
|
|
operator: '&'
|
|
|
|
},
|
|
|
|
range: [0, 1],
|
2016-10-04 22:00:03 +00:00
|
|
|
sort: [{key: 'title', operator: '+'}]
|
2014-01-02 10:11:27 +00:00
|
|
|
}, function(result) {
|
|
|
|
if (result.data.items.length) {
|
2016-10-04 22:00:03 +00:00
|
|
|
var id = result.data.items[0].title + '.'
|
2015-04-20 08:13:56 +00:00
|
|
|
+ result.data.items[0].extension;
|
2014-01-16 05:39:57 +00:00
|
|
|
valid && errorDialog(
|
|
|
|
filename == id
|
2015-04-20 08:13:56 +00:00
|
|
|
? Ox._(
|
|
|
|
'The file "{0}" already exists.',
|
|
|
|
[filename]
|
|
|
|
)
|
|
|
|
: Ox._(
|
|
|
|
'The file "{0}" already exists as "{1}".',
|
|
|
|
[filename, id]
|
|
|
|
)
|
2014-01-02 10:11:27 +00:00
|
|
|
).open();
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
} ,function() {
|
|
|
|
valid && $uploadDialog.open();
|
2013-03-24 11:30:18 +00:00
|
|
|
});
|
|
|
|
return {open: Ox.noop};
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorDialog(text) {
|
|
|
|
return $errorDialog = pandora.ui.iconDialog({
|
|
|
|
buttons: [
|
|
|
|
Ox.Button({
|
|
|
|
id: 'close',
|
2013-05-09 10:13:58 +00:00
|
|
|
title: Ox._('Close')
|
2013-03-24 11:30:18 +00:00
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
click: function() {
|
|
|
|
$errorDialog.close();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
],
|
2013-08-08 13:04:51 +00:00
|
|
|
content: text,
|
2014-01-02 10:11:27 +00:00
|
|
|
title: Ox._('Upload Document')
|
2013-03-24 11:30:18 +00:00
|
|
|
});
|
|
|
|
}
|
2013-03-24 09:43:24 +00:00
|
|
|
|
2014-01-02 10:11:27 +00:00
|
|
|
function uploadFile(part) {
|
2015-10-25 21:08:13 +00:00
|
|
|
var data = {
|
|
|
|
},
|
|
|
|
file = files[part],
|
2014-01-02 10:11:27 +00:00
|
|
|
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]));
|
2015-10-25 21:08:13 +00:00
|
|
|
if (options.id) {
|
|
|
|
data.id = options.id;
|
|
|
|
}
|
|
|
|
data.filename = filename;
|
2014-01-02 10:11:27 +00:00
|
|
|
upload = pandora.chunkupload({
|
2015-10-25 21:08:13 +00:00
|
|
|
data: data,
|
2014-01-02 10:11:27 +00:00
|
|
|
file: file,
|
|
|
|
url: '/api/upload/document/',
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
done: function(data) {
|
|
|
|
if (data.progress == 1) {
|
|
|
|
part++;
|
2014-02-14 13:38:59 +00:00
|
|
|
ids.push(data.response.id);
|
2014-01-02 10:11:27 +00:00
|
|
|
if (part == files.length) {
|
|
|
|
$progress.options({progress: data.progress});
|
2016-10-04 22:00:03 +00:00
|
|
|
callback && callback({ids: ids});
|
2014-02-14 13:38:59 +00:00
|
|
|
$uploadDialog.close();
|
2014-01-02 10:11:27 +00:00
|
|
|
} else {
|
|
|
|
uploadFile(part);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$message.html(Ox._('Upload failed.'))
|
2015-04-20 08:13:56 +00:00
|
|
|
$uploadDialog.options('buttons')[0].options({
|
|
|
|
title: Ox._('Close')
|
|
|
|
});
|
2014-01-02 10:11:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
progress: function(data) {
|
|
|
|
var progress = data.progress || 0;
|
2015-04-20 08:13:56 +00:00
|
|
|
progress = part / files.length + 1 / files.length * progress;
|
2014-01-02 10:11:27 +00:00
|
|
|
$progress.options({progress: progress});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-03-24 09:43:24 +00:00
|
|
|
};
|
|
|
|
|