Merge pull request #8520 from owncloud/jslint_polishing_in_file_upload

polish jslint for files-upload.js
This commit is contained in:
Lukas Reschke 2014-05-10 22:43:50 +02:00
commit 0cc7d84e16
1 changed files with 61 additions and 45 deletions

View File

@ -18,7 +18,7 @@
* - TODO music upload button * - TODO music upload button
*/ */
/* global OC, t, n */ /* global Files, FileList, jQuery, oc_requesttoken, humanFileSize, getUniqueName */
/** /**
* Function that will allow us to know if Ajax uploads are supported * Function that will allow us to know if Ajax uploads are supported
@ -65,7 +65,7 @@ OC.Upload = {
*/ */
cancelUploads:function() { cancelUploads:function() {
this.log('canceling uploads'); this.log('canceling uploads');
jQuery.each(this._uploads,function(i, jqXHR) { jQuery.each(this._uploads, function(i, jqXHR) {
jqXHR.abort(); jqXHR.abort();
}); });
this._uploads = []; this._uploads = [];
@ -83,7 +83,7 @@ OC.Upload = {
isProcessing:function() { isProcessing:function() {
var count = 0; var count = 0;
jQuery.each(this._uploads,function(i, data) { jQuery.each(this._uploads, function(i, data) {
if (data.state() === 'pending') { if (data.state() === 'pending') {
count++; count++;
} }
@ -205,14 +205,16 @@ OC.Upload = {
*/ */
add: function(e, data) { add: function(e, data) {
OC.Upload.log('add', e, data); OC.Upload.log('add', e, data);
var that = $(this); var that = $(this), freeSpace;
var freeSpace;
// we need to collect all data upload objects before starting the upload so we can check their existence // we need to collect all data upload objects before
// and set individual conflict actions. unfortunately there is only one variable that we can use to identify // starting the upload so we can check their existence
// the selection a data upload is part of, so we have to collect them in data.originalFiles // and set individual conflict actions. Unfortunately,
// turning singleFileUploads off is not an option because we want to gracefully handle server errors like // there is only one variable that we can use to identify
// already exists // the selection a data upload is part of, so we have to
// collect them in data.originalFiles turning
// singleFileUploads off is not an option because we want
// to gracefully handle server errors like 'already exists'
// create a container where we can store the data objects // create a container where we can store the data objects
if ( ! data.originalFiles.selection ) { if ( ! data.originalFiles.selection ) {
@ -244,14 +246,15 @@ OC.Upload = {
// in case folder drag and drop is not supported file will point to a directory // in case folder drag and drop is not supported file will point to a directory
// http://stackoverflow.com/a/20448357 // http://stackoverflow.com/a/20448357
if (!file.type && file.size%4096 === 0 && file.size <= 102400) { if ( ! file.type && file.size%4096 === 0 && file.size <= 102400) {
try { try {
reader = new FileReader(); var reader = new FileReader();
reader.readAsBinaryString(f); reader.readAsBinaryString(file);
} catch (NS_ERROR_FILE_ACCESS_DENIED) { } catch (NS_ERROR_FILE_ACCESS_DENIED) {
//file is a directory //file is a directory
data.textStatus = 'dirorzero'; data.textStatus = 'dirorzero';
data.errorThrown = t('files', 'Unable to upload {filename} as it is a directory or has 0 bytes', data.errorThrown = t('files',
'Unable to upload {filename} as it is a directory or has 0 bytes',
{filename: file.name} {filename: file.name}
); );
} }
@ -263,7 +266,8 @@ OC.Upload = {
// check PHP upload limit // check PHP upload limit
if (selection.totalBytes > $('#upload_limit').val()) { if (selection.totalBytes > $('#upload_limit').val()) {
data.textStatus = 'sizeexceedlimit'; data.textStatus = 'sizeexceedlimit';
data.errorThrown = t('files', 'Total file size {size1} exceeds upload limit {size2}', { data.errorThrown = t('files',
'Total file size {size1} exceeds upload limit {size2}', {
'size1': humanFileSize(selection.totalBytes), 'size1': humanFileSize(selection.totalBytes),
'size2': humanFileSize($('#upload_limit').val()) 'size2': humanFileSize($('#upload_limit').val())
}); });
@ -273,7 +277,8 @@ OC.Upload = {
freeSpace = $('#free_space').val(); freeSpace = $('#free_space').val();
if (freeSpace >= 0 && selection.totalBytes > freeSpace) { if (freeSpace >= 0 && selection.totalBytes > freeSpace) {
data.textStatus = 'notenoughspace'; data.textStatus = 'notenoughspace';
data.errorThrown = t('files', 'Not enough free space, you are uploading {size1} but only {size2} is left', { data.errorThrown = t('files',
'Not enough free space, you are uploading {size1} but only {size2} is left', {
'size1': humanFileSize(selection.totalBytes), 'size1': humanFileSize(selection.totalBytes),
'size2': humanFileSize($('#free_space').val()) 'size2': humanFileSize($('#free_space').val())
}); });
@ -384,31 +389,29 @@ OC.Upload = {
//fetch response from iframe //fetch response from iframe
response = data.result[0].body.innerText; response = data.result[0].body.innerText;
} }
var result=$.parseJSON(response); var result = $.parseJSON(response);
delete data.jqXHR; delete data.jqXHR;
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload');
if (result.status === 'error' && result.data && result.data.message){ if (result.status === 'error' && result.data && result.data.message){
data.textStatus = 'servererror'; data.textStatus = 'servererror';
data.errorThrown = result.data.message; data.errorThrown = result.data.message;
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload');
fu._trigger('fail', e, data); fu._trigger('fail', e, data);
} else if (typeof result[0] === 'undefined') { } else if (typeof result[0] === 'undefined') {
data.textStatus = 'servererror'; data.textStatus = 'servererror';
data.errorThrown = t('files', 'Could not get result from server.'); data.errorThrown = t('files', 'Could not get result from server.');
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload');
fu._trigger('fail', e, data); fu._trigger('fail', e, data);
} else if (result[0].status === 'existserror') { } else if (result[0].status === 'existserror') {
//show "file already exists" dialog //show "file already exists" dialog
var original = result[0]; var original = result[0];
var replacement = data.files[0]; var replacement = data.files[0];
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload');
OC.dialogs.fileexists(data, original, replacement, OC.Upload, fu); OC.dialogs.fileexists(data, original, replacement, OC.Upload, fu);
} else if (result[0].status !== 'success') { } else if (result[0].status !== 'success') {
//delete data.jqXHR; //delete data.jqXHR;
data.textStatus = 'servererror'; data.textStatus = 'servererror';
data.errorThrown = result[0].data.message; // error message has been translated on server data.errorThrown = result[0].data.message; // error message has been translated on server
var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload');
fu._trigger('fail', e, data); fu._trigger('fail', e, data);
} }
}, },
@ -440,7 +443,7 @@ OC.Upload = {
fileupload.on('fileuploadstart', function(e, data) { fileupload.on('fileuploadstart', function(e, data) {
OC.Upload.log('progress handle fileuploadstart', e, data); OC.Upload.log('progress handle fileuploadstart', e, data);
$('#uploadprogresswrapper input.stop').show(); $('#uploadprogresswrapper input.stop').show();
$('#uploadprogressbar').progressbar({value:0}); $('#uploadprogressbar').progressbar({value: 0});
$('#uploadprogressbar').fadeIn(); $('#uploadprogressbar').fadeIn();
}); });
fileupload.on('fileuploadprogress', function(e, data) { fileupload.on('fileuploadprogress', function(e, data) {
@ -509,7 +512,7 @@ OC.Upload = {
$('#new li').each(function(i,element) { $('#new li').each(function(i,element) {
if ($(element).children('p').length === 0) { if ($(element).children('p').length === 0) {
$(element).children('form').remove(); $(element).children('form').remove();
$(element).append('<p>'+$(element).data('text')+'</p>'); $(element).append('<p>' + $(element).data('text') + '</p>');
} }
}); });
}); });
@ -527,16 +530,16 @@ OC.Upload = {
$('#new .error').tipsy('hide'); $('#new .error').tipsy('hide');
$('#new li').each(function(i,element) { $('#new li').each(function(i, element) {
if ($(element).children('p').length === 0) { if ($(element).children('p').length === 0) {
$(element).children('form').remove(); $(element).children('form').remove();
$(element).append('<p>'+$(element).data('text')+'</p>'); $(element).append('<p>' + $(element).data('text') + '</p>');
} }
}); });
var type=$(this).data('type'); var type = $(this).data('type');
var text=$(this).children('p').text(); var text = $(this).children('p').text();
$(this).data('text',text); $(this).data('text', text);
$(this).children('p').remove(); $(this).children('p').remove();
// add input field // add input field
@ -553,7 +556,7 @@ OC.Upload = {
var filename = input.val(); var filename = input.val();
if (type === 'web' && filename.length === 0) { if (type === 'web' && filename.length === 0) {
throw t('files', 'URL cannot be empty'); throw t('files', 'URL cannot be empty');
} else if (type !== 'web' && !Files.isFileNameValid(filename)) { } else if (type !== 'web' && ! Files.isFileNameValid(filename)) {
// Files.isFileNameValid(filename) throws an exception itself // Files.isFileNameValid(filename) throws an exception itself
} else if (FileList.inList(filename)) { } else if (FileList.inList(filename)) {
throw t('files', '{new_name} already exists', {new_name: filename}); throw t('files', '{new_name} already exists', {new_name: filename});
@ -603,7 +606,10 @@ OC.Upload = {
case 'file': case 'file':
$.post( $.post(
OC.filePath('files', 'ajax', 'newfile.php'), OC.filePath('files', 'ajax', 'newfile.php'),
{dir:$('#dir').val(), filename:name}, {
dir: $('#dir').val(),
filename: name
},
function(result) { function(result) {
if (result.status === 'success') { if (result.status === 'success') {
FileList.add(result.data, {hidden: hidden, animate: true}); FileList.add(result.data, {hidden: hidden, animate: true});
@ -616,7 +622,10 @@ OC.Upload = {
case 'folder': case 'folder':
$.post( $.post(
OC.filePath('files','ajax','newfolder.php'), OC.filePath('files','ajax','newfolder.php'),
{dir:$('#dir').val(), foldername:name}, {
dir: $('#dir').val(),
foldername: name
},
function(result) { function(result) {
if (result.status === 'success') { if (result.status === 'success') {
FileList.add(result.data, {hidden: hidden, animate: true}); FileList.add(result.data, {hidden: hidden, animate: true});
@ -627,39 +636,46 @@ OC.Upload = {
); );
break; break;
case 'web': case 'web':
if (name.substr(0,8) !== 'https://' && name.substr(0,7) !== 'http://') { if (name.substr(0, 8) !== 'https://' && name.substr(0, 7) !== 'http://') {
name = 'http://' + name; name = 'http://' + name;
} }
var localName=name; var localName = name;
if (localName.substr(localName.length-1,1)==='/') {//strip / if (localName.substr(localName.length-1, 1) === '/') {//strip /
localName=localName.substr(0,localName.length-1); localName = localName.substr(0, localName.length-1);
} }
if (localName.indexOf('/')) {//use last part of url if (localName.indexOf('/')) { //use last part of url
localName=localName.split('/').pop(); localName = localName.split('/').pop();
} else { //or the domain } else { //or the domain
localName=(localName.match(/:\/\/(.[^\/]+)/)[1]).replace('www.',''); localName = (localName.match(/:\/\/(.[^\/]+)/)[1]).replace('www.', '');
} }
localName = getUniqueName(localName); localName = getUniqueName(localName);
//IE < 10 does not fire the necessary events for the progress bar. //IE < 10 does not fire the necessary events for the progress bar.
if ($('html.lte9').length === 0) { if ($('html.lte9').length === 0) {
$('#uploadprogressbar').progressbar({value:0}); $('#uploadprogressbar').progressbar({value: 0});
$('#uploadprogressbar').fadeIn(); $('#uploadprogressbar').fadeIn();
} }
var eventSource=new OC.EventSource(OC.filePath('files','ajax','newfile.php'),{dir:$('#dir').val(),source:name,filename:localName}); var eventSource = new OC.EventSource(
eventSource.listen('progress',function(progress) { OC.filePath('files', 'ajax', 'newfile.php'),
{
dir: $('#dir').val(),
source: name,
filename: localName
}
);
eventSource.listen('progress', function(progress) {
//IE < 10 does not fire the necessary events for the progress bar. //IE < 10 does not fire the necessary events for the progress bar.
if ($('html.lte9').length === 0) { if ($('html.lte9').length === 0) {
$('#uploadprogressbar').progressbar('value',progress); $('#uploadprogressbar').progressbar('value',progress);
} }
}); });
eventSource.listen('success',function(data) { eventSource.listen('success', function(data) {
var file = data; var file = data;
$('#uploadprogressbar').fadeOut(); $('#uploadprogressbar').fadeOut();
FileList.add(file, {hidden: hidden, animate: true}); FileList.add(file, {hidden: hidden, animate: true});
}); });
eventSource.listen('error',function(error) { eventSource.listen('error', function(error) {
$('#uploadprogressbar').fadeOut(); $('#uploadprogressbar').fadeOut();
var message = (error && error.message) || t('core', 'Error fetching URL'); var message = (error && error.message) || t('core', 'Error fetching URL');
OC.Notification.show(message); OC.Notification.show(message);
@ -670,12 +686,12 @@ OC.Upload = {
}); });
break; break;
} }
var li=form.parent(); var li = form.parent();
form.remove(); form.remove();
/* workaround for IE 9&10 click event trap, 2 lines: */ /* workaround for IE 9&10 click event trap, 2 lines: */
$('input').first().focus(); $('input').first().focus();
$('#content').focus(); $('#content').focus();
li.append('<p>'+li.data('text')+'</p>'); li.append('<p>' + li.data('text') + '</p>');
$('#new>a').click(); $('#new>a').click();
} catch (error) { } catch (error) {
input.attr('title', error); input.attr('title', error);